-
Notifications
You must be signed in to change notification settings - Fork 63
/
ConverterForm.cs
183 lines (149 loc) · 6.86 KB
/
ConverterForm.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
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;
namespace WebMConverter
{
public partial class ConverterForm : Form
{
private string[] _arguments;
//private Process _process;
private FFmpeg _ffmpegProcess;
private Timer _timer;
private bool _ended;
private bool _panic;
int currentPass = 0;
private bool _multipass;
private bool _cancelMultipass;
private MainForm _owner;
public ConverterForm(MainForm mainForm, string[] args)
{
InitializeComponent();
_arguments = args;
_owner = mainForm;
}
private void ProcessOnErrorDataReceived(object sender, DataReceivedEventArgs args)
{
if (args.Data != null)
textBoxOutput.Invoke((Action)(() => textBoxOutput.AppendText("\n" + args.Data)));
}
private void ProcessOnOutputDataReceived(object sender, DataReceivedEventArgs args)
{
if (args.Data != null)
textBoxOutput.Invoke((Action)(() => textBoxOutput.AppendText("\n" + args.Data)));
}
private void ConverterForm_Load(object sender, EventArgs e)
{
string argument = null;
_multipass = true;
if (_arguments.Length == 1)
{
_multipass = false;
argument = _arguments[0];
}
if (_multipass)
for (int i = 0; i < _arguments.Length; i++)
textBoxOutput.AppendText(string.Format("\nArguments for pass {0}: {1}", i + 1, _arguments[i]));
else
textBoxOutput.AppendText("\nArguments: " + argument);
if (_multipass)
MultiPass(_arguments);
else
SinglePass(argument);
}
private void SinglePass(string argument)
{
_ffmpegProcess = new FFmpeg(argument);
_ffmpegProcess.Process.ErrorDataReceived += ProcessOnErrorDataReceived;
_ffmpegProcess.Process.OutputDataReceived += ProcessOnOutputDataReceived;
_ffmpegProcess.Process.Exited += (o, args) => textBoxOutput.Invoke((Action)(() =>
{
if (_panic) return; //This should stop that one exception when closing the converter
textBoxOutput.AppendText("\n--- FFMPEG HAS EXITED ---");
buttonCancel.Enabled = false;
_timer = new Timer();
_timer.Interval = 500;
_timer.Tick += Exited;
_timer.Start();
}));
_ffmpegProcess.Start();
}
private void MultiPass(string[] arguments)
{
int passes = arguments.Length;
_ffmpegProcess = new FFmpeg(arguments[currentPass]);
_ffmpegProcess.Process.ErrorDataReceived += ProcessOnErrorDataReceived;
_ffmpegProcess.Process.OutputDataReceived += ProcessOnOutputDataReceived;
_ffmpegProcess.Process.Exited += (o, args) => textBoxOutput.Invoke((Action)(() =>
{
if (_panic) return; //This should stop that one exception when closing the converter
textBoxOutput.AppendText("\n--- FFMPEG HAS EXITED ---");
currentPass++;
if (currentPass < passes && !_cancelMultipass)
{
textBoxOutput.AppendText(string.Format("\n--- ENTERING PASS {0} ---", currentPass + 1));
MultiPass(arguments); //Sort of recursion going on here, be careful with stack overflows and shit
return;
}
buttonCancel.Enabled = false;
_timer = new Timer();
_timer.Interval = 500;
_timer.Tick += Exited;
_timer.Start();
}));
_ffmpegProcess.Start();
}
private void Exited(object sender, EventArgs eventArgs)
{
_timer.Stop();
var process = _ffmpegProcess.Process;
if (process.ExitCode != 0)
{
if (_cancelMultipass)
textBoxOutput.AppendText("\n\nConversion cancelled.");
else
{
textBoxOutput.AppendText(string.Format("\n\nffmpeg.exe exited with exit code {0}. That's usually bad.", process.ExitCode));
textBoxOutput.AppendText("\nIf you have no idea what went wrong, open an issue on GitHub and copy paste the output of this window there.");
}
pictureBox.BackgroundImage = Properties.Resources.cross;
if (process.ExitCode == -1073741819) //This error keeps happening for me if I set threads to anything above 1, might happen for other people too
MessageBox.Show("It appears ffmpeg.exe crashed because of a thread error. Set the amount of threads to 1 in the advanced tab and try again.", "FYI", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
textBoxOutput.AppendText("\n\nVideo converted succesfully!");
pictureBox.BackgroundImage = Properties.Resources.tick;
buttonPlay.Enabled = true;
}
buttonCancel.Text = "Close";
buttonCancel.Enabled = true;
_ended = true;
}
private void buttonCancel_Click(object sender, EventArgs e)
{
_cancelMultipass = true;
if (!_ended || _panic) //Prevent stack overflow
{
if (!_ffmpegProcess.Process.HasExited)
_ffmpegProcess.Process.Kill();
}
else
Close();
}
private void ConverterForm_FormClosing(object sender, FormClosingEventArgs e)
{
_panic = true; //Shut down while avoiding exceptions
buttonCancel_Click(sender, e);
}
private void ConverterForm_FormClosed(object sender, FormClosedEventArgs e)
{
_ffmpegProcess.Process.Dispose();
}
private void buttonPlay_Click(object sender, EventArgs e)
{
Process.Start(_owner.textBoxOut.Text); //Play result video
}
}
}