This repository was archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.cs
More file actions
342 lines (284 loc) · 12.5 KB
/
MainWindow.cs
File metadata and controls
342 lines (284 loc) · 12.5 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
using System.Diagnostics;
using System.Text;
namespace DannyRocko5976
{
public partial class MainWindow : Form
{
private static readonly Random random = new Random();
private bool compressing = false;
private double chosenSize = 0;
public MainWindow()
{
InitializeComponent();
SetupDragAndDropEventHandlers();
}
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
if (compressing) {
e.Cancel = true;
ShowErrorMessageBox("You are compressing something right now... don't close the app.");
}
}
private void SetupDragAndDropEventHandlers()
{
chooseInputVideoButton.DragEnter += (sender, e) => HandleDragEnter(e);
chooseInputVideoButton.DragDrop += (sender, e) => HandleDragDrop(e, selectedVideoPathTextbox, true);
chooseOutputFolderButton.DragEnter += (sender, e) => HandleDragEnter(e);
chooseOutputFolderButton.DragDrop += (sender, e) => HandleDragDrop(e, outputFolderPathTextbox, false);
}
private void HandleDragEnter(DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
private void HandleDragDrop(DragEventArgs e, TextBox targetTextbox, bool isVideoPath)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length > 0)
{
string filePath = files[0];
targetTextbox.Text = filePath;
if (isVideoPath)
{
double fileSizeInMB = GetFileSizeInMB(filePath);
if (fileSizeInMB > 8)
{
originalVideoSizeTextbox.Text = fileSizeInMB.ToString();
eightMB.Enabled = fileSizeInMB > 8;
twentyfiveMB.Enabled = fileSizeInMB > 25;
fiftyMB.Enabled = fileSizeInMB > 50;
hundredMB.Enabled = fileSizeInMB > 100;
}
else
{
selectedVideoPathTextbox.Clear();
originalVideoSizeTextbox.Clear();
eightMB.Enabled = twentyfiveMB.Enabled = fiftyMB.Enabled = hundredMB.Enabled = false;
eightMB.Checked = twentyfiveMB.Checked = fiftyMB.Checked = hundredMB.Checked = false;
ShowErrorMessageBox("Dude... that video is less than 8MB already.");
}
}
canICompressNowLOL();
}
}
private double GetFileSizeInMB(string filePath)
{
try
{
FileInfo fileInfo = new FileInfo(filePath);
return (double)fileInfo.Length / (1024 * 1024);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
return -1;
}
}
private void chooseInputVideoButton_Click(object sender, EventArgs e)
{
string videoFilePath = GetFilePathWithDialog("Video files (*.mp4;*.avi;*.mkv;*.wmv)|*.mp4;*.avi;*.mkv;*.wmv|All files (*.*)|*.*");
if (!string.IsNullOrEmpty(videoFilePath))
{
double fileSizeInMB = GetFileSizeInMB(videoFilePath);
selectedVideoPathTextbox.Text = videoFilePath;
originalVideoSizeTextbox.Text = fileSizeInMB.ToString();
eightMB.Enabled = fileSizeInMB > 8;
twentyfiveMB.Enabled = fileSizeInMB > 25;
fiftyMB.Enabled = fileSizeInMB > 50;
hundredMB.Enabled = fileSizeInMB > 100;
canICompressNowLOL();
}
else
{
selectedVideoPathTextbox.Clear();
originalVideoSizeTextbox.Clear();
eightMB.Enabled = twentyfiveMB.Enabled = fiftyMB.Enabled = hundredMB.Enabled = false;
eightMB.Checked = twentyfiveMB.Checked = fiftyMB.Checked = hundredMB.Checked = false;
}
}
private void chooseOutputFolderButton_Click(object sender, EventArgs e)
{
string selectedFolderPath = GetFolderPathWithDialog();
if (!string.IsNullOrEmpty(selectedFolderPath))
outputFolderPathTextbox.Text = selectedFolderPath;
canICompressNowLOL();
}
private string GetFilePathWithDialog(string filter = null)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
openFileDialog.Filter = filter ?? "All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
return openFileDialog.ShowDialog() == DialogResult.OK ? openFileDialog.FileName : null;
}
}
private string GetFolderPathWithDialog()
{
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
{
folderBrowserDialog.Description = "Select a folder LOL!";
folderBrowserDialog.ShowNewFolderButton = false;
return folderBrowserDialog.ShowDialog() == DialogResult.OK ? folderBrowserDialog.SelectedPath : null;
}
}
private void eightMB_CheckedChanged(object sender, EventArgs e) => UpdateChosenSize(8);
private void twentyfiveMB_CheckedChanged(object sender, EventArgs e) => UpdateChosenSize(25);
private void fiftyMB_CheckedChanged(object sender, EventArgs e) => UpdateChosenSize(50);
private void hundredMB_CheckedChanged(object sender, EventArgs e) => UpdateChosenSize(100);
private void UpdateChosenSize(double size)
{
chosenSize = size;
canICompressNowLOL();
}
private bool IsFfmpegInPath()
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "ffmpeg",
Arguments = "-version",
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
Process process = new Process
{
StartInfo = startInfo
};
process.Start();
process.WaitForExit();
return process.ExitCode == 0;
}
catch (Exception)
{
return false;
}
}
private void compressButton_Click(object sender, EventArgs e)
{
if (!IsFfmpegInPath())
{
ShowErrorMessageBox("FFmpeg is not in Path. Install FFmpeg by following this link: https://phoenixnap.com/kb/ffmpeg-windows");
return;
}
bool status = canICompressNowLOL();
if (!status)
{
ShowErrorMessageBox("For some reason, you can't compress just yet! Check something!");
return;
}
if (chosenSize == 0)
{
ShowErrorMessageBox("Dude your chosen compression size is 0 MB HOW?!");
return;
}
if (compressing)
{
ShowErrorMessageBox("Dude you already are compressing something right now WAIT!");
return;
}
compressing = true;
compressButton.Enabled = false;
compressButton.Text = "Compressing!";
string inputFilePath = selectedVideoPathTextbox.Text;
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(inputFilePath);
string outputFilePath = $"{outputFolderPathTextbox.Text}\\{fileNameWithoutExtension}_compressed_to_{chosenSize}mb_LOL{random.Next(1, 101)}.mp4";
long desiredFileSizeInBytes = (long)chosenSize * 1024 * 1024;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
CompressVideoToDesiredFileSize(inputFilePath, outputFilePath, desiredFileSizeInBytes);
stopwatch.Stop();
TimeSpan elapsedTime = stopwatch.Elapsed;
compressing = false;
compressButton.Text = "Compress Video";
canICompressNowLOL();
double fileSizeInMB = GetFileSizeInMB(outputFilePath);
Process.Start("explorer.exe", Path.GetDirectoryName(outputFilePath));
ShowSuccessMessageBox($"I think the compression is complete... The new file is: {outputFilePath}.\nEstimated new filesize is: {fileSizeInMB}MB LOL! Compression took: {elapsedTime.TotalSeconds} seconds.");
}
private void CompressVideoToDesiredFileSize(string inputFilePath, string outputFilePath, long desiredFileSizeInBytes)
{
try
{
string ffmpegArgs = $"-i \"{inputFilePath}\" -c:v libx264 -crf 23 -preset medium -vf scale=1280:-2 -c:a aac -b:a 128k \"{outputFilePath}\"";
RunFFmpegProcess(ffmpegArgs);
long compressedFileSize = new FileInfo(outputFilePath).Length;
while (compressedFileSize > desiredFileSizeInBytes)
{
string lowerCRFArgs = $"-i \"{outputFilePath}\" -c:v libx264 -crf 18 -preset medium -vf scale=1280:-2 -c:a aac -b:a 128k \"{outputFilePath}\"";
RunFFmpegProcess(lowerCRFArgs);
compressedFileSize = new FileInfo(outputFilePath).Length;
}
}
catch (Exception ex)
{
// ShowErrorMessageBox("Error during video compression: " + ex.Message);
Console.WriteLine("Error during video compression: " + ex.Message);
}
}
private void RunFFmpegProcess(string args)
{
using Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "ffmpeg",
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
},
EnableRaisingEvents = true
};
StringBuilder outputBuilder = new StringBuilder();
StringBuilder errorBuilder = new StringBuilder();
process.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
outputBuilder.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
errorBuilder.AppendLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
string output = outputBuilder.ToString();
string error = errorBuilder.ToString();
if (!string.IsNullOrEmpty(error))
{
// ShowErrorMessageBox("Error during video compression: " + error);
throw new InvalidOperationException("Error during video compression: " + error);
}
}
private void ShowErrorMessageBox(string text)
{
MessageBox.Show(text, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void ShowSuccessMessageBox(string text) {
MessageBox.Show(text, "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private bool canICompressNowLOL()
{
bool canCompress = !compressing && !string.IsNullOrEmpty(selectedVideoPathTextbox.Text) &&
!string.IsNullOrEmpty(outputFolderPathTextbox.Text) &&
(eightMB.Checked || twentyfiveMB.Checked || fiftyMB.Checked || hundredMB.Checked);
compressButton.Enabled = canCompress;
return canCompress;
}
}
}