From 74195d47663d2c649ae204f3ebfe2a8c6c83e366 Mon Sep 17 00:00:00 2001 From: Scrub <72096833+ScrubN@users.noreply.github.com> Date: Sun, 16 Apr 2023 00:18:06 -0400 Subject: [PATCH] Fix OverflowException during video finalization (#670) --- TwitchDownloaderCore/VideoDownloader.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/TwitchDownloaderCore/VideoDownloader.cs b/TwitchDownloaderCore/VideoDownloader.cs index e84aaf7f..6a76531f 100644 --- a/TwitchDownloaderCore/VideoDownloader.cs +++ b/TwitchDownloaderCore/VideoDownloader.cs @@ -166,8 +166,8 @@ private int RunFfmpegVideoCopy(IProgress 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(); process.ErrorDataReceived += (sender, e) => @@ -183,7 +183,12 @@ private int RunFfmpegVideoCopy(IProgress 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; } @@ -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]"));