FFMpeg (7.0) - 編譯 iOS SDK [2025]

Swift 引入FFMpeg

一、數(shù)據(jù)代碼準(zhǔn)備

1、下載 FFMpeg 源碼包

使用:https://ffmpeg.org//releases/
下載 ffmpeg-7.0.tar.gz 然后解壓

官網(wǎng): https://ffmpeg.org//download.html
macOS : https://evermeet.cx/ffmpeg/

2、下載編譯腳本

目前大家都是用這份腳本
https://github.com/kewlbear/FFmpeg-iOS-build-script

git clone https://github.com/kewlbear/FFmpeg-iOS-build-script.git

3、調(diào)整編譯腳本

1、修改 FFMPEG_VERSION 版本為你下載的 FFMpeg 版本。這里我改為 7.0。

2、將 ARCHS 中的 i386、armv7 去掉 ;

i386 用于32位處理器的模擬器、armv7 是32位真機(jī) 。這些架構(gòu)比較老,目前一般用不到,打包時(shí)可能會(huì)出現(xiàn)各種問題,不如先去掉。

3、ffmpeg 5.0以上的話,將 DEPLOYMENT_TARGET 改為 =13.0

4、CONFIGURE_FLAGS 添加 --disable-audiotoolbox ;

5、裁剪(可選)

build-ffmpeg.sh 中 CONFIGURE_FLAGS 可以添加選項(xiàng),來去掉不需要的功能,如:

--disable-ffplay  --disable-ffprobe  --disable-ffserver
# 禁止 ffplay、ffprobe、ffserver 

查看配置參數(shù)

% cd ffmpeg-7.0
% ./configure --help

二、安裝依賴

1、安裝 brew

brew 是 macOS 最常用的軟件/包管理工具

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

2、gas-preprocessor (不可少,否則無法解壓縮)

sudo git clone https://github.com/bigsen/gas-preprocessor.git  /usr/local/bin/gas

sudo cp /usr/local/bin/gas/gas-preprocessor.pl /usr/local/bin/gas-preprocessor.pl

sudo chmod 777 /usr/local/bin/gas-preprocessor.pl

sudo rm -rf /usr/local/bin/gas/

3、yams


curl http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz-o yasm-1.2.0.gz

tar-zxvf yasm-1.2.0.gz

cd yasm-1.2.0

./configure&&make-j4&&sudomakeinstall

4、x264、FDK-AAC(我打包時(shí)未用到也能成功,不知是否有后遺癥)
x264、FDK-AAC 是可選的,不加入也可以成功編譯 FFMpeg。
你也可以參考下面資料來加入編譯:

*   x264 : [https://ezcode.blog.csdn.net/article/details/138449980](https://ezcode.blog.csdn.net/article/details/138449980)
*   FDK-AAC : [https://ezcode.blog.csdn.net/article/details/138472805](https://ezcode.blog.csdn.net/article/details/138472805)

5、其他依賴項(xiàng)

brew install nasm yasm 

三、運(yùn)行編譯

1、運(yùn)行腳本

./build-ffmpeg.sh arm64 x86_64

如果你只考慮 iOS 真機(jī),或者 macOS 平臺(tái)使用,也可以只有一個(gè)選項(xiàng)。
注:個(gè)人測(cè)試發(fā)現(xiàn),使用 build-ffmpeg-iOS-framework.sh 可能會(huì)出現(xiàn) arm64 和 x86_64 架構(gòu)的包都打好了,但是合并 fat 會(huì)出問題;也許清空過去編譯的庫(kù),有用。但使用 上述命令 沒有出現(xiàn)這個(gè)情況。

2、結(jié)果

目錄下會(huì)多出三個(gè)文件夾:FFmpeg-iOS, scratch, thin (如果少FFmpeg-iOS 則編譯失敗)

% tree -L 2
.
├── FFmpeg-iOS
│   ├── include
│   └── lib
├── scratch
│   ├── arm64
│   └── x86_64
└── thin
    ├── arm64
    └── x86_64

10 directories, 0 files
13:53{cyan} 03 % tree -L 3
.
├── FFmpeg-Apple
│   ├── include
│   │   ├── libavcodec
│   │   ├── libavdevice
│   │   ├── libavfilter
│   │   ├── libavformat
│   │   ├── libavutil
│   │   ├── libswresample
│   │   └── libswscale
│   └── lib
│       ├── libavcodec.a
│       ├── libavdevice.a
│       ├── libavfilter.a
│       ├── libavformat.a
│       ├── libavutil.a
│       ├── libswresample.a
│       └── libswscale.a
├── scratch
│   ├── arm64
│   │   ├── Makefile
│   │   ├── config.h
│   │   ├── config_components.h
│   │   ├── doc
│   │   ├── ffbuild
│   │   ├── libavcodec
│   │   ├── libavdevice
│   │   ├── libavfilter
│   │   ├── libavformat
│   │   ├── libavutil
│   │   ├── libswresample
│   │   ├── libswscale
│   │   ├── src -> /Users/shushu/Documents/repos_apple/ff/FFmpeg-iOS-build-script/ffmpeg-7.0
│   │   └── tests
│   └── x86_64
│       ├── Makefile
│       ├── config.asm
│       ├── config.h
│       ├── config_components.h
│       ├── doc
│       ├── ffbuild
│       ├── libavcodec
│       ├── libavdevice
│       ├── libavfilter
│       ├── libavformat
│       ├── libavutil
│       ├── libswresample
│       ├── libswscale
│       ├── src -> /Users/shushu/Documents/repos_apple/ff/FFmpeg-iOS-build-script/ffmpeg-7.0
│       └── tests
└── thin
    ├── arm64
    │   ├── include
    │   ├── lib
    │   └── share
    └── x86_64
        ├── include
        ├── lib
        └── share

四、集成到 iOS 工程

1、拖拽包到工程

這里我將 FFmpeg-iOS 改為了 FFmpeg-Apple

2、修改 Search Paths

Header Search Paths : $(SRCROOT)/ffdemo/FFmpeg-Apple/include
Library Search Paths : $(SRCROOT)/ffdemo/FFmpeg-Apple/include

3、引入包

如果不引入,可能會(huì)報(bào)錯(cuò):Undefined symbols
出現(xiàn)這類報(bào)錯(cuò)信息,你也可以參考:https://ezcode.blog.csdn.net/article/details/138462466 來引入對(duì)應(yīng)的庫(kù)

libz.1.2.5.tbd
libbz2.1.0.tbd
libiconv.2.4.0.tbd
AVFoudation.framework
CoreMedia.framework
VideoToolBox.framework
AudioToolBox.framework

如果使用 sdl2,fdk-aac,x264,還需導(dǎo)入U(xiǎn)IKit.framework、Foundation.framework、CoreMotion.framework、MediaPlayer.framework、GameController.framework、 CoreHaptics.framework
若使用OpenGL繪制視頻幀,還需導(dǎo)入OpenGLES.framework、 QuartzCore.framework。

4、引入FFmpeg中.a 文件


截屏2025-02-17 16.25.55.png

5、修改 LiveMediaDemo-Bridging-Header 引入文件

引入頭文件

#include "libavcodec/avcodec.h"
#include "libavutil/opt.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#import "avformat.h"

運(yùn)行沒有報(bào)錯(cuò),就代表包可以用

參考: https://blog.csdn.net/lovechris00/article/details/138484142

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容