文章目录
- 一、下载 FFmpeg 源码
- 1、目录准备
- 2、克隆远程代码
- 3、查看源码目录
- 4、切换代码分支
- 二、FFmpeg 源码编译配置
- 1、编译配置命令 ./configure
- 2、编译配置解析
- 三、FFmpeg 源码编译
- 1、编译源码
- 2、安装编译后的 FFmpeg 程序
- 3、配置环境变量
- 4、验证 FFmpeg 版本号
- 四、编译结果查看
- 1、安装 tree 命令行工具
- 2、FFmpeg 编译输出内容 - 可执行程序 / 共享库 / 头文件
- 3、查看编译结果目录
在上一篇博客 【FFmpeg】在 Mac OS 中编译 FFmpeg 源码 ① ( homebrew 安装 | 通过 gitee 源安装 homebrew | 安装 FFmpeg 编译所需的软件包 ) 中 , 安装了 homebrew , 并使用 homebrew 安装了 编译 FFmpeg 源码需要安装的软件包 , 本篇博客开始下载 FFmpeg 源码并进行编译 ;
其它可参考的 FFmpeg 源码编译相关的博客 :
- 【Android FFMPEG 开发】音视频基础 和 FFMPEG 编译 ( 音视频基础 | MPEG-4 标准 | Android 开发环境 | FFMPEG 交叉编译 | 安卓项目导入配置 )
- 【Android FFMPEG 开发】FFMPEG 交叉编译配置 ( 下载 | 配置脚本 | 输出路径 | 函数库配置 | 程序配置 | 组件配置 | 编码解码配置 | 交叉编译配置 | 最终脚本 )
编译 FFmpeg 命令总结 :
- 下载 FFmpeg 代码 : 将代码下载到执行该命令的目录位置下的 ffmpeg4.2 文件中 ;
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg4.2
- 切换分支 : 进入上述下载后的源码目录 , 切换 remotes/origin/release/4.2 分支 ;
cd ffmpeg4.2
git checkout remotes/origin/release/4.2
- 编译配置 : 执行 ffmpeg4.2 目录下的源码中的 ./configure 可执行程序 ,
./configure --prefix=/usr/local/ffmpeg4.2 --enable-shared --enable-gpl --enable-nonfree --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libopus --enable-libxvid --samples=fate-suite --enable-opencl --enable-videotoolbox --enable-ffplay --disable-optimizations --disable-stripping --enable-debug=3 --extra-cflags="-I/opt/homebrew/include" --extra-ldflags="-L/opt/homebrew/lib"
- 正式编译 : 下面的命令是开启 8 线程编译源码 , 根据自己的 CPU 核数设置 , 4 或者 8 ;
make -j8
- 安装库文件 : 将源码编译后生成的 可执行程序 和 库文件 安装到本地文件中 ;
sudo make install
- 配置环境变量 : 使用
vim ~/.bash_profile
命令配置export PATH="$PATH:/usr/local/ffmpeg4.2/bin"
环境变量 , 然后执行source ~/.bash_profile
命令 , 更新环境变量 ;
- 也可以使用
Command + Shift + .
快捷键 , 显示隐藏的文件 , 直接右键打开文本目录进行编辑 ;
一、下载 FFmpeg 源码
1、目录准备
创建一个 ffmpeg 目录 /Users/hsl/001_Project/005_compile/ffmpeg
, 在 命令行终端 进入该目录中 , 之后将源码下载到该目录中 ;
在该目录中 将会编译 各个版本的 ffmpeg 源码 , 下面的命令都是在该目录中执行的 ;
2、克隆远程代码
执行
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg4.2
命令 , 复制 https://git.ffmpeg.org/ffmpeg.git
地址的 Git 存储库 到 本地的 ffmpeg4.2 目录 ;
下载后的 FFmpeg 源码如下图所示 :
3、查看源码目录
执行
cd ffmpeg4.2
命令 , 进入到上述下载的源码目录中 , 在我的电脑上进入的目录是 /Users/hsl/001_Project/005_compile/ffmpeg/ffmpeg4.2 目录 , 之前的命令都是在 /Users/hsl/001_Project/005_compile/ffmpeg/ 目录中执行的 ;
4、切换代码分支
然后 , 切换代码分支 , 执行
git checkout remotes/origin/release/4.2
命令 , 完成分支切换操作 ;
该命令的作用是 切换到名为 release/4.2 的远程分支 , 该分支在 origin 远程仓库中 ;
二、FFmpeg 源码编译配置
1、编译配置命令 ./configure
设置编译配置 , 执行如下命令 , 即可完成 FFmpeg 源码编译配置 ;
./configure --prefix=/usr/local/ffmpeg4.2 --enable-shared --enable-gpl --enable-nonfree --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libopus --enable-libxvid --samples=fate-suite --enable-opencl --enable-videotoolbox --enable-ffplay --disable-optimizations --disable-stripping --enable-debug=3 --extra-cflags="-I/opt/homebrew/include" --extra-ldflags="-L/opt/homebrew/lib"
输出如下内容 , 说明 源码编译配置成功 ;
编译配置命令执行详情 , 代码量很多 , 非必要不要展开 ; 该配置命令输出了使用的配置 , 开启了哪些功能 , 哪些编解码器是可用的 , 支持哪些协议 , 支持哪些过滤器 ;
hsl@hanshuliangdeMacBook-Air ffmpeg4.2 % ./configure --prefix=/usr/local/ffmpeg4.2 --enable-shared --enable-gpl --enable-nonfree --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libopus --enable-libxvid --samples=fate-suite --enable-opencl --enable-videotoolbox --enable-ffplay --disable-optimizations --disable-stripping --enable-debug=3 --extra-cflags="-I/opt/homebrew/include" --extra-ldflags="-L/opt/homebrew/lib"
install prefix /usr/local/ffmpeg4.2
source path .
C compiler gcc
C library
ARCH aarch64 (generic)
big-endian no
runtime cpu detection yes
NEON enabled yes
VFP enabled yes
debug symbols yes
strip symbols no
optimize for size no
optimizations no
static yes
shared yes
postprocessing support yes
network support yes
threading support pthreads
safe bitstream reader yes
texi2html enabled yes
perl enabled yes
pod2man enabled yes
makeinfo enabled no
makeinfo supports HTML no
External libraries:
appkit libass libtheora libxcb lzma
avfoundation libfdk_aac libvorbis libxcb_shape sdl2
bzlib libfreetype libvpx libxcb_shm securetransport
coreimage libmp3lame libx264 libxcb_xfixes zlib
iconv libopus libx265 libxvid
External libraries providing hardware acceleration:
audiotoolbox opencl videotoolbox
Libraries:
avcodec avfilter avutil swresample
avdevice avformat postproc swscale
Programs:
ffmpeg ffplay ffprobe
License: nonfree and unredistributable
hsl@hanshuliangdeMacBook-Air ffmpeg4.2 %
2、编译配置解析
./configure
编译配置 参数解析 :
-
--prefix=/usr/local/ffmpeg4.2
配置 编译后的 库文件 和 可执行程序 , 安装到哪个目录中 ; --enable-shared
配置 允许动态库编译 , 在 FFmpeg 开发时 ,
- 优先选择动态库链接 ,
- 静态库链接很复杂 , 需要手动指定很多库文件 , 和 额外配置 ;
- 编译后的动态库 , 可以直接在 QT 或者 Visual Studio 中引用 , 进行音视频应用开发 ;
-
--enable-gpl
配置 启用 GPL ( GNU General Public License ) 许可证支持 , 允许 FFmpeg 使用 GPL 许可证的代码或者库进行编译和链接 ; -
--enable-nonfree
配置 启用非自由代码的支持 , 非自由代码 是 受到专利 版权 限制的代码 , 不能自由使用分发 , 启用该选项会使用这些代码进行编译和链接 ; -
--samples=fate-suite
配置 指定 编译过程中使用的样例集 , fate-suite 是 FFmpeg 源码中的测试套件 , 包含了音频和视频文件测试功能 , 可测试相关音视频编解码等功能是否正确 ; -
--enable-opencl
配置 启用 OpenCL 加速支持 , OpenCL 全称 Open Computing Language , 是一种开放标准 , 允许利用各种不同类型的计算设备 CPU / GPU / FPGA 进行并行计算 , 此处使用该技术进行 视频编解码 / 滤镜处理 运算 , 能显著提高视频处理效率 ; -
--enable-videotoolbox
配置 启用 VideoToolbox 加速支持 , 这是 macOS 和 iOS 系统提供的一个框架 , 用于硬件加速视频编解码和处理 , 该参数仅在 Mac 和 iOS 中生效 ; -
--disable-optimizations
配置 禁止了优化 , 这样 debug 源码比较方面 , 正式版本 打包时 , 使用优化后的编译版本 , 商业应用中需要设置一系列的优化参数 ; -
--disable-stripping
配置 禁止 在安装时对生成的可执行文件进行剥离 ; 编译时剥离操作会去除可执行文件中的调试符号和其他不必要的信息 , 可减小文件大小并提高执行速度 ; 禁止剥离操作会增加文件大小和降低执行速度 ; -
--enable-debug=3
配置 启用 最高级别的调试信息 ; -
--extra-cflags="-I/opt/homebrew/include"
配置 用于指定 依赖的第三方库的头文件目录 ; -
--extra-ldflags="-L/opt/homebrew/lib"
配置 用于指定依赖的第三方库的库文件目录 ;
./configure
--prefix=/usr/local/ffmpeg4.2 // 配置编译后的程序和库安装的目录
--enable-shared // 允许动态库编译
--enable-gpl // 启用 GPL 协议
--enable-nonfree // 启用非自由代码
--enable-libass // 启用 libass 字幕渲染
--enable-libfdk-aac // 启用 libfdk-aac AAC 编解码
--enable-libfreetype // 启用 libfreetype 字体渲染
--enable-libmp3lame // 启用 libmp3lame MP3 编解码
--enable-libtheora // 启用 libtheora Theora 编解码
--enable-libvorbis // 启用 libvorbis Vorbis 编解码
--enable-libvpx // 启用 libvpx VP8/VP9 编解码
--enable-libx264 // 启用 libx264 H.264 编解码
--enable-libx265 // 启用 libx265 H.265 编解码
--enable-libopus // 启用 libopus Opus 编解码
--enable-libxvid // 启用 libxvid Xvid 编解码
--samples=fate-suite // 设置样例为 fate-suite
--enable-opencl // 启用 OpenCL 加速
--enable-videotoolbox // 启用 VideoToolbox 加速支持
--enable-ffplay // 启用 ffplay 播放器
--disable-optimizations // 禁用 优化
--disable-stripping // 禁用 剥离
--enable-debug=3 // 启用最高级别的调试信息
--extra-cflags="-I/opt/homebrew/include" // 指定依赖的第三方库的头文件目录
--extra-ldflags="-L/opt/homebrew/lib" // 指定依赖的第三方库的库文件目录
三、FFmpeg 源码编译
1、编译源码
执行
make -j4
命令 , 开始进行编译 ; -j4
参数指的是开启 4 个线程进行编译 ;
输出如下内容 , 编译完成 ;
2、安装编译后的 FFmpeg 程序
执行
sudo make install
命令 , 安装 FFmpeg 到本地系统中 ;
3、配置环境变量
执行
vim ~/.bash_profile
命令 , 使用 vim 编辑器 编辑 ~/.bash_profile 环境变量文件 , 上述编译的 FFmpeg 源码的编译后的命令行工具 , 函数库 , 安装到了 /usr/local/ffmpeg4.2/bin 目录中 , 将该目录设置到 环境变量 中 ;
查看环境变量文件 ;
执行
source ~/.bash_profile
命令 , 更新 环境变量文件 ;
4、验证 FFmpeg 版本号
此时在 Mac 中 , 可以执行
ffmpeg -version
命令 , 可查看当前 FFmpeg 命令 ;
四、编译结果查看
1、安装 tree 命令行工具
执行
brew install tree
命令 , 安装 tree 命令行工具 ;
2、FFmpeg 编译输出内容 - 可执行程序 / 共享库 / 头文件
FFmpeg 编译后 , 会输出 可执行文件 , 共享库 , 头文件 三种内容 ;
- 编译后的 输出目录 为
/usr/local/ffmpeg4.2/
; - 可执行文件 在输出目录的 bin 目录下 ,
/usr/local/ffmpeg4.2/bin
; - 共享库 在输出目录的 lib 目录下 ,
/usr/local/ffmpeg4.2/lib
; - 头文件 在输出目录的 include 目录下 ,
/usr/local/ffmpeg4.2/include
;
3、查看编译结果目录
之前编译时配置了 --prefix=/usr/local/ffmpeg4.2
配置 , 编译结果 库文件 和 可执行程序 输出目录为 /usr/local/ffmpeg4.2 ;
执行
tree /usr/local/ffmpeg4.2
命令 , 可查看 /usr/local/ffmpeg4.2 目录中的 文件和目录结构 ;
命令行输入结果如下 : 代码较多 , 这是 mac 编译 FFmpeg 后的完整输出代码 ;
hsl@hanshuliangdeMacBook-Air ffmpeg4.2 % pwd
/usr/local/ffmpeg4.2
hsl@hanshuliangdeMacBook-Air ffmpeg4.2 % tree /usr/local/ffmpeg4.2
/usr/local/ffmpeg4.2
├── bin
│ ├── ffmpeg
│ ├── ffplay
│ └── ffprobe
├── include
│ ├── libavcodec
│ │ ├── ac3_parser.h
│ │ ├── adts_parser.h
│ │ ├── avcodec.h
│ │ ├── avdct.h
│ │ ├── avfft.h
│ │ ├── d3d11va.h
│ │ ├── dirac.h
│ │ ├── dv_profile.h
│ │ ├── dxva2.h
│ │ ├── jni.h
│ │ ├── mediacodec.h
│ │ ├── qsv.h
│ │ ├── vaapi.h
│ │ ├── vdpau.h
│ │ ├── version.h
│ │ ├── videotoolbox.h
│ │ ├── vorbis_parser.h
│ │ └── xvmc.h
│ ├── libavdevice
│ │ ├── avdevice.h
│ │ └── version.h
│ ├── libavfilter
│ │ ├── avfilter.h
│ │ ├── buffersink.h
│ │ ├── buffersrc.h
│ │ └── version.h
│ ├── libavformat
│ │ ├── avformat.h
│ │ ├── avio.h
│ │ └── version.h
│ ├── libavutil
│ │ ├── adler32.h
│ │ ├── aes.h
│ │ ├── aes_ctr.h
│ │ ├── attributes.h
│ │ ├── audio_fifo.h
│ │ ├── avassert.h
│ │ ├── avconfig.h
│ │ ├── avstring.h
│ │ ├── avutil.h
│ │ ├── base64.h
│ │ ├── blowfish.h
│ │ ├── bprint.h
│ │ ├── bswap.h
│ │ ├── buffer.h
│ │ ├── camellia.h
│ │ ├── cast5.h
│ │ ├── channel_layout.h
│ │ ├── common.h
│ │ ├── cpu.h
│ │ ├── crc.h
│ │ ├── des.h
│ │ ├── dict.h
│ │ ├── display.h
│ │ ├── downmix_info.h
│ │ ├── encryption_info.h
│ │ ├── error.h
│ │ ├── eval.h
│ │ ├── ffversion.h
│ │ ├── fifo.h
│ │ ├── file.h
│ │ ├── frame.h
│ │ ├── hash.h
│ │ ├── hdr_dynamic_metadata.h
│ │ ├── hmac.h
│ │ ├── hwcontext.h
│ │ ├── hwcontext_cuda.h
│ │ ├── hwcontext_d3d11va.h
│ │ ├── hwcontext_drm.h
│ │ ├── hwcontext_dxva2.h
│ │ ├── hwcontext_mediacodec.h
│ │ ├── hwcontext_qsv.h
│ │ ├── hwcontext_vaapi.h
│ │ ├── hwcontext_vdpau.h
│ │ ├── hwcontext_videotoolbox.h
│ │ ├── imgutils.h
│ │ ├── intfloat.h
│ │ ├── intreadwrite.h
│ │ ├── lfg.h
│ │ ├── log.h
│ │ ├── lzo.h
│ │ ├── macros.h
│ │ ├── mastering_display_metadata.h
│ │ ├── mathematics.h
│ │ ├── md5.h
│ │ ├── mem.h
│ │ ├── motion_vector.h
│ │ ├── murmur3.h
│ │ ├── opt.h
│ │ ├── parseutils.h
│ │ ├── pixdesc.h
│ │ ├── pixelutils.h
│ │ ├── pixfmt.h
│ │ ├── random_seed.h
│ │ ├── rational.h
│ │ ├── rc4.h
│ │ ├── replaygain.h
│ │ ├── ripemd.h
│ │ ├── samplefmt.h
│ │ ├── sha.h
│ │ ├── sha512.h
│ │ ├── spherical.h
│ │ ├── stereo3d.h
│ │ ├── tea.h
│ │ ├── threadmessage.h
│ │ ├── time.h
│ │ ├── timecode.h
│ │ ├── timestamp.h
│ │ ├── tree.h
│ │ ├── twofish.h
│ │ ├── tx.h
│ │ ├── version.h
│ │ └── xtea.h
│ ├── libpostproc
│ │ ├── postprocess.h
│ │ └── version.h
│ ├── libswresample
│ │ ├── swresample.h
│ │ └── version.h
│ └── libswscale
│ ├── swscale.h
│ └── version.h
├── lib
│ ├── libavcodec.58.54.100.dylib
│ ├── libavcodec.58.dylib -> libavcodec.58.54.100.dylib
│ ├── libavcodec.a
│ ├── libavcodec.dylib -> libavcodec.58.54.100.dylib
│ ├── libavdevice.58.8.100.dylib
│ ├── libavdevice.58.dylib -> libavdevice.58.8.100.dylib
│ ├── libavdevice.a
│ ├── libavdevice.dylib -> libavdevice.58.8.100.dylib
│ ├── libavfilter.7.57.100.dylib
│ ├── libavfilter.7.dylib -> libavfilter.7.57.100.dylib
│ ├── libavfilter.a
│ ├── libavfilter.dylib -> libavfilter.7.57.100.dylib
│ ├── libavformat.58.29.100.dylib
│ ├── libavformat.58.dylib -> libavformat.58.29.100.dylib
│ ├── libavformat.a
│ ├── libavformat.dylib -> libavformat.58.29.100.dylib
│ ├── libavutil.56.31.100.dylib
│ ├── libavutil.56.dylib -> libavutil.56.31.100.dylib
│ ├── libavutil.a
│ ├── libavutil.dylib -> libavutil.56.31.100.dylib
│ ├── libpostproc.55.5.100.dylib
│ ├── libpostproc.55.dylib -> libpostproc.55.5.100.dylib
│ ├── libpostproc.a
│ ├── libpostproc.dylib -> libpostproc.55.5.100.dylib
│ ├── libswresample.3.5.100.dylib
│ ├── libswresample.3.dylib -> libswresample.3.5.100.dylib
│ ├── libswresample.a
│ ├── libswresample.dylib -> libswresample.3.5.100.dylib
│ ├── libswscale.5.5.100.dylib
│ ├── libswscale.5.dylib -> libswscale.5.5.100.dylib
│ ├── libswscale.a
│ ├── libswscale.dylib -> libswscale.5.5.100.dylib
│ └── pkgconfig
│ ├── libavcodec.pc
│ ├── libavdevice.pc
│ ├── libavfilter.pc
│ ├── libavformat.pc
│ ├── libavutil.pc
│ ├── libpostproc.pc
│ ├── libswresample.pc
│ └── libswscale.pc
└── share
├── doc
│ └── ffmpeg
│ ├── developer.html
│ ├── faq.html
│ ├── fate.html
│ ├── ffmpeg-all.html
│ ├── ffmpeg-bitstream-filters.html
│ ├── ffmpeg-codecs.html
│ ├── ffmpeg-devices.html
│ ├── ffmpeg-filters.html
│ ├── ffmpeg-formats.html
│ ├── ffmpeg-protocols.html
│ ├── ffmpeg-resampler.html
│ ├── ffmpeg-scaler.html
│ ├── ffmpeg-utils.html
│ ├── ffmpeg.html
│ ├── ffplay-all.html
│ ├── ffplay.html
│ ├── ffprobe-all.html
│ ├── ffprobe.html
│ ├── general.html
│ ├── git-howto.html
│ ├── libavcodec.html
│ ├── libavdevice.html
│ ├── libavfilter.html
│ ├── libavformat.html
│ ├── libavutil.html
│ ├── libswresample.html
│ ├── libswscale.html
│ ├── mailing-list-faq.html
│ ├── nut.html
│ └── platform.html
├── ffmpeg
│ ├── examples
│ │ ├── Makefile
│ │ ├── README
│ │ ├── avio_dir_cmd.c
│ │ ├── avio_reading.c
│ │ ├── decode_audio.c
│ │ ├── decode_video.c
│ │ ├── demuxing_decoding.c
│ │ ├── encode_audio.c
│ │ ├── encode_video.c
│ │ ├── extract_mvs.c
│ │ ├── filter_audio.c
│ │ ├── filtering_audio.c
│ │ ├── filtering_video.c
│ │ ├── http_multiclient.c
│ │ ├── hw_decode.c
│ │ ├── metadata.c
│ │ ├── muxing.c
│ │ ├── qsvdec.c
│ │ ├── remuxing.c
│ │ ├── resampling_audio.c
│ │ ├── scaling_video.c
│ │ ├── transcode_aac.c
│ │ ├── transcoding.c
│ │ ├── vaapi_encode.c
│ │ └── vaapi_transcode.c
│ ├── ffprobe.xsd
│ ├── libvpx-1080p.ffpreset
│ ├── libvpx-1080p50_60.ffpreset
│ ├── libvpx-360p.ffpreset
│ ├── libvpx-720p.ffpreset
│ └── libvpx-720p50_60.ffpreset
└── man
├── man1
│ ├── ffmpeg-all.1
│ ├── ffmpeg-bitstream-filters.1
│ ├── ffmpeg-codecs.1
│ ├── ffmpeg-devices.1
│ ├── ffmpeg-filters.1
│ ├── ffmpeg-formats.1
│ ├── ffmpeg-protocols.1
│ ├── ffmpeg-resampler.1
│ ├── ffmpeg-scaler.1
│ ├── ffmpeg-utils.1
│ ├── ffmpeg.1
│ ├── ffplay-all.1
│ ├── ffplay.1
│ ├── ffprobe-all.1
│ └── ffprobe.1
└── man3
├── libavcodec.3
├── libavdevice.3
├── libavfilter.3
├── libavformat.3
├── libavutil.3
├── libswresample.3
└── libswscale.3
21 directories, 241 files
hsl@hanshuliangdeMacBook-Air ffmpeg4.2 %