Skip to content

Commit

Permalink
feat: parallel youtube downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Nov 30, 2023
1 parent fdf9237 commit eeb50c1
Show file tree
Hide file tree
Showing 6 changed files with 443 additions and 259 deletions.
7 changes: 6 additions & 1 deletion lib/controller/ffmpeg_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ class NamidaFFMPEG {
final audioFile = File(audioPath);
final originalStats = keepOriginalFileStats ? await audioFile.stat() : null;

final cacheFile = File("${AppDirs.APP_CACHE}/${audioPath.hashCode}.${audioPath.getExtension}");
String ext = 'm4a';
try {
ext = audioPath.getExtension;
} catch (_) {}

final cacheFile = File("${AppDirs.APP_CACHE}/${audioPath.hashCode}.$ext");
final didSuccess = await _ffmpegExecute('-i "$audioPath" -i "$thumbnailPath" -map 0:a -map 1 -codec copy -disposition:v attached_pic -y "${cacheFile.path}"');
final canSafelyMoveBack = didSuccess && await cacheFile.exists() && await cacheFile.length() > 0;
if (canSafelyMoveBack) {
Expand Down
48 changes: 48 additions & 0 deletions lib/youtube/controller/parallel_downloads_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'dart:async';

import 'package:get/get_rx/get_rx.dart';

import 'package:namida/core/extensions.dart';

class YoutubeParallelDownloadsHandler {
static final YoutubeParallelDownloadsHandler inst = YoutubeParallelDownloadsHandler._internal();
YoutubeParallelDownloadsHandler._internal();

/// Use this to wait till a place has been freed.
Future<void>? get waitForParallelCompleter => _maxParallelDownloadsCompleter?.future;

/// Max number of items that can be downloaded simultaniously.
int get maxParallelDownloadingItems => _maxParallelDownloadingItems.value;

void refreshCompleterStatus() => _tryReAssignMaxParallelDownloadsCompleter();

int _currentDownloadingItemsCount = 0;
final _maxParallelDownloadingItems = 1.obs;

/// used to control the parallel process, stopping the download loop or continuing it.
Completer<void>? _maxParallelDownloadsCompleter;

/// updates the current downloading items number, and triggering completer re-assign
/// which either stops ongoing downloads or continues them.
set currentDownloadingItemsCount(int value) {
_currentDownloadingItemsCount = value;
_tryReAssignMaxParallelDownloadsCompleter();
}

void inc() => currentDownloadingItemsCount = _currentDownloadingItemsCount + 1;
void dec() => currentDownloadingItemsCount = _currentDownloadingItemsCount - 1;

void setMaxParalellDownloads(int count) {
_maxParallelDownloadingItems.value = count.withMinimum(1);
_tryReAssignMaxParallelDownloadsCompleter();
}

void _tryReAssignMaxParallelDownloadsCompleter() {
if (_currentDownloadingItemsCount >= _maxParallelDownloadingItems.value) {
_maxParallelDownloadsCompleter ??= Completer<void>();
} else {
_maxParallelDownloadsCompleter?.completeIfWasnt();
_maxParallelDownloadsCompleter = null;
}
}
}
Loading

0 comments on commit eeb50c1

Please sign in to comment.