Example #1
0
 public static function ProcessSource($source)
 {
     $file = File::Temporary(TEMP_DIR, 'txt');
     switch ($source['source']) {
         case self::SOURCE_CLIPBOARD:
             if (String::IsEmpty($_REQUEST[self::FIELD_CLIPBOARD])) {
                 throw new BaseException('The Clipboard field was empty');
             }
             file_put_contents($file, String::FormatNewlines($_REQUEST[self::FIELD_CLIPBOARD]));
             break;
         case self::SOURCE_UPLOAD:
             $upload = $_FILES[self::FIELD_UPLOAD];
             // Check for errors
             if ($upload['error'] != UPLOAD_ERR_OK) {
                 throw new BaseException(Uploads::CodeToMessage($upload['error']));
             }
             if (move_uploaded_file($upload['tmp_name'], $file) === false) {
                 throw new BaseException('Could not process uploaded file');
             }
             break;
         case self::SOURCE_URL:
             $http = new HTTP();
             if ($http->Get($_REQUEST[self::FIELD_URL], $_REQUEST[self::FIELD_URL])) {
                 file_put_contents($file, String::FormatNewlines($http->body));
             } else {
                 throw new BaseException('Could not access URL: ' . $http->error);
             }
             break;
     }
     return basename($file);
 }
Example #2
0
 private static function ConvertToMp4($vi, $filename, $directory, $vbitrate, $abitrate, $scale, $callback)
 {
     $tools = Video_Tools::Get();
     $vbitrate = ($vbitrate <= 40 ? 'crf=' : 'ratetol=1.0:bitrate=') . $vbitrate;
     $tmp_file = File::Temporary($directory, self::EXTENSION_AVI);
     $cmd = (!empty($tools->nice) ? $tools->nice . ' ' : '') . $tools->mencoder . ' ' . escapeshellarg($filename) . ' ' . '-o ' . escapeshellarg($tmp_file) . ' ' . '-sws 9 ' . '-noskip ' . '-ovc x264 ' . '-x264encopts ' . escapeshellarg($vbitrate . ':bframes=1:me=umh:partitions=all:trellis=1:qp_step=4:qcomp=0.7:direct_pred=auto:keyint=300:threads=' . self::THREADS) . ' ' . '-vf ' . escapeshellarg((!empty($scale) ? $scale . ',' : '') . 'harddup') . ' ' . ($vi->has_audio ? '-oac faac -faacopts ' . escapeshellarg('br=' . $abitrate . ':mpeg=4:object=2') . ' -channels ' . self::CHANNELS_AAC . ' -srate ' . self::SAMPLE_RATE_AAC . ' ' : ' -nosound ') . '-ofps ' . escapeshellarg($vi->video_fps);
     if (self::ExecuteCmdAsync($cmd, $callback)) {
         File::Delete($tmp_file);
         throw new BaseException('Video conversion was interrupted by user request');
     }
     // Verify video file generated
     if (filesize($tmp_file) == 0) {
         File::Delete($tmp_file);
         throw new BaseException('Unable to convert video file to H.264/AAC MP4', $filename, $output);
     }
     // Get the filenames of the extracted raw streams
     $directory = Dir::StripTrailingSlash($directory);
     $basename = basename($tmp_file, '.' . self::EXTENSION_AVI);
     $videofile = $directory . '/' . $basename . '_video.h264';
     $audiofile_raw = $directory . '/' . $basename . '_audio.raw';
     $audiofile = $directory . '/' . $basename . '_audio.aac';
     // Extract the raw streams
     $cmd = (!empty($tools->nice) ? $tools->nice . ' ' : '') . $tools->mp4box . ' -aviraw video ' . escapeshellarg($tmp_file) . ' 2>&1';
     self::Log($cmd);
     $output = shell_exec($cmd);
     self::Log($output);
     if (!file_exists($videofile)) {
         File::Delete($tmp_file);
         throw new BaseException('Unable to extract video from file using MP4Box', $videofile, $output);
     }
     $output_file = File::Temporary($directory, self::EXTENSION_MP4);
     // Process video files that do have an audio stream
     if ($vi->has_audio) {
         $cmd = (!empty($tools->nice) ? $tools->nice . ' ' : '') . $tools->mp4box . ' -aviraw audio ' . escapeshellarg($tmp_file) . ' 2>&1';
         self::Log($cmd);
         $output = shell_exec($cmd);
         self::Log($output);
         if (!file_exists($audiofile_raw)) {
             File::Delete($tmp_file);
             File::Delete($videofile);
             throw new BaseException('Unable to extract audio from file using MP4Box', $audiofile_raw, $output);
         }
         rename($audiofile_raw, $audiofile);
         $cmd = (!empty($tools->nice) ? $tools->nice . ' ' : '') . $tools->mp4box . ' -add ' . escapeshellarg($videofile) . ' -add ' . escapeshellarg($audiofile) . ' -fps ' . escapeshellarg($vi->video_fps) . ' -inter 500 ' . escapeshellarg($output_file) . ' 2>&1';
         self::Log($cmd);
         $output = shell_exec($cmd);
         self::Log($output);
         File::Delete($audiofile);
     } else {
         $cmd = (!empty($tools->nice) ? $tools->nice . ' ' : '') . $tools->mp4box . ' -add ' . escapeshellarg($videofile) . ' -fps ' . escapeshellarg($vi->video_fps) . ' -inter 500 ' . escapeshellarg($output_file) . ' 2>&1';
         self::Log($cmd);
         $output = shell_exec($cmd);
         self::Log($output);
     }
     // Remove temporary files
     File::Delete($tmp_file);
     File::Delete($videofile);
     if (!file_exists($output_file)) {
         throw new BaseException('Unable to generate MP4 file using MP4Box', $output_file, $output);
     }
     return $output_file;
 }