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 = file_get_contents($file);
$compress_path = PUBLIC_PATH;
$compress_file = $compress_path . $file_name . '.mp4';
$compress_after_file = $compress_path . $file_name . '_compress.mp4';
try{
file_put_contents($compress_file, $file_content);
$video_info;
exec(FFMPEG_PATH . "ffmpeg -i {$compress_file} 2>&1", $video_info);
$video_info = implode(' ', $video_info);
$bitrate = ''; // 比特率
$resolution = ''; // 分辨率
if(preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $video_info, $match)) {
$bitrate = $match[3];
}
if(preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $video_info, $match)) {
$resolution = $match[3];
}
$file_size = filesize($compress_file);
$file_size = intval($file_size / 1048576);
if(empty($bitrate)) throwErr('找不到比特率信息');
if(empty($resolution)) throwErr('找不到分辨率信息');
if($file_size < 10) throwErr('视频大小不足10M,不需要压缩', null, 1100);
$resolution = explode('x', $resolution);
$bitrate_update = '';
$resolution_width_update = '';
$resolution_height_update = '';
$bitrate_update = $this->getVideoCompressBitrate($bitrate);
$resolution_percent = 0;
if($resolution[0] > $resolution[1]) {
if($resolution[1] > 320) {
$resolution_percent = $resolution[1] <= 520 ? 0.8 : 0.5;
}
}else {
if($resolution[0] > 320) {
$resolution_percent = $resolution[0] <= 520 ? 0.8 : 0.5;
}
}
if($resolution_percent > 0) {
$resolution_width_update = intval($resolution[0] * $resolution_percent);
$resolution_height_update = intval($resolution[1] * $resolution_percent);
}
if(empty($bitrate_update) && empty($resolution_width_update)) throwErr('比特率和分辨率同时不满足压缩条件', null, 1100);
$compress_bitrate = '';
$compress_resolution = '';
if(!empty($bitrate_update)) {
$compress_bitrate = "-b:v {$bitrate_update}k";
}
if(!empty($resolution_width_update)) {
$compress_resolution = "-s {$resolution_width_update}x{$resolution_height_update}";
}
$compress_exec = FFMPEG_PATH . "ffmpeg -i {$compress_file} %s% %v% {$compress_after_file}";
$compress_exec = str_replace(array('%s%', '%v%'), array($compress_resolution, $compress_bitrate), $compress_exec);
exec($compress_exec);
unlink($compress_file);
return array('compress_file' => $compress_after_file);
}catch(\Exception $e) {
unlink($compress_file);
return array();
}
}
/* 获取视频压缩比特率 */
public function getVideoCompressBitrate($bitrate, $query_count = 0) {
$bitrate_update = '';
if($bitrate >= 700) {
if($bitrate <= 1000) {
$bitrate_update = intval($bitrate * 0.8);
}else {
$bitrate_update = intval($bitrate * 0.5);
}
}
if(empty($bitrate_update)) {
return $query_count == 0 ? $bitrate_update : $bitrate;
}else {
return $this->getVideoCompressBitrate($bitrate_update, ++$query_count);
}
}
这里提供PHP获取视频的所有信息代码:
$info = '';
exec(FFMPEG_PATH . "ffmpeg -i {$compress_file} 2>&1", $info);
$data = array();
if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
$data['duration'] = $match[1]; //播放时间
$arr_duration = explode(':', $match[1]);
$data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数
$data['start'] = $match[2]; //开始时间
$data['bitrate'] = $match[3]; //码率(kb)
}
if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {
$data['vcodec'] = $match[1]; //视频编码格式
$data['vformat'] = $match[2]; //视频格式
$data['resolution'] = $match[3]; //视频分辨率
$arr_resolution = explode('x', $match[3]);
$data['width'] = $arr_resolution[0];
$data['height'] = $arr_resolution[1];
}
if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {
$data['acodec'] = $match[1]; //音频编码
$data['asamplerate'] = $match[2]; //音频采样频率
}
if (isset($data['seconds']) && isset($data['start'])) {
$data['play_time'] = $data['seconds'] + $data['start']; //实际播放时间
}
return $data;