前言

“GitHub 上的 ijkplayer 是‘尸体’,B站内部的 ijkplayer 是‘活体’。”
那么,鞭尸有何帮助?

  1. 老项目应该还有一些在维护,需要做一些适配的时候,我能提供一些帮助
  2. 新人想上手直播拉流的,可以参考一下本文档。

概述

macOS 上为 iOS 平台如何编译 ijkplayer ?此文为您解答

ijkplayer 是基于 FFmpeg 的轻量级 Android/iOS 视频播放器,具有以下特点:

  • 支持 iOS/Android 平台
  • 基于 FFmpeg n4.0 版本
  • 支持硬件加速解码
  • 支持直播流播放 (RTMP, HLS)
  • 可定制化的编解码器支持

编译结果总览

成功编译的架构


| 架构 | 目标平台 | SDK | 库文件大小 | 编译时间 |
|-----|---------|-----|-----------|---------|
| **arm64** | iOS 真机 | iPhoneOS | ~25 MB | ~8分钟 |
| **x86_64** | iOS 模拟器 | iPhoneSimulator | ~31 MB | ~8分钟 |
| **Universal** | 通用库 | 合并 | ~56 MB | ~1分钟 |

生成的库文件

build/universal/lib/
├── libavcodec.a    (37.6 MB) - 编解码器核心库
├── libavfilter.a   (1.4 MB)  - 音视频过滤器库
├── libavformat.a   (10.9 MB) - 封装格式处理库
├── libavutil.a     (3.0 MB)  - 工具函数库
├── libswresample.a (696 KB)  - 音频重采样库
└── libswscale.a    (4.3 MB)  - 视频缩放库

环境要求与配置

系统要求

  • macOS版本: 10.15+ (Catalina 或更高)
  • Xcode版本: 14.0+ (本次使用 26.0.1)
  • 磁盘空间: 至少 5GB 可用空间
  • 内存: 建议 8GB+

必需工具

# 安装 Homebrew (如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安装必需工具
brew install yasm        # 汇编器
brew install pkg-config   # 包配置工具 (可选)
brew install nasm        # 另一个汇编器 (可选)

# 验证 Xcode 命令行工具
xcode-select --install
xcode-select -p

环境变量配置

# 添加到 ~/.zshrc 或 ~/.bash_profile
export PATH="/usr/local/bin:$PATH"
export DEVELOPER_DIR=$(xcode-select -p)

项目初始化与配置修改

1. 克隆项目

git clone https://github.com/bilibili/ijkplayer.git ijkplayer-ios
cd ijkplayer-ios
git checkout -B latest k0.8.8

2. 初始化 iOS 构建环境

./init-ios.sh

这个脚本会:

  • 下载 gas-preprocessor (ARM 汇编预处理器)
  • 拉取 FFmpeg 源代码
  • 为每个架构创建 FFmpeg 分支
  • 设置配置文件链接

3. 关键配置修改

3.1 架构配置 (compile-ffmpeg.sh)

# 只使用现代架构
FF_ALL_ARCHS_MODERN="arm64 x86_64"
FF_ALL_ARCHS=$FF_ALL_ARCHS_MODERN

3.2 Xcode 26+ 兼容性修复 (tools/do-compile-ffmpeg.sh)

移除废弃的 Bitcode 标志:

# arm64 架构
elif [ "$FF_ARCH" = "arm64" ]; then
    FF_XCODE_BITCODE=""  # 原来是 "-fembed-bitcode"

# x86_64 架构
elif [ "$FF_ARCH" = "x86_64" ]; then
    FF_XCODE_BITCODE=""  # 原来是 "-fembed-bitcode"

更新最低 iOS 版本:

# arm64
FF_XCRUN_OSVERSION="-miphoneos-version-min=12.0"

# x86_64
FF_XCRUN_OSVERSION="-mios-simulator-version-min=12.0"

添加编译器兼容性标志:

FFMPEG_CFLAGS="$FFMPEG_CFLAGS -Wno-error=incompatible-function-pointer-types"
FFMPEG_CFLAGS="$FFMPEG_CFLAGS -Wno-error=int-conversion"

4. 选择编解码器配置

cd ios
ln -s module-lite.sh config/module.sh  # 精简版 (推荐)
# 或
ln -s module-default.sh config/module.sh  # 完整版

编译步骤详解

完整编译流程

cd ios

# 1. 清理之前的编译文件
./compile-ffmpeg.sh clean

# 2. 编译所有架构 (约15-20分钟)
./compile-ffmpeg.sh all

# 或分步编译:
./compile-ffmpeg.sh arm64   # 编译真机版本
./compile-ffmpeg.sh x86_64  # 编译模拟器版本
./compile-ffmpeg.sh lipo    # 合并为通用库

编译过程说明

阶段 1: 配置 (Configure)

  • 检测编译环境
  • 生成 Makefile
  • 配置编解码器选项
  • 设置编译标志

阶段 2: 编译 (Compile)

  • 编译 libavutil (基础工具)
  • 编译 libavcodec (编解码器)
  • 编译 libavformat (格式处理)
  • 编译 libavfilter (过滤器)
  • 编译 libswscale (缩放)
  • 编译 libswresample (重采样)

阶段 3: 安装 (Install)

  • 复制库文件到输出目录
  • 安装头文件
  • 生成 pkg-config 文件

阶段 4: 合并 (Lipo)

  • 使用 lipo 工具合并多架构
  • 生成通用库文件
  • 合并头文件

编译输出结构

ios/build/
├── ffmpeg-arm64/           # ARM64 架构构建
│   └── output/
│       ├── include/        # 头文件
│       │   ├── libavcodec/
│       │   ├── libavformat/
│       │   ├── libavutil/
│       │   └── ...
│       └── lib/           # 静态库
│           ├── libavcodec.a
│           ├── libavformat.a
│           └── ...
├── ffmpeg-x86_64/         # x86_64 架构构建
│   └── output/
│       ├── include/
│       └── lib/
└── universal/             # 通用库 (Fat Binary)
    ├── include/           # 合并的头文件
    └── lib/              # 合并的库文件
        ├── libavcodec.a   # 包含 arm64 + x86_64
        ├── libavformat.a
        └── ...

验证编译结果

检查库架构

# 验证通用库包含的架构
lipo -info build/universal/lib/libavcodec.a
# 输出: Architectures in the fat file: libavcodec.a are: x86_64 arm64

# 查看库详细信息
lipo -detailed_info build/universal/lib/libavcodec.a

# 检查符号表
nm build/universal/lib/libavcodec.a | grep avcodec_version

验证头文件

# 确认头文件存在
ls -la build/universal/include/libavcodec/avcodec.h
ls -la build/universal/include/libavformat/avformat.h

集成到 iOS 项目

方法一:使用 Demo 项目

# 打开 Demo 项目
open IJKMediaDemo/IJKMediaDemo.xcodeproj

# 在 Xcode 中:
# 1. 选择目标设备 (模拟器或真机)
# 2. 点击 Run 按钮
# 3. 测试视频播放功能

方法二:集成到自己的项目

1. 添加库文件

将以下文件添加到项目:

build/universal/lib/*.a  # 所有静态库

2. 配置 Build Settings

Header Search Paths:
$(PROJECT_DIR)/path/to/build/universal/include

Library Search Paths:
$(PROJECT_DIR)/path/to/build/universal/lib

Other Linker Flags:
-lz -lbz2 -liconv

3. 添加系统框架

在 Build Phases → Link Binary With Libraries 添加:

  • VideoToolbox.framework
  • CoreMedia.framework
  • CoreVideo.framework
  • CoreGraphics.framework
  • AVFoundation.framework
  • AudioToolbox.framework
  • UIKit.framework
  • Foundation.framework
  • QuartzCore.framework
  • OpenGLES.framework
  • CFNetwork.framework
  • Security.framework
  • MobileCoreServices.framework

4. 示例代码

#import <IJKMediaFramework/IJKMediaFramework.h>

// 创建播放器
IJKFFMoviePlayerController *player = [[IJKFFMoviePlayerController alloc]
    initWithContentURL:[NSURL URLWithString:@"http://example.com/video.mp4"]
    withOptions:nil];

// 准备播放
[player prepareToPlay];

// 添加播放视图
player.view.frame = self.view.bounds;
[self.view addSubview:player.view];

// 开始播放
[player play];

支持的功能配置

编解码器支持 (module-lite.sh)

解码器 (Decoders)

  • 视频: H.264, HEVC/H.265, VP8, VP9, FLV, H.263
  • 音频: AAC, AAC-LATM, MP3, FLAC

编码器 (Encoders)

  • 图像: PNG (用于截图)

封装格式 (Formats)

  • 输入: FLV, MP4/MOV, HLS, MPEG-TS, MKV/WebM
  • 输出: MP4

网络协议 (Protocols)

  • HTTP/HTTPS
  • RTMP/RTMPT
  • HLS
  • FTP
  • TCP/UDP
  • 文件系统

编译优化选项

  • --enable-small: 减小库文件大小
  • --enable-optimizations: 启用优化
  • --enable-neon: ARM NEON 优化 (仅 ARM64)
  • --enable-debug: 包含调试符号
  • --enable-pic: 位置无关代码

常见问题与解决方案

问题 1: yasm 未找到

错误: yasm not found
解决: brew install yasm

问题 2: 编译器类型检查错误

错误: incompatible function pointer types
解决: 已通过添加 -Wno-error=incompatible-function-pointer-types 解决

问题 3: iOS 版本 API 不兼容

错误: 'SecIdentityCreate' is only available on iOS 11.2 or newer
解决: 更新最低 iOS 版本到 12.0

问题 4: Bitcode 相关错误

错误: -mllvm and -bitcode_bundle cannot be used together
解决: 移除所有 -fembed-bitcode 标志

问题 5: 架构不匹配

错误: Undefined symbols for architecture x86_64
解决: 确保编译了所需架构并正确链接

性能指标

库文件大小对比

配置总大小说明
module-lite.sh~56 MB精简版,基本编解码器
module-default.sh~120 MB完整版,所有编解码器
module-lite-hevc.sh~65 MB精简版 + HEVC

编译时间

操作时间CPU 使用率
清理1-2 分钟
编译 arm647-10 分钟高 (100%)
编译 x86_647-10 分钟高 (100%)
合并库<1 分钟
总计15-22 分钟-

编译日志与警告说明

预期警告 (可忽略)

  1. 废弃 API 警告

    warning: 'codec' is deprecated
    warning: 'refcounted_frames' is deprecated

    这些是 FFmpeg 内部的兼容性代码。

  2. 函数指针类型警告

    warning: incompatible function pointer types

    已通过编译标志抑制为警告。

  3. NEON 指令警告

    warning: instruction movi.2d with immediate #0

    ARM 汇编优化相关,不影响功能。


更新与维护

更新 FFmpeg 版本

cd ijkplayer-ios
git pull origin master
./init-ios.sh
# 重新应用配置修改
# 重新编译

清理编译缓存

./compile-ffmpeg.sh clean
rm -rf build/
rm -rf ffmpeg-*/

切换编解码器配置

cd config
rm module.sh
ln -s module-default.sh module.sh  # 或其他配置
cd ..
./compile-ffmpeg.sh clean
./compile-ffmpeg.sh all

参考资源

官方文档

相关项目

技术规范


许可证信息

ijkplayer

  • 许可证: LGPL v2.1+
  • 版权所有: Bilibili

FFmpeg

  • 许可证: LGPL v2.1+ (本配置)
  • 可选 GPL 许可 (需要重新配置)

使用注意

  • LGPL 要求动态链接或提供目标文件
  • 如果修改了 FFmpeg 源码,需要公开修改部分
  • 商业使用请仔细阅读许可证条款

总结

  1. 成功适配 Xcode 26.0.1
  2. 移除废弃的 Bitcode 支持
  3. 更新最低 iOS 版本到 12.0
  4. 生成 arm64 和 x86_64 架构库
  5. 创建通用库 (Universal Binary)
  6. 验证所有库文件正确生成

编译产物可直接用于 iOS 应用开发,支持真机和模拟器调试。