From 965e756dc4e7d80c5ed487368b129bb675467396 Mon Sep 17 00:00:00 2001 From: BobSilent Date: Tue, 2 Nov 2021 10:40:05 +0100 Subject: [PATCH] Add Configure on FFMpegArgumentProcessor to fuently configure ffoptions per run. --- FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs | 37 +++++++++++++++----- FFMpegCore/FFOptions.cs | 17 ++++++--- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs index 68738be..209cc36 100644 --- a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs +++ b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs @@ -14,6 +14,7 @@ namespace FFMpegCore public class FFMpegArgumentProcessor { private static readonly Regex ProgressRegex = new Regex(@"time=(\d\d:\d\d:\d\d.\d\d?)", RegexOptions.Compiled); + private readonly List> _configurations; private readonly FFMpegArguments _ffMpegArguments; private Action? _onPercentageProgress; private Action? _onTimeProgress; @@ -22,12 +23,13 @@ public class FFMpegArgumentProcessor internal FFMpegArgumentProcessor(FFMpegArguments ffMpegArguments) { + _configurations = new List>(); _ffMpegArguments = ffMpegArguments; } public string Arguments => _ffMpegArguments.Text; - private event EventHandler CancelEvent = null!; + private event EventHandler CancelEvent = null!; /// /// Register action that will be invoked during the ffmpeg processing, when a progress time is output and parsed and progress percentage is calculated. @@ -70,10 +72,15 @@ public FFMpegArgumentProcessor CancellableThrough(CancellationToken token, int t token.Register(() => CancelEvent?.Invoke(this, timeout)); return this; } + public FFMpegArgumentProcessor Configure(Action configureOptions) + { + _configurations.Add(configureOptions); + return this; + } public bool ProcessSynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null) { - using var instance = PrepareInstance(ffMpegOptions ?? GlobalFFOptions.Current, out var cancellationTokenSource); - var errorCode = -1; + var options = GetConfiguredOptions(ffMpegOptions); + using var instance = PrepareInstance(options, out var cancellationTokenSource); void OnCancelEvent(object sender, int timeout) { @@ -87,7 +94,8 @@ void OnCancelEvent(object sender, int timeout) } CancelEvent += OnCancelEvent; instance.Exited += delegate { cancellationTokenSource.Cancel(); }; - + + var errorCode = -1; try { errorCode = Process(instance, cancellationTokenSource).ConfigureAwait(false).GetAwaiter().GetResult(); @@ -100,14 +108,14 @@ void OnCancelEvent(object sender, int timeout) { CancelEvent -= OnCancelEvent; } - + return HandleCompletion(throwOnError, errorCode, instance.ErrorData); } public async Task ProcessAsynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null) { - using var instance = PrepareInstance(ffMpegOptions ?? GlobalFFOptions.Current, out var cancellationTokenSource); - var errorCode = -1; + var options = GetConfiguredOptions(ffMpegOptions); + using var instance = PrepareInstance(options, out var cancellationTokenSource); void OnCancelEvent(object sender, int timeout) { @@ -120,7 +128,8 @@ void OnCancelEvent(object sender, int timeout) } } CancelEvent += OnCancelEvent; - + + var errorCode = -1; try { errorCode = await Process(instance, cancellationTokenSource).ConfigureAwait(false); @@ -163,6 +172,18 @@ private bool HandleCompletion(bool throwOnError, int exitCode, IReadOnlyList /// Working directory for the ffmpeg/ffprobe instance @@ -27,16 +28,24 @@ public class FFOptions public Encoding Encoding { get; set; } = Encoding.Default; /// - /// + /// /// public Dictionary ExtensionOverrides { get; set; } = new Dictionary { { "mpegts", ".ts" }, }; - + /// /// Whether to cache calls to get ffmpeg codec, pixel- and container-formats /// public bool UseCache { get; set; } = true; + + /// + object ICloneable.Clone() => Clone(); + + /// + /// Creates a new object that is a copy of the current instance. + /// + public FFOptions Clone() => (FFOptions)MemberwiseClone(); } } \ No newline at end of file