Skip to content

Commit

Permalink
fix: fix yt downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Aug 1, 2024
1 parent 9f521e9 commit ef65992
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
7 changes: 4 additions & 3 deletions lib/controller/video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class VideoController {
}) async {
final tr = Player.inst.currentTrack?.track;
if (tr == null) return null;
final dv = await fetchVideoFromYoutube(id, stream: stream, mainStreams: mainStreams);
final dv = await fetchVideoFromYoutube(id, stream: stream, mainStreams: mainStreams, canContinue: () => settings.enableVideoPlayback.value);
if (!settings.enableVideoPlayback.value) return null;
if (_canExecuteForCurrentTrackOnly(tr)) {
currentVideo.value = dv;
Expand All @@ -392,6 +392,7 @@ class VideoController {
String? id, {
VideoStreamsResult? mainStreams,
VideoStream? stream,
required bool Function() canContinue,
}) async {
_downloadTimerCancel();
if (id == null || id == '') return null;
Expand All @@ -410,7 +411,7 @@ class VideoController {
_downloadTimer = Timer.periodic(const Duration(seconds: 1), (_) => updateCurrentBytes());

VideoStream? streamToUse = stream;
if (stream == null || mainStreams?.hasExpired() != false) {
if (stream == null || (mainStreams?.hasExpired() ?? true)) {
// expired null or true
mainStreams = await YoutubeInfoController.video.fetchVideoStreams(id);
if (mainStreams != null) {
Expand All @@ -419,7 +420,7 @@ class VideoController {
}
}

if (streamToUse == null) {
if (streamToUse == null || !canContinue()) {
if (_canExecuteForCurrentTrackOnly(initialTrack)) {
currentDownloadedBytes.value = null;
_downloadTimerCancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,31 @@ class _VideoInfoController {
return res;
}

/// By default, this will force a network request since most implementations use this as fallback to [fetchVideoPageSync].
YoutiPieVideoPageResult? fetchVideoPageSync(String videoId) {
final res = YoutiPie.cacheBuilder.forVideoPage(videoId: videoId);
return res.read();
}

/// By default, this will force a network request since most implementations use this as fallback to [fetchVideoStreamsSync].
Future<VideoStreamsResult?> fetchVideoStreams(String videoId, {bool forceRequest = true}) async {
final res = await YoutiPie.video.fetchVideoStreams(
id: videoId,
details: forceRequest ? ExecuteDetails.forceRequest() : null,
client: _usedClient,
);
// -- preparing jsplayer *before* fetching streams is important, fetching *after* will return non-working urls.
if (_requiresJSPlayer) {
// -- await preparing before returning result
if (!YoutiPie.cipher.isPrepared) {
await ensureJSPlayerInitialized();
}
}
final res = await YoutiPie.video.fetchVideoStreams(
id: videoId,
details: forceRequest ? ExecuteDetails.forceRequest() : null,
client: _usedClient,
);
return res;
}

YoutiPieVideoPageResult? fetchVideoPageSync(String videoId) {
final res = YoutiPie.cacheBuilder.forVideoPage(videoId: videoId);
return res.read();
}

/// Returns cached streams result if exist. you need to ensure that urls didn't expire ![VideoStreamsResult.hasExpired] before consuming them.
///
/// Enable [bypassJSCheck] if you gurantee using [VideoStreamsResult.info] only.
VideoStreamsResult? fetchVideoStreamsSync(String videoId, {bool bypassJSCheck = false}) {
final res = YoutiPie.cacheBuilder.forVideoStreams(videoId: videoId);
final cached = res.read();
Expand Down
9 changes: 5 additions & 4 deletions lib/youtube/controller/youtube_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ class _YTDownloadManager with PortsProvider<SendPort> {
if (url == null || url.host.isEmpty) return false;

final filePath = file.path;
if (_downloadCompleters[filePath] != null) return _downloadCompleters[filePath]!.future;
_downloadCompleters[filePath]?.completeIfWasnt(false);
_downloadCompleters[filePath] = Completer<bool>();

Expand Down Expand Up @@ -1291,7 +1292,7 @@ class _YTDownloadManager with PortsProvider<SendPort> {
fileStream.add(data);
progressPort.send(data.length);
}
bool? movedSuccessfully;
bool movedSuccessfully = false;
if (moveTo != null && moveToRequiredBytes != null) {
try {
final fileStats = file.statSync();
Expand All @@ -1301,11 +1302,11 @@ class _YTDownloadManager with PortsProvider<SendPort> {
moveTo,
goodBytesIfCopied: (fileLength) => fileLength >= moveToRequiredBytes - allowance,
);
if (movedFile == null) movedSuccessfully = false;
movedSuccessfully = movedFile != null;
}
} catch (_) {}
}
return sendPort.send(MapEntry(filePath, movedSuccessfully ?? true));
return sendPort.send(MapEntry(filePath, movedSuccessfully));
} catch (_) {
// client force closed
return sendPort.send(MapEntry(filePath, false));
Expand Down Expand Up @@ -1340,7 +1341,7 @@ class _YTDownloadManager with PortsProvider<SendPort> {

void _onFileFinish(String path, bool? value) {
if (value != null) _downloadCompleters[path]?.completeIfWasnt(value);
_downloadCompleters[path] = null;
_downloadCompleters[path] = null; // important
_progressPorts[path]?.close();
_progressPorts[path] = null;
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: namida
description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter
publish_to: "none"
version: 3.7.5-beta+240801001
version: 3.7.55-beta+240801001

environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down

0 comments on commit ef65992

Please sign in to comment.