From e24b652695ee1805454f6a16b4037e89db146cc1 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Tue, 25 Feb 2020 23:19:30 +0100 Subject: [PATCH] Add support for drawtext argument Former-commit-id: ebd9ff0adc978a4ddc8ade9ad196caee37c7a625 --- FFMpegCore.Test/ArgumentBuilderTest.cs | 34 ++++++++++++++++++- .../FFMPEG/Argument/Atoms/DrawTextArgument.cs | 31 +++++++++++++++++ FFMpegCore/FFMPEG/FFMpeg.cs | 2 +- FFMpegCore/FFMpegCore.csproj | 4 +-- 4 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 FFMpegCore/FFMPEG/Argument/Atoms/DrawTextArgument.cs diff --git a/FFMpegCore.Test/ArgumentBuilderTest.cs b/FFMpegCore.Test/ArgumentBuilderTest.cs index 9dd1b9a..2b605b1 100644 --- a/FFMpegCore.Test/ArgumentBuilderTest.cs +++ b/FFMpegCore.Test/ArgumentBuilderTest.cs @@ -3,6 +3,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; +using FFMpegCore.FFMPEG; namespace FFMpegCore.Test { @@ -174,6 +175,37 @@ public void Builder_BuildString_Speed() Assert.AreEqual(str, "-i \"input.mp4\" -preset fast \"output.mp4\""); } + [TestMethod] + public void Builder_BuildString_DrawtextFilter() + { + + var container = new ArgumentContainer + { + new InputArgument("input.mp4"), + new DrawTextArgument("Stack Overflow", "/path/to/font.ttf", + ("fontcolor", "white"), + ("fontsize", "24"), + ("box", "1"), + ("boxcolor", "black@0.5"), + ("boxborderw", "5"), + ("x", "(w-text_w)/2"), + ("y", "(h-text_h)/2")), + new OutputArgument("output.mp4") + }; + var result = new FFMpeg().Convert(container); + + var str = GetArgumentsString(new DrawTextArgument("Stack Overflow", "/path/to/font.ttf", + ("fontcolor", "white"), + ("fontsize", "24"), + ("box", "1"), + ("boxcolor", "black@0.5"), + ("boxborderw", "5"), + ("x", "(w-text_w)/2"), + ("y", "(h-text_h)/2"))); + + Assert.AreEqual("-i \"input.mp4\" -vf drawtext=\"text='Stack Overflow': fontfile=/path/to/font.ttf: fontcolor=white: fontsize=24: box=1: boxcolor=black@0.5: boxborderw=5: x=(w-text_w)/2: y=(h-text_h)/2\" \"output.mp4\"", str); + } + [TestMethod] public void Builder_BuildString_StartNumber() { @@ -186,7 +218,7 @@ public void Builder_BuildString_StartNumber() public void Builder_BuildString_Threads_1() { var str = GetArgumentsString(new ThreadsArgument(50)); - + Assert.AreEqual(str, "-i \"input.mp4\" -threads 50 \"output.mp4\""); } diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/DrawTextArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/DrawTextArgument.cs new file mode 100644 index 0000000..5daf264 --- /dev/null +++ b/FFMpegCore/FFMPEG/Argument/Atoms/DrawTextArgument.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +using System.Linq; + +namespace FFMpegCore.FFMPEG.Argument +{ + /// + /// Drawtext video filter argument + /// + public class DrawTextArgument : Argument> + { + public DrawTextArgument(string text, string fontPath, params (string, string)[] optionalArguments) + : base(new[] {("text", text), ("fontfile", fontPath)}.Concat(optionalArguments)) + { + } + + public override string GetStringValue() + { + return $"-vf drawtext=\"{string.Join(": ", Value.Select(FormatArgumentPair))}\" "; + } + + private static string FormatArgumentPair((string key, string value) pair) + { + return $"{pair.key}={EncloseIfContainsSpace(pair.value)}"; + } + + private static string EncloseIfContainsSpace(string input) + { + return input.Contains(" ") ? $"'{input}'" : input; + } + } +} \ No newline at end of file diff --git a/FFMpegCore/FFMPEG/FFMpeg.cs b/FFMpegCore/FFMPEG/FFMpeg.cs index 783530b..be5412d 100644 --- a/FFMpegCore/FFMPEG/FFMpeg.cs +++ b/FFMpegCore/FFMPEG/FFMpeg.cs @@ -437,7 +437,7 @@ public VideoInfo ReplaceAudio(VideoInfo source, FileInfo audio, FileInfo output, return new VideoInfo(output); } - + public VideoInfo Convert(ArgumentContainer arguments) { var output = ((OutputArgument) arguments[typeof(OutputArgument)]).GetAsFileInfo(); diff --git a/FFMpegCore/FFMpegCore.csproj b/FFMpegCore/FFMpegCore.csproj index 3b45e3c..4bee5f9 100644 --- a/FFMpegCore/FFMpegCore.csproj +++ b/FFMpegCore/FFMpegCore.csproj @@ -10,9 +10,9 @@ 1.0.12 1.1.0.0 1.1.0.0 - Minor fixes and refactoring + Add support for drawtext 8 - 1.1.0 + 1.2.0 Vlad Jerca, Malte Rosenbjerg ffmpeg ffprobe convert video audio mediafile resize analyze muxing GitHub