一、安装 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
vim /etc/php.ini
/disable_functions
PHP 案例
注意 ????
$cmd 变量中是 FFMpeg 执行脚本的绝对路径
以下代码估计也满足不了你的需求,根据自身情况调整啦,你可以的👍 。还有,说好的视频剪辑呢?😂
<?php
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;
}
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);
}
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);
}
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)
);
}
}
|