-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathHelper.cs
More file actions
261 lines (233 loc) · 9.91 KB
/
Helper.cs
File metadata and controls
261 lines (233 loc) · 9.91 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace MasterOfWebM
{
class Helper
{
private const int BITCONVERSION = 8 * 1024; // Converts the filesize to Kilobits
// Version check variables
private static String downloadUrl = "";
private static Version newVersion = null;
private static String xmlUrl = "https://raw.githubusercontent.com/MasterOfWebM/WebM-Converter/master/update.xml";
private static XmlTextReader reader = null;
/// <summary>
/// This function intakes the time format, so it can convert it to flat seconds
/// </summary>
/// <param name="input">A string that is formatted as HH:MM:SS</param>
/// <returns>The seconds.</returns>
public static double convertToSeconds(String input) {
string[] time = input.Split(':');
return Convert.ToDouble(time[0]) * 3600 + Convert.ToDouble(time[1]) * 60 + Convert.ToDouble(time[2]);
}
/// <summary>
/// This function calculates the bitrate required to fit into a file size.
/// </summary>
/// <param name="size">The requested file size in MB</param>
/// <param name="length">The length of the footage in seconds</param>
/// <returns>The bitrate in kilobits</returns>
public static double calcBitrate(String size, String length)
{
return Math.Floor(Convert.ToDouble(size) * BITCONVERSION / Convert.ToDouble(length));
}
/// <summary>
/// Obtains the file size of a given file
/// </summary>
/// <param name="file">The file that needs to be calculated</param>
/// <returns>The file size of a given file</returns>
public static double getFileSize(String file)
{
FileInfo fi = new FileInfo(@file);
double fileSize = fi.Length;
return Math.Round(fileSize / 1024, 2);
}
/// <summary>
/// Calls ffmpeg to encode the video
/// </summary>
/// <param name="command">The base command string (passes are entered automatically by this class)</param>
/// <param name="fileOutput">The path to the output</param>
public static void encodeVideo(String command, String fileOutput)
{
String commandPass1 = "-pass 1 -f webm NUL";
String commandPass2 = "-pass 2 ";
// Pass 1
var pass1 = Process.Start("ffmpeg", command + commandPass1);
pass1.WaitForExit();
// Pass 2
var pass2 = Process.Start("ffmpeg", command + commandPass2 + "\"" + fileOutput + "\"");
pass2.WaitForExit();
}
/// <summary>
/// Checks to see if ffmpeg has a font.conf installed, and if it doesn't
/// it will install one for the user to support subtitles
/// </summary>
/// <returns>If the current FFmpeg installation has a font config installed</returns>
public static bool checkFFmpegFontConfig()
{
// Spawn process to check if ffmpeg is installed and find out where it is
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "/k where ffmpeg & exit";
p.StartInfo.CreateNoWindow = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
if (output == "")
{
MessageBox.Show("FFmpeg is not installed, please either put it in the same directory\n"+
"as this program or in your 'PATH' Environment Variable.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
else
{
// Get rid of the newline at the end of the output
output = output.Replace(Environment.NewLine, "");
// Get the root directory of ffmpeg
output = Path.GetDirectoryName(@output);
if (File.Exists(output + "\\fonts\\fonts.conf"))
{
return true;
}
else
{
if (Directory.Exists(output + "\\fonts"))
{
// If the directory actually exists, just write the config file
try
{
File.Copy("fonts\\fonts.conf", output + "\\fonts\\fonts.conf");
return true;
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException)
{
MessageBox.Show("Failed to create the fonts config due to\n" +
"Unathorized Access. Please start this program with Administrator\n" +
"privileges.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
else
{
MessageBox.Show("Something went wrong with writing the config\n" +
"file. Please message Master Of WebM to figure it out.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
else
{
// If neither the directory, or file exists, then create them both
try
{
Directory.CreateDirectory(output + "\\fonts");
File.Copy("fonts\\fonts.conf", output + "\\fonts\\fonts.conf");
return true;
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException)
{
MessageBox.Show("Failed to create the fonts config due to\n" +
"Unathorized Access. Please start this program with Administrator\n" +
"privileges.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
else
{
MessageBox.Show("Something went wrong with writing the config\n" +
"file. Please message Master Of WebM to figure it out.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
}
}
}
/// <summary>
/// Verifies the version of the program.
/// It will prompt the user if the program is
/// outdated.
/// </summary>
public static void checkUpdate()
{
try
{
reader = new XmlTextReader(xmlUrl);
reader.MoveToContent();
string elementName = "";
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "webmconverter"))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
elementName = reader.Name;
}
else
{
if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
{
switch (elementName)
{
case "version":
newVersion = new Version(reader.Value);
break;
case "url":
downloadUrl = reader.Value;
break;
}
}
}
}
}
}
catch (Exception ex)
{
// Error out to not disrupt the user
Debug.WriteLine(ex.Message);
}
finally
{
if (reader != null)
{
reader.Close();
}
}
// Current version of the application
Version appVersion = Assembly.GetExecutingAssembly().GetName().Version;
if (appVersion.CompareTo(newVersion) < 0)
{
if (MessageBox.Show("You are currently out of date.\nWould you like to update now?", "Version out of date", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
var update = Process.Start(downloadUrl);
}
}
}
/// <summary>
/// Checks if a subtitle (subs.ass | subs.srt) exists
/// and deletes it.
/// </summary>
public static void subsCheck()
{
if(File.Exists("subs.ass"))
{
File.Delete("subs.ass");
}
else if (File.Exists("subs.srt"))
{
File.Delete("subs.srt");
}
}
}
}