Advertisement
Guest User

Untitled

a guest
Jul 11th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1.     function parseVideo($path){
  2.         $out = new stdClass();
  3.  
  4.         $path = escapeshellarg($path);
  5.         $data = json_decode(shell_exec("ffprobe -v quiet -print_format json -show_format -show_streams $path"));
  6.  
  7.         $has_video=0;
  8.         $has_audio=0;
  9.  
  10.         $out->audio_bitrate = 0;
  11.         $out->video_bitrate = 0;
  12.  
  13.         foreach($data->streams as $input){
  14.             if($input->codec_type == "audio" && $has_audio == 0){
  15.                 $has_audio = 1;
  16.                 $out->audio_bitrate = $input->bit_rate;
  17.                 $out->audio_codec = $input->codec_name;
  18.             } else if($input->codec_type == "video" && $has_video == 0){
  19.                 $has_video = 1;
  20.                 $out->video_bitrate = $input->bit_rate;
  21.                 $out->video_codec = $input->codec_name;
  22.             }
  23.         }
  24.  
  25.         $out->filesize = $data->format->size/1024; // filesize in kilobytes
  26.         $out->duration = intval($data->format->duration); // Video duration in seconds
  27.         $out->combined_bitrate = $data->format->bit_rate;
  28.  
  29.         return $out;
  30.     }
  31.  
  32.     function convertVideo($source_path,$hash){
  33.         $video_info = parseVideo($source_path);
  34.  
  35.         $vcodec;
  36.         $acodec;
  37.         $x264opts = "";
  38.         $audio_opts = "";
  39.  
  40.         if($video_info->video_codec == "h264" && $video_info->video_bitrate < 1048576){
  41.             $vcodec = "copy";
  42.         } else {
  43.             $vcodec = "x264";
  44.             $x264opts = "-x264opts bitrate=756:vbv-maxrate=756:vbv-bufsize=378";
  45.         }
  46.  
  47.         if($video_info->audio_codec == "aac" && $video_info->audio_bitrate < 1024*164){
  48.             $acodec = "copy";
  49.         } else {
  50.             $acodec = "libfdk_aac";
  51.             $audio_opts = "-ab 128k";
  52.         }
  53.  
  54.         $command = "/usr/bin/ffmpeg -i $source_path $x264opts -vcodec $vcodec -acodec $acodec $audio_opts -movflags +faststart -preset faster /var/www/media/video/{$hash}.mp4 -y >/var/www/ffmpeg_logs/{$hash}.log 2>&1 &";
  55.        
  56.         if(!file_exists("/var/www/media/video/$hash.mp4")){
  57.             $ffmpeg_log = "/var/www/ffmpeg_logs/$hash.log";
  58.             if(file_exists($ffmpeg_log))
  59.                 @unlink($ffmpeg_log);
  60.            
  61.             shell_exec($command);
  62.         }
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement