在 macOS 上从源代码构建 FFmpeg

本页介绍如何在 macOS 上配置和构建FFmpeg。其中包括

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

获取 FFmpeg 源代码

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

  • FFmpeg 下载页面下载
  • 从 git 克隆。例如,此命令将 7.1 版的 FFmpeg 源代码克隆到~/ffmpeg
    % git clone --branch n7.1 https://git.ffmpeg.org/ffmpeg.git ffmpeg

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

以下段落假定您将 FFmpeg 源代码存储在~/ffmpeg 下。

先决条件

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

安装 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 ,该路径将存储在配置Qt Multimedia 时使用的FFMPEG_DIR 变量中。

配置和构建 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 将构建结果合并为通用二进制文件。

© 2025 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.