Add support for drawtext argument

This commit is contained in:
Malte Rosenbjerg 2020-02-25 23:19:30 +01:00
parent 6e37592e24
commit ebd9ff0adc
4 changed files with 67 additions and 4 deletions

View file

@ -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\"");
}

View file

@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Linq;
namespace FFMpegCore.FFMPEG.Argument
{
/// <summary>
/// Drawtext video filter argument
/// </summary>
public class DrawTextArgument : Argument<IEnumerable<(string key, string value)>>
{
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;
}
}
}

View file

@ -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();

View file

@ -10,9 +10,9 @@
<Version>1.0.12</Version>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<PackageReleaseNotes>Minor fixes and refactoring</PackageReleaseNotes>
<PackageReleaseNotes>Add support for drawtext</PackageReleaseNotes>
<LangVersion>8</LangVersion>
<PackageVersion>1.1.0</PackageVersion>
<PackageVersion>1.2.0</PackageVersion>
<Authors>Vlad Jerca, Malte Rosenbjerg</Authors>
<PackageTags>ffmpeg ffprobe convert video audio mediafile resize analyze muxing</PackageTags>
<RepositoryType>GitHub</RepositoryType>