Move NotifyOnProgress processing to ErrorData

Former-commit-id: 5d62be654a
This commit is contained in:
keg247 2022-06-19 23:31:18 -04:00
parent b7e1c082e3
commit 1e372e3e29
2 changed files with 54 additions and 44 deletions

View file

@ -1,4 +1,7 @@
using FFMpegCore.Enums;
using FFMpegCore.Arguments;
using FFMpegCore.Enums;
using FFMpegCore.Exceptions;
using FFMpegCore.Pipes;
using FFMpegCore.Test.Resources;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
@ -8,11 +11,8 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FFMpegCore.Arguments;
using FFMpegCore.Exceptions;
using FFMpegCore.Pipes;
using System.Threading;
using System.Threading.Tasks;
namespace FFMpegCore.Test
{
@ -505,14 +505,22 @@ public void Video_UpdatesProgress()
var percentageDone = 0.0;
var timeDone = TimeSpan.Zero;
void OnPercentageProgess(double percentage) => percentageDone = percentage;
void OnTimeProgess(TimeSpan time) => timeDone = time;
var analysis = FFProbe.Analyse(TestResources.Mp4Video);
void OnPercentageProgess(double percentage)
{
if (percentage < 100) percentageDone = percentage;
}
void OnTimeProgess(TimeSpan time)
{
if (time < analysis.Duration) timeDone = time;
}
var success = FFMpegArguments
.FromFileInput(TestResources.Mp4Video)
.OutputToFile(outputFile, false, opt => opt
.WithDuration(TimeSpan.FromSeconds(2)))
.WithDuration(analysis.Duration))
.NotifyOnProgress(OnPercentageProgess, analysis.Duration)
.NotifyOnProgress(OnTimeProgess)
.ProcessSynchronously();
@ -520,7 +528,9 @@ public void Video_UpdatesProgress()
Assert.IsTrue(success);
Assert.IsTrue(File.Exists(outputFile));
Assert.AreNotEqual(0.0, percentageDone);
Assert.AreNotEqual(100.0, percentageDone);
Assert.AreNotEqual(TimeSpan.Zero, timeDone);
Assert.AreNotEqual(analysis.Duration, timeDone);
}
[TestMethod, Timeout(10000)]

View file

@ -1,13 +1,13 @@
using System;
using FFMpegCore.Exceptions;
using FFMpegCore.Helpers;
using Instances;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using FFMpegCore.Exceptions;
using FFMpegCore.Helpers;
using Instances;
namespace FFMpegCore
{
@ -204,10 +204,10 @@ private ProcessArguments PrepareProcessArguments(FFOptions ffOptions,
var processArguments = new ProcessArguments(startInfo);
cancellationTokenSource = new CancellationTokenSource();
if (_onOutput != null || _onTimeProgress != null || (_onPercentageProgress != null && _totalTimespan != null))
if (_onOutput != null)
processArguments.OutputDataReceived += OutputData;
if (_onError != null)
if (_onError != null || _onTimeProgress != null || (_onPercentageProgress != null && _totalTimespan != null))
processArguments.ErrorDataReceived += ErrorData;
return processArguments;
@ -216,12 +216,6 @@ private ProcessArguments PrepareProcessArguments(FFOptions ffOptions,
private void ErrorData(object sender, string msg)
{
_onError?.Invoke(msg);
}
private void OutputData(object sender, string msg)
{
Debug.WriteLine(msg);
_onOutput?.Invoke(msg);
var match = ProgressRegex.Match(msg);
if (!match.Success) return;
@ -233,5 +227,11 @@ private void OutputData(object sender, string msg)
var percentage = Math.Round(processed.TotalSeconds / _totalTimespan.Value.TotalSeconds * 100, 2);
_onPercentageProgress(percentage);
}
private void OutputData(object sender, string msg)
{
Debug.WriteLine(msg);
_onOutput?.Invoke(msg);
}
}
}