-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
395 lines (349 loc) · 14.8 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading; // for Dispatcher
using Microsoft.Win32; // WPF's OpenFileDialog
using WinForms = System.Windows.Forms; // Alias for FolderBrowserDialog, etc;
namespace DVConverter
{
public partial class MainWindow : Window
{
private Process _ffmpegProcess;
private bool _isConverting;
private TimeSpan _estimatedTotalDuration = TimeSpan.Zero;
public MainWindow()
{
InitializeComponent();
}
// ---------- DRAG & DROP HANDLERS ----------
private void bdDropZone_DragEnter(object sender, System.Windows.DragEventArgs e)
{
if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
{
bdDropZone.Background = new SolidColorBrush(System.Windows.Media.Colors.LightSteelBlue);
}
}
private void bdDropZone_DragLeave(object sender, System.Windows.DragEventArgs e)
{
// revert the color
bdDropZone.Background = new SolidColorBrush(
(System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#BDBDBD"));
}
private void bdDropZone_DragOver(object sender, System.Windows.DragEventArgs e)
{
if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
{
var files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
if (files.Length > 0)
{
string ext = Path.GetExtension(files[0]).ToLower();
e.Effects = (ext == ".mp4" || ext == ".mkv" || ext == ".ts")
? System.Windows.DragDropEffects.Copy
: System.Windows.DragDropEffects.None;
}
}
e.Handled = true;
}
private void bdDropZone_Drop(object sender, System.Windows.DragEventArgs e)
{
// revert color
bdDropZone.Background = new SolidColorBrush(
(System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#BDBDBD"));
if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
{
var files = (string[]) e.Data.GetData(System.Windows.DataFormats.FileDrop);
if (files != null && files.Length > 0)
{
txtInput.Text = files[0];
// also populate the output dir with the same folder
txtOutputDir.Text = Path.GetDirectoryName(files[0]);
}
}
}
// ---------- BROWSE INPUT ----------
private void btnBrowseInput_Click(object sender, RoutedEventArgs e)
{
var ofd = new Microsoft.Win32.OpenFileDialog
{
Filter = "Video Files|*.mp4;*.mkv;*.ts|All Files|*.*"
};
if (ofd.ShowDialog() == true)
{
txtInput.Text = ofd.FileName;
}
}
// ---------- BROWSE OUTPUT FOLDER ----------
private void btnBrowseOutput_Click(object sender, RoutedEventArgs e)
{
var dlg = new WinForms.FolderBrowserDialog();
if (dlg.ShowDialog() == WinForms.DialogResult.OK)
{
txtOutputDir.Text = dlg.SelectedPath;
}
}
// ---------- The core logic ----------
private void onBtnClick(object sender, RoutedEventArgs e)
{
if (_isConverting)
{
CancelConversion();
}
else
{
TxtDropZone.Text = "Conversion started.. Please wait";
Convert();
}
}
private void Convert()
{
string inputPath = txtInput.Text;
if (string.IsNullOrWhiteSpace(inputPath) || !File.Exists(inputPath))
{
System.Windows.MessageBox.Show("Please choose a valid input file.");
return;
}
// 1) Which color space? (HDR10 / SDR / HLG)
string colorSpace = "hdr10"; // default
if (rbSDR.IsChecked == true) colorSpace = "sdr";
if (rbHLG.IsChecked == true) colorSpace = "hlg";
if (rbNone.IsChecked == true) colorSpace = "none";
// 2) Which codec? (h264 / h265)
// We'll pick the correct ffmpeg encoder name
string videoCodec = "libx265";
if (rbH264.IsChecked == true)
{
videoCodec = "libx264";
}
// 3) Parse the user-chosen average bitrate
int bitrate = 15000;
if (!string.IsNullOrWhiteSpace(txtBitrate.Text))
{
int.TryParse(txtBitrate.Text, out bitrate);
if (bitrate < 1000) bitrate = 15000; // fallback
}
// Output directory optional
string outputDir = txtOutputDir.Text;
if (string.IsNullOrWhiteSpace(outputDir) || !Directory.Exists(outputDir))
{
// default = same folder as input
outputDir = Path.GetDirectoryName(inputPath);
}
// Build output file name
string ext = Path.GetExtension(inputPath);
string baseName = Path.GetFileNameWithoutExtension(inputPath);
string outputPath = Path.Combine(outputDir, $"{baseName}_{colorSpace}_{videoCodec}_{bitrate}k{ext}");
// Get total duration for progress
_estimatedTotalDuration = GetVideoDuration(inputPath);
// FFmpeg path
string ffmpegPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tools", "ffmpeg.exe");
// Build the color filter & x265 params
string videoFilter = GetVideoFilter(colorSpace);
// If user chose x264, we skip x265-specific params
// or you can add your own x264 param logic.
string x265Params = "";
if (videoCodec == "libx265")
{
x265Params = GetX265Params(colorSpace);
}
// Final FFmpeg arguments
// We'll share a common structure, but adapt for h264 or h265
// For h264, we might skip the x265-specific param
// For h265, we skip x264 params, obviously.
string arguments =
"-nostdin " +
"-loglevel error " +
"-stats " +
"-y " +
"-init_hw_device vulkan=vulkan " +
"-filter_hw_device vulkan " +
$"-i \"{inputPath}\" " +
$"-vf \"{videoFilter}\" " +
$"-c:v {videoCodec} " + // This can be libx264 or libx265
"-c:a copy " +
"-c:s copy " +
$"-b:v {bitrate}k ";
// If we do x265, add -x265-params
if (videoCodec == "libx265" && !string.IsNullOrEmpty(x265Params))
{
arguments += $"-x265-params \"{x265Params}\" ";
}
arguments += $"\"{outputPath}\"";
var startInfo = new ProcessStartInfo
{
FileName = ffmpegPath,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true
};
_ffmpegProcess = new Process { StartInfo = startInfo, EnableRaisingEvents = true };
_ffmpegProcess.ErrorDataReceived += OnDataReceived;
_ffmpegProcess.OutputDataReceived += OnDataReceived;
_ffmpegProcess.Exited += OnProcessExited;
try
{
_ffmpegProcess.Start();
_ffmpegProcess.BeginErrorReadLine();
_ffmpegProcess.BeginOutputReadLine();
BtnStart.Content = "Cancel";
_isConverting = true;
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("Error starting ffmpeg:\n" + ex.Message);
}
}
// ---------- FILTERS & PARAMS ----------
private string GetVideoFilter(string encodingType)
{
switch (encodingType.ToLower())
{
case "hdr10":
return "hwupload,libplacebo=peak_detect=false:colorspace=9:color_primaries=9:color_trc=16:range=tv:format=yuv420p10le,hwdownload,format=yuv420p10le";
case "sdr":
return "hwupload,libplacebo=peak_detect=false:colorspace=bt709:color_primaries=bt709:color_trc=bt709:range=tv:format=yuv420p10le,hwdownload,format=yuv420p10le";
case "hlg":
return "hwupload,libplacebo=peak_detect=false:colorspace=9:color_primaries=9:color_trc=14:range=tv:format=yuv420p10le,hwdownload,format=yuv420p10le";
case "none":
return "hwupload,libplacebo=colorspace=bt709:color_primaries=bt709:color_trc=bt709:range=tv:format=yuv420p,hwdownload,format=yuv420p";
default:
throw new ArgumentException("Invalid encoding type (hdr10, sdr, hlg, hdr10plus).");
}
}
private string GetX265Params(string encodingType)
{
switch (encodingType.ToLower())
{
case "hdr10":
return
"repeat-headers=1:sar=1:hrd=1:aud=1:open-gop=0:hdr10=1:sao=0:rect=0:cutree=0:deblock=-3-3:strong-intra-smoothing=0:chromaloc=2:aq-mode=1:vbv-maxrate=160000:vbv-bufsize=160000:max-luma=1023:max-cll=0,0:master-display=G(8500,39850)B(6550,23000)R(35400,15650)WP(15635,16450)L(10000000,1):preset=slow";
case "sdr":
return "deblock=-3-3:vbv-bufsize=62500:vbv-maxrate=50000:fast-pskip=0:dct-decimate=0:level=5.1:ref=5:psy-rd=1.05,0.15:subme=7:me=umh:me_range=48:preset=slow";
case "hlg":
return "open-gop=0:atc-sei=18:pic_struct=0:preset=slow";
case "none":
return "preset=medium";
default:
return "";
}
}
//buy me a coffee function BuyCoffee_Click
private void BuyCoffee_Click(object sender, RoutedEventArgs e)
{
string link = "https://paypal.me/SlavomirDurej?country.x=GB&locale.x=en_GB";
try
{
// For .NET Core and .NET 5+
Process.Start(new ProcessStartInfo
{
FileName = link,
UseShellExecute = true
});
}
catch (Exception ex)
{
// Optional: Handle exceptions (e.g., log the error or notify the user)
System.Windows.Forms.MessageBox.Show("Unable to open the link. Please try again later.", "Error", WinForms.MessageBoxButtons.OK, WinForms.MessageBoxIcon.Error);
}
}
private void CancelConversion()
{
if (_ffmpegProcess != null && !_ffmpegProcess.HasExited)
{
_ffmpegProcess.Kill();
BtnStart.Content = "Start Conversion";
_isConverting = false;
progressBar.Value = 0;
}
}
// ---------- HANDLING OUTPUT ----------
private void OnDataReceived(object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrEmpty(e.Data)) return;
Dispatcher.Invoke(() =>
{
// parse e.Data for time=...
if (TryParseTime(e.Data, out TimeSpan currentPos) && _estimatedTotalDuration.TotalSeconds > 0)
{
double progress = (currentPos.TotalSeconds / _estimatedTotalDuration.TotalSeconds) * 100.0;
progressBar.Value = progress;
double remainSec = _estimatedTotalDuration.TotalSeconds - currentPos.TotalSeconds;
TxtDropZone.Text = $"Converting... {progress:0.0}% " +
$"ETA: {TimeSpan.FromSeconds(remainSec):hh\\:mm\\:ss}";
}
});
//log e.Data here to a console
Console.WriteLine(e.Data);
}
private void OnProcessExited(object sender, EventArgs e)
{
Dispatcher.Invoke(() =>
{
_isConverting = false;
progressBar.Value = 100;
TxtDropZone.Text = "Done!";
BtnStart.Content = "Start Conversion";
});
}
// ---------- TIME PARSING ----------
private bool TryParseTime(string ffmpegLine, out TimeSpan result)
{
result = TimeSpan.Zero;
var match = Regex.Match(ffmpegLine, @"time=(\d+:\d+:\d+\.\d+)");
if (match.Success)
{
if (TimeSpan.TryParse(match.Groups[1].Value, out TimeSpan t))
{
result = t;
return true;
}
}
return false;
}
// ---------- DURATION ----------
private TimeSpan GetVideoDuration(string inputFile)
{
TimeSpan duration = TimeSpan.Zero;
string ffprobePath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"Tools", "ffprobe.exe"
);
// format=duration gives container-level duration
var args = $"-v error -show_entries format=duration " +
$"-of default=noprint_wrappers=1:nokey=1 \"{inputFile}\"";
try
{
var psi = new ProcessStartInfo
{
FileName = ffprobePath,
Arguments = args,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var proc = Process.Start(psi))
{
string output = proc?.StandardOutput.ReadToEnd();
proc?.WaitForExit();
if (double.TryParse(
output,
System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.InvariantCulture,
out double seconds))
{
duration = TimeSpan.FromSeconds(seconds);
}
}
}
catch
{
// If ffprobe fails, no duration. We'll skip progress %.
}
return duration;
}
}
}