Swift 引入FFMpeg
一、数据代码准备
1、下载 FFMpeg 源码包
使用:https://ffmpeg.org//releases/
下载 ffmpeg-7.0.tar.gz 然后解压
官网: 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、调整编译脚本
1、修改 FFMPEG_VERSION 版本为你下载的 FFMpeg 版本。这里我改为 7.0。
2、将 ARCHS 中的 i386、armv7 去掉 ;
i386 用于32位处理器的模拟器、armv7 是32位真机 。这些架构比较老,目前一般用不到,打包时可能会出现各种问题,不如先去掉。
3、ffmpeg 5.0以上的话,将 DEPLOYMENT_TARGET 改为 =13.0
4、CONFIGURE_FLAGS 添加 --disable-audiotoolbox ;
5、裁剪(可选)
build-ffmpeg.sh 中 CONFIGURE_FLAGS 可以添加选项,来去掉不需要的功能,如:
--disable-ffplay --disable-ffprobe --disable-ffserver
# 禁止 ffplay、ffprobe、ffserver
查看配置参数
% 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(我打包时未用到也能成功,不知是否有后遗症)
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、其他依赖项
brew install nasm yasm
三、运行编译
1、运行脚本
./build-ffmpeg.sh arm64 x86_64
如果你只考虑 iOS 真机,或者 macOS 平台使用,也可以只有一个选项。
注:个人测试发现,使用 build-ffmpeg-iOS-framework.sh 可能会出现 arm64 和 x86_64 架构的包都打好了,但是合并 fat 会出问题;也许清空过去编译的库,有用。但使用 上述命令 没有出现这个情况。
2、结果
目录下会多出三个文件夹: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、引入包
如果不引入,可能会报错:Undefined symbols
出现这类报错信息,你也可以参考:https://ezcode.blog.csdn.net/article/details/138462466 来引入对应的库
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,还需导入UIKit.framework、Foundation.framework、CoreMotion.framework、MediaPlayer.framework、GameController.framework、 CoreHaptics.framework
若使用OpenGL绘制视频帧,还需导入OpenGLES.framework、 QuartzCore.framework。
4、引入FFmpeg中.a 文件
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"
运行没有报错,就代表包可以用
参考: https://blog.csdn.net/lovechris00/article/details/138484142