Skip to content

Commit

Permalink
Fix lockup when downloading clips with metadata (#907)
Browse files Browse the repository at this point in the history
* Change generated clip temp file name

* Add process exit check to mitigate possible .NET bug
  • Loading branch information
ScrubN authored Nov 28, 2023
1 parent fa95c9b commit 4b74386
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion TwitchDownloaderCore/ClipDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StreamCopyProgress>(DownloadProgressHandler), cancellationToken);
Expand All @@ -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));
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4b74386

Please sign in to comment.