Skip to content

Commit

Permalink
Fix OverflowException during video finalization (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN authored Apr 16, 2023
1 parent 36b4bc0 commit 74195d4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions TwitchDownloaderCore/VideoDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ private int RunFfmpegVideoCopy(IProgress<ProgressReport> progress, string downlo
};

var videoLength = TimeSpan.Zero;
var videoLengthRegex = new Regex(@"(?<=^\s?\s?Duration: )\d\d:\d\d:\d\d\.\d\d", RegexOptions.Multiline);
var encodingTimeRegex = new Regex(@"(?<=time=)\d\d:\d\d:\d\d\.\d\d", RegexOptions.Compiled);
var videoLengthRegex = new Regex(@"(?<=^\s?\s?Duration:\s)(\d\d):(\d\d):(\d\d)\.(\d\d)", RegexOptions.Multiline);
var encodingTimeRegex = new Regex(@"(?<=time=)(\d\d):(\d\d):(\d\d)\.(\d\d)", RegexOptions.Compiled);
var logQueue = new ConcurrentQueue<string>();

process.ErrorDataReceived += (sender, e) =>
Expand All @@ -183,7 +183,12 @@ private int RunFfmpegVideoCopy(IProgress<ProgressReport> progress, string downlo
if (!videoLengthMatch.Success)
return;

videoLength = TimeSpan.Parse(videoLengthMatch.ValueSpan);
// TimeSpan.Parse insists that hours cannot be greater than 24, thus we must use the TimeSpan ctor.
if (!int.TryParse(videoLengthMatch.Groups[1].ValueSpan, out var hours)) return;
if (!int.TryParse(videoLengthMatch.Groups[2].ValueSpan, out var minutes)) return;
if (!int.TryParse(videoLengthMatch.Groups[3].ValueSpan, out var seconds)) return;
if (!int.TryParse(videoLengthMatch.Groups[4].ValueSpan, out var milliseconds)) return;
videoLength = new TimeSpan(0, hours, minutes, seconds, milliseconds);
return;
}

Expand Down Expand Up @@ -212,7 +217,13 @@ private static void HandleFfmpegOutput(string output, Regex encodingTimeRegex, T
if (!encodingTimeMatch.Success)
return;

var encodingTime = TimeSpan.Parse(encodingTimeMatch.ValueSpan);
// TimeSpan.Parse insists that hours cannot be greater than 24, thus we must use the TimeSpan ctor.
if (!int.TryParse(encodingTimeMatch.Groups[1].ValueSpan, out var hours)) return;
if (!int.TryParse(encodingTimeMatch.Groups[2].ValueSpan, out var minutes)) return;
if (!int.TryParse(encodingTimeMatch.Groups[3].ValueSpan, out var seconds)) return;
if (!int.TryParse(encodingTimeMatch.Groups[4].ValueSpan, out var milliseconds)) return;
var encodingTime = new TimeSpan(0, hours, minutes, seconds, milliseconds);

var percent = (int)(encodingTime.TotalMilliseconds / videoLength.TotalMilliseconds * 100);

progress.Report(new ProgressReport(ReportType.SameLineStatus, $"Finalizing Video {percent}% [4/4]"));
Expand Down

0 comments on commit 74195d4

Please sign in to comment.