一、编译
1.1 Reference
官网:https://ffmpeg.org/
Guide: https://trac.ffmpeg.org/wiki
1.2 FFmpeg Source Code Structure
• libavutil: 一些简化编程的函数的库,包括 随机数的生成、数据结构、数学公式,以及多媒体核心组件
• libavcodec: audio/video codecs 的编解码库
• libavformat: 各种多媒体容器格式的 demuxers & muxers 的库
• libavdevice: 媒体输入输出设备与各种常见媒体软件框架之间的交互的库
• libavfilter: 音频、视频、字幕的滤镜处理框架库
• libswscale: 执行高优化的图像缩放、色域或像素格式转换 等功能的库
• libswresample: 执行高优化的音频 重采样、重矩阵、和采样格式转换 等功能的库
• libavcodec
provides implementation of a wider range of codecs.
• libavformat
implements streaming protocols, container formats and basic I/O access.
• libavutil
includes hashers, decompressors and miscellaneous utility functions.
• libavfilter
provides a mean to alter decoded Audio and Video through chain of filters.
• libavdevice
provides an abstraction to access capture and playback devices.
• libswresample
implements audio mixing and resampling routines.
• libswscale
implements color conversion and scaling routines.
1.3 Pre-install FFmpeg
- 创建一个用于学习 FFmpeg 的目录, say $HOME/learning
cd $HOME/learning
# ffmpeg_sources 放置所有下载的源码
mkdir ffmpeg_sources
# ffmpeg_build 用于生成文件和libraries
mkdir ffmpeg_build
# bin 放置所有生成的二进制文件,包括 ffmpeg, ffprobe, x264, x265
mkdir bin
- 安装基础依赖
sudo yum -y install autoconf automake bzip2 bzip2-devel cmake freetype-devel gcc gcc-c++ git libtool make mercurial pkgconfig zlib-devel
- NASM
组装器,加速构建
cd ffmpeg_sources
curl -O -L https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.bz2
tar xjvf nasm-2.15.05.tar.bz2
cd nasm-2.15.05
sudo sh autogen.sh
./configure --prefix="$HOME/learning/ffmpeg_build" --bindir="$HOME/learning/bin"
make -j4
sudo make install
- YASM
用于支持 FFmpeg 中的汇编优化
cd ffmpeg_sources
curl -O -L https://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="$HOME/learning/ffmpeg_build" --bindir="$HOME/learning/bin"
make -j4
sudo make install
- glib(坑多,建议跳过直接安装 pkgconfig)
用于编译 pkgconfig,安装过程比较曲折,建议编译 pkgconfig 可以使用内置 glib ,就可以跳过这一步
cd ffmpeg_sources
curl -O -L http://ftp.gnome.org/pub/GNOME/sources/glib/2.54/glib-2.54.3.tar.xz
xz -d glib-2.54.3.tar.xz
tar -xvf glib-2.54.3.tar
cd glib-2.54.3
./configure --enable-libmount=no
sudo make -j4
sudo make install
注:./configure 过程中遇到的问题,参考 https://blog.csdn.net/windeal3203/article/details/76608248
若遇到 “No package 'libffi' found”
sudo yum -y install libffi-devel
若遇到 “No package 'libpcre' found”
curl -O -L https://sourceforge.net/projects/pcre/files/pcre/8.41/pcre-8.41.zip
unzip pcre-8.41.zip
cd pcre-8.41
./configure --enable-utf8 --enable-unicode-properties
sudo make -j4
sudo make install
若遇到“error: *** Could not find libmount”
sudo yum -y install libmount-devel.x86_64
# 如果安装成功了还是提示无法找到 libmount,configure glib 的时候增加 --enable-libmount=no 选项,影响未知
- pkgconfig
用于检测可以编进 FFmpeg 的 libraries
cd ffmpeg_sources
curl -O -L https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
tar xzvf pkg-config-0.29.2.tar.gz
cd pkg-config-0.29.2
./configure --with-internal-glib
sudo make -j4
sudo make install
若遇到“error: Either a previously installed pkg-config or "glib-2.0 >= 2.16" could not be found”,./configure 增加 --with-internal-glib 选项,使用用内置 glib
- libx264
视频 H264 编码器,要求 ffmpeg 编译的 configure 选项有 --enable-gpl --enable-libx264
cd ffmpeg_sources
git clone --depth 1 https://code.videolan.org/videolan/x264.git
cd x264
PKG_CONFIG_PATH="$HOME/learning/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/learning/ffmpeg_build" --bindir="$HOME/learning/bin" --enable-static
make -j4
sudo make install
- libx265
视频 H265 编码器,要求 ffmpeg 编译的 configure 选项有 --enable-gpl --enable-libx265
cd ffmpeg_sources
git clone https://bitbucket.org/multicoreware/x265_git --depth 1
cd ./x265_git/build/linux
sudo cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/learning/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make -j4
sudo make install
- libfdk_aac
AAC 音频编码器,要求 ffmpeg 编译的 configure 选项有 --enable-libfdk_aac 和 --enable-nonfree
cd ffmpeg_sources
git clone --depth 1 https://github.com/mstorsjo/fdk-aac
cd fdk-aac
# git 拉代码特别慢的情况下,可以直接下载 sourceforge 的打包版本
# wget https://downloads.sourceforge.net/opencore-amr/fdk-aac-2.0.1.tar.gz
# tar xzvf fdk-aac-2.0.1.tar.gz
# cd fdk-aac-2.0.1
autoreconf -fiv
./configure --prefix="$HOME/learning/ffmpeg_build" --disable-shared
make -j4
sudo make install
- libopus
OPUS 音频编码器,要求 ffmpeg 编译的 configure 选项有 --enable-libopus
cd ffmpeg_sources
curl -O -L https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz
tar xzvf opus-1.3.1.tar.gz
cd opus-1.3.1
./configure --prefix="$HOME/learning/ffmpeg_build" --disable-shared
make -j4
sudo make install
1.4 Compile FFmpeg with x264, x265, aac and opus
cd ffmpeg_sources
curl -O -L https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
tar xjvf ffmpeg-snapshot.tar.bz2
cd ffmpeg
PATH="$HOME/learning/bin:$PATH" PKG_CONFIG_PATH="$HOME/learning/ffmpeg_build/lib/pkgconfig" ./configure \
--prefix="$HOME/learning/ffmpeg_build" \
--pkg-config-flags="--static" \
--extra-cflags="-I$HOME/learning/ffmpeg_build/include" \
--extra-ldflags="-L$HOME/learning/ffmpeg_build/lib" \
--extra-libs=-lpthread \
--extra-libs=-lm \
--bindir="$HOME/learning/bin" \
--enable-gpl \
--enable-libfdk_aac \
--enable-libopus \
--enable-libx264 \
--enable-libx265 \
--enable-nonfree
make -j4
sudo make install
hash -d ffmpeg
若 ./configure 遇到:ERROR: x265 not found using pkg-config,查看 ffbuild/config.log 发现是"Perhaps you should add the directory containing `x265.pc'"问题时,在 $HOME/learning/bin/ffmpeg_build/lib/pkgconfig 路径下自己写一个 x265.pc,内容如下:
prefix=$HOME/learning/ffmpeg_build
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: x265
Description: H.265/HEVC encoder library
Version: 3.5
Libs: -L${libdir} -lx265
Libs.private: -lstdc++ -lm -ldl -lpthread
Cflags: -I${includedir}
再执行 ./configure 即可。
至此,支持视频 264&265 音频 opus&aac 编解码的 ffmpeg & ffprobe 就编译完成了,可以在 $HOME/learning/bin 里找到。