第一步/首先安装必要工具
1 2 3 4 5
| gcc make yasm libsdl1.2-dev libstl2-dev
|
第三步/编译安装FFmpeg
1 2 3 4 5
| tar -xvf ffmpeg_4.4.2.orig.tar.xz cd ffmpeg-4.4.2 ./configure make make install
|
第四步/安装完成后进行测试
1 2 3 4 5 6 7 8 9 10 11 12
| ffmpeg -version 返回结果 ffmpeg version 4.4.2 Copyright (c) 2000-2021 the FFmpeg developers built with gcc 9 (Ubuntu 9.3.0-17ubuntu1~20.04) configuration: libavutil 56. 70.100 / 56. 70.100 libavcodec 58.134.100 / 58.134.100 libavformat 58. 76.100 / 58. 76.100 libavdevice 58. 13.100 / 58. 13.100 libavfilter 7.110.100 / 7.110.100 libswscale 5. 9.100 / 5. 9.100 libswresample 3. 9.100 / 3. 9.100
|
使用
命令行使用
1 2 3 4 5 6 7 8 9
| -c:指定编码器 -c copy:直接复制,不经过重新编码(这样比较快) -c:v:指定视频编码器 -c:a:指定音频编码器 -i:指定输入文件 -an:去除音频流 -vn: 去除视频流 -preset:指定输出的视频质量,会影响文件的生成速度,有以下几个可用的值 ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow。 -y:不经过确认,输出时直接覆盖同名文件。
|
1 2
| ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpeg -i input_file -acodec copy -vn output_file_audio //分离音频流
|
转换编码格式(transcoding)指的是, 将视频文件从一种编码转成另一种编码。比如转成 H.264 编码,一般使用编码器libx264,所以只需指定输出文件的视频编码器即可。
1
| ffmpeg -i [input.file] -c:v libx264 output.mp4
|
从1080p转到480p
1
| ffmpeg -i input.mp4 -vf scale=480:-1 output.mp4
|
添加音轨(muxing)指的是,将外部音频加入视频,比如添加背景音乐或旁白。
1
| ffmpeg -i input.aac -i input.mp4 output.mp4
|
-ss 01:23:45表示截取的时间戳, -vframes 1指定只截取一帧,-q:v 2表示输出的图片质量,一般是1到5之间(1 为质量最高)
1
| ffmpeg -ss 01:23:45 -i input -vframes 1 -q:v 2 output.jpg
|
1
| ffmpeg -loop 1 -i cover.jpg -i input.mp3 -c:v libx264 -c:a aac -b:a 192k -shortest output.mp4
|
在PHP中使用
1 2 3 4 5 6
| 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 5 6
| 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; }
|
1 2 3 4 5 6
| require 'vendor/autoload.php'; $ffmpeg = FFMpeg\FFMpeg::create(); $video = $ffmpeg->open('video.mpg'); $video->filters()->resize(new FFMpeg\Coordinate\Dimension(320, 240))->synchronize(); $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))->save('frame.jpg'); $video->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4')->save(new FFMpeg\Format\Video\WMV(), 'export-wmv.wmv')->save(new FFMpeg\Format\Video\WebM(), 'export-webm.webm');
|