Add NotifyOnOutput

This commit is contained in:
Malte Rosenbjerg 2020-12-18 00:40:09 +01:00
parent 8fe7377bbd
commit cfee86199b
4 changed files with 37 additions and 7 deletions

View file

@ -7,6 +7,7 @@
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using FFMpegCore.Arguments; using FFMpegCore.Arguments;
using FFMpegCore.Exceptions; using FFMpegCore.Exceptions;
@ -550,6 +551,27 @@ public void Video_UpdatesProgress()
Assert.AreNotEqual(TimeSpan.Zero, timeDone); Assert.AreNotEqual(TimeSpan.Zero, timeDone);
} }
[TestMethod, Timeout(10000)]
public void Video_OutputsData()
{
var outputFile = new TemporaryFile("out.mp4");
var dataReceived = false;
FFMpegOptions.Configure(opt => opt.Encoding = Encoding.UTF8);
var success = FFMpegArguments
.FromFileInput(TestResources.Mp4Video)
.WithGlobalOptions(options => options
.WithVerbosityLevel(VerbosityLevel.Info))
.OutputToFile(outputFile, false, opt => opt
.WithDuration(TimeSpan.FromSeconds(2)))
.NotifyOnOutput((_, _) => dataReceived = true)
.ProcessSynchronously();
Assert.IsTrue(dataReceived);
Assert.IsTrue(success);
Assert.IsTrue(File.Exists(outputFile));
}
[TestMethod, Timeout(10000)] [TestMethod, Timeout(10000)]
public void Video_TranscodeInMemory() public void Video_TranscodeInMemory()
{ {

View file

@ -17,6 +17,7 @@ public class FFMpegArgumentProcessor
private readonly FFMpegArguments _ffMpegArguments; private readonly FFMpegArguments _ffMpegArguments;
private Action<double>? _onPercentageProgress; private Action<double>? _onPercentageProgress;
private Action<TimeSpan>? _onTimeProgress; private Action<TimeSpan>? _onTimeProgress;
private Action<string, DataType>? _onOutput;
private TimeSpan? _totalTimespan; private TimeSpan? _totalTimespan;
internal FFMpegArgumentProcessor(FFMpegArguments ffMpegArguments) internal FFMpegArgumentProcessor(FFMpegArguments ffMpegArguments)
@ -39,6 +40,11 @@ public FFMpegArgumentProcessor NotifyOnProgress(Action<TimeSpan> onTimeProgress)
_onTimeProgress = onTimeProgress; _onTimeProgress = onTimeProgress;
return this; return this;
} }
public FFMpegArgumentProcessor NotifyOnOutput(Action<string, DataType> onOutput)
{
_onOutput = onOutput;
return this;
}
public FFMpegArgumentProcessor CancellableThrough(out Action cancel) public FFMpegArgumentProcessor CancellableThrough(out Action cancel)
{ {
cancel = () => CancelEvent?.Invoke(this, EventArgs.Empty); cancel = () => CancelEvent?.Invoke(this, EventArgs.Empty);
@ -140,7 +146,7 @@ private Instance PrepareInstance(out CancellationTokenSource cancellationTokenSo
var instance = new Instance(startInfo); var instance = new Instance(startInfo);
cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource = new CancellationTokenSource();
if (_onTimeProgress != null || (_onPercentageProgress != null && _totalTimespan != null)) if (_onOutput != null || _onTimeProgress != null || (_onPercentageProgress != null && _totalTimespan != null))
instance.DataReceived += OutputData; instance.DataReceived += OutputData;
return instance; return instance;
@ -157,8 +163,10 @@ private static bool HandleException(bool throwOnError, Exception e, IReadOnlyLis
private void OutputData(object sender, (DataType Type, string Data) msg) private void OutputData(object sender, (DataType Type, string Data) msg)
{ {
var match = ProgressRegex.Match(msg.Data);
Debug.WriteLine(msg.Data); Debug.WriteLine(msg.Data);
_onOutput?.Invoke(msg.Data, msg.Type);
var match = ProgressRegex.Match(msg.Data);
if (!match.Success) return; if (!match.Success) return;
var processed = TimeSpan.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture); var processed = TimeSpan.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);

View file

@ -41,6 +41,8 @@ static FFMpegOptions()
public string RootDirectory { get; set; } = DefaultRoot; public string RootDirectory { get; set; } = DefaultRoot;
public string TempDirectory { get; set; } = DefaultTemp; public string TempDirectory { get; set; } = DefaultTemp;
public bool UseCache { get; set; } = true;
public Encoding Encoding { get; set; } = Encoding.Default;
public string FFmpegBinary() => FFBinary("FFMpeg"); public string FFmpegBinary() => FFBinary("FFMpeg");
@ -48,9 +50,6 @@ static FFMpegOptions()
public Dictionary<string, string> ExtensionOverrides { get; private set; } = new Dictionary<string, string>(); public Dictionary<string, string> ExtensionOverrides { get; private set; } = new Dictionary<string, string>();
public bool UseCache { get; set; } = true;
public Encoding Encoding { get; set; } = Encoding.Default;
private static string FFBinary(string name) private static string FFBinary(string name)
{ {
var ffName = name.ToLowerInvariant(); var ffName = name.ToLowerInvariant();

View file

@ -9,9 +9,10 @@
<Version>3.0.0.0</Version> <Version>3.0.0.0</Version>
<AssemblyVersion>3.0.0.0</AssemblyVersion> <AssemblyVersion>3.0.0.0</AssemblyVersion>
<FileVersion>3.0.0.0</FileVersion> <FileVersion>3.0.0.0</FileVersion>
<PackageReleaseNotes>- Also include ffmpeg output data on non-zero exit code</PackageReleaseNotes> <PackageReleaseNotes>- Support for changing encoding used for parsing ffmpeg/ffprobe output
- Support for registrering action to invoke on ffmpeg output</PackageReleaseNotes>
<LangVersion>8</LangVersion> <LangVersion>8</LangVersion>
<PackageVersion>3.2.4</PackageVersion> <PackageVersion>3.3.0</PackageVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<Authors>Malte Rosenbjerg, Vlad Jerca</Authors> <Authors>Malte Rosenbjerg, Vlad Jerca</Authors>
<PackageTags>ffmpeg ffprobe convert video audio mediafile resize analyze muxing</PackageTags> <PackageTags>ffmpeg ffprobe convert video audio mediafile resize analyze muxing</PackageTags>