From c6f1d94a74112d8d4949e1b6d35295b5ebb83bbf Mon Sep 17 00:00:00 2001 From: alex6dj Date: Mon, 4 Oct 2021 09:54:38 -0400 Subject: [PATCH] Initial audio filter implementation Former-commit-id: 78a703fc938c3c44e9bf967c5ce8bf74758580ea --- .../FFMpeg/Arguments/AudioFiltersArgument.cs | 53 +++++++++++++++++++ FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs | 7 +++ 2 files changed, 60 insertions(+) create mode 100644 FFMpegCore/FFMpeg/Arguments/AudioFiltersArgument.cs diff --git a/FFMpegCore/FFMpeg/Arguments/AudioFiltersArgument.cs b/FFMpegCore/FFMpeg/Arguments/AudioFiltersArgument.cs new file mode 100644 index 0000000..834b784 --- /dev/null +++ b/FFMpegCore/FFMpeg/Arguments/AudioFiltersArgument.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using FFMpegCore.Enums; +using FFMpegCore.Exceptions; + +namespace FFMpegCore.Arguments +{ + public class AudioFiltersArgument : IArgument + { + public readonly AudioFilterOptions Options; + + public AudioFiltersArgument(AudioFilterOptions options) + { + Options = options; + } + + public string Text => GetText(); + + private string GetText() + { + if (!Options.Arguments.Any()) + throw new FFMpegArgumentException("No audio-filter arguments provided"); + + var arguments = Options.Arguments + .Where(arg => !string.IsNullOrEmpty(arg.Value)) + .Select(arg => + { + var escapedValue = arg.Value.Replace(",", "\\,"); + return string.IsNullOrEmpty(arg.Key) ? escapedValue : $"{arg.Key}={escapedValue}"; + }); + + return $"-af \"{string.Join(", ", arguments)}\""; + } + } + + public interface IAudioFilterArgument + { + public string Key { get; } + public string Value { get; } + } + + public class AudioFilterOptions + { + public List Arguments { get; } = new List(); + + private AudioFilterOptions WithArgument(IAudioFilterArgument argument) + { + Arguments.Add(argument); + return this; + } + } +} \ No newline at end of file diff --git a/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs b/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs index 41ac38c..ca6628a 100644 --- a/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs +++ b/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs @@ -43,6 +43,13 @@ public FFMpegArgumentOptions WithVideoFilters(Action videoFi return WithArgument(new VideoFiltersArgument(videoFilterOptionsObj)); } + public FFMpegArgumentOptions WithAudioFilters(Action audioFilterOptions) + { + var audioFilterOptionsObj = new AudioFilterOptions(); + audioFilterOptions(audioFilterOptionsObj); + return WithArgument(new AudioFiltersArgument(audioFilterOptionsObj)); + } + public FFMpegArgumentOptions WithFramerate(double framerate) => WithArgument(new FrameRateArgument(framerate)); public FFMpegArgumentOptions WithoutMetadata() => WithArgument(new RemoveMetadataArgument()); public FFMpegArgumentOptions WithSpeedPreset(Speed speed) => WithArgument(new SpeedPresetArgument(speed));