From 469a76dbd6e353401a5dd2542d4db4120e3d49be Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Fri, 5 Sep 2025 22:40:18 +0600 Subject: [PATCH] fix: yt-dlp playback not working and add partial support for HLS streaming --- lib/provider/server/routes/playback.dart | 18 ++++++++++++++++++ lib/services/youtube_engine/yt_dlp_engine.dart | 9 ++++----- pubspec.lock | 4 ++-- pubspec.yaml | 2 +- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/provider/server/routes/playback.dart b/lib/provider/server/routes/playback.dart index f31de9cb..c81d968f 100644 --- a/lib/provider/server/routes/playback.dart +++ b/lib/provider/server/routes/playback.dart @@ -117,6 +117,24 @@ class ServerPlaybackRoutes { return dio.head(url, options: options); }); + // Redirect to m3u8 link directly as it handles range requests internally + if (contentLengthRes?.headers.value("content-type") == + "application/vnd.apple.mpegurl") { + return ( + response: dio_lib.Response( + statusCode: 301, + statusMessage: "M3U8 Redirect", + headers: Headers.fromMap({ + "location": [url], + "content-type": ["application/vnd.apple.mpegurl"], + }), + requestOptions: RequestOptions(path: request.requestedUri.toString()), + isRedirect: true, + ), + bytes: null, + ); + } + final contentLength = contentLengthRes?.headers.value("content-length"); /// Forcing partial content range as mpv sometimes greedily wants diff --git a/lib/services/youtube_engine/yt_dlp_engine.dart b/lib/services/youtube_engine/yt_dlp_engine.dart index ebabc345..28665fe9 100644 --- a/lib/services/youtube_engine/yt_dlp_engine.dart +++ b/lib/services/youtube_engine/yt_dlp_engine.dart @@ -11,9 +11,7 @@ import 'package:http_parser/http_parser.dart'; class YtDlpEngine implements YouTubeEngine { StreamManifest _parseFormats(List formats, videoId) { final audioOnlyStreams = formats - .where( - (f) => f["resolution"] == "audio only" && f["manifest_url"] == null, - ) + .where((f) => f["resolution"] == "audio only") .sorted((a, b) => a["quality"] > b["quality"] ? 1 : -1) .map((f) { final filesize = f["filesize"] ?? f["filesize_approx"]; @@ -22,13 +20,14 @@ class YtDlpEngine implements YouTubeEngine { 0, Uri.parse(f["url"]), StreamContainer.parse( - f["container"]?.replaceAll("_dash", "").replaceAll("m4a", "mp4"), + f["container"]?.replaceAll("_dash", "").replaceAll("m4a", "mp4") ?? + (f["protocol"] == "m3u8_native" ? "m3u8" : "mp4"), ), filesize != null ? FileSize(filesize) : FileSize.unknown, Bitrate( (((f["abr"] ?? f["tbr"] ?? 0) * 1000) as num).toInt(), ), - f["acodec"] ?? "webm", + f["acodec"] ?? "aac", f["format_note"], [], MediaType.parse( diff --git a/pubspec.lock b/pubspec.lock index ea043c65..55569f1d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -2881,8 +2881,8 @@ packages: dependency: "direct main" description: path: "." - ref: e2d82305fab18566408d6f8758361017d1640c3d - resolved-ref: e2d82305fab18566408d6f8758361017d1640c3d + ref: "4e5310e14af74bdbb51e2a4766e66d6c6a2562a8" + resolved-ref: "4e5310e14af74bdbb51e2a4766e66d6c6a2562a8" url: "https://github.com/KRTirtho/yt_dlp_dart.git" source: git version: "1.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index d3008786..fc014ef9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -132,7 +132,7 @@ dependencies: yt_dlp_dart: git: url: https://github.com/KRTirtho/yt_dlp_dart.git - ref: e2d82305fab18566408d6f8758361017d1640c3d + ref: 4e5310e14af74bdbb51e2a4766e66d6c6a2562a8 flutter_new_pipe_extractor: git: url: https://github.com/KRTirtho/flutter_new_pipe_extractor.git