Added ffmpeg preset options

This commit is contained in:
N00MKRAD
2020-11-25 17:48:27 +01:00
parent 8ddd9fc2a1
commit d6aeb96457
5 changed files with 94 additions and 25 deletions

View File

@@ -9,6 +9,8 @@ namespace Flowframes
{
class FFmpegCommands
{
static string hdrFilter = @"-vf select=gte(n\,%frNum%),zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p";
static string videoEncArgs = "-pix_fmt yuv420p -movflags +faststart -vf \"crop = trunc(iw / 2) * 2:trunc(ih / 2) * 2\"";
static string divisionFilter = "\"crop = trunc(iw / 2) * 2:trunc(ih / 2) * 2\"";
static string pngComprArg = "-compression_level 3";
@@ -42,7 +44,7 @@ namespace Flowframes
public static async void ExtractSingleFrame(string inputFile, int frameNum, bool hdr, bool delSrc)
{
string hdrStr = "";
if (hdr) hdrStr = FFmpegStrings.hdrFilter;
if (hdr) hdrStr = hdrFilter;
string args = "-i \"" + inputFile + "\" " + hdrStr
+ " -vf \"select=eq(n\\," + frameNum + ")\" -vframes 1 \"" + inputFile + "-frame" + frameNum + ".png\"";
await AvProcess.RunFfmpeg(args, AvProcess.LogMode.Hidden);
@@ -55,9 +57,9 @@ namespace Flowframes
Logger.Log($"Encoding MP4 video with CRF {crf}...");
int nums = IOUtils.GetFilenameCounterLength(Directory.GetFiles(inputDir, $"*.{imgFormat}")[0], prefix);
string enc = useH265 ? "libx265" : "libx264";
string loopStr = "";
if (looptimes > 0) loopStr = $"-stream_loop {looptimes}";
string args = $" {loopStr} -framerate {fps.ToString().Replace(",",".")} -i \"{inputDir}\\{prefix}%0{nums}d.{imgFormat}\" -c:v {enc} -crf {crf} {videoEncArgs} -threads {Config.GetInt("ffEncThreads")} -c:a copy {outPath.Wrap()}";
string loopStr = (looptimes > 0) ? $"-stream_loop {looptimes}" : "";
string presetStr = $"-preset {Config.Get("ffEncPreset")}";
string args = $" {loopStr} -framerate {fps.ToString().Replace(",",".")} -i \"{inputDir}\\{prefix}%0{nums}d.{imgFormat}\" -c:v {enc} -crf {crf} {presetStr} {videoEncArgs} -threads {Config.GetInt("ffEncThreads")} -c:a copy {outPath.Wrap()}";
await AvProcess.RunFfmpeg(args, AvProcess.LogMode.OnlyLastLine);
if (delSrc)
DeleteSource(inputDir);
@@ -67,18 +69,18 @@ namespace Flowframes
{
Logger.Log($"Encoding MP4 video with CRF {crf}...");
string enc = useH265 ? "libx265" : "libx264";
string loopStr = "";
if (looptimes > 0) loopStr = $"-stream_loop {looptimes}";
string args = $" {loopStr} -vsync 1 -f concat -safe 0 -i {framesFile.Wrap()} -r {fps.ToString().Replace(",", ".")} -c:v {enc} -crf {crf} {videoEncArgs} -threads {Config.GetInt("ffEncThreads")} -c:a copy {outPath.Wrap()}";
string loopStr = (looptimes > 0) ? $"-stream_loop {looptimes}" : "";
string presetStr = $"-preset {Config.Get("ffEncPreset")}";
string args = $" {loopStr} -vsync 1 -f concat -safe 0 -i {framesFile.Wrap()} -r {fps.ToString().Replace(",", ".")} -c:v {enc} -crf {crf} {presetStr} {videoEncArgs} -threads {Config.GetInt("ffEncThreads")} -c:a copy {outPath.Wrap()}";
await AvProcess.RunFfmpeg(args, AvProcess.LogMode.OnlyLastLine);
}
public static async Task ConvertFramerate (string inputPath, string outPath, bool useH265, int crf, float newFps, bool delSrc = false)
{
Logger.Log($"Changing video frame rate...");
string enc = "libx264";
if (useH265) enc = "libx265";
string args = $" -i {inputPath.Wrap()} -filter:v fps=fps={newFps} -c:v {enc} -crf {crf} -pix_fmt yuv420p -movflags +faststart {outPath.Wrap()}";
string enc = useH265 ? "libx265" : "libx264";
string presetStr = $"-preset {Config.Get("ffEncPreset")}";
string args = $" -i {inputPath.Wrap()} -filter:v fps=fps={newFps} -c:v {enc} -crf {crf} {presetStr} -pix_fmt yuv420p -movflags +faststart {outPath.Wrap()}";
await AvProcess.RunFfmpeg(args, AvProcess.LogMode.OnlyLastLine);
if (delSrc)
DeleteSource(inputPath);

View File

@@ -8,6 +8,6 @@ namespace Flowframes
{
class FFmpegStrings
{
public static string hdrFilter = @"-vf select=gte(n\,%frNum%),zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p";
}
}

View File

@@ -126,6 +126,10 @@
this.ncnnThreads = new System.Windows.Forms.ComboBox();
this.label44 = new System.Windows.Forms.Label();
this.panel12 = new System.Windows.Forms.Panel();
this.label45 = new System.Windows.Forms.Label();
this.label46 = new System.Windows.Forms.Label();
this.label47 = new System.Windows.Forms.Label();
this.ffEncPreset = new System.Windows.Forms.ComboBox();
this.settingsTabList.SuspendLayout();
this.generalTab.SuspendLayout();
this.tabListPage2.SuspendLayout();
@@ -1098,6 +1102,10 @@
// debugTab
//
this.debugTab.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(48)))), ((int)(((byte)(48)))));
this.debugTab.Controls.Add(this.ffEncPreset);
this.debugTab.Controls.Add(this.label47);
this.debugTab.Controls.Add(this.label46);
this.debugTab.Controls.Add(this.label45);
this.debugTab.Controls.Add(this.label41);
this.debugTab.Controls.Add(this.ffprobeCountFrames);
this.debugTab.Controls.Add(this.label40);
@@ -1118,7 +1126,7 @@
//
this.label41.AutoSize = true;
this.label41.ForeColor = System.Drawing.Color.Silver;
this.label41.Location = new System.Drawing.Point(308, 100);
this.label41.Location = new System.Drawing.Point(308, 280);
this.label41.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7);
this.label41.Name = "label41";
this.label41.Size = new System.Drawing.Size(423, 13);
@@ -1129,7 +1137,7 @@
// ffprobeCountFrames
//
this.ffprobeCountFrames.AutoSize = true;
this.ffprobeCountFrames.Location = new System.Drawing.Point(280, 100);
this.ffprobeCountFrames.Location = new System.Drawing.Point(280, 280);
this.ffprobeCountFrames.Name = "ffprobeCountFrames";
this.ffprobeCountFrames.Size = new System.Drawing.Size(15, 14);
this.ffprobeCountFrames.TabIndex = 73;
@@ -1138,7 +1146,7 @@
// label40
//
this.label40.AutoSize = true;
this.label40.Location = new System.Drawing.Point(10, 100);
this.label40.Location = new System.Drawing.Point(10, 280);
this.label40.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7);
this.label40.Name = "label40";
this.label40.Size = new System.Drawing.Size(162, 13);
@@ -1149,7 +1157,7 @@
//
this.label38.AutoSize = true;
this.label38.ForeColor = System.Drawing.Color.Silver;
this.label38.Location = new System.Drawing.Point(570, 71);
this.label38.Location = new System.Drawing.Point(570, 224);
this.label38.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7);
this.label38.Name = "label38";
this.label38.Size = new System.Drawing.Size(131, 13);
@@ -1160,7 +1168,7 @@
//
this.panel11.BackgroundImage = global::Flowframes.Properties.Resources.baseline_create_white_18dp_semiTransparent;
this.panel11.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.panel11.Location = new System.Drawing.Point(536, 67);
this.panel11.Location = new System.Drawing.Point(536, 220);
this.panel11.Name = "panel11";
this.panel11.Size = new System.Drawing.Size(21, 21);
this.panel11.TabIndex = 61;
@@ -1170,16 +1178,16 @@
//
this.ffEncThreads.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
this.ffEncThreads.ForeColor = System.Drawing.Color.White;
this.ffEncThreads.Location = new System.Drawing.Point(280, 67);
this.ffEncThreads.Location = new System.Drawing.Point(280, 220);
this.ffEncThreads.MinimumSize = new System.Drawing.Size(4, 21);
this.ffEncThreads.Name = "ffEncThreads";
this.ffEncThreads.Size = new System.Drawing.Size(250, 20);
this.ffEncThreads.Size = new System.Drawing.Size(250, 21);
this.ffEncThreads.TabIndex = 70;
//
// label37
//
this.label37.AutoSize = true;
this.label37.Location = new System.Drawing.Point(10, 70);
this.label37.Location = new System.Drawing.Point(10, 220);
this.label37.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7);
this.label37.Name = "label37";
this.label37.Size = new System.Drawing.Size(204, 13);
@@ -1190,7 +1198,7 @@
//
this.panel9.BackgroundImage = global::Flowframes.Properties.Resources.baseline_create_white_18dp_semiTransparent;
this.panel9.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.panel9.Location = new System.Drawing.Point(536, 37);
this.panel9.Location = new System.Drawing.Point(536, 67);
this.panel9.Name = "panel9";
this.panel9.Size = new System.Drawing.Size(21, 21);
this.panel9.TabIndex = 60;
@@ -1209,7 +1217,7 @@
"30",
"45",
"60"});
this.autoDedupFrames.Location = new System.Drawing.Point(280, 37);
this.autoDedupFrames.Location = new System.Drawing.Point(280, 67);
this.autoDedupFrames.Name = "autoDedupFrames";
this.autoDedupFrames.Size = new System.Drawing.Size(250, 21);
this.autoDedupFrames.TabIndex = 34;
@@ -1217,7 +1225,7 @@
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(10, 40);
this.label7.Location = new System.Drawing.Point(10, 70);
this.label7.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(228, 13);
@@ -1227,7 +1235,7 @@
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(10, 10);
this.label3.Location = new System.Drawing.Point(10, 40);
this.label3.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(148, 13);
@@ -1245,7 +1253,7 @@
"No",
"Yes, Close After Execution",
"Yes, Keep Open After Process Has Exited"});
this.cmdDebugMode.Location = new System.Drawing.Point(280, 7);
this.cmdDebugMode.Location = new System.Drawing.Point(280, 37);
this.cmdDebugMode.Name = "cmdDebugMode";
this.cmdDebugMode.Size = new System.Drawing.Size(250, 21);
this.cmdDebugMode.TabIndex = 27;
@@ -1310,6 +1318,58 @@
this.panel12.TabIndex = 58;
this.toolTip1.SetToolTip(this.panel12, "Allows custom input.");
//
// label45
//
this.label45.AutoSize = true;
this.label45.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label45.Location = new System.Drawing.Point(10, 10);
this.label45.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7);
this.label45.Name = "label45";
this.label45.Size = new System.Drawing.Size(63, 16);
this.label45.TabIndex = 75;
this.label45.Text = "General";
//
// label46
//
this.label46.AutoSize = true;
this.label46.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label46.Location = new System.Drawing.Point(10, 190);
this.label46.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7);
this.label46.Name = "label46";
this.label46.Size = new System.Drawing.Size(65, 16);
this.label46.TabIndex = 76;
this.label46.Text = "FFmpeg";
//
// label47
//
this.label47.AutoSize = true;
this.label47.Location = new System.Drawing.Point(10, 250);
this.label47.Margin = new System.Windows.Forms.Padding(10, 10, 10, 7);
this.label47.Name = "label47";
this.label47.Size = new System.Drawing.Size(85, 13);
this.label47.TabIndex = 77;
this.label47.Text = "Encoding Preset";
//
// ffEncPreset
//
this.ffEncPreset.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
this.ffEncPreset.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ffEncPreset.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.ffEncPreset.ForeColor = System.Drawing.Color.White;
this.ffEncPreset.FormattingEnabled = true;
this.ffEncPreset.Items.AddRange(new object[] {
"veryfast",
"faster",
"fast",
"medium",
"slow",
"slower",
"veryslow"});
this.ffEncPreset.Location = new System.Drawing.Point(280, 247);
this.ffEncPreset.Name = "ffEncPreset";
this.ffEncPreset.Size = new System.Drawing.Size(250, 21);
this.ffEncPreset.TabIndex = 78;
//
// SettingsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -1443,5 +1503,9 @@
private System.Windows.Forms.ComboBox ncnnThreads;
private System.Windows.Forms.Label label43;
private System.Windows.Forms.Panel panel12;
private System.Windows.Forms.ComboBox ffEncPreset;
private System.Windows.Forms.Label label47;
private System.Windows.Forms.Label label46;
private System.Windows.Forms.Label label45;
}
}

View File

@@ -38,12 +38,12 @@ namespace Flowframes.Forms
h264Crf.Text = h264Crf.GetInt().Clamp(0, 40).ToString();
h265Crf.Text = h265Crf.GetInt().Clamp(0, 40).ToString();
gifskiQ.Text = gifskiQ.GetInt().Clamp(0, 100).ToString();
ncnnThreads.Text = ncnnThreads.GetInt().Clamp(1, 8).ToString();
// Remove spaces...
torchGpus.Text = torchGpus.Text.Replace(" ", "");
ncnnGpus.Text = ncnnGpus.Text.Replace(" ", "");
// Force numbers...
ffEncThreads.Text = ffEncThreads.GetInt().ToString();
ncnnThreads.Text = ncnnThreads.GetInt().ToString();
// General
ConfigParser.SaveGuiElement(maxVidHeight);
@@ -75,6 +75,7 @@ namespace Flowframes.Forms
ConfigParser.SaveComboxIndex(cmdDebugMode);
ConfigParser.SaveGuiElement(autoDedupFrames);
ConfigParser.SaveGuiElement(ffEncThreads);
ConfigParser.SaveGuiElement(ffEncPreset);
ConfigParser.SaveGuiElement(ffprobeCountFrames);
}
@@ -113,6 +114,7 @@ namespace Flowframes.Forms
ConfigParser.LoadComboxIndex(cmdDebugMode);
ConfigParser.LoadGuiElement(autoDedupFrames);
ConfigParser.LoadGuiElement(ffEncThreads);
ConfigParser.LoadGuiElement(ffEncPreset);
ConfigParser.LoadGuiElement(ffprobeCountFrames);
}

View File

@@ -102,6 +102,7 @@ namespace Flowframes.IO
if (key == "ncnnThreads") return WriteDefault("ncnnThreads", "2");
// Debug / Other / Experimental
if (key == "ffprobeCountFrames") return WriteDefault("ffprobeCountFrames", "False");
if (key == "ffEncPreset") return WriteDefault("ffEncPreset", "medium");
// Tile Sizes
if (key == "tilesize_RIFE_NCNN") return WriteDefault("tilesize_RIFE_NCNN", "2048");
if (key == "tilesize_DAIN_NCNN") return WriteDefault("tilesize_DAIN_NCNN", "512");