예제 #1
0
 /**
  * convert video to mp4
  *
  * @param string $file 
  * @return void
  * @author Andy Bennett
  */
 public static function video_mp4($file = null)
 {
     if (is_null($file)) {
         throw new Exception("No filepath supplied", 1);
     }
     if (!file_exists($file)) {
         throw new Exception("Invalid filepath supplied", 1);
     }
     // get the filename parts
     $path = pathinfo($file, PATHINFO_DIRNAME);
     $filename = pathinfo($file, PATHINFO_FILENAME);
     $tmp_dir = Kohana::config('ffmpeg.tmp_path');
     // set the output details
     $output = $path . '/' . $filename . '-c.mp4';
     $infile = $file;
     $options = "-vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256 \\\n\t\t\t\t\t-partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \\\n\t\t\t   \t\t-me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \\\n\t\t\t   \t\t-flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \\\n\t\t\t\t\t-r 25 -g 5 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10 \\\n\t\t\t\t\t-qmax 51 -qdiff 4";
     $str = 'cd ' . $tmp_dir . '; ' . Kohana::config('ffmpeg.binaries_path') . " -y -i {$infile} -an -pass 1 -threads 2 {$options} {$output}";
     // echo( $str );
     exec($str . ' 2>&1', $buffer1);
     $has_audio = ffmpeg::has_audio($file);
     $audio = $has_audio ? "-acodec libfaac -ar 44100 -ab 96k" : "-an";
     $str = 'cd ' . $tmp_dir . '; ' . Kohana::config('ffmpeg.binaries_path') . " -y -i {$infile} {$audio} -pass 2 -threads 2 {$options} {$output}; /usr/bin/MP4Box -hint {$output}";
     // echo( $str );
     exec($str . ' 2>&1', $buffer2);
     // qt-faststart $tmpfile $outfile
     if (!file_exists($output) or !filesize($output)) {
         Kohana::log('error', 'Video encoding error');
         Kohana::log('error', Kohana::debug($buffer1));
         Kohana::log('error', Kohana::debug($buffer2));
         // var_dump( $buffer );
         throw new Exception('Video conversion error');
     }
     return $output;
 }