一、安装 Ffmpeg 工作
1.1 安装依赖
yum -y install gcc
# 安装 yasm
# 第一种方法: yum 安装
yum -y install yasm
# 第二种: 编译安装 (时间应该比较长)
## 下载包
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
## 解压编译安装 yasm
tar zxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure
make && make install
1.2 编译安装 Ffmpeg
# 下载包
wget https://johnvansickle.com/ffmpeg/release-source/ffmpeg-4.1.tar.xz
# 解压编译安装 Ffmpeg (过程太长, 做好30分钟准备, 先跳到底下看会儿 PHP 代码)
tar xvJf ffmpeg-4.1.tar.xz
cd ffmpeg-4.1
./configure --enable-shared --prefix=/usr/local/ffmpeg
make && make install
1.3 配置动态链接库路径
vim /etc/ld.so.conf
# 在末行增加以下内容
/usr/local/ffmpeg/lib/
# 配置生效
ldconfig
1.4 配置环境变量
vim /etc/profile
# 在末行增加以下内容
export PATH=$PATH:/usr/local/ffmpeg/bin
# 保存退出
:wq
# 配置生效
source /etc/profile
1.5 查看 Ffmpeg 版本
# 输出版本就没问题, 注意是一个 -
ffmpeg -version
1.6 简单测试
# 从 1.mp4 视频中 截取第 34 帧 生成 1.jpg 图片
ffmpeg -i 1.mp4 -ss 34 1.jpg
二、PHP 使用
2.1 php.ini 打开放开 shell_exec 与 exec
# 查看 php.ini 位置
php --ini
# 根据自已情况找到 php.ini , 这里举例 php.ini 在 /etc 下
vim /etc/php.ini
# 查找 disable_functions 行, (vim 普通模式按 : 进入 vim 命令行模式,不知道 vim 模式的快去学哦)
/disable_functions
# 在 disable_functions 配置项中去掉 exec 与 shell_exec
# 保存退出
# 重启服务使配置生效
PHP 案例
注意 ⚠️⚠️
$cmd 变量中是 FFMpeg 执行脚本的绝对路径
以下代码估计也满足不了你的需求,根据自身情况调整啦,你可以的👍 。还有,说好的视频剪辑呢?😂
<?php
/**
* 视频时长转秒
* @param $time 00:00:00
* @return int
*/
function timeToSec($time)
{
$p = explode(':',$time);
$c = count($p);
$hour = $minute = $sec = 0;
if ($c == 3) {
$hour = intval($p[0]);
$minute = intval($p[1]);
$sec = intval($p[2]);
} else if($c == 2){
$minute = intval($p[0]);
$sec = intval($p[1]);
} else if ($c == 1){
$sec = intval($p[0]);
}
$secs = $hour * 3600 + $minute * 60 + $sec;
return $secs;
}
/**
* 视频截图
* @param string $video_url 视频路径
* @param integer $time 截图秒数点
* @param string $img_url 生成的图片路径与名称
*/
function makeImage($video_url='', $time='', $img_url=''){
if (empty($video_url) || empty($time) || empty($img_url)) return false;
$cmd = "/usr/local/ffmpeg/4.4_1/bin/ffmpeg -ss {$time} -i {$video_url} -y -f image2 -vframes 1 {$img_url}";
$cmd = iconv('UTF-8', 'GB2312', $cmd);
shell_exec($cmd);
}
/**
* 获得视频文件的总时长
* @param string $file 视频文件路径
* @return string|null
*/
function getTime($file){
$re = array();
$cmd = "/usr/local/ffmpeg/4.4_1/bin/ffmpeg -i {$file} 2>&1";
$cmd = iconv('UTF-8', 'GB2312', $cmd);
exec($cmd, $re);
$info = implode("\n", $re);
$match = array();
preg_match("/Duration:(.*?),/", $info, $match);
if($match)
{
$duration = date("H:i:s", strtotime($match[1]));
}else
{
$duration = NULL;
}
return timeToSec($duration);
}
/**
* 获得视频信息
* @param string $file 视频文件路径
* @return array
*/
private function getVideoInfo($file){
$re = array();
$cmd = "/usr/local/ffmpeg/4.4_1/bin/ffmpeg -i {$file} 2>&1";
$cmd = iconv('UTF-8', 'GB2312', $cmd);
exec($cmd, $re);
$info = implode("\n", $re);
$match = array();
preg_match("/\d{2,}x\d+/", $info, $match);
list($width, $height) = explode("x", $match[0]);
$match = array();
preg_match("/Duration:(.*?),/", $info, $match);
if($match)
{
$duration = date("H:i:s", strtotime($match[1]));
}else
{
$duration = NULL;
}
$match = array();
preg_match("/bitrate:(.*kb\/s)/", $info, $match);
$bitrate = $match[1];
if(!$width && !$height && !$duration && !$bitrate){
return false;
}else{
return array(
"file" => $file,
"width" => $width,
"height" => $height,
"duration" => $duration,
"bitrate" => $bitrate,
"secends" => $this->timeToSec($duration)
);
}
}