国内视频网站 都是伪高清
最近为客户定制转码系统 参考Youtube的码率
暂时未做SDR和HDR的判断 这个Youtube的码率不一样
但是我通过判断视频fps来改变码率 代码见下
逻辑是这样
然后通过获得源视频的尺寸和比特率
来计算该视频是不是可以被转码为更高清晰度和标准尺寸
默认转360p的单个版本
如果存在更高的版本 追加
进入转码队列
以NASA的1080P的太空测试视频做个测试
Media Descriptors Commands
Comand 1: /usr/local/bin/mediainfo –Inform=file://media/media_general.txt media/earth_night_rotate_1080.mov
Comand 2: /usr/local/bin/mediainfo –Inform=file://media/media_video.txt media/earth_night_rotate_1080.mov
Media Info
$vinfo[‘General_FileExtension’] = ‘mov’;
$vinfo[‘General_Duration’] = ‘30000’; #长度
$vinfo[‘General_Format’] = ‘MPEG-4’;
$vinfo[‘General_Format_Profile’] = ‘QuickTime’;
$vinfo[‘General_CodecID’] = ‘qt’;
$vinfo[‘Video_Format’] = ‘AVC’;
$vinfo[‘Video_Format/Info’] = ‘Advanced Video Codec’;
$vinfo[‘Video_CodecID’] = ‘avc1’;
$vinfo[‘Video_CodecID/Info’] = ‘Advanced Video Coding’;
$vinfo[‘Video_Width’] = ‘1920’;
$vinfo[‘Video_Height’] = ‘1080’; #尺寸
$vinfo[‘Video_PixelAspectRatio’] = ‘1.000’;
$vinfo[‘Video_DisplayAspectRatio’] = ‘1.778’;
$vinfo[‘Video_BitRate_Mode’] = ”;
$vinfo[‘Video_BitRate’] = ‘15372097’; #码率
$vinfo[‘Video_BitRate_Minimum’] = ”;
$vinfo[‘Video_BitRate_Maximum’] = ”;
$vinfo[‘Video_FrameRate_Mode’] = ‘CFR’;
$vinfo[‘Video_FrameRate’] = ‘30.000’; #帧
Encoder Details
$video_type = normal
$aspect = 16:9
$vinfo[‘Original_Width’] = 1920
$vinfo[‘Video_Width’] = 1920
源视频码率15372097
帧<30 fps
ffmpeg -y -i media/earth_night_rotate_1080.mov \
-s 1920×1080 -c:v libx264 -b:v 8000k -maxrate 2237k -bufsize 2237k -c:a libfdk_aac -b:a 128k -movflags faststart -f mp4 out/10_1080p.mp4 \
-s 1280×720 -c:v libx264 -b:v 5000k -maxrate 1535k -bufsize 1536k -c:a libfdk_aac -b:a 128k -movflags faststart -f mp4 out/10_720p.mp4 \
-s 854×480 -c:v libx264 -b:v 2500k -maxrate 960k -bufsize 960k -c:a libfdk_aac -b:a 64k -movflags faststart -f mp4 out/10_480p.mp4 \
-s 640×360 -c:v libx264 -b:v 1000k -maxrate 448k -bufsize 448k -c:a libfdk_aac -b:a 64k -movflags faststart -f mp4 out/10_360p.mp4
ffmpeg 命令是测试的 所以带宽需求设的很低
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
// 输出码率计算 $vinfo['p2160p'] = false; $vinfo['p1440p'] = false; $vinfo['p1080p'] = false; $vinfo['p720p'] = false; $vinfo['p480p'] = false; $vinfo['p360p'] = true; // 默认转码 360P 标清 $vinfo['p240p'] = false; $bitrate = $vinfo['Video_BitRate']; print_r($bitrate); if($vinfo['Video_FrameRate'] > 30) { echo '>30'; if ($vinfo['Video_Height'] >= '4320' && $bitrate > '53000000' ) { $vinfo['p2160p'] = true; } if ($vinfo['Video_Height'] >= '2160' && $bitrate > '24000000' ) { $vinfo['p1440p'] = true; } if ($vinfo['Video_Height'] >= '1080' && $bitrate > '12000000' ) { $vinfo['p1080p'] = true; } if ($vinfo['Video_Height'] >= '720' && $bitrate > '7500000' ) { $vinfo['p720p'] = true; } if ($vinfo['Video_Height'] >= '480' && $bitrate > '4000000' ) { $vinfo['p480p'] = true; } if ($vinfo['Video_Height'] >= '360' && $bitrate > '1500000' ) { $vinfo['p360p'] = true; } if ($vinfo['Video_Height'] >= '240' ) { //$vinfo['p240p'] = true; } } else { echo '<30'; if ($vinfo['Video_Height'] >= '4320' && $bitrate > '35000000' ) { $vinfo['p2160p'] = true; } if ($vinfo['Video_Height'] >= '2160' && $bitrate > '16000000' ) { $vinfo['p1440p'] = true; } if ($vinfo['Video_Height'] >= '1080' && $bitrate > '8000000' ) { $vinfo['p1080p'] = true; } if ($vinfo['Video_Height'] >= '720' && $bitrate > "5000000" ) { $vinfo['p720p'] = true; } if ($vinfo['Video_Height'] >= '480' && $bitrate > '2500000' ) { $vinfo['p480p'] = true; } if ($vinfo['Video_Height'] >= '360' && $bitrate > '1000000' ) { $vinfo['p360p'] = true; } if ($vinfo['Video_Height'] >= '240' && $bitrate > '500000' ) { //$vinfo['p240p'] = true; } } //print_r($vinfo); //print_R(get_duration($q['q_path'])); $cmd = 'ffmpeg -y -i '.$file. ' \\'; $outfile = $v_dir.'/'.$vid; if($vinfo['p2160p']) { $cmd .= "\n". '-s 7680×4320 -c:v libx264 -b:v 80089k -maxrate 80089k -bufsize 80089k -c:a libfdk_aac -b:a 192k -movflags faststart -f mp4 '.$outfile.'_4k.mp4 \\'; } if ($vinfo['p1440p']) { $cmd .= "\n".'-s 3840×2160 -c:v libx264 -b:v 35000k -maxrate 24448k -bufsize 24448k -c:a libfdk_aac -b:a 128k -movflags faststart -f mp4 '.$outfile.'_2k.mp4 \\'; } if($vinfo['p1080p']){ $cmd .="\n".'-s 1920x1080 -c:v libx264 -b:v 8000k -maxrate 2237k -bufsize 2237k -c:a libfdk_aac -b:a 128k -movflags faststart -f mp4 '.$outfile.'_1080p.mp4 \\'; } if($vinfo['p720p']){ $cmd .="\n". '-s 1280x720 -c:v libx264 -b:v 5000k -maxrate 1535k -bufsize 1536k -c:a libfdk_aac -b:a 128k -movflags faststart -f mp4 '. $outfile .'_720p.mp4 \\'; } if($vinfo['p480p']){ $cmd .= "\n".'-s 854x480 -c:v libx264 -b:v 2500k -maxrate 960k -bufsize 960k -c:a libfdk_aac -b:a 64k -movflags faststart -f mp4 '. $outfile .'_480p.mp4 \\'; } if($vinfo['p360p']){ $cmd .= "\n". '-s 640x360 -c:v libx264 -b:v 1000k -maxrate 448k -bufsize 448k -c:a libfdk_aac -b:a 64k -movflags faststart -f mp4 '. $outfile .'_360p.mp4 '; } if($vinfo['p240p']){ //$cmd .="\n". '-s 426x240 -c:v libx264 -b:v 1000k -maxrate 178k -bufsize 178k -c:a libfdk_aac -b:a 64k -movflags faststart -f mp4 '. $outfile .'_240p.mp4 \\'; } echo $cmd; //生成缩略图 //@gen_thumb($vid,$file); //发送给后台运行转码命令 返回任务PID $lg = 'temp/log/' .$vid. '.log2'; $pid = run_in_background($cmd.' > '.$lg); //后端每秒检查PID 是不是还在存在 如果不存在 就代表转码完成 运行检查文件存在且无错误, 生成缩略图,同步图片和视频至存储服务器 删除本地文件 销毁所有关联PID进程 |