ffmpeg源码编译 ios


本篇文章所有步骤作者本人都经过实际操作,童叟无欺。

一、H264配置

1、下载源码

官网地址:
http://www.videolan.org/developers/x264.html
github镜像:
https://github.com/mirror/x264

  1. 从官网下载得到最新的x264源码包,解压后得到一个名为x264-snapshot-xxx的文件夹。
  2. 为了后续脚本编译,将上面的x264-snapshot-xxx文件夹重命名为x264。

如果没有梯子,可以使用github镜像。

2、下载编译脚本

github地址:
https://github.com/kewlbear/x264-ios

  1. 下载x264的编译脚本文件,将下载得到的build-x264.sh放到上面的x264文件夹同一级目录下。(.sh文件和x264文件夹同一层级)
  2. 安装插件gas-preprocessor.pl
  • 下载

    git clone https://github.com/libav/gas-preprocessor
    
    cd gas-preprocessor
    
  • 拷贝

    sudo cp gas-preprocessor.pl /usr/local/bin
    
  • 给权限

    chmod 777 /usr/local/bin/gas-preprocessor.pl
    
  1. cd至 .sh文件所在目录,在确保路径中无中文的情况下(有中文会在编译中提示找不到文件),运行脚本 ./build-x264.sh

  2. 脚本运行完毕后,会生成4个文件夹:x264-iOS,scratch-x264,thin-x264。查看x264-iOS中lib文件夹下的libx264.a。
    命令为:

    lipo -info libx264.a 
    

正常会提示Architectures in the fat file: libx264.a are: armv7 x86_64 arm64 i386,同时支持armv7、 armv7s、i386、x86_64、arm64所有格式

3、脚本修改

我的环境i386架构编译有点问题,但是个人觉得不太重要就跳过了,build-x264.sh修改如下:

#i386编译有问题
#ARCHS="arm64 x86_64 i386 armv7 armv7s"
ARCHS="arm64 x86_64 armv7"

4、编译

./build-x264.sh

二、aac配置

1、下载源码

官网地址:
http://www.linuxfromscratch.org/blfs/view/svn/multimedia/fdk-aac.html
github地址:
https://github.com/mstorsjo/fdk-aac
最新版本为2.0.1

2、下载编译脚本

github地址:
https://github.com/kewlbear/fdk-aac-build-script-for-iOS

把源码解压,把编译脚本build-fdk-aac.sh放到解压出来的fdk-aac-2.0.1同级目录,根据需要编译的archs
及fdk-aac源码目录修改脚本。

3、编译环境配置

安装automake和libtool工具:

brew install automake libtool

4、脚本修改

#根据自己的需要修改
ARCHS="arm64 x86_64 i386 armv7"

# directories
#fdk-aac-2.0.1是同目录下源码所在的文件夹
SOURCE="fdk-aac-2.0.1"
FAT="fdk-aac-ios"

5、编译

fdk-aac-build-script-for-iOS目录下操作:

cd fdk-aac-2.0.1

./autogen.sh

cd ..

./build-fdk-aac.sh

编译完后会生成fdk-aac-ios目录,头文件和静态库都包含其中。

三、ffmpeg配置

1、下载源码

github地址:
https://github.com/kewlbear/FFmpeg-iOS-build-script

2、配置h264和aac路径

把编译好的h264和aac目录拷贝到FFmpeg-iOS-build-script目录下,
该目录所有文件如下:

MacdeMacBook-Pro-2:FFmpeg-iOS-build-script mac$ ls -l
total 40
drwxr-xr-x   4 mac  staff   128  7 21 18:03 FFmpeg-iOS
-rw-r--r--   1 mac  staff   971  7 21 15:32 README.md
-rwxr-xr-x   1 mac  staff  6100  7 21 15:32 build-ffmpeg-iOS-framework.sh
-rwxr-xr-x   1 mac  staff  3255  7 21 15:32 build-ffmpeg-tvos.sh
-rwxr-xr-x@  1 mac  staff  3550  7 21 17:48 build-ffmpeg.sh
drwxr-xr-x   4 mac  staff   128  7 21 16:28 fat-x264
drwxr-xr-x@  4 mac  staff   128 12 12  2019 fdk-aac-ios
drwx------  36 mac  staff  1152  8  6  2019 ffmpeg-4.2
drwxr-xr-x   6 mac  staff   192  7 21 15:55 scratch
drwxr-xr-x   5 mac  staff   160  7 21 15:49 thin

修改编译脚本build-ffmpeg.sh

# absolute path to x264 library
# h264 ios库路径
X264=`pwd`/fat-x264

# aac ios库路径
FDK_AAC=`pwd`/fdk-aac-ios

3、环境配置

  1. 安装gas-preprocessor:
    https://github.com/libav/gas-preprocessor
    编译h264时已经做过此操作,可跳过

  2. 安装yasm库:

brew install yasm

4、脚本修改

由于我本地的h264没有编译i386架构,所以此处也需要修改。

#i386的h264编译有问题
#ARCHS="arm64 armv7 x86_64 i386"
ARCHS="arm64 armv7 x86_64"

5、编译

执行编译脚本

  ./build-ffmpeg.sh

四、踩坑

1、问题1

问题描述:

Makefile:1: /Users/mac/Desktop/webrtc/FFmpeg-iOS-build-script/ffmpeg-3.3/Makefile: No such file or directory
make: *** No rule to make target `/Users/mac/Desktop/webrtc/FFmpeg-iOS-build-script/ffmpeg-3.3/Makefile'.  Stop.

解决方案:
网上说是路径有汉字导致,但我本地并没有。我只是把目录拷贝到别的路径,应该是系统的缓存,导致总是从之前的目录查找。
重新git clone一份。

2、问题2

问题描述:
h264源码编译x86_64时,提示如下:

building x86_64...
Found no assembler
Minimum version is nasm-2.13

解决方案:

brew install nasm

3、问题3

问题描述:
h264源码编译i386时,提示如下:

building i386...
No working C compiler found.

解决方案:
。。。
I386架构在ios上是虚拟机,就跳过了。

五、使用

把编译好的FFmpeg-iOS、x264-iOS、fdk-aac-ios拖到xcode中,如果是swift工程,使用时要先创建桥接文件,在xcode直接随便新建一个c文件,会提示是否自动创建桥接文件,选择创建,然后就可以把这个临时c文件删除。
在桥接文件xxx-Bridging-Header.h中添加#include "libavformat/avformat.h"就可以使用ffmpeg。

六、编译好的资源

也可以直接下载我编译好的资源:
ffmpeg、aac、h264静态库和头文件

参考链接:
https://www.jianshu.com/p/e4f2eb6f3eb8
https://www.dazhuanlan.com/2019/12/05/5de88be2d3a08/
https://www.dazhuanlan.com/2019/11/06/5dc1a3384ade3/
https://www.jianshu.com/p/129de9d6b21d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值