From 0db1b3627808254914696593aee56a84289bc451 Mon Sep 17 00:00:00 2001 From: Scrub <72096833+ScrubN@users.noreply.github.com> Date: Fri, 5 Jan 2024 01:56:55 -0500 Subject: [PATCH] Fix clip downloader sometimes getting stuck when encoding metadata (#926) * Actually fix #914 * Kill FFmpeg on error if it hasn't exited yet * Log instead of throwing --- TwitchDownloaderCore/ClipDownloader.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/TwitchDownloaderCore/ClipDownloader.cs b/TwitchDownloaderCore/ClipDownloader.cs index 1945c2e5..882d29e7 100644 --- a/TwitchDownloaderCore/ClipDownloader.cs +++ b/TwitchDownloaderCore/ClipDownloader.cs @@ -77,7 +77,7 @@ void DownloadProgressHandler(StreamCopyProgress streamProgress) 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.Log, "Unable to serialize metadata. The download has been completed without custom metadata.")); } _progress.Report(new ProgressReport(ReportType.SameLineStatus, "Encoding Clip Metadata 100%")); @@ -149,12 +149,13 @@ private async Task EncodeClipWithMetadata(string inputFile, string destinationFi { var metadataFile = $"{inputFile}_metadata.txt"; + Process process = null; try { await FfmpegMetadata.SerializeAsync(metadataFile, clipMetadata.broadcaster.displayName, downloadOptions.Id, clipMetadata.title, clipMetadata.createdAt, clipMetadata.viewCount, videoMomentEdges: new[] { clipChapter }, cancellationToken: cancellationToken); - var process = new Process + process = new Process { StartInfo = { @@ -169,9 +170,9 @@ await FfmpegMetadata.SerializeAsync(metadataFile, clipMetadata.broadcaster.displ }; process.Start(); + process.BeginErrorReadLine(); // 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; @@ -179,6 +180,12 @@ await FfmpegMetadata.SerializeAsync(metadataFile, clipMetadata.broadcaster.displ } finally { + if (process is { HasExited: false }) + { + process.Kill(); + await Task.Delay(100, cancellationToken); + } + File.Delete(metadataFile); } }