作者:风信子的春天R | 来源:互联网 | 2023-07-22 12:07
PHP使用ffmpeg压缩视频
视频压缩可以有一下方式:
1.压缩分辨率
例如视频的分辨率是720x1280,可以压缩分辨率为360x640
2.压缩帧数
通过压缩视频的帧数来压缩,可以压缩视频的帧数到1秒10帧甚至1秒5帧
3.压缩比特率
即压缩视频的码率,要求不高的话可以压缩到700kb/s左右
4.压缩音频码率
压缩视频的音频码率,一般可以压缩到128kb/s或者64kb/s
5.修改视频编码方式
现在普遍是H.264编码,已经是最优编码
ffmpeg命令
1.查看视频信息
ffmpeg -i video.mp4
bitrate:1724kb/s就是比特率,128kb/s就是音频的码率,720x1280就是分辨率
2.压缩
我这边压缩视频的做法是只压缩比特率和分辨率,这样对视频的影响最小
ffmpeg -i video.mp4 -s 360x640 -b:v 862k new.mp4
其中-s 360x640表示修改分辨率为360x640
其中-b:v 862k表示修改比特率为862kb/s
其中video.mp4为原视频
其中new.mp4为压缩后的视频地址
其他压缩命令:
1.压缩帧数
ffmpeg -i video.mp4 -r 5 new.mp4
其中-r 5表示1秒5帧
2.压缩音频码率
ffmpeg -i video.mp4 -b:a 64k new.mp4
其中-b:a 64k表示音频码率为64k/s
3.修改编码方式
ffmpeg -i video.mp4 -vcodec libx264 new.mp4
其中-vcodec libx264表示H.264编码
上面的命令都是可以组合使用的,找到符合需求的命令组合压缩视频即可。
PHP代码实现获取视频信息以及压缩(压缩的组合命令是分辨率和比特率):
/* 视频压缩 */
public function compressVideo($file, $file_name) {$file_content &#61; file_get_contents($file);$compress_path &#61; PUBLIC_PATH;$compress_file &#61; $compress_path . $file_name . &#39;.mp4&#39;;$compress_after_file &#61; $compress_path . $file_name . &#39;_compress.mp4&#39;;try{file_put_contents($compress_file, $file_content);$video_info;exec(FFMPEG_PATH . "ffmpeg -i {$compress_file} 2>&1", $video_info);$video_info &#61; implode(&#39; &#39;, $video_info);$bitrate &#61; &#39;&#39;; // 比特率$resolution &#61; &#39;&#39;; // 分辨率if(preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $video_info, $match)) {$bitrate &#61; $match[3];}if(preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $video_info, $match)) {$resolution &#61; $match[3];}$file_size &#61; filesize($compress_file);$file_size &#61; intval($file_size / 1048576);if(empty($bitrate)) throwErr(&#39;找不到比特率信息&#39;);if(empty($resolution)) throwErr(&#39;找不到分辨率信息&#39;);if($file_size <10) throwErr(&#39;视频大小不足10M&#xff0c;不需要压缩&#39;, null, 1100);$resolution &#61; explode(&#39;x&#39;, $resolution);$bitrate_update &#61; &#39;&#39;;$resolution_width_update &#61; &#39;&#39;;$resolution_height_update &#61; &#39;&#39;;$bitrate_update &#61; $this->getVideoCompressBitrate($bitrate);$resolution_percent &#61; 0;if($resolution[0] > $resolution[1]) {if($resolution[1] > 320) {$resolution_percent &#61; $resolution[1] <&#61; 520 ? 0.8 : 0.5;}}else {if($resolution[0] > 320) {$resolution_percent &#61; $resolution[0] <&#61; 520 ? 0.8 : 0.5;}}if($resolution_percent > 0) {$resolution_width_update &#61; intval($resolution[0] * $resolution_percent);$resolution_height_update &#61; intval($resolution[1] * $resolution_percent);}if(empty($bitrate_update) && empty($resolution_width_update)) throwErr(&#39;比特率和分辨率同时不满足压缩条件&#39;, null, 1100);$compress_bitrate &#61; &#39;&#39;;$compress_resolution &#61; &#39;&#39;;if(!empty($bitrate_update)) {$compress_bitrate &#61; "-b:v {$bitrate_update}k";}if(!empty($resolution_width_update)) {$compress_resolution &#61; "-s {$resolution_width_update}x{$resolution_height_update}";}$compress_exec &#61; FFMPEG_PATH . "ffmpeg -i {$compress_file} %s% %v% {$compress_after_file}";$compress_exec &#61; str_replace(array(&#39;%s%&#39;, &#39;%v%&#39;), array($compress_resolution, $compress_bitrate), $compress_exec);exec($compress_exec);unlink($compress_file);return array(&#39;compress_file&#39; &#61;> $compress_after_file);}catch(\Exception $e) {unlink($compress_file);return array();}
}/* 获取视频压缩比特率 */
public function getVideoCompressBitrate($bitrate, $query_count &#61; 0) {$bitrate_update &#61; &#39;&#39;;if($bitrate >&#61; 700) {if($bitrate <&#61; 1000) {$bitrate_update &#61; intval($bitrate * 0.8);}else {$bitrate_update &#61; intval($bitrate * 0.5);}}if(empty($bitrate_update)) {return $query_count &#61;&#61; 0 ? $bitrate_update : $bitrate;}else {return $this->getVideoCompressBitrate($bitrate_update, &#43;&#43;$query_count);}
}
这里提供PHP获取视频的所有信息代码&#xff1a;
$info &#61; &#39;&#39;;
exec(FFMPEG_PATH . "ffmpeg -i {$compress_file} 2>&1", $info);$data &#61; array();
if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {$data[&#39;duration&#39;] &#61; $match[1]; //播放时间$arr_duration &#61; explode(&#39;:&#39;, $match[1]);$data[&#39;seconds&#39;] &#61; $arr_duration[0] * 3600 &#43; $arr_duration[1] * 60 &#43; $arr_duration[2]; //转换播放时间为秒数$data[&#39;start&#39;] &#61; $match[2]; //开始时间$data[&#39;bitrate&#39;] &#61; $match[3]; //码率(kb)
}
if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {$data[&#39;vcodec&#39;] &#61; $match[1]; //视频编码格式$data[&#39;vformat&#39;] &#61; $match[2]; //视频格式$data[&#39;resolution&#39;] &#61; $match[3]; //视频分辨率$arr_resolution &#61; explode(&#39;x&#39;, $match[3]);$data[&#39;width&#39;] &#61; $arr_resolution[0];$data[&#39;height&#39;] &#61; $arr_resolution[1];
}
if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {$data[&#39;acodec&#39;] &#61; $match[1]; //音频编码$data[&#39;asamplerate&#39;] &#61; $match[2]; //音频采样频率
}
if (isset($data[&#39;seconds&#39;]) && isset($data[&#39;start&#39;])) {$data[&#39;play_time&#39;] &#61; $data[&#39;seconds&#39;] &#43; $data[&#39;start&#39;]; //实际播放时间
}
return $data;