66 lines
2.2 KiB
C#
66 lines
2.2 KiB
C#
using System;
|
|
using EncodingSampleTest;
|
|
using FFMpegCore;
|
|
using FFMpegCore.Enums;
|
|
|
|
string ffmpegPath = Path.Combine(Environment.CurrentDirectory, "vendor");
|
|
string tempPath = Path.Combine(Environment.CurrentDirectory, "tmp");
|
|
string outputDir = Path.Combine(Environment.CurrentDirectory, "output");
|
|
Directory.CreateDirectory(outputDir);
|
|
Directory.CreateDirectory(tempPath);
|
|
const string inputFile = "testVid8k.mp4";
|
|
|
|
GlobalFFOptions.Configure(options => {
|
|
options.BinaryFolder = ffmpegPath;
|
|
options.TemporaryFilesFolder = tempPath;
|
|
});
|
|
|
|
if(!File.Exists(inputFile)) return;
|
|
|
|
var mediaInfo = FFProbe.Analyse(inputFile);
|
|
if (mediaInfo.PrimaryVideoStream == null) {
|
|
Console.WriteLine("No video stream found.");
|
|
return;
|
|
}
|
|
var W = mediaInfo.PrimaryVideoStream.Width;
|
|
var H = mediaInfo.PrimaryVideoStream.Height;
|
|
|
|
for (int qp = 32; qp <= 160; qp += 32) {
|
|
await AV1Encode(W, H, qp);
|
|
}
|
|
|
|
/*
|
|
for (int qp = 32; qp <= 250; qp += 32) {
|
|
await AV1Encode(1920, 1080, qp);
|
|
}*/
|
|
|
|
|
|
|
|
Task AV1Encode(int W = -1, int H = -1, int QP = 23) { // AV1 is visually lossless at QP 23
|
|
var outputFile = Path.Combine(outputDir, $"output_av1-{W}x{H}_qp{QP}.mp4");
|
|
var ffmpegArgs = FFMpegArguments
|
|
.FromFileInput(inputFile, true, options => options
|
|
.WithHardwareAcceleration()
|
|
)
|
|
.OutputToFile(outputFile, true, options => options
|
|
.CopyChannel(Channel.Audio)
|
|
.CopyChannel(Channel.Subtitle)
|
|
.WithVideoCodec("hevc_nvenc")
|
|
.WithArgument(new NvencSpeedPreset(NvencSpeed.p4))
|
|
.WithArgument(new NvencTuneArgument(NvencTune.hq))
|
|
.WithArgument(new NvencHighBitDepthArgument(true))
|
|
.WithArgument(new NvencQPArgument((byte)QP))
|
|
.WithVideoFilters(filterOptions => {
|
|
if (W > 0 && H > 0) filterOptions.Scale(W, H);
|
|
})
|
|
.WithFastStart()
|
|
)
|
|
.WithLogLevel(FFMpegLogLevel.Info)
|
|
.NotifyOnProgress(progress => Console.WriteLine($"Encoding {outputFile}: {progress:g}/{mediaInfo.Duration:g} {progress/mediaInfo.Duration:P}"));
|
|
|
|
Console.WriteLine(ffmpegArgs.Arguments);
|
|
|
|
var res = ffmpegArgs.ProcessSynchronously();
|
|
return Task.CompletedTask;
|
|
}
|