创建容器
docker run -it -v /mnt/f/ffmpeg:/mnt/f/ffmpeg --name ffmpeg 49a981f2b85f /bin/bash
在 Linux 上编译 FFmpeg:
安装依赖库:
bash
sudo apt-get update
sudo apt-get install build-essential yasm cmake libtool libc6 libc6-dev unzip wget
下载 FFmpeg 源代码:
bash
git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg
#查看远程分支
git branch -r
#切换分支
git checkout -b origin/release/5.1 origin/release/5.1
配置和编译:
bash
./configure --enable-shared
make -j20 # 使用多个核心进行编译,可以根据你的 CPU 核心数进行调整
sudo make install
上述命令假设你在 FFmpeg 源代码目录下执行。--enable-shared 表示编译生成共享库。
WARNING: pkg-config not found, library detection may fail.
bash
sudo apt-get update
sudo apt-get install pkg-config
No rule to make target 'libavcodec/x86/lpc.asm', needed by 'libavcodec/x86/lpc.o'. Stop
bash
sudo apt-get update
sudo apt-get install yasm
安装路径
bash
root@cec0f3db763d:~# whereis ffmpeg
ffmpeg: /usr/local/bin/ffmpeg
配置环境
sudo gedit /etc/ld.so.conf
加入一行
安装成功
bash
root@cec0f3db763d:~# ffmpeg
ffmpeg version n5.1.4-1-gae14d9c06b Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.2)
configuration: --enable-shared
libavutil 57. 28.100 / 57. 28.100
libavcodec 59. 37.100 / 59. 37.100
libavformat 59. 27.100 / 59. 27.100
libavdevice 59. 7.100 / 59. 7.100
libavfilter 8. 44.100 / 8. 44.100
libswscale 6. 7.100 / 6. 7.100
libswresample 4. 7.100 / 4. 13.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
切换到挂载目录
cd /mnt/f/ffmpeg/
查询视频信息
ffprobe IVR_20231214_172440.mp4
bash
root@cec0f3db763d:/mnt/f/ffmpeg# ffprobe IVR_20231214_170605.mp4
ffprobe version n5.1.4-1-gae14d9c06b Copyright (c) 2007-2023 the FFmpeg developers
built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.2)
configuration: --enable-shared
libavutil 57. 28.100 / 57. 28.100
libavcodec 59. 37.100 / 59. 37.100
libavformat 59. 27.100 / 59. 27.100
libavdevice 59. 7.100 / 59. 7.100
libavfilter 8. 44.100 / 8. 44.100
libswscale 6. 7.100 / 6. 7.100
libswresample 4. 7.100 / 4. 13.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'IVR_20231214_170605.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.45.100
Duration: 00:00:10.40, start: 0.000000, bitrate: 3459 kb/s
Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, bt470bg/bt470bg/bt709, progressive), 1024x768, 3455 kb/s, 42.22 fps, 50 tbr, 90k tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
提取所有帧
ffmpeg -i VID_20231214_091908.mp4 -vf "select=1" -vsync vfr frame/%04d.jpg
ffmpeg -i VID_20231214_091908.mp4 -vf "select=gt(scene\,0.1)" -vsync vfr frame/%04d.jpg
- 使用了 select 过滤器,该过滤器根据场景的变化选择帧。gt(scene,0.5) 表示选择那些场景变化比 0.5 大的帧。
捕捉到视频中场景变化较大的帧。 - -vsync vfr 选项指定使用可变帧率,因为我们选择了帧,而不是按照常规的固定帧率。
- -vsync 选项为 cfr(恒定帧率)
- frames/%04d.jpg 指定输出的帧图像保存在 frames 目录下,文件名为四位数字格式。
视频转换
ffmpeg -i IVR_20231222_144442.mp4 -c:v libx264 -c:a aac output_video.mp4
- -c:v libx264: 指定视频编码器为 libx264(H.264)。
- -preset medium: 使用中等质量的预设,你可以根据需要调整。
- -crf 23: 控制视频质量,数值越小,质量越高,18-28 之间是常用的范围。
- -c:a aac -b:a 192k: 指定音频编码器为 AAC,音频比特率为 192kbps。
sudo apt install libx264-dev
检查是否支持
ffmpeg -codecs | grep libx264
安装h264
sudo apt-get install libx264-dev
下载源码
git clone https://code.videolan.org/videolan/x264.git
cd x264
编译和安装 libx264
bash
./configure --enable-shared
make
sudo make install
asm错误
bash
root@cec0f3db763d:/x264# ./configure --enable-shared
Found no assembler
Minimum version is nasm-2.13
If you really want to compile without asm, configure with --disable-asm.
安装 NASM: 按照错误消息的建议,安装 nasm
bash
sudo apt-get install nasm
h264编译成功
bash
root@cec0f3db763d:/x264# ./configure --enable-shared
platform: X86_64
byte order: little-endian
system: LINUX
cli: yes
libx264: internal
shared: yes
static: no
bashcompletion: no
asm: yes
interlaced: yes
avs: yes
lavf: yes
ffms: no
mp4: no
gpl: yes
thread: posix
opencl: yes
filters: resize crop select_every
lto: no
debug: no
gprof: no
strip: no
PIC: yes
bit depth: all
chroma format: all
重新编译 ffmpeg
bash
#git clone https://github.com/FFmpeg/FFmpeg.git
#cd FFmpeg
./configure --enable-libx264
make
sudo make install
错误
这个错误表明在尝试编译 ffmpeg 时,缺少对 GPL 许可证的支持。libx264 是一个使用 GPL 许可证的视频编码库
bash
ibx264 is gpl and --enable-gpl is not specified.
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.libera.chat.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
解决
./configure --enable-gpl
./configure --enable-shared --enable-gpl --enable-libx264
sudo ldconfig