From 73157cf759b0c8043255bd56228610259dd495a0 Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Sun, 10 Nov 2024 21:02:33 -0500 Subject: [PATCH] Support changing stream IDs depending on container type --- .../Tools/FfmpegConcatList.cs | 28 ++++++++++++++----- TwitchDownloaderCore/VideoDownloader.cs | 17 ++++++++++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/TwitchDownloaderCore/Tools/FfmpegConcatList.cs b/TwitchDownloaderCore/Tools/FfmpegConcatList.cs index e8c3b37d..f9e4785b 100644 --- a/TwitchDownloaderCore/Tools/FfmpegConcatList.cs +++ b/TwitchDownloaderCore/Tools/FfmpegConcatList.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; @@ -12,7 +13,7 @@ public static class FfmpegConcatList { private const string LINE_FEED = "\u000A"; - public static async Task SerializeAsync(string filePath, M3U8 playlist, Range videoListCrop, CancellationToken cancellationToken = default) + public static async Task SerializeAsync(string filePath, M3U8 playlist, Range videoListCrop, StreamIds streamIds, CancellationToken cancellationToken = default) { await using var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read); await using var sw = new StreamWriter(fs) { NewLine = LINE_FEED }; @@ -27,16 +28,29 @@ public static async Task SerializeAsync(string filePath, M3U8 playlist, Range vi await sw.WriteAsync(DownloadTools.RemoveQueryString(stream.Path)); await sw.WriteLineAsync('\''); - await sw.WriteLineAsync("stream"); - await sw.WriteLineAsync("exact_stream_id 0x100"); // Audio - await sw.WriteLineAsync("stream"); - await sw.WriteLineAsync("exact_stream_id 0x101"); // Video - await sw.WriteLineAsync("stream"); - await sw.WriteLineAsync("exact_stream_id 0x102"); // Subtitle + foreach (var id in streamIds.Ids) + { + await sw.WriteLineAsync("stream"); + await sw.WriteLineAsync($"exact_stream_id {id}"); + } await sw.WriteAsync("duration "); await sw.WriteLineAsync(stream.PartInfo.Duration.ToString(CultureInfo.InvariantCulture)); } } + + public record StreamIds + { + public static readonly StreamIds TransportStream = new("0x100", "0x101", "0x102"); + public static readonly StreamIds Mp4 = new("0x1", "0x2"); + public static readonly StreamIds None = new(); + + private StreamIds(params string[] ids) + { + Ids = ids; + } + + public IEnumerable Ids { get; } + } } } \ No newline at end of file diff --git a/TwitchDownloaderCore/VideoDownloader.cs b/TwitchDownloaderCore/VideoDownloader.cs index b2f74b92..74c264bd 100644 --- a/TwitchDownloaderCore/VideoDownloader.cs +++ b/TwitchDownloaderCore/VideoDownloader.cs @@ -109,7 +109,8 @@ await FfmpegMetadata.SerializeAsync(metadataPath, downloadOptions.Id.ToString(), videoChapterResponse.data.video.moments.edges); var concatListPath = Path.Combine(downloadFolder, "concat.txt"); - await FfmpegConcatList.SerializeAsync(concatListPath, playlist, videoListCrop, cancellationToken); + var streamIds = GetStreamIds(playlist); + await FfmpegConcatList.SerializeAsync(concatListPath, playlist, videoListCrop, streamIds, cancellationToken); outputFs.Close(); @@ -318,6 +319,20 @@ private async Task VerifyDownloadedParts(ICollection playlist, Rang } } + private FfmpegConcatList.StreamIds GetStreamIds(M3U8 playlist) + { + var path = DownloadTools.RemoveQueryString(playlist.Streams.FirstOrDefault()?.Path ?? ""); + var extension = Path.GetExtension(path); + if (extension is ".mp4") + return FfmpegConcatList.StreamIds.Mp4; + + if (extension is ".ts") + return FfmpegConcatList.StreamIds.TransportStream; + + _progress.LogWarning("No file extension was found! Assuming TS."); + return FfmpegConcatList.StreamIds.TransportStream; + } + private async Task RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string concatListPath, string metadataPath, decimal startOffset, decimal endDuration, TimeSpan videoLength, bool disableAudioCopy, CancellationToken cancellationToken) { using var process = new Process