本页内容

在 macOS 上从源代码构建 FFmpeg

本页面介绍了如何在 macOS 上配置和构建FFmpeg。所需步骤如下:

  • 获取 FFmpeg 源代码。
  • 安装所需的依赖项。
  • 通过命令行配置 FFmpeg。
  • 构建开发库。

获取 FFmpeg 源代码

您可以通过以下方式获取 FFmpeg 源代码:

  • 从FFmpeg 下载页面下载。
  • 从 Git 克隆。例如,以下命令将 FFmpeg 9.0.1 版本的源代码克隆到~/ffmpeg 。
    git clone --branch n9.0.1 https://git.ffmpeg.org/ffmpeg.git ffmpeg

建议使用与Qt Multimedia 主页中所述相同的 FFmpeg 版本。

下文假设您将 FFmpeg 源代码存储在~/ffmpeg 目录下。

先决条件

要构建 FFmpeg,需要以下工具和软件包:

  • Homebrew。
  • Homebrew 软件包(yasm)。

安装 Homebrew

要安装 Homebrew,请执行以下命令:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

安装 Homebrew 软件包

要安装 Homebrew 软件包 Yasm,请运行:

brew install yasm

配置和编译 FFmpeg

在~/ffmpeg 目录下创建一个build 目录,并进入该目录:

mkdir ~/ffmpeg/build
cd ~/ffmpeg/build

要配置 FFmpeg,请运行:

../configure --prefix=/usr/local/ffmpeg --disable-doc --enable-network --enable-shared

--prefix 参数指定了构建完成后FFmpeg开发库的安装路径。文档选项并非必需,但应启用网络功能。若要将FFmpeg构建为静态库,请省略--enable-shared 选项。

在之前的代码片段中,--prefix 参数特意使用了绝对路径。 如果指定相对路径(例如,../install ),则会使用该相对路径引用依赖项,而非使用@rpath 指定的正确路径。使用绝对路径会导致 FFmpeg 构建结果不具备可移植性。若要使用相对路径并确保 FFmpeg 构建的可移植性,需要使用otool 手动修正依赖项。

当configure 命令执行完毕后,请使用make 命令构建并安装 FFmpeg。

make -j install

如果构建过程顺利完成且无错误,FFmpeg 开发库将安装在/usr/local/ffmpeg 目录下。若通过Qt Multimedia 进行构建,该路径将存储在FFMPEG_DIR 变量中,该变量在配置Qt Multimedia 时会被用到。

配置和构建 FFmpeg 通用二进制文件

要在 macOS 上创建通用二进制文件(例如,同时支持 x86_64 和 arm64 架构),请按照以下步骤操作:

  • 为 arm64 架构配置并构建 FFmpeg:
    ../configure --prefix=/usr/local/ffmpeg/arm64 --disable-doc --enable-network \
        --enable-shared --enable-cross-compile --arch=arm64 --cc="clang -arch arm64"
    make -j install
  • 为 x86_64 架构配置和构建 FFmpeg:
    ../configure --prefix=/usr/local/ffmpeg/x86_64 --disable-doc --enable-network \
        --enable-shared --enable-cross-compile --arch=x86_64 --cc="clang -arch x86_64"
    make -j install
  • 使用lipo 将构建结果合并为通用二进制文件。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.