在 macOS 上为 iOS 从源代码构建 FFmpeg
本页介绍如何为 iOS 配置和构建FFmpeg框架。iOS 的编译是交叉编译,假定使用 macOS 作为主机系统。所需步骤如下
- 准备构建环境
- 获取 FFmpeg 源代码
- 从命令行配置 FFmpeg
- 构建动态库
- 从 FFmpeg 库创建 iOS 框架
- 创建 XCFrameworks
- 嵌入框架
注: 以下文档假定您在~/ffmpeg
下使用 FFmpeg 库和代码。
准备构建环境
iOS 的构建环境由 Apple 的 Xcode 应用程序提供,其中包括工具链(编译器、链接器和其他工具)以及 iOS 平台-SDK(头文件和库),您可根据这些工具进行构建和链接。
获取 FFmpeg 源代码
您可以通过以下方式获取 FFmpeg 源代码:
- 从FFmpeg 下载页面下载。
- 从 git 克隆。例如,这些命令将 7.1 版的 FFmpeg 源代码克隆到
~/ffmpeg/ffmpeg_src
。% cd ~/ffmpeg/ % git clone --branch n7.1 https://git.ffmpeg.org/ffmpeg.git ffmpeg_src
注意: 建议使用与Qt Multimedia 主页中记录的相同的 FFmpeg 版本。
配置和构建 FFmpeg
在~/ffmpeg
目录内创建build
和installed
目录,并导航至build
:
% mkdir ~/ffmpeg/build % mkdir ~/ffmpeg/installed % cd ~/ffmpeg/build
要配置 FFmpeg,请运行:
% ../ffmpeg_src/configure --disable-programs --disable-doc --enable-network --enable-shared --disable-static \ --sysroot="$(xcrun --sdk iphoneos --show-sdk-path)" \ --enable-cross-compile \ --arch=arm64 \ --prefix=../installed \ --cc="xcrun --sdk iphoneos clang -arch arm64" \ --cxx="xcrun --sdk iphoneos clang++ -arch arm64" \ --extra-ldflags="-miphoneos-version-min=16.0" \ --install-name-dir='@rpath' \ --disable-audiotoolbox
不需要命令行程序和文档;应启用网络功能。--disable-static
--sysroot
指定交叉编译树的根目录。--prefix
参数指定构建后安装 FFmpeg 库的路径。--extra-ldflags
设置 iOS 的最低版本。--install-name-dir
是一个字符串,指定苹果平台上已安装目标的共享库 "install_name "字段的目录部分。禁用 AudioToolBox,因为 FFmpeg 使用的 AudioToolBox 框架部分在 iOS 上不可用。
注: 在上例中,将 "iphoneos "替换为 "iphonesimulator",将 "miphoneos-version "替换为 "mios-simulator-version-min",可将 FFmpeg 配置为适合 iOS 模拟器的库。
要获得其他配置选项的帮助,请运行:
% ../ffmpeg_src/configure --help
配置完成后,构建 FFmpeg 库:
% make -j install
从 FFmpeg 库创建 iOS 框架
要在 iOS 应用程序中运行,必须将 FFmpeg 动态库转换为框架并嵌入到应用程序捆绑包中。框架是一个分层目录,它将动态库、图像文件、本地化字符串、头文件和参考文档等资源封装在一个包中。在我们的例子中,FFmpeg 框架只包含动态库和相应的 Info.plist 文件。例如,转换为框架的 libavcodec 将成为libavcodec.framework
目录,其中包含这些内容:
- Info.plist(包含框架说明)
- libavcodec(动态链接库)
与我们在上一步中构建的动态库不同,iOS 框架中的动态库没有版本控制,也没有 "dylib "扩展名。这就要求我们在动态库中固定库标识名称和依赖关系。otool
工具可以帮助我们找到这些名称。例如
%otool -L ../installed/lib/libavcodec.dylib
为我们提供了这些名称(我们只显示与 FFmpeg 相关的名称):
@rpath/libavcodec.61.dylib (compatibility version 61.0.0, current version 61.19.100) @rpath/libswresample.5.dylib (compatibility version 5.0.0, current version 5.3.100) @rpath/libavutil.59.dylib (compatibility version 59.0.0, current version 59.39.100)
要修复这些名称,请使用install_name_tool
实用程序。实用程序选项如下
-id
- 允许更改共享库标识名称。-change
- 允许更改依赖的共享库安装名称。
该脚本会将之前构建的动态链接库转换为框架。代码假定从build
目录运行,框架位于~/ffmpeg/installed/framework
目录:
#!/usr/bin/env bash # Creates an Info.plist file for a given framework: build_info_plist() { local file_path="$1" local framework_name="$2" local framework_id="$3" # Minimum version must be the same we used when building FFmpeg. local minimum_version_key="MinimumOSVersion" local minimum_os_version="16.0" local supported_platforms="iPhoneOS" info_plist="<?xml version=\"1.0\" encoding=\"UTF-8\"?> <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"> <plist version=\"1.0\"> <dict> <key>CFBundleDevelopmentRegion</key> <string>en</string> <key>CFBundleExecutable</key> <string>${framework_name}</string> <key>CFBundleIdentifier</key> <string>${framework_id}</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>${framework_name}</string> <key>CFBundlePackageType</key> <string>FMWK</string> <key>CFBundleShortVersionString</key> <string>7.0.2</string> <key>CFBundleVersion</key> <string>7.0.2</string> <key>CFBundleSignature</key> <string>????</string> <key>${minimum_version_key}</key> <string>${minimum_os_version}</string> <key>CFBundleSupportedPlatforms</key> <array> <string>${supported_platforms}</string> </array> <key>NSPrincipalClass</key> <string></string> </dict> </plist>" echo $info_plist | tee ${file_path} 1>/dev/null } dylib_regex="^@rpath/.*\.dylib$" # Creates framework from a dylib file: create_framework() { local framework_name="$1" local ffmpeg_library_path="../installed" local framework_complete_path="${ffmpeg_library_path}/framework/${framework_name}.framework/${framework_name}" # Create framework directory and copy dylib file to this directory: mkdir -p "${ffmpeg_library_path}/framework/${framework_name}.framework" cp "${ffmpeg_library_path}/lib/${framework_name}.dylib" "${ffmpeg_library_path}/framework/${framework_name}.framework/${framework_name}" # Change the shared library identification name, removing version number and 'dylib' extension; # \c Frameworks part of the name is needed since this is where frameworks will be installed in # an application bundle: install_name_tool -id @rpath/Frameworks/${framework_name}.framework/${framework_name} "${framework_complete_path}" # Add Info.plist file into the framework directory: build_info_plist "${ffmpeg_library_path}/framework/${framework_name}.framework/Info.plist" "${framework_name}" "io.qt.ffmpegkit."${framework_name} otool -L "$framework_complete_path" | awk '/\t/ {print $1}' | egrep "$dylib_regex" | while read -r dependency_path; do found_name=$(tmp=${dependency_path/*\/}; echo ${tmp/\.*}) if [ "$found_name" != "$framework_name" ] then # Change the dependent shared library install name to remove version number and 'dylib' extension: install_name_tool -change "$dependency_path" @rpath/Frameworks/${found_name}.framework/${found_name} "${framework_complete_path}" fi done } ffmpeg_libs="libavcodec libavformat libavutil libswresample libswscale" for name in $ffmpeg_libs; do create_framework $name done
创建多平台二进制框架捆绑包
XCFramework bundle 是一个包,其中包含为多个平台(例如 iOS 和 iOS 模拟器)构建所需的框架和库。要创建这样一个框架,请使用xcodebuild
工具。例如,如果 iOS 和模拟器的框架分别位于~/ffmpeg/installed/arm64
和~/ffmpeg/installed/arm64-simulator
目录中,实用程序参数如下:
%xcodebuild -create-xcframework -framework ../installed/arm64/libavcodec.framework -framework ../installed/arm64-simulator/libavcodec.framework -output ../installed/framework/libavcodec.xcframework
嵌入框架
将 FFmpeg 框架嵌入应用程序捆绑包。有关如何使用 XCode 嵌入框架的信息,请参阅
© 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.