From 55a7e74817161df5200338b18507c3c5fa1e49dd Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Mon, 2 Mar 2020 22:50:04 +0100 Subject: [PATCH] Fix progress and add unit test --- FFMpegCore.Test/VideoTest.cs | 27 +++++++++++++++++++++++++++ FFMpegCore/FFMPEG/FFMpeg.cs | 23 +++++------------------ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index cef729f..eff0d44 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -351,5 +351,32 @@ public void Video_Duration() { output.Delete(); } } + + [TestMethod] + public void Video_UpdatesProgress() { + var output = Input.OutputLocation(VideoType.Mp4); + + var percentageDone = 0.0; + void OnProgess(double percentage) => percentageDone = percentage; + Encoder.OnProgress += OnProgess; + + var arguments = new ArgumentContainer + { + new InputArgument(VideoLibrary.LocalVideo), + new DurationArgument(TimeSpan.FromSeconds(8)), + new OutputArgument(output) + }; + + try { + Encoder.Convert(arguments); + Encoder.OnProgress -= OnProgess; + + Assert.IsTrue(File.Exists(output.FullName)); + Assert.AreNotEqual(0.0, percentageDone); + } finally { + if (File.Exists(output.FullName)) + output.Delete(); + } + } } } diff --git a/FFMpegCore/FFMPEG/FFMpeg.cs b/FFMpegCore/FFMPEG/FFMpeg.cs index be5412d..43f6943 100644 --- a/FFMpegCore/FFMPEG/FFMpeg.cs +++ b/FFMpegCore/FFMPEG/FFMpeg.cs @@ -479,8 +479,6 @@ public void Stop() private readonly string _ffmpegPath; private TimeSpan _totalTime; - private volatile StringBuilder _errorOutput = new StringBuilder(); - private bool RunProcess(ArgumentContainer container, FileInfo output) { _instance?.Dispose(); @@ -492,7 +490,7 @@ private bool RunProcess(ArgumentContainer container, FileInfo output) if (!File.Exists(output.FullName) || new FileInfo(output.FullName).Length == 0) { - throw new FFMpegException(FFMpegExceptionType.Process, _errorOutput); + throw new FFMpegException(FFMpegExceptionType.Process, string.Join("\n", _instance.ErrorData)); } return exitCode == 0; @@ -509,31 +507,20 @@ private void Cleanup(IEnumerable pathList) } } - private static readonly Regex ProgressRegex = new Regex(@"\w\w:\w\w:\w\w", RegexOptions.Compiled); + private static readonly Regex ProgressRegex = new Regex(@"time=(\d\d:\d\d:\d\d.\d\d?)", RegexOptions.Compiled); private Instance _instance; private void OutputData(object sender, (DataType Type, string Data) msg) { - var (type, data) = msg; - - if (data == null) return; - if (type == DataType.Error) - { - _errorOutput.AppendLine(data); - return; - } - #if DEBUG - Trace.WriteLine(data); + Trace.WriteLine(msg.Data); #endif - if (OnProgress == null) return; - if (!data.Contains("frame")) return; - var match = ProgressRegex.Match(data); + var match = ProgressRegex.Match(msg.Data); if (!match.Success) return; - var processed = TimeSpan.Parse(match.Value, CultureInfo.InvariantCulture); + var processed = TimeSpan.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture); var percentage = Math.Round(processed.TotalSeconds / _totalTime.TotalSeconds * 100, 2); OnProgress(percentage); }