Mac 编译 FFmpeg

FFmpeg 可执行文件下载地址

https://johnvansickle.com/ffmpeg/

1、目标

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

2、源码下载

源码地址: 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 install libass

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

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

安装 libmp3lame

https://sourceforge.net/projects/lame/files/lame/

执行下面命令:

cd lame-3.100
./configure
make
sudo make install

报错解决:

Undefined symbols for architecture arm64:
  "_lame_init_old", referenced from:
      <initial-undefines>
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [libmp3lame.la] Error 1
make[2]: *** [all-recursive] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

lame-3.100/include 文件夹中的 libmp3lame.sym 中的lame_init_old这项删除,重新 .configure

3.2 configure

首先进入源码目录

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

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

./configure --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-openssl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-libmp3lame --enable-libass --enable-pthreads
  • --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

  • --enable-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

sudo 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                                                                                                       ─╯
ffmpeg version 6.1 Copyright (c) 2000-2023 the FFmpeg developers
built with Apple clang version 15.0.0 (clang-1500.3.9.4)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-libmp3lame
libavutil      58. 29.100 / 58. 29.100
libavcodec     60. 31.102 / 60. 31.102
libavformat    60. 16.100 / 60. 16.100
libavdevice    60.  3.100 / 60.  3.100
libavfilter     9. 12.100 /  9. 12.100
libswscale      7.  5.100 /  7.  5.100
libswresample   4. 12.100 /  4. 12.100
libpostproc    57.  3.100 / 57.  3.100

参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。