From 4b74386cf66aed3b566a1a0a01e089fb2ea0ca3e Mon Sep 17 00:00:00 2001 From: Scrub <72096833+ScrubN@users.noreply.github.com> Date: Tue, 28 Nov 2023 18:38:26 -0500 Subject: [PATCH] Fix lockup when downloading clips with metadata (#907) * Change generated clip temp file name * Add process exit check to mitigate possible .NET bug --- TwitchDownloaderCore/ClipDownloader.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/TwitchDownloaderCore/ClipDownloader.cs b/TwitchDownloaderCore/ClipDownloader.cs index c7e6fcf5..3198af7b 100644 --- a/TwitchDownloaderCore/ClipDownloader.cs +++ b/TwitchDownloaderCore/ClipDownloader.cs @@ -63,7 +63,7 @@ void DownloadProgressHandler(StreamCopyProgress streamProgress) TwitchHelper.CreateDirectory(downloadOptions.TempFolder); } - var tempFile = Path.Combine(downloadOptions.TempFolder, $"clip_{DateTimeOffset.Now.ToUnixTimeMilliseconds()}_{Path.GetRandomFileName()}"); + var tempFile = Path.Combine(downloadOptions.TempFolder, $"{downloadOptions.Id}_{DateTimeOffset.UtcNow.Ticks}.mp4"); try { await DownloadFileTaskAsync(downloadUrl, tempFile, downloadOptions.ThrottleKib, new Progress(DownloadProgressHandler), cancellationToken); @@ -74,6 +74,12 @@ void DownloadProgressHandler(StreamCopyProgress streamProgress) var clipChapter = TwitchHelper.GenerateClipChapter(clipInfo.data.clip); await EncodeClipWithMetadata(tempFile, downloadOptions.Filename, clipInfo.data.clip, clipChapter, cancellationToken); + if (!File.Exists(downloadOptions.Filename)) + { + File.Move(tempFile, downloadOptions.Filename); + throw new FileNotFoundException("Unable to serialize metadata (is FFmpeg missing?). The download has been completed without custom metadata."); + } + _progress.Report(new ProgressReport(ReportType.SameLineStatus, "Encoding Clip Metadata 100%")); _progress.Report(new ProgressReport(100)); } @@ -163,6 +169,12 @@ await FfmpegMetadata.SerializeAsync(metadataFile, clipMetadata.broadcaster.displ }; process.Start(); + + // If the process has exited before we call WaitForExitAsync, the thread locks up. + // This was probably not intended by the .NET team, but it's an issue regardless. + if (process.HasExited) + return; + await process.WaitForExitAsync(cancellationToken); } finally