mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2025-01-18 20:46:43 +00:00
Add Configure on FFMpegArgumentProcessor to fuently configure ffoptions per run.
Former-commit-id: 965e756dc4
This commit is contained in:
parent
eb880884bd
commit
a5b5965924
2 changed files with 42 additions and 12 deletions
|
@ -14,6 +14,7 @@ namespace FFMpegCore
|
||||||
public class FFMpegArgumentProcessor
|
public class FFMpegArgumentProcessor
|
||||||
{
|
{
|
||||||
private static readonly Regex ProgressRegex = new Regex(@"time=(\d\d:\d\d:\d\d.\d\d?)", RegexOptions.Compiled);
|
private static readonly Regex ProgressRegex = new Regex(@"time=(\d\d:\d\d:\d\d.\d\d?)", RegexOptions.Compiled);
|
||||||
|
private readonly List<Action<FFOptions>> _configurations;
|
||||||
private readonly FFMpegArguments _ffMpegArguments;
|
private readonly FFMpegArguments _ffMpegArguments;
|
||||||
private Action<double>? _onPercentageProgress;
|
private Action<double>? _onPercentageProgress;
|
||||||
private Action<TimeSpan>? _onTimeProgress;
|
private Action<TimeSpan>? _onTimeProgress;
|
||||||
|
@ -22,12 +23,13 @@ public class FFMpegArgumentProcessor
|
||||||
|
|
||||||
internal FFMpegArgumentProcessor(FFMpegArguments ffMpegArguments)
|
internal FFMpegArgumentProcessor(FFMpegArguments ffMpegArguments)
|
||||||
{
|
{
|
||||||
|
_configurations = new List<Action<FFOptions>>();
|
||||||
_ffMpegArguments = ffMpegArguments;
|
_ffMpegArguments = ffMpegArguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Arguments => _ffMpegArguments.Text;
|
public string Arguments => _ffMpegArguments.Text;
|
||||||
|
|
||||||
private event EventHandler<int> CancelEvent = null!;
|
private event EventHandler<int> CancelEvent = null!;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Register action that will be invoked during the ffmpeg processing, when a progress time is output and parsed and progress percentage is calculated.
|
/// 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));
|
token.Register(() => CancelEvent?.Invoke(this, timeout));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public FFMpegArgumentProcessor Configure(Action<FFOptions> configureOptions)
|
||||||
|
{
|
||||||
|
_configurations.Add(configureOptions);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
public bool ProcessSynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null)
|
public bool ProcessSynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null)
|
||||||
{
|
{
|
||||||
using var instance = PrepareInstance(ffMpegOptions ?? GlobalFFOptions.Current, out var cancellationTokenSource);
|
var options = GetConfiguredOptions(ffMpegOptions);
|
||||||
var errorCode = -1;
|
using var instance = PrepareInstance(options, out var cancellationTokenSource);
|
||||||
|
|
||||||
void OnCancelEvent(object sender, int timeout)
|
void OnCancelEvent(object sender, int timeout)
|
||||||
{
|
{
|
||||||
|
@ -87,7 +94,8 @@ void OnCancelEvent(object sender, int timeout)
|
||||||
}
|
}
|
||||||
CancelEvent += OnCancelEvent;
|
CancelEvent += OnCancelEvent;
|
||||||
instance.Exited += delegate { cancellationTokenSource.Cancel(); };
|
instance.Exited += delegate { cancellationTokenSource.Cancel(); };
|
||||||
|
|
||||||
|
var errorCode = -1;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
errorCode = Process(instance, cancellationTokenSource).ConfigureAwait(false).GetAwaiter().GetResult();
|
errorCode = Process(instance, cancellationTokenSource).ConfigureAwait(false).GetAwaiter().GetResult();
|
||||||
|
@ -100,14 +108,14 @@ void OnCancelEvent(object sender, int timeout)
|
||||||
{
|
{
|
||||||
CancelEvent -= OnCancelEvent;
|
CancelEvent -= OnCancelEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
return HandleCompletion(throwOnError, errorCode, instance.ErrorData);
|
return HandleCompletion(throwOnError, errorCode, instance.ErrorData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> ProcessAsynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null)
|
public async Task<bool> ProcessAsynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null)
|
||||||
{
|
{
|
||||||
using var instance = PrepareInstance(ffMpegOptions ?? GlobalFFOptions.Current, out var cancellationTokenSource);
|
var options = GetConfiguredOptions(ffMpegOptions);
|
||||||
var errorCode = -1;
|
using var instance = PrepareInstance(options, out var cancellationTokenSource);
|
||||||
|
|
||||||
void OnCancelEvent(object sender, int timeout)
|
void OnCancelEvent(object sender, int timeout)
|
||||||
{
|
{
|
||||||
|
@ -120,7 +128,8 @@ void OnCancelEvent(object sender, int timeout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CancelEvent += OnCancelEvent;
|
CancelEvent += OnCancelEvent;
|
||||||
|
|
||||||
|
var errorCode = -1;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
errorCode = await Process(instance, cancellationTokenSource).ConfigureAwait(false);
|
errorCode = await Process(instance, cancellationTokenSource).ConfigureAwait(false);
|
||||||
|
@ -163,6 +172,18 @@ private bool HandleCompletion(bool throwOnError, int exitCode, IReadOnlyList<str
|
||||||
return exitCode == 0;
|
return exitCode == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private FFOptions GetConfiguredOptions(FFOptions? ffOptions)
|
||||||
|
{
|
||||||
|
var options = ffOptions ?? GlobalFFOptions.Current.Clone();
|
||||||
|
|
||||||
|
foreach (var configureOptions in _configurations)
|
||||||
|
{
|
||||||
|
configureOptions(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
private Instance PrepareInstance(FFOptions ffOptions,
|
private Instance PrepareInstance(FFOptions ffOptions,
|
||||||
out CancellationTokenSource cancellationTokenSource)
|
out CancellationTokenSource cancellationTokenSource)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FFMpegCore
|
namespace FFMpegCore
|
||||||
{
|
{
|
||||||
public class FFOptions
|
public class FFOptions : ICloneable
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Working directory for the ffmpeg/ffprobe instance
|
/// Working directory for the ffmpeg/ffprobe instance
|
||||||
|
@ -27,16 +28,24 @@ public class FFOptions
|
||||||
public Encoding Encoding { get; set; } = Encoding.Default;
|
public Encoding Encoding { get; set; } = Encoding.Default;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Dictionary<string, string> ExtensionOverrides { get; set; } = new Dictionary<string, string>
|
public Dictionary<string, string> ExtensionOverrides { get; set; } = new Dictionary<string, string>
|
||||||
{
|
{
|
||||||
{ "mpegts", ".ts" },
|
{ "mpegts", ".ts" },
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether to cache calls to get ffmpeg codec, pixel- and container-formats
|
/// Whether to cache calls to get ffmpeg codec, pixel- and container-formats
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool UseCache { get; set; } = true;
|
public bool UseCache { get; set; } = true;
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
object ICloneable.Clone() => Clone();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new object that is a copy of the current instance.
|
||||||
|
/// </summary>
|
||||||
|
public FFOptions Clone() => (FFOptions)MemberwiseClone();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue