Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function parseVideo($path){
- $out = new stdClass();
- $path = escapeshellarg($path);
- $data = json_decode(shell_exec("ffprobe -v quiet -print_format json -show_format -show_streams $path"));
- $has_video=0;
- $has_audio=0;
- $out->audio_bitrate = 0;
- $out->video_bitrate = 0;
- foreach($data->streams as $input){
- if($input->codec_type == "audio" && $has_audio == 0){
- $has_audio = 1;
- $out->audio_bitrate = $input->bit_rate;
- $out->audio_codec = $input->codec_name;
- } else if($input->codec_type == "video" && $has_video == 0){
- $has_video = 1;
- $out->video_bitrate = $input->bit_rate;
- $out->video_codec = $input->codec_name;
- }
- }
- $out->filesize = $data->format->size/1024; // filesize in kilobytes
- $out->duration = intval($data->format->duration); // Video duration in seconds
- $out->combined_bitrate = $data->format->bit_rate;
- return $out;
- }
- function convertVideo($source_path,$hash){
- $video_info = parseVideo($source_path);
- $vcodec;
- $acodec;
- $x264opts = "";
- $audio_opts = "";
- if($video_info->video_codec == "h264" && $video_info->video_bitrate < 1048576){
- $vcodec = "copy";
- } else {
- $vcodec = "x264";
- $x264opts = "-x264opts bitrate=756:vbv-maxrate=756:vbv-bufsize=378";
- }
- if($video_info->audio_codec == "aac" && $video_info->audio_bitrate < 1024*164){
- $acodec = "copy";
- } else {
- $acodec = "libfdk_aac";
- $audio_opts = "-ab 128k";
- }
- $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 &";
- if(!file_exists("/var/www/media/video/$hash.mp4")){
- $ffmpeg_log = "/var/www/ffmpeg_logs/$hash.log";
- if(file_exists($ffmpeg_log))
- @unlink($ffmpeg_log);
- shell_exec($command);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement