Mac 编译 FFmpeg

1、目标

  • 编译出 ffmpegffprobeffplay 三个命令行工具
  • 只产生动态库,不产生静态库
  • fdk-aacx264x265 集成到 FFmpeg

2、源码下载

  • 下载源码 ffmpeg-4.3.2.tar.xz

源码地址: https://github.com/FFmpeg/FFmpeg

3、编译

3.1 依赖项

  • brew install yasm

    • ffmpeg的编译过程依赖yasm
    • 若未安装yasm会出现错误:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
  • brew install sdl2

    • ffplay 依赖于 sdl2
    • 如果缺少sdl2,就无法编译出ffplay
  • brew install fdk-aac

    • 不然会出现错误:ERROR: libfdk_aac not found
  • brew install x264

    • 不然会出现错误:ERROR: libx264 not found
    • x264 地址
  • brew install x265

    • 不然会出现错误:ERROR: libx265 not found

其实 x264x265sdl2 都在曾经执行 brew install ffmpeg 的时候安装过了

  • 可以通过 brew list 的结果查看是否安装过

    • brew list | grep fdk
    • brew list | grep x26
    • brew list | grep -E 'fdk|x26'
  • 如果已经安装过,可以不用再执行 brew install

3.2 configure

首先进入源码目录

# 我的源码放在了Downloads目录下
cd ~/Downloads/ffmpeg-4.3.2

然后执行源码目录下的 configure 脚本,设置一些编译参数,做一些编译前的准备工作。

./configure --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl  --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
  • --prefix

    • 用以指定编译好的FFmpeg安装到哪个目录
    • 一般放到 /usr/local/ffmpeg 中即可
  • --enable-shared

    • 生成动态库
  • --disable-static

    • 不生成静态库
  • --enable-libfdk-aac

    • 将fdk-aac内置到FFmpeg中
  • --enable-libx264

    • 将x264内置到FFmpeg中
  • --enable-libx265

    • 将 x265 内置到 FFmpeg 中
  • --enable-gpl

    • x264、x265 要求开启 GPL License
  • --enable-nonfree

      • fdk-aac与GPL不兼容,需要通过开启nonfree进行配置

你可以通过 configure --help 命令查看每一个配置项的作用。

./configure --help | grep static

# 结果如下所示
--disable-static         do not build static libraries [no]

3.3 编译

接下来开始解析源代码目录中的 Makefile 文件,进行编译。-j8 表示允许同时执行8个编译任务。

make -j8

3.3.1 关于 Makefile

  • Makefile 描述了整个项目的编译和链接等规则

    • 比如哪些文件需要编译?哪些文件不需要编译?哪些文件需要先编译?哪些文件需要后编译?等等
  • Makefile 可以使项目的编译变得自动化,不需要每次都手动输入一堆源文件和参数
    比如原来需要这么写:gcc test1.c test2.c test3.c -o test

3.4 安装

将编译好的库安装到指定的位置:/usr/local/ffmpeg

make install

安装完毕后,/usr/local/ffmpeg 的目录结构如下所示

ffmpeg 目录结构

3.5 配置 PATH

为了让 bin 目录中的 ffmpeg、ffprobe、ffplay 在任意位置都能够使用,需要先将 bin 目录配置到环境变量 PATH

  • .zshrc
# 编辑.zshrc
vim ~/.zshrc

# .zshrc文件中写入以下内容
export PATH=/usr/local/ffmpeg/bin:$PATH

# 让.zshrc生效
source ~/.zshrc

如果你用的是 bash,而不是 zsh,只需要将上面的.zshrc 换成 .bash_profile

3.6 验证

在命令行上输入 ffmpeg -version 进行验证

ffmpeg -version

# 结果如下所示
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
built with Apple clang version 13.0.0 (clang-1300.0.29.30)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil      56. 51.100 / 56. 51.100
libavcodec     58. 91.100 / 58. 91.100
libavformat    58. 45.100 / 58. 45.100
libavdevice    58. 10.100 / 58. 10.100
libavfilter     7. 85.100 /  7. 85.100
libswscale      5.  7.100 /  5.  7.100
libswresample   3.  7.100 /  3.  7.100
libpostproc    55.  7.100 / 55.  7.100

参考

  • M了个J - 编译 FFmpeg

你可能感兴趣的:(Mac 编译 FFmpeg)