153 行
5.2 KiB
C#
153 行
5.2 KiB
C#
using System.Collections.ObjectModel;
|
|
using ProxyForge.Encoding;
|
|
using ProxyForge.Sources;
|
|
|
|
namespace ProxyForge.Tests;
|
|
|
|
public sealed class FFmpegArgumentsTests
|
|
{
|
|
[Fact]
|
|
public void WriteEncodeEmitsTheRawVideoPipeline()
|
|
{
|
|
var request = new FFmpegEncodeRequest(@"C:\cache\proxy.mp4", 960, 540, new FrameRate(30000, 1001), 544_320, 30, "h264_qsv");
|
|
|
|
var arguments = new Collection<string>();
|
|
FFmpegArguments.WriteEncode(arguments, in request);
|
|
|
|
string[] expected =
|
|
[
|
|
"-hide_banner",
|
|
"-loglevel", "error",
|
|
"-y",
|
|
"-f", "rawvideo",
|
|
"-pix_fmt", "bgra",
|
|
"-s", "960x540",
|
|
"-r", "30000/1001",
|
|
"-i", "-",
|
|
"-an", "-sn", "-dn",
|
|
"-c:v", "h264_qsv",
|
|
"-b:v", "544320",
|
|
"-g", "30",
|
|
"-pix_fmt", "yuv420p",
|
|
"-colorspace", "bt709",
|
|
"-color_primaries", "bt709",
|
|
"-color_trc", "bt709",
|
|
"-color_range", "tv",
|
|
"-f", "mp4",
|
|
@"C:\cache\proxy.mp4",
|
|
];
|
|
|
|
Assert.Equal(expected, arguments);
|
|
}
|
|
|
|
[Fact]
|
|
public void WriteEncodeKeepsStandardInputOpen()
|
|
{
|
|
var request = new FFmpegEncodeRequest(@"C:\cache\proxy.mp4", 320, 240, new FrameRate(30, 1), 100_000, 1, "h264");
|
|
|
|
var arguments = new Collection<string>();
|
|
FFmpegArguments.WriteEncode(arguments, in request);
|
|
|
|
Assert.DoesNotContain("-nostdin", arguments);
|
|
}
|
|
|
|
[Fact]
|
|
public void WriteEncodePassesTheOutputPathWithoutQuoting()
|
|
{
|
|
var request = new FFmpegEncodeRequest(@"C:\cache dir\a b'c,d.mp4", 320, 240, new FrameRate(30, 1), 100_000, 1, "h264");
|
|
|
|
var arguments = new Collection<string>();
|
|
FFmpegArguments.WriteEncode(arguments, in request);
|
|
|
|
Assert.Contains(@"C:\cache dir\a b'c,d.mp4", arguments);
|
|
Assert.DoesNotContain(arguments, argument => argument.Contains('"'));
|
|
}
|
|
|
|
[Fact]
|
|
public void WriteEncoderProbeEncodesASingleBlackFrameToNothing()
|
|
{
|
|
var arguments = new Collection<string>();
|
|
FFmpegArguments.WriteEncoderProbe(arguments, "h264_nvenc", 960, 540);
|
|
|
|
string[] expected =
|
|
[
|
|
"-hide_banner",
|
|
"-nostdin",
|
|
"-loglevel", "error",
|
|
"-f", "lavfi",
|
|
"-i", "color=c=black:s=960x540:r=30",
|
|
"-frames:v", "1",
|
|
"-pix_fmt", "yuv420p",
|
|
"-c:v", "h264_nvenc",
|
|
"-f", "null",
|
|
"-",
|
|
];
|
|
|
|
Assert.Equal(expected, arguments);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(960, 540, "960x540")]
|
|
[InlineData(2, 2, "2x2")]
|
|
[InlineData(3840, 2160, "3840x2160")]
|
|
public void BuildFrameSizeUsesInvariantNumbers(int width, int height, string expected)
|
|
=> Assert.Equal(expected, FFmpegArguments.BuildFrameSize(width, height));
|
|
|
|
[Theory]
|
|
[InlineData(30, 1, "30/1")]
|
|
[InlineData(30000, 1001, "30000/1001")]
|
|
[InlineData(24, 1, "24/1")]
|
|
public void BuildFrameRateEmitsTheRationalForm(int numerator, int denominator, string expected)
|
|
=> Assert.Equal(expected, FFmpegArguments.BuildFrameRate(new FrameRate(numerator, denominator)));
|
|
|
|
[Fact]
|
|
public void BuildColorSourceMatchesTheLavfiSyntax()
|
|
=> Assert.Equal("color=c=black:s=1280x720:r=30", FFmpegArguments.BuildColorSource(1280, 720));
|
|
|
|
[Fact]
|
|
public void CalculateVideoBitrateScalesThePixelRate()
|
|
=> Assert.Equal(544_320L, FFmpegArguments.CalculateVideoBitrate(960, 540, 30d, 50));
|
|
|
|
[Fact]
|
|
public void CalculateVideoBitrateAtFullScaleIsTwiceTheHalfScale()
|
|
=> Assert.Equal(2L * FFmpegArguments.CalculateVideoBitrate(960, 540, 30d, 50), FFmpegArguments.CalculateVideoBitrate(960, 540, 30d, 100));
|
|
|
|
[Fact]
|
|
public void CalculateVideoBitrateClampsToTheMinimum()
|
|
=> Assert.Equal(100_000L, FFmpegArguments.CalculateVideoBitrate(2, 2, 1d, 1));
|
|
|
|
[Fact]
|
|
public void CalculateVideoBitrateClampsToTheMaximum()
|
|
=> Assert.Equal(100_000_000L, FFmpegArguments.CalculateVideoBitrate(7680, 4320, 240d, 200));
|
|
|
|
[Theory]
|
|
[InlineData(0d)]
|
|
[InlineData(-1d)]
|
|
[InlineData(double.NaN)]
|
|
[InlineData(double.PositiveInfinity)]
|
|
public void CalculateVideoBitrateUsesTheDefaultRateForAnUnknownFrameRate(double frameRate)
|
|
=> Assert.Equal(
|
|
FFmpegArguments.CalculateVideoBitrate(960, 540, FFmpegArguments.DefaultFrameRate, 50),
|
|
FFmpegArguments.CalculateVideoBitrate(960, 540, frameRate, 50));
|
|
|
|
[Fact]
|
|
public void CalculateVideoBitrateClampsAnExcessiveFrameRate()
|
|
=> Assert.Equal(
|
|
FFmpegArguments.CalculateVideoBitrate(960, 540, 240d, 50),
|
|
FFmpegArguments.CalculateVideoBitrate(960, 540, 100_000d, 50));
|
|
|
|
[Fact]
|
|
public void CalculateVideoBitrateClampsATinyFrameRate()
|
|
=> Assert.Equal(
|
|
FFmpegArguments.CalculateVideoBitrate(960, 540, 1d, 50),
|
|
FFmpegArguments.CalculateVideoBitrate(960, 540, 0.001d, 50));
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(-1)]
|
|
public void CalculateVideoBitrateTreatsANonPositiveScaleAsFullScale(int scale)
|
|
=> Assert.Equal(
|
|
FFmpegArguments.CalculateVideoBitrate(960, 540, 30d, 100),
|
|
FFmpegArguments.CalculateVideoBitrate(960, 540, 30d, scale));
|
|
}
|