From 5fb2305b5b05627dc116de13ae9e5279304fcaed Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Sat, 24 Oct 2020 23:33:44 +0200 Subject: [PATCH] Add HardwareAccelerationArgument --- FFMpegCore.Test/ArgumentBuilderTest.cs | 13 +++++++++++++ .../FFMpeg/Arguments/ForcePixelFormat.cs | 2 +- .../Arguments/HardwareAccelerationArgument.cs | 18 ++++++++++++++++++ .../FFMpeg/Enums/HardwareAccelerationDevice.cs | 14 ++++++++++++++ FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs | 1 + 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 FFMpegCore/FFMpeg/Arguments/HardwareAccelerationArgument.cs create mode 100644 FFMpegCore/FFMpeg/Enums/HardwareAccelerationDevice.cs diff --git a/FFMpegCore.Test/ArgumentBuilderTest.cs b/FFMpegCore.Test/ArgumentBuilderTest.cs index 6421560..6c79d98 100644 --- a/FFMpegCore.Test/ArgumentBuilderTest.cs +++ b/FFMpegCore.Test/ArgumentBuilderTest.cs @@ -61,6 +61,19 @@ public void Builder_BuildString_BitStream() var str = FFMpegArguments.FromFileInput("input.mp4").OutputToFile("output.mp4", false, opt => opt.WithBitStreamFilter(Channel.Audio, Filter.H264_Mp4ToAnnexB)).Arguments; Assert.AreEqual("-i \"input.mp4\" -bsf:a h264_mp4toannexb \"output.mp4\"", str); } + + [TestMethod] + public void Builder_BuildString_HardwareAcceleration_Auto() + { + var str = FFMpegArguments.FromFileInput("input.mp4").OutputToFile("output.mp4", false, opt => opt.WithHardwareAcceleration()).Arguments; + Assert.AreEqual("-i \"input.mp4\" -hwaccel \"output.mp4\"", str); + } + [TestMethod] + public void Builder_BuildString_HardwareAcceleration_Specific() + { + var str = FFMpegArguments.FromFileInput("input.mp4").OutputToFile("output.mp4", false, opt => opt.WithHardwareAcceleration(HardwareAccelerationDevice.CUVID)).Arguments; + Assert.AreEqual("-i \"input.mp4\" -hwaccel cuvid \"output.mp4\"", str); + } [TestMethod] public void Builder_BuildString_Concat() diff --git a/FFMpegCore/FFMpeg/Arguments/ForcePixelFormat.cs b/FFMpegCore/FFMpeg/Arguments/ForcePixelFormat.cs index 7614ae3..8402552 100644 --- a/FFMpegCore/FFMpeg/Arguments/ForcePixelFormat.cs +++ b/FFMpegCore/FFMpeg/Arguments/ForcePixelFormat.cs @@ -4,7 +4,7 @@ namespace FFMpegCore.Arguments { public class ForcePixelFormat : IArgument { - public string PixelFormat { get; private set; } + public string PixelFormat { get; } public string Text => $"-pix_fmt {PixelFormat}"; public ForcePixelFormat(string format) diff --git a/FFMpegCore/FFMpeg/Arguments/HardwareAccelerationArgument.cs b/FFMpegCore/FFMpeg/Arguments/HardwareAccelerationArgument.cs new file mode 100644 index 0000000..da4b9ee --- /dev/null +++ b/FFMpegCore/FFMpeg/Arguments/HardwareAccelerationArgument.cs @@ -0,0 +1,18 @@ +using FFMpegCore.Enums; + +namespace FFMpegCore.Arguments +{ + public class HardwareAccelerationArgument : IArgument + { + public HardwareAccelerationDevice HardwareAccelerationDevice { get; } + + public HardwareAccelerationArgument(HardwareAccelerationDevice hardwareAccelerationDevice) + { + HardwareAccelerationDevice = hardwareAccelerationDevice; + } + + public string Text => HardwareAccelerationDevice != HardwareAccelerationDevice.Auto + ? $"-hwaccel {HardwareAccelerationDevice.ToString().ToLower()}" + : "-hwaccel"; + } +} \ No newline at end of file diff --git a/FFMpegCore/FFMpeg/Enums/HardwareAccelerationDevice.cs b/FFMpegCore/FFMpeg/Enums/HardwareAccelerationDevice.cs new file mode 100644 index 0000000..1d92f53 --- /dev/null +++ b/FFMpegCore/FFMpeg/Enums/HardwareAccelerationDevice.cs @@ -0,0 +1,14 @@ +namespace FFMpegCore.Enums +{ + public enum HardwareAccelerationDevice + { + Auto, + D3D11VA, + DXVA2, + QSV, + CUVID, + VDPAU, + VAAPI, + LibMFX + } +} \ No newline at end of file diff --git a/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs b/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs index a50d370..e41f8ee 100644 --- a/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs +++ b/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs @@ -31,6 +31,7 @@ internal FFMpegArgumentOptions() { } public FFMpegArgumentOptions WithDuration(TimeSpan? duration) => WithArgument(new DurationArgument(duration)); public FFMpegArgumentOptions WithFastStart() => WithArgument(new FaststartArgument()); public FFMpegArgumentOptions WithFrameOutputCount(int frames) => WithArgument(new FrameOutputCountArgument(frames)); + public FFMpegArgumentOptions WithHardwareAcceleration(HardwareAccelerationDevice hardwareAccelerationDevice = HardwareAccelerationDevice.Auto) => WithArgument(new HardwareAccelerationArgument(hardwareAccelerationDevice)); public FFMpegArgumentOptions UsingShortest(bool shortest = true) => WithArgument(new ShortestArgument(shortest)); public FFMpegArgumentOptions UsingMultithreading(bool multithread) => WithArgument(new ThreadsArgument(multithread));