1 首选需要安装ffmpeg
安装ffmpeg
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
检查是否安装成功
ffmpeg -version
1.查看视频信息
ffmpeg -i video.mp4
我这边压缩视频的做法是只压缩比特率和分辨率,这样对视频的影响最小
ffmpeg -i 12.mp4 -s 1280x720 -b:v 1M new5.mp4
比特率 -b:v 值越大清晰度越高
12.mp4 原视频
new5.mp4 压缩过的视频名称
下面就php 为例
$shell 代码
ffmpeg -i /www/wwwroot/www.jsbaoshui.com/storage/app/public/data_dir/uploads/vidio/0/20230222142502437.mp4 -s 1280x720 -b:v 2M -fs 10M /www/wwwroot/www.jsbaoshui.com/storage/app/public/data_dir/uploads/vidio_ya/0/20230222142502437.mp4 2>&1
$goods_video 是上传到服务器上面的原视频 下面是处理压缩过程中的代码
$out_dir = 'data_dir' . "/uploads/vidio_ya/" . $ru_id . "/" . $upload->getNewFileName();
$out_d = storage_public('data_dir' . '/uploads/vidio_ya/' . $ru_id . "/");
if (!file_exists($out_d)) {
make_dir($out_d);
}
$toSize = '10M';
$goods_video_ne = storage_public($goods_video);
$out_dir_ne = storage_public($out_dir);
// -y 自动确认覆盖 -s 压缩后分辨率 -b:v 视频码率 -fs $toSize 最大视频大小,超过后裁剪
$shell = "ffmpeg -i " . $goods_video_ne . " -s 1280x720 -b:v 2M -fs " . $toSize . " ". $out_dir_ne . " 2>&1";
$ret = shell_exec($shell);
-fs $toSize