From 27a2219b8e86a8e62172c8e1ecd09a48fd076439 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Fri, 14 May 2021 01:02:00 +0200 Subject: [PATCH] Fixes, renames and missing stuff --- FFMpegCore.Test/ArgumentBuilderTest.cs | 11 ++++++ FFMpegCore/FFMpeg/Arguments/SetMirrorVideo.cs | 36 ------------------- .../FFMpeg/Arguments/SetMirroringArgument.cs | 32 +++++++++++++++++ .../FFMpeg/Arguments/VideoFiltersArgument.cs | 13 +++++-- .../FFMpeg/Enums/{Mirror.cs => Mirroring.cs} | 2 +- 5 files changed, 55 insertions(+), 39 deletions(-) delete mode 100644 FFMpegCore/FFMpeg/Arguments/SetMirrorVideo.cs create mode 100644 FFMpegCore/FFMpeg/Arguments/SetMirroringArgument.cs rename FFMpegCore/FFMpeg/Enums/{Mirror.cs => Mirroring.cs} (76%) diff --git a/FFMpegCore.Test/ArgumentBuilderTest.cs b/FFMpegCore.Test/ArgumentBuilderTest.cs index 18a8c7d..daa3eda 100644 --- a/FFMpegCore.Test/ArgumentBuilderTest.cs +++ b/FFMpegCore.Test/ArgumentBuilderTest.cs @@ -192,6 +192,17 @@ public void Builder_BuildString_Transpose() Assert.AreEqual("-i \"input.mp4\" -vf \"transpose=2\" \"output.mp4\"", str); } + [TestMethod] + public void Builder_BuildString_Mirroring() + { + var str = FFMpegArguments.FromFileInput("input.mp4") + .OutputToFile("output.mp4", false, opt => opt + .WithVideoFilters(filterOptions => filterOptions + .Mirror(Mirroring.Horizontal))) + .Arguments; + Assert.AreEqual("-i \"input.mp4\" -vf \"hflip\" \"output.mp4\"", str); + } + [TestMethod] public void Builder_BuildString_TransposeScale() { diff --git a/FFMpegCore/FFMpeg/Arguments/SetMirrorVideo.cs b/FFMpegCore/FFMpeg/Arguments/SetMirrorVideo.cs deleted file mode 100644 index d394856..0000000 --- a/FFMpegCore/FFMpeg/Arguments/SetMirrorVideo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using FFMpegCore.Enums; -using System; -using System.Collections.Generic; -using System.Text; - -namespace FFMpegCore.Arguments -{ - public class SetMirrorVideo : IVideoFilterArgument - { - public SetMirrorVideo(Mirror value) - { - _value = value; - } - - public Mirror _value { get; set; } - - public string Key => string.Empty; - - public string Value - { - get - { - switch (_value) - { - case Mirror.Horizontal: - return "hflip"; - case Mirror.Vertical: - return "vflip"; - default: - throw new ArgumentOutOfRangeException("SetMirrorVideo: argument not found"); - } - - } - } - } -} diff --git a/FFMpegCore/FFMpeg/Arguments/SetMirroringArgument.cs b/FFMpegCore/FFMpeg/Arguments/SetMirroringArgument.cs new file mode 100644 index 0000000..f042f77 --- /dev/null +++ b/FFMpegCore/FFMpeg/Arguments/SetMirroringArgument.cs @@ -0,0 +1,32 @@ +using FFMpegCore.Enums; +using System; +using System.Collections.Generic; +using System.Text; + +namespace FFMpegCore.Arguments +{ + public class SetMirroringArgument : IVideoFilterArgument + { + public SetMirroringArgument(Mirroring mirroring) + { + Mirroring = mirroring; + } + + public Mirroring Mirroring { get; set; } + + public string Key => string.Empty; + + public string Value + { + get + { + return Mirroring switch + { + Mirroring.Horizontal => "hflip", + Mirroring.Vertical => "vflip", + _ => throw new ArgumentOutOfRangeException(nameof(Mirroring)) + }; + } + } + } +} diff --git a/FFMpegCore/FFMpeg/Arguments/VideoFiltersArgument.cs b/FFMpegCore/FFMpeg/Arguments/VideoFiltersArgument.cs index f7fef93..fa4ae1e 100644 --- a/FFMpegCore/FFMpeg/Arguments/VideoFiltersArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/VideoFiltersArgument.cs @@ -17,12 +17,20 @@ public VideoFiltersArgument(VideoFilterOptions options) public string Text => GetText(); - public string GetText() + private string GetText() { if (!Options.Arguments.Any()) throw new FFMpegArgumentException("No video-filter arguments provided"); - return $"-vf \"{string.Join(", ", Options.Arguments.Where(arg => !string.IsNullOrEmpty(arg.Value)).Select(arg => $"{arg.Key}={arg.Value.Replace(",", "\\,")}"))}\""; + 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 $"-vf \"{string.Join(", ", arguments)}\""; } } @@ -40,6 +48,7 @@ public class VideoFilterOptions public VideoFilterOptions Scale(int width, int height) => WithArgument(new ScaleArgument(width, height)); public VideoFilterOptions Scale(Size size) => WithArgument(new ScaleArgument(size)); public VideoFilterOptions Transpose(Transposition transposition) => WithArgument(new TransposeArgument(transposition)); + public VideoFilterOptions Mirror(Mirroring mirroring) => WithArgument(new SetMirroringArgument(mirroring)); public VideoFilterOptions DrawText(DrawTextOptions drawTextOptions) => WithArgument(new DrawTextArgument(drawTextOptions)); private VideoFilterOptions WithArgument(IVideoFilterArgument argument) diff --git a/FFMpegCore/FFMpeg/Enums/Mirror.cs b/FFMpegCore/FFMpeg/Enums/Mirroring.cs similarity index 76% rename from FFMpegCore/FFMpeg/Enums/Mirror.cs rename to FFMpegCore/FFMpeg/Enums/Mirroring.cs index 72e98e4..5768163 100644 --- a/FFMpegCore/FFMpeg/Enums/Mirror.cs +++ b/FFMpegCore/FFMpeg/Enums/Mirroring.cs @@ -1,6 +1,6 @@ namespace FFMpegCore.Enums { - public enum Mirror + public enum Mirroring { Vertical, Horizontal