利用FFmpeg 获取视频第一贞作为封面图

时间:2021-11-08 11:03:57

FFmpeg是视频处理最常用的开源软件。
它功能强大,用途广泛,大量用于视频网站和商业软件(比如 Youtube 和 iTunes),也是许多音频和视频格式的标准编码/解码实现。

FFmpeg安装

下载fmmpeg代码

1

git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg

安装依赖库文件

主要安装三个:yasm ,sdl1.2 和 sdl2.0

安装 yasm

1

sudo apt-get install yasm

安装sdl1.2

1

sudo apt-get install libsdl1.2-dev

安装 sdl2.0

1

sudo apt-get install libstl2-dev

编译安装FFmpeg

进入到解压之后的 ffmpeg文件夹,依次执行以下命令:

./configure
makesudo make install

测试安装成功与否

输入以下命令查看输出:

图片.png

linux下 FFmpeg 命令介绍

FFmpeg 常用的命令行参数如下。

1

2

3

4

5

6

7

8

9

10

-c:指定编码器

-c copy:直接复制,不经过重新编码(这样比较快)

-c:v:指定视频编码器

-c:a:指定音频编码器

-i:指定输入文件

-an:去除音频流

-vn: 去除视频流

-preset:指定输出的视频质量,会影响文件的生成速度,

有以下几个可用的值 ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow。

-y:不经过确认,输出时直接覆盖同名文件。

常见命令

查看文件信息

查看视频文件的元信息,比如编码格式和比特率,可以只使用-i参数。
$ ffmpeg -i input.mp4

分离视频音频流
ffmpeg -i input_file -vcodec copy -an output_file_video  //分离视频流ffmpeg -i input_file -acodec copy -vn output_file_audio  //分离音频流
视频转码

转换编码格式(transcoding)指的是, 将视频文件从一种编码转成另一种编码。比如转成 H.264 编码,一般使用编码器libx264,所以只需指定输出文件的视频编码器即可。
$ ffmpeg -i [input.file] -c:v libx264 output.mp4

改变分辨率

下面是改变视频分辨率(transsizing)的例子,从 1080p 转为 480p 。

$ ffmpeg \
-i input.mp4 \
-vf scale=480:-1 \
output.mp4
添加音轨

添加音轨(muxing)指的是,将外部音频加入视频,比如添加背景音乐或旁白。

$ ffmpeg \
-i input.aac -i input.mp4 \output.mp4
截图
$ ffmpeg \
-ss 01:23:45 \
-i input \
-vframes 1 -q:v 2 \
output.jpg

上面例子中,-ss 01:23:45表示截取的时间戳, -vframes 1指定只截取一帧,-q:v 2表示输出的图片质量,一般是1到5之间(1 为质量最高)。

为音频添加封面

下面命令可以将音频文件,转为带封面的视频文件

$ ffmpeg \
-loop 1 \
-i cover.jpg -i input.mp3 \
-c:v libx264 -c:a aac -b:a 192k -shortest \
output.mp4

转回主题PHP中FFmpeg使用

exec方法

视频压缩

1

2

3

4

public static function reSize($path$outPath$toSize){

    $shell "ffmpeg -i " $path " -strict -2  -y -fs " $toSize "  "$outPath " 2>&1";

    exec($shell$output$ret);    return $ret;

}

视频截图

1

2

3

4

public static function screenshot($path, $outPath){

    $shell = "ffmpeg -i " . $path . " -ss 1 -y -frames:v 1 -q:v 1 " . $outPath . " 2>&1";

    exec($shell, $output, $ret);    return $res;

}

开启这几个函数太危险了。

php-ffmpeg扩展

composer安装php-ffmpeg

composer require php-ffmpeg/php-ffmpeg

基于TP6做的测试

1

2

3

4

5

6

7

8

9

10

//创建一个命令行任务 

php think make:command test

#增加配置文件

return [

    // 指令定义

    'commands' => [

        'test' => 'app\command\Test'

    ],

];

代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

declare (strict_types = 1);

namespace app\command;

use FFMpeg\Coordinate\TimeCode;

use FFMpeg\FFMpeg;

use think\console\Command;

use think\console\Input;

use think\console\input\Argument;

use think\console\input\Option;

use think\console\Output;

class Test extends Command

{

    protected function configure()

    {

        // 指令配置

        $this->setName('app\command\test')

            ->setDescription('the app\command\test command');

    }

    protected function execute(Input $input, Output $output)

    {

        // 主要代码

        $tmp = FFMpeg::create();

        $tmp->open('xxx/v.f100800.mp4')->frame(TimeCode::fromSeconds(3))->save('frame.jpg');

        // 指令输出

        $output->writeln('app\command\test');

    }

}

已到看生成的图片了。就是执行有点慢,估计和视频大小有关系。百M以上视频处理没测试。

图片.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值