Skip to content

Commit 9e28bc2

Browse files
committed
Added a loop for Ultra quality
The loop implemented attempts to lower the bitrate until the targeted size is met. It will run 10 times or until the target size is met. If it goes past 10 passes, it will give the user an error message with recommendations.
1 parent ee57690 commit 9e28bc2

2 files changed

Lines changed: 68 additions & 16 deletions

File tree

Helper.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
56
using System.Text;
@@ -45,5 +46,24 @@ public static double getFileSize(String file)
4546

4647
return Math.Round(fileSize / 1024, 2);
4748
}
49+
50+
/// <summary>
51+
/// Calls ffmpeg to encode the video
52+
/// </summary>
53+
/// <param name="command">The base command string (passes are entered automatically by this class)</param>
54+
/// <param name="fileOutput">The path to the output</param>
55+
public static void encodeVideo(String command, String fileOutput)
56+
{
57+
String commandPass1 = "-pass 1 -f webm NUL";
58+
String commandPass2 = "-pass 2 ";
59+
60+
// Pass 1
61+
var pass1 = Process.Start("ffmpeg", command + commandPass1);
62+
pass1.WaitForExit();
63+
64+
// Pass 2
65+
var pass2 = Process.Start("ffmpeg", command + commandPass2 + "\"" + fileOutput + "\"");
66+
pass2.WaitForExit();
67+
}
4868
}
4969
}

formMain.cs

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ private void btnConvert_Click(object sender, EventArgs e)
6161

6262
// Base command where each element gets replaced
6363
String baseCommand = "-y {time1} -i \"{input}\" {time2} -t {length} -c:v libvpx -b:v {bitrate} {scale} -threads {threads} {quality} -an ";
64-
String commandPass1 = "-pass 1 -f webm NUL";
65-
String commandPass2 = "-pass 2 ";
6664
String filterCommands = null;
6765

6866
// Verification boolean just incase the user messes up
6967
bool verified = true;
7068
bool filters = false;
7169

70+
double bitrate = 0;
71+
7272
// Validates if the user input a value for txtInput
7373
if (txtInput.Text == "")
7474
{
@@ -160,7 +160,7 @@ private void btnConvert_Click(object sender, EventArgs e)
160160
}
161161
else
162162
{
163-
double bitrate = Helper.calcBitrate(txtMaxSize.Text, txtLength.Text);
163+
bitrate = Helper.calcBitrate(txtMaxSize.Text, txtLength.Text);
164164

165165
// Changes the quality to what the user selected
166166
switch (comboQuality.Text)
@@ -174,7 +174,6 @@ private void btnConvert_Click(object sender, EventArgs e)
174174
baseCommand = baseCommand.Replace("{bitrate}", bitrate.ToString() + "K");
175175
break;
176176
case "Ultra":
177-
// TODO: Add a method to get the output as close to user specified filesize as possible
178177
baseCommand = baseCommand.Replace("{quality}", "-quality best -auto-alt-ref 1 -lag-in-frames 16 -slices 8");
179178
bitrate = Convert.ToDouble(bitrate) * 1024;
180179
baseCommand = baseCommand.Replace("{bitrate}", bitrate.ToString());
@@ -200,13 +199,7 @@ private void btnConvert_Click(object sender, EventArgs e)
200199

201200
try
202201
{
203-
// Pass 1
204-
var pass1 = Process.Start("ffmpeg", baseCommand + commandPass1);
205-
pass1.WaitForExit();
206-
207-
// Pass 2
208-
var pass2 = Process.Start("ffmpeg", baseCommand + commandPass2 + "\"" + txtOutput.Text + "\"");
209-
pass2.WaitForExit();
202+
Helper.encodeVideo(baseCommand, txtOutput.Text);
210203
}
211204
catch (Win32Exception ex)
212205
{
@@ -215,16 +208,55 @@ private void btnConvert_Click(object sender, EventArgs e)
215208
Debug.WriteLine(ex);
216209
}
217210

218-
if (Helper.getFileSize(txtOutput.Text) < Convert.ToDouble(txtMaxSize.Text) * 1024)
211+
double fileSize = Helper.getFileSize(txtOutput.Text);
212+
213+
if (fileSize < Convert.ToDouble(txtMaxSize.Text) * 1024)
219214
{
220215
// Clears the output box so user's don't overwrite their previous output
221-
txtOutput.Text = null;
216+
txtOutput.Text = "";
222217
}
223218
else
224219
{
225-
MessageBox.Show("The final clip is larger than " + txtMaxSize.Text + "MB.\n" +
226-
"This occured because the clip's resolution was too large,\n" +
227-
"and/or because the clip was too long for the inputted size.");
220+
if (comboQuality.Text == "Ultra")
221+
{
222+
/*
223+
* Automatically attempt to create a smaller file
224+
* If it doesn't work within 10 attempts, recommend
225+
* 'Best' quality
226+
*/
227+
int passes = 0;
228+
229+
while (fileSize > Convert.ToDouble(txtMaxSize.Text) * 1024 && passes <= 10)
230+
{
231+
// Lowers the bitrate by 1k
232+
bitrate -= 1000;
233+
234+
// Replacing the whole command just in case the file name contains the same numbers
235+
baseCommand = baseCommand.Replace("-b:v " + (bitrate + 1000), "-b:v " + bitrate);
236+
237+
Helper.encodeVideo(baseCommand, txtOutput.Text);
238+
passes++;
239+
240+
// Gets the filesize after encoding
241+
fileSize = Helper.getFileSize(txtOutput.Text);
242+
}
243+
244+
if (fileSize < Convert.ToDouble(txtMaxSize.Text) * 1024)
245+
{
246+
txtOutput.Text = "";
247+
}
248+
else
249+
MessageBox.Show("Could not get the file size below " + txtMaxSize.Text + "MB.\n" +
250+
"Try using 'Best' quality, and if that doesn't work,\n" +
251+
"you must reduce your resolution and/or shorten the length.",
252+
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
253+
}
254+
else
255+
{
256+
MessageBox.Show("The final clip is larger than " + txtMaxSize.Text + "MB.\n" +
257+
"This occured because the clip's resolution was too large,\n" +
258+
"and/or because the clip was too long for the inputted size.");
259+
}
228260
}
229261
}
230262

0 commit comments

Comments
 (0)