下载ffmpeg
为了ffmpeg不被别人直接访问,不建议存放在public目录下。这里以存放data目录为例:
在extend目录下新建扩展类
为了好区分前后台的扩展和归类,这里新建一个admin目录,并在admin目录下再建一个video目录
代码↓
<?php
namespace admin\video;
class video{
private $rootPath; //根路径 绝对路径
private $ffmpeg; //ffmpeg文件路径
private $outFileDir; //封面存储路径 绝对路径
private $fileName; //文件名
function __construct(){
$this->rootPath = ROOT_PATH.'public/';
$this->ffmpeg = 'data/extend/ffmpeg/ffmpeg.exe'; //引入ffmpeg.exe文件
$this->fileName = uniqid('video_').'.jpg';
$this->outFileDir = 'upload/videocover/'.date("Ymd",time()).'/';
}
// 获取视频封面 尺寸跟随视频尺寸而定
function getVideoCover($file){
//待处理视频
$in_file = $this->rootPath.$file;
//缩略图保存路径
$out_file = $this->rootPath.$this->outFileDir.$this->fileName;
//shell脚本
$shell = "ffmpeg -i $in_file -y -f image2 -ss 1 -vframes 1 $out_file 2>&1";
//调用php的exec方法去执行脚本
exec($shell, $output, $return_val);
//获取输出信息
// dump($output);
return $this->outFileDir.$this->fileName;
}
}
注意:用到路径的地方都需要写绝对路径!
控制器
<?php
namespace app\admin\controller;
use admin\video\video; //引入刚才写的类库
class Test extends Base{
public function index(){
//视频地址
$file = 'shipin.mp4';
$video = new video;
$vimg = $video->getVideoCover($file);
dump($vimg);
}
}
大功完成√
ffmpeg补充
按帧数截取
1、从头截取 (前30帧)
ffmpeg -s 1920x1080 -i input.yuv -c:v rawvideo -filter:v select="gt(n\, -1)" -vframes 30 out30.yuv
ffmpeg -s 1920x1080 -i input.yuv -c:v rawvideo -filter:v select="between(n\, 0\, 29)" out30.yuv
ffmpeg -r 1 -ss 0 -i input.yuv -vcodec copy -vframes 30 output.yuv
2、中间截取 (30-100帧)
ffmpeg -s 1920x1080 -i input.yuv -c:v rawvideo -filter:v select="between(n\, 30\, 100)" out.yuv
3、按时间截取
ffmpeg -s 1920x1080 -i input.yuv -c:v rawvideo -filter:v select="between(t\, 10\, 20)" out.yuv
ffmpeg -s 1920x1080 -r 1 -ss 10 -t 10 -i input.yuv -c:v copy out.yuv