-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathformMain.cs
More file actions
454 lines (403 loc) · 18 KB
/
formMain.cs
File metadata and controls
454 lines (403 loc) · 18 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace MasterOfWebM
{
public partial class formMain : Form
{
// ********************
// Variables
// ********************
private String THREADS = Environment.ProcessorCount.ToString(); // Obtains the number of threads the computer has
private String runningDirectory = AppDomain.CurrentDomain.BaseDirectory; // Obtains the root directory
Regex verifyLength = new Regex(@"^\d{1,3}"); // Regex to verify if txtLength is properly typed in
Regex verifyTimeStart = new Regex(@"^[0-6]\d:[0-6]\d:[0-6]\d"); // Regex to verify if txtStartTime is properly typed in
Regex verifyWidth = new Regex(@"^\d{1,4}"); // Regex to verify if txtWidth is properly typed in
Regex verifyMaxSize = new Regex(@"^\d{1,4}"); // Regex to verify if txtMaxSize is properly typed in
Regex verifyCrop = new Regex(@"^\d{1,4}:\d{1,4}:\d{1,4}:\d{1,4}"); // Regex to verify if txtCrop is properly typed in
// ********************
// Functions
// ********************
public formMain()
{
InitializeComponent();
}
// As soon as the user clicks on txtTimeStart, get rid of the informational text
private void txtTimeStart_Enter(object sender, EventArgs e)
{
if (txtTimeStart.Text == "HH:MM:SS")
{
txtTimeStart.Text = "";
txtTimeStart.ForeColor = Color.Black;
}
}
// Check if the user clicks away without typing anything into txtTimeStart
private void txtTimeStart_Leave(object sender, EventArgs e)
{
if (txtTimeStart.Text == "")
{
txtTimeStart.Text = "HH:MM:SS";
txtTimeStart.ForeColor = Color.Silver;
}
}
private void btnConvert_Click(object sender, EventArgs e)
{
// Delete any existing temp subtitle file
Helper.subsCheck();
// Disable btnConvert so user's cant click on it multiple times
btnConvert.Enabled = false;
// Base command where each element gets replaced
String baseCommand = "-y {time1} -i \"{input}\" {time2} -t {length} -c:v libvpx -b:v {bitrate} {scale} -threads {threads} {metadata} {quality} {audio} ";
String filterCommands = null;
// Verification boolean just incase the user messes up
bool verified = true;
bool filters = false;
double bitrate = 0;
// Validates if the user input a value for txtInput
if (txtInput.Text == "")
{
verified = false;
MessageBox.Show("An input file needs to be selected", "Verification Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
baseCommand = baseCommand.Replace("{input}", txtInput.Text);
}
// Validates if the user input a value for txtOutput
if (txtOutput.Text == "")
{
verified = false;
MessageBox.Show("An output file needs to be selected", "Verification Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// Validates if the user input a value for txtTimeStart
if (!verifyTimeStart.IsMatch(txtTimeStart.Text))
{
verified = false;
MessageBox.Show("The time format is messed up.\nPlease use HH:MM:SS", "Verification Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
// Calculates the seconds from the time-code
double seconds = Helper.convertToSeconds(txtTimeStart.Text);
if (seconds > 30)
{
if (txtSubs.Text == "")
{
// If not subtitles exist
baseCommand = baseCommand.Replace("{time1}", "-ss " + Convert.ToString(seconds - 30));
baseCommand = baseCommand.Replace("{time2}", "-ss 30");
}
else
{
// If subtitles exist
baseCommand = baseCommand.Replace(" {time1}", "");
baseCommand = baseCommand.Replace("{time2}", "-ss " + Convert.ToString(seconds));
}
}
else
{
baseCommand = baseCommand.Replace(" {time1}", "");
baseCommand = baseCommand.Replace("{time2}", "-ss " + seconds);
}
}
// Validates if the user input a value for txtLength
if (!verifyLength.IsMatch(txtLength.Text))
{
verified = false;
MessageBox.Show("The length of the video is not properly set", "Verification Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
baseCommand = baseCommand.Replace("{length}", txtLength.Text);
}
// Validates if the user input a value for txtCrop
if (!verifyCrop.IsMatch(txtCrop.Text))
{
if (txtCrop.Text != "o_w:o_h:x:y")
{
verified = false;
MessageBox.Show("The crop field is not properly set\nSyntax:\nout_x:out_y:x:y\n\nout_x & out_y is the output size\nx & y are where you begin your crop",
"Verification Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
filters = true;
filterCommands += filterCommands == null ? "crop=" + txtCrop.Text : ",crop=" + txtCrop.Text;
}
// Check if we need to add subtitles
if (txtSubs.Text != "")
{
switch (Path.GetExtension(txtSubs.Text))
{
case ".ass":
filters = true;
File.Copy(txtSubs.Text, runningDirectory + "subs.ass");
filterCommands += filterCommands == null ? "ass=subs.ass" : ",ass=subs.ass";
break;
case ".srt":
filters = true;
File.Copy(txtSubs.Text, runningDirectory + "subs.srt");
filterCommands += filterCommands == null ? "subtitles=subs.srt" : ",subtitles=subs.srt";
break;
}
}
// Validates if the user input a value for txtWidth
if (!verifyWidth.IsMatch(txtWidth.Text))
{
if (txtWidth.Text != "")
{
verified = false;
MessageBox.Show("The width is not properly set", "Verification Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
filters = true;
filterCommands += filterCommands == null ? "scale=" + txtWidth.Text + ":-1" : ",scale=" + txtWidth.Text + ":-1";
}
// Validates if the user input a value for txtMaxSize
if (!verifyMaxSize.IsMatch(txtMaxSize.Text))
{
verified = false;
MessageBox.Show("The maxium file size is not properly set", "Verification Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
try
{
bitrate = Helper.calcBitrate(txtMaxSize.Text, txtLength.Text);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
// If audio is requested
if (checkAudio.Checked)
{
// TODO: Give bitrate options for audio (currently enforcing 48k)
// TODO: Disable audio encoding on first pass to speed up encoding
bitrate -= 48;
baseCommand = baseCommand.Replace("{audio}", "-c:a libvorbis -b:a 48k");
}
else
{
baseCommand = baseCommand.Replace("{audio}", "-an");
}
// Changes the quality to what the user selected
switch (comboQuality.Text)
{
case "Good":
baseCommand = baseCommand.Replace("{quality}", "-quality good -cpu-used 0");
baseCommand = baseCommand.Replace("{bitrate}", bitrate.ToString() + "K");
break;
case "Best":
baseCommand = baseCommand.Replace("{quality}", "-quality best -auto-alt-ref 1 -lag-in-frames 16 -slices 8");
baseCommand = baseCommand.Replace("{bitrate}", bitrate.ToString() + "K");
break;
case "Ultra":
baseCommand = baseCommand.Replace("{quality}", "-quality best -auto-alt-ref 1 -lag-in-frames 16 -slices 8");
bitrate = Convert.ToDouble(bitrate) * 1024;
baseCommand = baseCommand.Replace("{bitrate}", bitrate.ToString());
break;
}
}
// If any filters are being used, add them to baseCommand
if (filters)
{
filterCommands = "-vf " + filterCommands;
baseCommand = baseCommand.Replace("{scale}", filterCommands);
}
else
{
baseCommand = baseCommand.Replace(" {scale}", "");
}
// Validates if the user input a value for txtTitle
if (txtTitle.Text != "")
{
baseCommand = baseCommand.Replace("{metadata}", string.Format("-metadata title=\"{0}\"", txtTitle.Text.Replace("\"", "\\\"")));
}
// If everything is valid, continue with the conversion
if (verified)
{
baseCommand = baseCommand.Replace("{threads}", THREADS);
try
{
Helper.encodeVideo(baseCommand, txtOutput.Text);
}
catch (Win32Exception ex)
{
MessageBox.Show("It appears you are missing ffmpeg. Please\ngo obtain a copy of it, and put it in the same\nfolder as this executable.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Debug.WriteLine(ex);
}
double fileSize = Helper.getFileSize(txtOutput.Text);
if (fileSize < Convert.ToDouble(txtMaxSize.Text) * 1024)
{
// Clears the output box so user's don't overwrite their previous output
txtOutput.Text = "";
if (txtSubs.Text != "")
{
switch (Path.GetExtension(txtSubs.Text))
{
case ".ass":
File.Delete(runningDirectory + "\\subs.ass");
break;
case ".srt":
File.Delete(runningDirectory + "\\subs.srt");
break;
}
txtSubs.Text = "";
}
}
else
{
if (comboQuality.Text == "Ultra")
{
/*
* Automatically attempt to create a smaller file
* If it doesn't work within 10 attempts, recommend
* 'Best' quality
*/
int passes = 0;
while (fileSize > Convert.ToDouble(txtMaxSize.Text) * 1024 && passes <= 10)
{
// Lowers the bitrate by 1k
bitrate -= 1000;
// Replacing the whole command just in case the file name contains the same numbers
baseCommand = baseCommand.Replace("-b:v " + (bitrate + 1000), "-b:v " + bitrate);
Helper.encodeVideo(baseCommand, txtOutput.Text);
passes++;
// Gets the filesize after encoding
fileSize = Helper.getFileSize(txtOutput.Text);
}
if (fileSize < Convert.ToDouble(txtMaxSize.Text) * 1024)
{
txtOutput.Text = "";
if (txtSubs.Text != "")
{
switch (Path.GetExtension(txtSubs.Text))
{
case ".ass":
File.Delete(runningDirectory + "\\subs.ass");
break;
case ".srt":
File.Delete(runningDirectory + "\\subs.srt");
break;
}
txtSubs.Text = "";
}
}
else
MessageBox.Show("Could not get the file size below " + txtMaxSize.Text + "MB.\n" +
"Try using 'Best' quality, and if that doesn't work,\n" +
"you must reduce your resolution and/or shorten the length.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("The final clip is larger than " + txtMaxSize.Text + "MB.\n" +
"This occured because the clip's resolution was too large,\n" +
"and/or because the clip was too long for the inputted size.");
}
}
}
// Re-enable the button after a run
btnConvert.Enabled = true;
}
private void btnInput_Click(object sender, EventArgs e)
{
if (inputFileDialog.ShowDialog() == DialogResult.OK)
{
txtInput.Text = inputFileDialog.FileName;
}
}
private void btnOutput_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
txtOutput.Text = saveFileDialog1.FileName;
}
}
private void formMain_Load(object sender, EventArgs e)
{
lblThreads.Text = "Threads: " + THREADS;
comboQuality.SelectedIndex = 0;
// Calls the font config checker, and if something went wrong, it disables converting
if (!Helper.checkFFmpegFontConfig())
{
btnConvert.Enabled = false;
}
Helper.checkUpdate();
}
// Handles when the user focuses txtCrop
private void txtCrop_Enter(object sender, EventArgs e)
{
txtCrop.Left -= 46;
txtCrop.Size = new System.Drawing.Size(115, 20);
txtCrop.ForeColor = Color.Black;
if (txtCrop.Text == "o_w:o_h:x:y")
{
txtCrop.Text = "";
}
}
// Handles when the user unfocuses txtCrop
private void txtCrop_Leave(object sender, EventArgs e)
{
txtCrop.Left += 46;
txtCrop.Size = new System.Drawing.Size(69, 20);
if (txtCrop.Text == "")
{
txtCrop.Text = "o_w:o_h:x:y";
txtCrop.ForeColor = Color.Silver;
}
}
private void btnSubs_Click(object sender, EventArgs e)
{
if (subsFileDialog.ShowDialog() == DialogResult.OK)
{
txtSubs.Text = subsFileDialog.FileName;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
txtInput.Text = txtOutput.Text = txtSubs.Text = txtLength.Text = txtWidth.Text = "";
txtTimeStart.Text = "HH:MM:SS";
txtTimeStart.ForeColor = Color.Silver;
txtMaxSize.Text = "3";
txtCrop.Text = "o_w:o_h:x:y";
txtCrop.ForeColor = Color.Silver;
txtTitle.Text = "";
comboQuality.SelectedIndex = 0;
checkAudio.Checked = false;
}
private void comboQuality_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboQuality.Text == "Ultra")
MessageBox.Show("Ultra quality will try getting just under your\n" +
"'Max Size'. This program will run ffmpeg up to 11 \n" +
"times and currently there is no way of stopping it.\n\n" +
"USE AT YOUR OWN RISK.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void formMain_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void formMain_DragDrop(object sender, DragEventArgs e)
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
txtInput.Text = files[0];
}
}
}