Skip to content

Commit

Permalink
Fix clip downloader sometimes getting stuck when encoding metadata (#926
Browse files Browse the repository at this point in the history
)

* Actually fix #914

* Kill FFmpeg on error if it hasn't exited yet

* Log instead of throwing
  • Loading branch information
ScrubN authored Jan 5, 2024
1 parent e270962 commit 0db1b36
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions TwitchDownloaderCore/ClipDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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%"));
Expand Down Expand Up @@ -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 =
{
Expand All @@ -169,16 +170,22 @@ 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;

await process.WaitForExitAsync(cancellationToken);
}
finally
{
if (process is { HasExited: false })
{
process.Kill();
await Task.Delay(100, cancellationToken);
}

File.Delete(metadataFile);
}
}
Expand Down

0 comments on commit 0db1b36

Please sign in to comment.