FFMpeg压缩处理视频
<?php
$xd_file = '原视频地址路径和名';
$file = dirname(dirname(dirname(__FILE__))) . '/' . $xd_file;
$file_data = pathinfo($file);
// PHP获取视频的所有信息
$info = shell_exec("ffprobe -v quiet -print_format json -show_format -show_streams " . $file);
//$info = shell_exec("ffmpeg -i " . $file . " 2>&1");echo'<pre>';var_dump($info);die;
//# 获取视频时长、开始时间、码率:
//if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
// $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: (.*?), (.*?\(.*?\)), (.*?)[, ]/", $info, $match)) {
// $data['vcodec'] = $match[1]; // 视频编码格式
$data['vcodec'] = substr($match[1], 0, strrpos($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: (.*?), (\d*) Hz/", $info, $match)) {
// $data['acodec'] = $match[1]; // 音频编码
$data['acodec'] = substr($match[1], 0, strrpos($match[1], " (")); // 音频编码
// $data['asamplerate'] = $match[2]; // 音频采样频率
//}
//# 实际播放时间:
//if (isset($data['seconds']) && isset($data['start'])) {
// $data['play_time'] = $data['seconds'] + $data['start']; // 实际播放时间
//}
//$data['size'] = filesize($file); // 文件大小
//echo '<pre>';
//var_dump($data);
//die;
$video_data = json_decode($info, true);
$data['vcodec'] = $video_data['streams'][0]['codec_name']; // 视频编码格式
$data['width'] = $video_data['streams'][0]['width']; // 视频宽度
$data['height'] = $video_data['streams'][0]['height']; // 视频高度
$data['bit_rate'] = $video_data['streams'][0]['bit_rate']; // 1400000
$data['r_frame_rate'] = $video_data['streams'][0]['r_frame_rate']; // 30
$data['acodec'] = $video_data['streams'][1]['codec_name']; // 音频编码格式
$data['streams_v_tags'] = $video_data['streams'][0]['tags'];
$data['streams_a_tags'] = $video_data['streams'][1]['tags'];
$data['format_tags'] = $video_data['format']['tags'];
$r_frame_rate = 0;
if (strpos($data['r_frame_rate'], '/') !== false) {
$r_frame_rate = explode("/", $data['r_frame_rate'])[0] / explode("/", $data['r_frame_rate'])[1];
} else {
$r_frame_rate = $data['r_frame_rate'];
}
if ($data['bit_rate'] > 1400000) {
$new_file_name = '新视频地址路径和名';
// 有视频旋转属性 对调分辨率
if (isset($data['streams_v_tags']['totate']) && !empty($data['streams_v_tags']['totate'])) {
$aaa = $data['width'];
$data['width'] = $data['height'];
$data['height'] = $aaa;
}
// 设置视频元数据
$metadata = '';
foreach ($data['format_tags'] as $k1 => $v1) {
if ($k1 != 'major_brand' && $k1 != 'minor_version' && $k1 != 'compatible_brands') {
$metadata .= " -metadata " . $k1 . "=''";
}
}
$v_metadata = '';
foreach ($data['streams_v_tags'] as $k2 => $v2) {
if ($k2 != 'handler_name') {
$v_metadata .= " -metadata:s:v:0 " . $k2 . "=''";
}
}
$a_metadata = '';
foreach ($data['streams_a_tags'] as $k3 => $v3) {
if ($k3 != 'handler_name') {
$a_metadata .= " -metadata:s:a:0 " . $k3 . "=''";
}
}
$exec = "ffmpeg -i " . $file;
if (!empty($metadata)) {
$exec .= $metadata;
}
if (!empty($v_metadata)) {
$exec .= $v_metadata;
}
if (!empty($a_metadata)) {
$exec .= $a_metadata;
}
// 帧率 > 30 改为30 帧率
if ($r_frame_rate > 30) {
$exec .= " -r 30";
}
// 宽 > 高 横屏
if ($data['width'] > $data['height']) {
if ($data['height'] > 720) {
$exec .= " -s 1280x720 -b:v 1300k ";
} else {
$exec .= " -b:v 1300k ";
}
} else { // 高 > 宽 竖屏
if ($data['width'] > 720) {
$exec .= " -s 720x1280 -b:v 1300k ";
} else {
$exec .= " -b:v 1300k ";
}
}
// 音频编码是 aac
if ($data['acodec'] == 'aac') {
$exec .= "-strict -2 ";
}
// 视频编码不是 h264
if ($data['vcodec'] != 'h264') {
$exec .= "-vcodec h264 ";
}
// 音频编码是 aac
if ($data['acodec'] == 'aac') {
$exec .= "-strict -2 ";
}
$exec .= $new_file_name;
shell_exec($exec);
// 删除原文件
if (unlink(dirname(dirname(dirname(__FILE__))) . '/' . $xd_file)) {
// 重命名新文件名为原文件名
rename($new_file_name, $file_data['dirname'] . '/' . $file_data['filename'] . '.' . $file_data['extension']);
}
} else {
return $xd_file;
}
Linux 宝塔面板安装FFMpeg和编码库
yum install https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
yum install http://rpmfind.net/linux/epel/7/x86_64/Packages/s/SDL2-2.0.14-2.el7.x86_64.rpm
yum install ffmpeg
yum -y install ffmpeg-devel
|