Skip to content

Commit 00befb4

Browse files
committed
Subsampling for metrics
1 parent 65a13be commit 00befb4

File tree

7 files changed

+94
-24
lines changed

7 files changed

+94
-24
lines changed

ff-utils-winforms/Forms/MainForm.Utils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ private void SelectGetMetrics(object sender, EventArgs e)
4040
{
4141
currentTask = RunTask.TaskType.UtilGetMetrics;
4242
UpdatePanels();
43+
utilsMetricsConfBtn_Click(null, null);
4344
}
4445

4546
private void utilsMetricsConfBtn_Click(object sender, EventArgs e)
@@ -56,6 +57,7 @@ private void utilsMetricsConfBtn_Click(object sender, EventArgs e)
5657
if (form.DialogResult != DialogResult.OK)
5758
return;
5859

60+
UtilGetMetrics.subsample = form.Subsample;
5961
UtilGetMetrics.alignMode = form.AlignMode;
6062
UtilGetMetrics.vmafModel = form.VmafModel;
6163
UtilGetMetrics.runVmaf = form.CheckedBoxes[0];

ff-utils-winforms/Forms/Utils/UtilsMetricsForm.Designer.cs

Lines changed: 46 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ff-utils-winforms/Forms/Utils/UtilsMetricsForm.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Nmkoder.Data;
22
using Nmkoder.Data.Ui;
3+
using Nmkoder.Extensions;
34
using Nmkoder.IO;
45
using Nmkoder.UI.Tasks;
56
using System;
@@ -17,6 +18,7 @@ namespace Nmkoder.Forms.Utils
1718
public partial class UtilsMetricsForm : Form
1819
{
1920
public bool[] CheckedBoxes { get; set; }
21+
public int Subsample { get; set; } = 1;
2022
public int AlignMode { get; set; } = 0;
2123
public int VmafModel { get; set; } = 0;
2224
public string VideoLq { get; set; }
@@ -51,6 +53,7 @@ private void LoadVideoBox (ComboBox box, string videoPath)
5153
private void UtilsMetricsForm_Load(object sender, EventArgs e)
5254
{
5355
align.SelectedIndex = UtilGetMetrics.alignMode;
56+
subsample.SelectedIndex = (UtilGetMetrics.subsample - 1).Clamp(0, 64);
5457
}
5558

5659
private void UtilsMetricsForm_Shown(object sender, EventArgs e)
@@ -73,16 +76,26 @@ private void UtilsMetricsForm_Shown(object sender, EventArgs e)
7376
}
7477
}
7578

79+
bool pressedOk = false;
80+
7681
private void confirmBtn_Click(object sender, EventArgs e)
7782
{
83+
Subsample = subsample.SelectedIndex + 1;
7884
AlignMode = align.SelectedIndex;
7985
VmafModel = vmafMdl.SelectedIndex;
8086
CheckedBoxes = new bool[] { vmaf.Checked, ssim.Checked, psnr.Checked };
8187
VideoLq = ((MediaFile)encodedVideo.SelectedItem).TruePath;
8288
VideoHq = ((MediaFile)referenceVideo.SelectedItem).TruePath;
8389
DialogResult = DialogResult.OK;
90+
pressedOk = true;
8491
Close();
8592
Program.mainForm.BringToFront();
8693
}
94+
95+
private void UtilsMetricsForm_FormClosing(object sender, FormClosingEventArgs e)
96+
{
97+
if(!pressedOk)
98+
confirmBtn_Click(null, null);
99+
}
87100
}
88101
}

ff-utils-winforms/Main/RandomUtils.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ namespace Nmkoder.Main
1111
{
1212
class RandomUtils
1313
{
14-
public static async Task RecoverDoneJson()
14+
public static async Task RecoverDoneJson(string dir)
1515
{
16-
string dir = @"C:\Files\Videos\Encoding\tenet\mrg";
17-
1816
FileInfo[] chunks = IoUtils.GetFileInfosSorted(dir, false, "*.ivf");
1917

2018
string s = "";
@@ -32,21 +30,34 @@ public static async Task RecoverDoneJson()
3230
Logger.Log($"Done");
3331
}
3432

35-
public static async Task ValidateDoneJson()
33+
public static async Task ValidateDoneJson(string jsonPath, string chunksDir)
3634
{
3735
try
3836
{
39-
string jsonPath = @"C:\Software\Nmkoder\data\av1anTemp\1635170121824\done.json";
40-
string chunksDir = @"C:\Software\Nmkoder\data\av1anTemp\1635170121824\encode";
37+
int minSizeKb = 10;
4138

42-
string[] filenames = File.ReadAllText(jsonPath).Split('{')[2].Split('}')[0].Split(',').Select(x => x.Split(':')[0].Remove("\"")).ToArray();
39+
string doneJsonText = File.ReadAllText(jsonPath);
40+
string[] filenames = doneJsonText.Split('{')[2].Split('}')[0].Split(',').Select(x => x.Split(':')[0].Remove("\"")).ToArray();
4341
//string[] chunks = IoUtils.GetFilesSorted(chunksDir, false, "*.ivf");
4442

43+
string newJsonText = doneJsonText.Split('{')[1] + "{";
44+
4545
foreach (string f in filenames)
4646
{
47-
bool exists = File.Exists(Path.Combine(chunksDir, f + ".ivf"));
48-
Logger.Log($"{f} => {(exists ? "exists" : "does not exist!")}");
47+
string path = Path.Combine(chunksDir, f + ".ivf");
48+
bool valid = File.Exists(path) && new FileInfo(path).Length > (1024 * minSizeKb);
49+
//Logger.Log($"{f} => {(valid ? "exists" : "does not exist!")}");
50+
51+
if (valid)
52+
newJsonText += $"{(f + ".ivf").Wrap()}";
53+
else
54+
Logger.Log($"Chunk {f} does not exist!");
4955
}
56+
57+
newJsonText += "},\"audio_done\":true}";
58+
59+
File.Move(jsonPath, jsonPath + ".bak");
60+
File.WriteAllText(jsonPath, newJsonText);
5061
}
5162
catch (Exception e)
5263
{

ff-utils-winforms/Media/AvProcess.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public static async Task RunAv1an(string args, string workingDir, LogMode logMod
158158
try
159159
{
160160
string dir = Path.Combine(GetDir(), "av1an");
161-
IoUtils.TryDeleteIfExists(Paths.GetAv1anTempPath());
161+
//IoUtils.TryDeleteIfExists(Paths.GetAv1anTempPath());
162162
string tempDir = Path.Combine(Paths.GetAv1anTempPath(), ((long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString());
163163
Directory.CreateDirectory(tempDir);
164164
bool show = Config.GetBool(Config.Key.av1anCmdVisible, true); // = Config.GetInt(Config.Key.cmdDebugMode) > 0;

ff-utils-winforms/Media/FfmpegOutputHandler.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public static void LogOutput(string line, ref string appendStr, string logFilena
8383
RunTask.Cancel($"Error: {line}\n\nYou tried to mux a non-GIF stream into a GIF file.");
8484
return;
8585
}
86+
87+
if (line.Contains("Width and height of input videos must be same"))
88+
{
89+
RunTask.Cancel($"Error: {line}");
90+
return;
91+
}
8692
}
8793

8894
static void UpdateFfmpegProgress(string ffmpegTime)

ff-utils-winforms/UI/Tasks/UtilGetMetrics.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class UtilGetMetrics
2323
public static bool runPsnr;
2424
public static int alignMode = 0;
2525
public static int vmafModel = 0;
26+
public static int subsample = 0;
2627

2728
public static async Task Run(bool fixRate = true)
2829
{
@@ -44,7 +45,7 @@ public static async Task Run(bool fixRate = true)
4445
if (runVmaf)
4546
{
4647
Logger.Log("Calculating VMAF...");
47-
string vmafFilter = $"libvmaf={Paths.GetVmafPath(true, GetVmafModel())}:n_threads={Environment.ProcessorCount}";
48+
string vmafFilter = $"libvmaf={Paths.GetVmafPath(true, GetVmafModel())}:n_threads={Environment.ProcessorCount}:n_subsample={subsample}";
4849
string args = $"{r} {vidLq.GetFfmpegInputArg()} {r} {vidHq.GetFfmpegInputArg()} -filter_complex {f}{vmafFilter} -f null -";
4950
string output = await AvProcess.RunFfmpeg(args, AvProcess.LogMode.OnlyLastLine, "info", true, true);
5051
List<string> vmafLines = output.SplitIntoLines().Where(x => x.Contains("VMAF score: ")).ToList();
@@ -63,7 +64,8 @@ public static async Task Run(bool fixRate = true)
6364
if (runSsim)
6465
{
6566
Logger.Log("Calculating SSIM...");
66-
string args = $"{r} {vidLq.GetFfmpegInputArg()} {r} {vidHq.GetFfmpegInputArg()} -filter_complex {f}ssim -f null -";
67+
string select = subsample > 1 ? $"select=not(mod(n-1\\,{subsample}))," : "";
68+
string args = $"{r} {vidLq.GetFfmpegInputArg()} {r} {vidHq.GetFfmpegInputArg()} -filter_complex {f}{select}ssim -f null -";
6769
string output = await AvProcess.GetFfmpegOutputAsync(args, false, true);
6870
List<string> ssimLines = output.SplitIntoLines().Where(x => x.Contains("] SSIM ")).ToList();
6971

@@ -81,7 +83,8 @@ public static async Task Run(bool fixRate = true)
8183
if (runPsnr)
8284
{
8385
Logger.Log("Calculating PSNR...");
84-
string args = $"{r} {vidLq.GetFfmpegInputArg()} {r} {vidHq.GetFfmpegInputArg()} -filter_complex {f}psnr -f null -";
86+
string select = subsample > 1 ? $"select=not(mod(n-1\\,{subsample}))," : "";
87+
string args = $"{r} {vidLq.GetFfmpegInputArg()} {r} {vidHq.GetFfmpegInputArg()} -filter_complex {f}{select}psnr -f null -";
8588
string output = await AvProcess.GetFfmpegOutputAsync(args, false, true);
8689
List<string> psnrLines = output.SplitIntoLines().Where(x => x.Contains("] PSNR ")).ToList();
8790

0 commit comments

Comments
 (0)