Skip to content

Commit b84db2e

Browse files
committed
Improved color data transfer util, now only prints metadata if 1 file or in batch mode
1 parent 1cd3300 commit b84db2e

File tree

6 files changed

+150
-72
lines changed

6 files changed

+150
-72
lines changed

ff-utils-winforms/Data/VideoColorData.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ public class VideoColorData
2727
public override string ToString()
2828
{
2929
return
30-
$"Colour transfer: {ColorTransfer}" +
31-
$"\nColour matrix coefficients: {ColorMatrixCoeffs}" +
32-
$"\nColour primaries: {ColorPrimaries}" +
33-
$"\nColour range: {ColorRange}" +
34-
$"\nRed colour coordinate x: {RedX}" +
35-
$"\nRed colour coordinate y: {RedY}" +
36-
$"\nGreen colour coordinate x: {GreenX}" +
37-
$"\nGreen colour coordinate y: {GreenY}" +
38-
$"\nBlue colour coordinate y: {BlueX}" +
39-
$"\nBlue colour coordinate x: {BlueY}" +
40-
$"\nWhite colour coordinate y: {WhiteX}" +
41-
$"\nWhite colour coordinate x: {WhiteY}" +
30+
$"Color transfer: {ColorTransfer}" +
31+
$"\nColor matrix coefficients: {ColorMatrixCoeffs}" +
32+
$"\nColor primaries: {ColorPrimaries}" +
33+
$"\nColor range: {ColorRange}" +
34+
$"\nRed color coordinate x: {RedX}" +
35+
$"\nRed color coordinate y: {RedY}" +
36+
$"\nGreen color coordinate x: {GreenX}" +
37+
$"\nGreen color coordinate y: {GreenY}" +
38+
$"\nBlue color coordinate y: {BlueX}" +
39+
$"\nBlue color coordinate x: {BlueY}" +
40+
$"\nWhite color coordinate y: {WhiteX}" +
41+
$"\nWhite color coordinate x: {WhiteY}" +
4242
$"\nMaximum luminance: {LumaMax}" +
4343
$"\nMinimum luminance: {LumaMin}";
4444
}

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ partial class MainForm
2525
{
2626
private RunTask.TaskType currentTask;
2727

28-
public RunTask.TaskType GetUtilsTaskType ()
28+
public RunTask.TaskType GetUtilsTaskType()
2929
{
3030
return currentTask;
3131
}
@@ -71,22 +71,30 @@ private void SelectColorData(object sender, EventArgs e)
7171
{
7272
currentTask = RunTask.TaskType.UtilColorData;
7373
UpdatePanels();
74-
utilsColorDataConfBtn_Click(null, null);
74+
//utilsColorDataConfBtn_Click(null, null);
7575
}
7676

7777

7878
private void utilsColorDataConfBtn_Click(object sender, EventArgs e)
7979
{
80-
if (fileListBox.Items.Count < 2)
81-
{
82-
Logger.Log($"You need to load at least 2 files into the file list to use this utility!");
83-
return;
84-
}
80+
// if (fileListBox.Items.Count < 2)
81+
// {
82+
// Logger.Log($"You need to load at least 2 files into the file list to use this utility!");
83+
// return;
84+
// }
8585

8686
Utils.UtilsColorDataForm form = new Utils.UtilsColorDataForm();
87+
88+
if (form.IsDisposed)
89+
return;
90+
8791
form.ShowDialog();
92+
SetColorDataFormVars(form);
93+
}
8894

89-
if (form.DialogResult != DialogResult.OK)
95+
public void SetColorDataFormVars(Utils.UtilsColorDataForm form)
96+
{
97+
if (form == null || form.DialogResult != DialogResult.OK)
9098
return;
9199

92100
UtilColorData.copyColorSpace = form.TransferColorSpace;
@@ -101,7 +109,7 @@ private void SelectOcr(object sender, EventArgs e)
101109
UpdatePanels();
102110
}
103111

104-
private void UpdatePanels ()
112+
private void UpdatePanels()
105113
{
106114
utilsBitratesPanel.BorderStyle = (currentTask == RunTask.TaskType.UtilReadBitrates) ? BorderStyle.FixedSingle : BorderStyle.None;
107115
utilsMetricsPanel.BorderStyle = (currentTask == RunTask.TaskType.UtilGetMetrics) ? BorderStyle.FixedSingle : BorderStyle.None;

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

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

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

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Nmkoder.Data.Ui;
33
using Nmkoder.Extensions;
44
using Nmkoder.IO;
5+
using Nmkoder.Main;
6+
using Nmkoder.UI;
57
using Nmkoder.UI.Tasks;
68
using System;
79
using System.Data;
@@ -18,12 +20,31 @@ public partial class UtilsColorDataForm : Form
1820
public string VideoSrc { get; set; }
1921
public string VideoTarget { get; set; }
2022

23+
private bool batchMode = false;
24+
private bool closeRightAway = false;
2125
ListBox fileList = Program.mainForm.fileListBox;
2226

23-
public UtilsColorDataForm()
27+
public UtilsColorDataForm(bool close = false)
2428
{
2529
InitializeComponent();
26-
30+
batchMode = RunTask.currentFileListMode == RunTask.FileListMode.BatchProcess;
31+
closeRightAway = close || batchMode;
32+
33+
if (closeRightAway)
34+
Opacity = 0;
35+
36+
if (batchMode)
37+
{
38+
if(!close)
39+
Logger.Log($"In batch processing mode, this util can only be used to read the metadata! Use the Multi File Mode for transferring.");
40+
41+
if (TrackList.current == null)
42+
{
43+
pressedOk = true;
44+
Close();
45+
}
46+
}
47+
2748
copyColorSpace.Checked = UtilColorData.copyColorSpace;
2849
copyHdrData.Checked = UtilColorData.copyHdrData;
2950
AcceptButton = confirmBtn;
@@ -50,20 +71,35 @@ private void UtilsColorDataForm_Shown(object sender, EventArgs e)
5071
{
5172
ListBox fileList = Program.mainForm.fileListBox;
5273

53-
foreach(FileListEntry entry in fileList.Items)
74+
if (batchMode)
5475
{
55-
encodedVideo.Items.Add(entry.File);
56-
referenceVideo.Items.Add(entry.File);
76+
if(TrackList.current != null)
77+
sourceVideo.Items.Add(TrackList.current.File);
5778
}
79+
else
80+
{
81+
foreach (FileListEntry entry in fileList.Items)
82+
{
83+
sourceVideo.Items.Add(entry.File);
84+
targetVideo.Items.Add(entry.File);
85+
}
86+
}
87+
88+
LoadVideoBox(sourceVideo, UtilColorData.vidSrc);
5889

59-
LoadVideoBox(encodedVideo, UtilGetMetrics.vidLq);
60-
LoadVideoBox(referenceVideo, UtilGetMetrics.vidHq);
90+
if (!batchMode && fileList.Items.Count > 1)
91+
LoadVideoBox(targetVideo, UtilColorData.vidTarget);
6192

62-
if (encodedVideo.SelectedIndex < 0 || referenceVideo.SelectedIndex < 0)
93+
if (sourceVideo.SelectedIndex < 0 || targetVideo.SelectedIndex < 0)
6394
{
64-
encodedVideo.SelectedItem = encodedVideo.Items.OfType<MediaFile>().OrderByDescending(x => x.Size).Last();
65-
referenceVideo.SelectedItem = referenceVideo.Items.OfType<MediaFile>().OrderByDescending(x => x.Size).First();
95+
sourceVideo.SelectedItem = sourceVideo.Items.OfType<MediaFile>().OrderByDescending(x => x.Size).First();
96+
97+
if (!batchMode && fileList.Items.Count > 1)
98+
targetVideo.SelectedItem = targetVideo.Items.OfType<MediaFile>().OrderByDescending(x => x.Size).Last();
6699
}
100+
101+
if (closeRightAway)
102+
Close();
67103
}
68104

69105
bool pressedOk = false;
@@ -72,8 +108,13 @@ private void confirmBtn_Click(object sender, EventArgs e)
72108
{
73109
TransferColorSpace = copyColorSpace.Checked;
74110
TransferHdrData = copyHdrData.Checked;
75-
VideoSrc = ((MediaFile)encodedVideo.SelectedItem).TruePath;
76-
VideoTarget = ((MediaFile)referenceVideo.SelectedItem).TruePath;
111+
112+
if(sourceVideo.SelectedItem != null)
113+
VideoSrc = ((MediaFile)sourceVideo.SelectedItem).TruePath;
114+
115+
if (targetVideo.SelectedItem != null)
116+
VideoTarget = ((MediaFile)targetVideo.SelectedItem).TruePath;
117+
77118
DialogResult = DialogResult.OK;
78119
pressedOk = true;
79120
Close();

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Nmkoder.Data;
2+
using Nmkoder.Forms.Utils;
23
using Nmkoder.IO;
34
using Nmkoder.Main;
45
using Nmkoder.Utils;
@@ -20,20 +21,48 @@ class UtilColorData
2021

2122
public static async Task Run()
2223
{
23-
if (RunTask.currentFileListMode == RunTask.FileListMode.BatchProcess)
24-
{
25-
Logger.Log($"Color Data Utility: Didn't run because this util only works in Multi File Mode!");
26-
return;
27-
}
24+
// if (RunTask.currentFileListMode == RunTask.FileListMode.BatchProcess)
25+
// {
26+
// Logger.Log($"Color Data Utility: Didn't run because this util only works in Multi File Mode!");
27+
// return;
28+
// }
2829

2930
Program.mainForm.SetWorking(true);
30-
Logger.Log($"Transferring color data from {Path.GetFileName(vidSrc)} to {Path.GetFileName(vidTarget)}.");
3131

3232
try
3333
{
34-
VideoColorData data = await ColorDataUtils.GetColorData(vidSrc);
35-
Logger.Log(data.ToString());
36-
await ColorDataUtils.SetColorData(vidTarget, data);
34+
UtilsColorDataForm form = new UtilsColorDataForm(true);
35+
form.ShowDialog();
36+
Program.mainForm.SetColorDataFormVars(form);
37+
38+
if (!File.Exists(vidTarget))
39+
{
40+
Logger.Log($"Only one file loaded - Will only print metadata for {Path.GetFileName(vidSrc)}.");
41+
VideoColorData d = await ColorDataUtils.GetColorData(vidSrc);
42+
List<string> lines = new List<string>();
43+
if (!string.IsNullOrWhiteSpace(d.ColorTransfer)) lines.Add($"Color transfer: {d.ColorTransfer}");
44+
if (!string.IsNullOrWhiteSpace(d.ColorMatrixCoeffs)) lines.Add($"Colour matrix coefficients: {d.ColorMatrixCoeffs}");
45+
if (!string.IsNullOrWhiteSpace(d.ColorMatrixCoeffs)) lines.Add($"Colour primaries: {d.ColorPrimaries}");
46+
if (!string.IsNullOrWhiteSpace(d.ColorRange)) lines.Add($"Colour range: {d.ColorRange}");
47+
if (!string.IsNullOrWhiteSpace(d.RedX) && !string.IsNullOrWhiteSpace(d.RedY)) lines.Add($"Red color coordinates X/Y: {d.RedX}/{d.RedY}");
48+
if (!string.IsNullOrWhiteSpace(d.GreenX) && !string.IsNullOrWhiteSpace(d.GreenY)) lines.Add($"Green color coordinates X/Y: {d.GreenX}/{d.GreenY}");
49+
if (!string.IsNullOrWhiteSpace(d.BlueX) && !string.IsNullOrWhiteSpace(d.BlueY)) lines.Add($"Blue color coordinates X/Y: {d.BlueX}/{d.BlueY}");
50+
if (!string.IsNullOrWhiteSpace(d.WhiteX) && !string.IsNullOrWhiteSpace(d.WhiteY)) lines.Add($"White color coordinates X/Y: {d.WhiteX}/{d.WhiteY}");
51+
if (!string.IsNullOrWhiteSpace(d.LumaMin)) lines.Add($"Minimum luminance: { d.LumaMin}");
52+
if (!string.IsNullOrWhiteSpace(d.LumaMax)) lines.Add($"Maximum luminance: { d.LumaMax}");
53+
54+
if (lines.Count > 0)
55+
Logger.Log(string.Join("\n", lines));
56+
else
57+
Logger.Log($"No metadata found.");
58+
}
59+
else
60+
{
61+
Logger.Log($"Transferring color data from {Path.GetFileName(vidSrc)} to {Path.GetFileName(vidTarget)}.");
62+
VideoColorData data = await ColorDataUtils.GetColorData(vidSrc);
63+
Logger.Log(data.ToString());
64+
await ColorDataUtils.SetColorData(vidTarget, data);
65+
}
3766
}
3867
catch(Exception e)
3968
{

ff-utils-winforms/Utils/ColorDataUtils.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,20 @@ public static async Task SetColorData(string path, VideoColorData d)
136136
$"--colour-transfer-characteristics 0:{d.ColorTransfer} " +
137137
$"--colour-primaries 0:{d.ColorPrimaries} " +
138138
$"--max-luminance 0:{d.LumaMax} " +
139-
$"--min-luminance 0:{d.LumaMin} " +
139+
$"--min-luminance 0:{d.LumaMin} " +
140140
$"--chromaticity-coordinates 0:{d.RedX},{d.RedY},{d.GreenX},{d.GreenY},{d.BlueX},{d.BlueY} " +
141141
$"--white-colour-coordinates 0:{d.WhiteX},{d.WhiteY} " +
142142
$"{path.Wrap()}";
143143

144144
await AvProcess.RunMkvMerge(args, true);
145145

146-
//long filesizeDiff = Math.Abs(new FileInfo(path).Length - new FileInfo(tmpPath).Length);
147-
float filesizeFactor = new FileInfo(tmpPath).Length / new FileInfo(path).Length;
148-
Logger.Log($"{MethodBase.GetCurrentMethod().DeclaringType}: Filesize ratio of remuxed file against original: {filesizeFactor}");
146+
int filesizeDiffKb = (int)((Math.Abs(new FileInfo(path).Length - new FileInfo(tmpPath).Length)) / 1024);
147+
double filesizeFactor = (double)(new FileInfo(tmpPath).Length) / (double)(new FileInfo(path).Length);
148+
Logger.Log($"{MethodBase.GetCurrentMethod().DeclaringType}: Filesize ratio of remuxed file against original: {filesizeFactor}", true);
149149

150-
if (filesizeFactor < 0.95f || filesizeFactor > 1.05f)
150+
if (filesizeDiffKb > 1024 && (filesizeFactor < 0.95d || filesizeFactor > 1.05d))
151151
{
152-
Logger.Log($"Warning: Output file size is not within 5% of the original size! Won't delete original to be sure.");
152+
Logger.Log($"Warning: Output file size differs by >1MB is not within 5% of the original size! Won't delete original to be sure.");
153153
}
154154
else
155155
{

0 commit comments

Comments
 (0)