From 7254b6f538d9e60d407386cddaf482ed41386654 Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Sat, 9 Nov 2024 21:51:34 -0500 Subject: [PATCH 1/2] Verbose log FFmpeg working dir and args --- TwitchDownloaderCore/ChatRenderer.cs | 2 ++ TwitchDownloaderCore/VideoDownloader.cs | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/TwitchDownloaderCore/ChatRenderer.cs b/TwitchDownloaderCore/ChatRenderer.cs index 5241de47..b8819993 100644 --- a/TwitchDownloaderCore/ChatRenderer.cs +++ b/TwitchDownloaderCore/ChatRenderer.cs @@ -390,6 +390,8 @@ private FfmpegProcess GetFfmpegProcess(FileInfo fileInfo) } }; + _progress.LogVerbose($"Running FFmpeg in \"{process.StartInfo.WorkingDirectory}\" with args: {process.StartInfo.Arguments}"); + process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); diff --git a/TwitchDownloaderCore/VideoDownloader.cs b/TwitchDownloaderCore/VideoDownloader.cs index b32d10f6..4a0fbe8a 100644 --- a/TwitchDownloaderCore/VideoDownloader.cs +++ b/TwitchDownloaderCore/VideoDownloader.cs @@ -386,6 +386,8 @@ private async Task RunFfmpegVideoCopy(string tempFolder, FileInfo outputFil HandleFfmpegOutput(e.Data, encodingTimeRegex, videoLength); }; + _progress.LogVerbose($"Running FFmpeg in \"{process.StartInfo.WorkingDirectory}\" with args: {CombineArguments(process.StartInfo.ArgumentList)}"); + process.Start(); process.BeginErrorReadLine(); @@ -401,6 +403,17 @@ private async Task RunFfmpegVideoCopy(string tempFolder, FileInfo outputFil } while (!process.HasExited || !logQueue.IsEmpty); return process.ExitCode; + + static string CombineArguments(IEnumerable args) + { + return string.Join(' ', args.Select(x => + { + if (!x.StartsWith('"') && !x.StartsWith('\'') && x.Contains(' ')) + return $"\"{x}\""; + + return x; + })); + } } private void HandleFfmpegOutput(string output, Regex encodingTimeRegex, TimeSpan videoLength) From d6148e9fd1c145b9f07ebe2d4cae6e6ab248f96a Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Sat, 9 Nov 2024 21:55:42 -0500 Subject: [PATCH 2/2] Use FFmpeg path when logging --- TwitchDownloaderCore/ChatRenderer.cs | 2 +- TwitchDownloaderCore/VideoDownloader.cs | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/TwitchDownloaderCore/ChatRenderer.cs b/TwitchDownloaderCore/ChatRenderer.cs index b8819993..2851ccd3 100644 --- a/TwitchDownloaderCore/ChatRenderer.cs +++ b/TwitchDownloaderCore/ChatRenderer.cs @@ -390,7 +390,7 @@ private FfmpegProcess GetFfmpegProcess(FileInfo fileInfo) } }; - _progress.LogVerbose($"Running FFmpeg in \"{process.StartInfo.WorkingDirectory}\" with args: {process.StartInfo.Arguments}"); + _progress.LogVerbose($"Running \"{renderOptions.FfmpegPath}\" in \"{process.StartInfo.WorkingDirectory}\" with args: {process.StartInfo.Arguments}"); process.Start(); process.BeginErrorReadLine(); diff --git a/TwitchDownloaderCore/VideoDownloader.cs b/TwitchDownloaderCore/VideoDownloader.cs index 4a0fbe8a..3e1841d6 100644 --- a/TwitchDownloaderCore/VideoDownloader.cs +++ b/TwitchDownloaderCore/VideoDownloader.cs @@ -90,6 +90,8 @@ private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputF CheckAvailableStorageSpace(qualityPlaylist.StreamInfo.Bandwidth, videoLength); + var streamIds = GetStreamIds(playlist); + if (Directory.Exists(downloadFolder)) Directory.Delete(downloadFolder, true); TwitchHelper.CreateDirectory(downloadFolder); @@ -109,7 +111,7 @@ 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); + await FfmpegConcatList.SerializeAsync(concatListPath, playlist, videoListCrop, streamIds, cancellationToken); outputFs.Close(); @@ -145,6 +147,20 @@ await FfmpegMetadata.SerializeAsync(metadataPath, downloadOptions.Id.ToString(), } } + 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 void CheckAvailableStorageSpace(int bandwidth, TimeSpan videoLength) { var videoSizeInBytes = VideoSizeEstimator.EstimateVideoSize(bandwidth, @@ -318,7 +334,8 @@ private async Task VerifyDownloadedParts(ICollection playlist, Rang } } - private async Task RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string concatListPath, string metadataPath, decimal startOffset, decimal endDuration, TimeSpan videoLength, bool disableAudioCopy, CancellationToken cancellationToken) + 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 { @@ -386,7 +403,7 @@ private async Task RunFfmpegVideoCopy(string tempFolder, FileInfo outputFil HandleFfmpegOutput(e.Data, encodingTimeRegex, videoLength); }; - _progress.LogVerbose($"Running FFmpeg in \"{process.StartInfo.WorkingDirectory}\" with args: {CombineArguments(process.StartInfo.ArgumentList)}"); + _progress.LogVerbose($"Running \"{downloadOptions.FfmpegPath}\" in \"{process.StartInfo.WorkingDirectory}\" with args: {CombineArguments(process.StartInfo.ArgumentList)}"); process.Start(); process.BeginErrorReadLine();