Skip to content

Commit

Permalink
Merge pull request #341 from glebmxyz/pad-video-filter
Browse files Browse the repository at this point in the history
Add pad video filter
  • Loading branch information
rosenbjerg authored Jan 31, 2023
2 parents 1ad13e0 + e76ebdc commit 6460f3c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
40 changes: 40 additions & 0 deletions FFMpegCore.Test/ArgumentBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,5 +495,45 @@ public void Builder_BuildString_Audible_AAXC_Decryption()

Assert.AreEqual("-audible_key 123 -audible_iv 456 -i \"input.aaxc\" -map_metadata 0 -id3v2_version 3 -vn -c:a copy \"output.m4b\" -y", str);
}

[TestMethod]
public void Builder_BuildString_PadFilter()
{
var str = FFMpegArguments
.FromFileInput("input.mp4")
.OutputToFile("output.mp4", false, opt => opt
.WithVideoFilters(filterOptions => filterOptions
.Pad(PadOptions
.Create("max(iw,ih)", "ow")
.WithParameter("x", "(ow-iw)/2")
.WithParameter("y", "(oh-ih)/2")
.WithParameter("color", "violet")
.WithParameter("eval", "frame"))))
.Arguments;

Assert.AreEqual(
"-i \"input.mp4\" -vf \"pad=width=max(iw\\,ih):height=ow:x=(ow-iw)/2:y=(oh-ih)/2:color=violet:eval=frame\" \"output.mp4\"",
str);
}

[TestMethod]
public void Builder_BuildString_PadFilter_Alt()
{
var str = FFMpegArguments
.FromFileInput("input.mp4")
.OutputToFile("output.mp4", false, opt => opt
.WithVideoFilters(filterOptions => filterOptions
.Pad(PadOptions
.Create("4/3")
.WithParameter("x", "(ow-iw)/2")
.WithParameter("y", "(oh-ih)/2")
.WithParameter("color", "violet")
.WithParameter("eval", "frame"))))
.Arguments;

Assert.AreEqual(
"-i \"input.mp4\" -vf \"pad=aspect=4/3:x=(ow-iw)/2:y=(oh-ih)/2:color=violet:eval=frame\" \"output.mp4\"",
str);
}
}
}
65 changes: 65 additions & 0 deletions FFMpegCore/FFMpeg/Arguments/PadArgument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FFMpegCore.Extend;

namespace FFMpegCore.Arguments
{
public class PadArgument : IVideoFilterArgument
{
private readonly PadOptions _options;

public PadArgument(PadOptions options)
{
_options = options;
}

public string Key => "pad";
public string Value => _options.TextInternal;

}

public class PadOptions
{
public readonly Dictionary<string, string> Parameters = new Dictionary<string, string>();

internal string TextInternal => string.Join(":", Parameters.Select(parameter => parameter.FormatArgumentPair(true)));

public static PadOptions Create(string? width, string? height)
{
return new PadOptions(width, height);
}

public static PadOptions Create(string aspectRatio)
{
return new PadOptions(aspectRatio);
}

public PadOptions WithParameter(string key, string value)
{
Parameters.Add(key, value);
return this;
}

private PadOptions(string? width, string? height)
{
if (width == null && height == null)
{
throw new Exception("At least one of the parameters must be not null");
}
if (width != null)
{
Parameters.Add("width", width);
}
if (height != null)
{
Parameters.Add("height", height);
}
}

private PadOptions(string aspectRatio)
{
Parameters.Add("aspect", aspectRatio);
}
}
}
1 change: 1 addition & 0 deletions FFMpegCore/FFMpeg/Arguments/VideoFiltersArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class VideoFilterOptions
public VideoFilterOptions HardBurnSubtitle(SubtitleHardBurnOptions subtitleHardBurnOptions) => WithArgument(new SubtitleHardBurnArgument(subtitleHardBurnOptions));
public VideoFilterOptions BlackDetect(double minimumDuration = 2.0, double pictureBlackRatioThreshold = 0.98, double pixelBlackThreshold = 0.1) => WithArgument(new BlackDetectArgument(minimumDuration, pictureBlackRatioThreshold, pixelBlackThreshold));
public VideoFilterOptions BlackFrame(int amount = 98, int threshold = 32) => WithArgument(new BlackFrameArgument(amount, threshold));
public VideoFilterOptions Pad(PadOptions padOptions) => WithArgument(new PadArgument(padOptions));

private VideoFilterOptions WithArgument(IVideoFilterArgument argument)
{
Expand Down

0 comments on commit 6460f3c

Please sign in to comment.