@@ -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