iOS 開(kāi)發(fā)之編譯ffmpeg 及報(bào)錯(cuò)解析
一、腳本編譯
1. 編譯Mac下可用 FFmpeg
編譯Mac下可用 FFmpeg,主要是可以在mac中,使用 FFmpeg 進(jìn)行操作視頻等。
Homebrew介紹
簡(jiǎn)稱(chēng)brew,是Mac OSX上的軟件包管理工具,能在Mac中方便的安裝軟件或者卸載軟件。
Homebrew安裝
打開(kāi)終端執(zhí)行
ruby -e "$(curl -fsSL[https://raw.githubusercontent.com/Homebrew/install/master/install)](https://links.jianshu.com/go?to=https%3A%2F%2Fraw.githubusercontent.com%2FHomebrew%2Finstall%2Fmaster%2Finstall)”)"
Homebrew命令使用
搜索軟件:brew search 軟件名, 如brew search wget
安裝軟件:brew install 軟件名, 如brew install wget
卸載軟件:brew remove 軟件名,如brew remove wget
通過(guò)Homebrew 安裝 FFmpeg
終端執(zhí)行:執(zhí)行 brew install ffmpeg --with-libvpx --with-libvorbis --with-ffplay
在終端中執(zhí)行一下命令,等待安裝完成即可:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝好Homebrew,然后終端執(zhí)行 "brew install ffmpeg",等待完成即可。
執(zhí)行結(jié)束,在終端中輸入ffmpeg,驗(yàn)證是否安裝成功。
2. 編譯 iOS 下 FFmpeg
主要是用于iOS下可用的FFmpeg
手動(dòng)編譯FFmpeg網(wǎng)上有一些方法,但是稍顯復(fù)雜而陳舊, 所以現(xiàn)在大部分都是使用FFmpeg-iOS-build-script.sh這個(gè)腳本,比較方便直接
FFmpeg-iOS-build-script 是一個(gè)外國(guó)人寫(xiě)的自動(dòng)編譯的腳本,腳本則會(huì)自動(dòng)從github中把ffmpeg源碼下到本地并開(kāi)始編譯出iOS可用的庫(kù),支持各種架構(gòu)。
在進(jìn)行編譯之前,需要做一些準(zhǔn)備工作安裝必備文件:
2.1 安裝 gas-preprocessor
后面運(yùn)行 FFmpeg-iOS-build-script 這個(gè)自動(dòng)編譯腳本需要 gas-preprocessor .
安裝步驟(依次執(zhí)行下面命令):
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/
2.2 安裝 yams
yasm是匯編編譯器,因?yàn)閒fmpeg中為了提高效率用到了匯編指令,所以編譯時(shí)需要安裝
安裝步驟(依次執(zhí)行下面命令):
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
二、 配置 FFmpeg 編譯腳本
下載編譯腳本
git clone https://github.com/kewlbear/FFmpeg-iOS-build-script.git
配置FFmpeg版本
當(dāng)前使用的是4.3.1版本,不過(guò)最新版本已經(jīng)更新到####6.0
官方地址:https://ffmpeg.org/download.html?aemtn=tg-on
注意:在使用最新版本的時(shí)候,可能會(huì)存在iOS版本的兼容性問(wèn)題,后面會(huì)說(shuō)到,例如 FFmpeg 6.0版本 iOS版本需要在 13.0以上才可以編譯
根據(jù)需要編譯出需要的庫(kù)文件,修改配置 build-ffmpeg.sh 腳本里面 CONFIGURE_FLAGS 后面的內(nèi)容,可用把很多不需要的東西,都禁止掉,這樣編譯出來(lái)的庫(kù)就不會(huì)非常龐大
例如我們?cè)诰幾g最新版本的f fmpeg-6.0的時(shí)候,會(huì)報(bào) audiotoolbox 存在錯(cuò)誤的問(wèn)題,我們只需要在CONFIGURE_FLAGS 后面字符串中加入: --disable-audiotoolbox 字符串就可以解決
配置選項(xiàng)參數(shù)
在ffmpeg源碼目錄下,終端執(zhí)行 ./configure --help可用查看全部參數(shù),下面簡(jiǎn)單列出部分參數(shù)
--disable-static :// 不構(gòu)建靜態(tài)庫(kù)[默認(rèn):關(guān)閉]
--enable-shared :// 構(gòu)建共享庫(kù)
--enable-gpl : // 允許使用GPL代碼。
--enable-nonfree :// 允許使用非免費(fèi)代碼。
--disable-doc : // 不構(gòu)造文檔
--disable-avfilter :// 禁止視頻過(guò)濾器支持
--enable-small : // 啟用優(yōu)化文件尺寸大?。奚俣龋?--cross-compile : // 使用交叉編譯
--disable-hwaccels :// 禁用所有硬件加速(本機(jī)不存在硬件加速器,所有不需要)
--disable-network :// 禁用網(wǎng)絡(luò)
--disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver
// 禁止ffmpeg、ffplay、ffprobe、ffserver
--disable-avdevice --disable-avcodec --disable-avcore
// 禁止libavdevice、libavcodec、libavcore
--list-decoders : // 顯示所有可用的解碼器
--list-encoders : // 顯示所有可用的編碼器
--list-hwaccels : // 顯示所有可用的硬件加速器
--list-protocols : // 顯示所有可用的協(xié)議
--list-indevs : // 顯示所有可用的輸入設(shè)備
--list-outdevs : // 顯示所有可用的輸出設(shè)備
--list-filters :// 顯示所有可用的過(guò)濾器
--list-parsers :// 顯示所有的解析器
--list-bsfs : // 顯示所有可用的數(shù)據(jù)過(guò)濾器
--disable-encoder=NAME : // 禁用XX編碼器 | disables encoder NAME
--enable-encoder=NAME : // 用XX編碼器 | enables encoder NAME
--disable-decoders : // 禁用所有解碼器 | disables all decoders
--disable-decoder=NAME : // 禁用XX解碼器 | disables decoder NAME
--enable-decoder=NAME : // 啟用XX解碼器 | enables decoder NAME
--disable-encoders : // 禁用所有編碼器 | disables all encoders
--disable-muxer=NAME : // 禁用XX混音器 | disables muxer NAME
--enable-muxer=NAME : // 啟用XX混音器 | enables muxer NAME
--disable-muxers : // 禁用所有混音器 | disables all muxers
--disable-demuxer=NAME : // 禁用XX解軌器 | disables demuxer NAME
--enable-demuxer=NAME : // 啟用XX解軌器 | enables demuxer NAME
--disable-demuxers : // 禁用所有解軌器 | disables all demuxers
--enable-parser=NAME : // 啟用XX剖析器 | enables parser NAME
--disable-parser=NAME : // 禁用XX剖析器 | disables parser NAME
--disable-parsers : // 禁用所有剖析器 | disa
運(yùn)行腳本生成 FFmpeg:
./build-ffmpeg.sh
三、編譯中存在的報(bào)錯(cuò)
1、FFmpeg-5.0以上編譯,報(bào)audiotoolbox錯(cuò)誤:
src/libavdevice/audiotoolbox.m:78:5: error: unknown type name 'AudioDeviceID'; did you mean 'AudioFileID'?
AudioDeviceID *devices;
^~~~~~~~~~~~~
AudioFileID
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.2.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioFile.h:197:35: note: 'AudioFileID' declared here
typedef struct OpaqueAudioFileID *AudioFileID;
^
src/libavdevice/audiotoolbox.m:84:5: error: use of undeclared identifier 'AudioObjectPropertyAddress'
AudioObjectPropertyAddress prop;
^
src/libavdevice/audiotoolbox.m:85:5: error: use of undeclared identifier 'prop'
prop.mSelector = kAudioHardwarePropertyDevices;
^
src/libavdevice/audiotoolbox.m:85:22: error: use of undeclared identifier 'kAudioHardwarePropertyDevices'
prop.mSelector = kAudioHardwarePropertyDevices;
^
src/libavdevice/audiotoolbox.m:86:5: error: use of undeclared identifier 'prop'
prop.mScope = kAudioObjectPropertyScopeGlobal;
^
src/libavdevice/audiotoolbox.m:86:22: error: use of undeclared identifier 'kAudioObjectPropertyScopeGlobal'
prop.mScope = kAudioObjectPropertyScopeGlobal;
^
src/libavdevice/audiotoolbox.m:87:5: error: use of undeclared identifier 'prop'
prop.mElement = kAudioObjectPropertyElementMaster;
^
src/libavdevice/audiotoolbox.m:87:22: error: use of undeclared identifier 'kAudioObjectPropertyElementMaster'; did you mean 'kAudioUnitProperty_ElementName'?
prop.mElement = kAudioObjectPropertyElementMaster;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kAudioUnitProperty_ElementName
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.2.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioUnitProperties.h:900:2: note: 'kAudioUnitProperty_ElementName' declared here
kAudioUnitProperty_ElementName = 30,
^
src/libavdevice/audiotoolbox.m:88:11: error: implicit declaration of function 'AudioObjectGetPropertyDataSize' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &prop, 0, NULL, &data_size);
^
src/libavdevice/audiotoolbox.m:88:11: note: did you mean 'AudioQueueGetPropertySize'?
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.2.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioQueue.h:1458:1: note: 'AudioQueueGetPropertySize' declared here
AudioQueueGetPropertySize( AudioQueueRef inAQ,
^
src/libavdevice/audiotoolbox.m:88:42: error: use of undeclared identifier 'kAudioObjectSystemObject'
err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &prop, 0, NULL, &data_size);
^
src/libavdevice/audiotoolbox.m:88:69: error: use of undeclared identifier 'prop'
err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &prop, 0, NULL, &data_size);
^
src/libavdevice/audiotoolbox.m:92:38: error: use of undeclared identifier 'AudioDeviceID'
num_devices = data_size / sizeof(AudioDeviceID);
^
src/libavdevice/audiotoolbox.m:94:30: error: expected expression
devices = (AudioDeviceID*)(av_malloc(data_size));
^
src/libavdevice/audiotoolbox.m:94:16: error: use of undeclared identifier 'AudioDeviceID'
devices = (AudioDeviceID*)(av_malloc(data_size));
^
src/libavdevice/audiotoolbox.m:95:11: error: implicit declaration of function 'AudioObjectGetPropertyData' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &prop, 0, NULL, &data_size, devices);
^
CC libavdevice/utils.o
src/libavdevice/audiotoolbox.m:95:11: note: did you mean 'AudioObjectGetPropertyDataSize'?
src/libavdevice/audiotoolbox.m:88:11: note: 'AudioObjectGetPropertyDataSize' declared here
err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &prop, 0, NULL, &data_size);
^
src/libavdevice/audiotoolbox.m:95:38: error: use of undeclared identifier 'kAudioObjectSystemObject'
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &prop, 0, NULL, &data_size, devices);
^
src/libavdevice/audiotoolbox.m:95:65: error: use of undeclared identifier 'prop'
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &prop, 0, NULL, &data_size, devices);
^
src/libavdevice/audiotoolbox.m:104:9: error: use of undeclared identifier 'prop'
prop.mScope = kAudioDevicePropertyScopeInput;
^
src/libavdevice/audiotoolbox.m:104:23: error: use of undeclared identifier 'kAudioDevicePropertyScopeInput'
prop.mScope = kAudioDevicePropertyScopeInput;
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [libavdevice/audiotoolbox.o] Error 1
make: *** Waiting for unfinished jobs....
分析:
audiotoolbox 主要是對(duì)音頻的硬編解碼起到作用,因?yàn)榧夹g(shù)更新太快,f fmpeg中并沒(méi)有寫(xiě)向下兼容的問(wèn)題,導(dǎo)致在iOS版本較低時(shí)編譯會(huì)存在一些無(wú)法設(shè)備的屬性、變量以及方法,所以導(dǎo)致編譯報(bào)錯(cuò),所以這里的解決方案是,直接禁止編譯 audiotoolbox。
解決方案:
所以我們只需要 "build-ffmpeg.sh"中的在CONFIGURE_FLAGS 后面字符串中加入: --disable-audiotoolbox 字符串就可以解決
2、FFmpeg-4.0 到 FFmpeg-5.0以下編譯,報(bào)“l(fā)ibrist/librist.h”錯(cuò):
src/libavformat/librist.c:36:10: fatal error: 'librist/librist.h' file not found
#include <librist/librist.h>
分析:
因?yàn)樵诖税姹竞笠雔ibrist,但是編譯腳本中 iOS最低支持的版本是 8.0
DEPLOYMENT_TARGET="8.0"
當(dāng)改掉 DEPLOYMENT_TARGET="9.0"后此問(wèn)題變被解決,具體原因不知,猜測(cè)是iOS 9.0后引入的 librist庫(kù)
解決方案:
所以我們只需要在 "build-ffmpeg.sh"中將 DEPLOYMENT_TARGET="8.0" 改為
DEPLOYMENT_TARGET="9.0" 或以上變可解決此問(wèn)題
3、FFmpeg-5.0以上編譯, Target 版本 小于 13.0 時(shí)報(bào)錯(cuò)
xcrun -sdk macosx metal src/libavfilter/metal/vf_yadif_videotoolbox.metal -o libavfilter/metal/vf_yadif_videotoolbox.metal.air
CC libavfilter/motion_estimation.o
src/libavfilter/metal/utils.m:35:21: error: 'supportsFamily:' is only available on iOS 13.0 or newer [-Werror,-Wunguarded-availability-new]
if ([device supportsFamily:MTLGPUFamilyCommon3]) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.2.sdk/System/Library/Frameworks/Metal.framework/Headers/MTLDevice.h:744:1: note: 'supportsFamily:' has been marked as being introduced in iOS 13.0 here, but the deployment target is iOS 9.0.0
- (BOOL)supportsFamily:(MTLGPUFamily)gpuFamily API_AVAILABLE(macos(10.15), ios(13.0));
^
src/libavfilter/metal/utils.m:35:21: note: enclose 'supportsFamily:' in an @available check to silence this warning
if ([device supportsFamily:MTLGPUFamilyCommon3]) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/libavfilter/metal/utils.m:35:36: error: 'MTLGPUFamilyCommon3' is only available on iOS 13.0 or newer [-Werror,-Wunguarded-availability-new]
if ([device supportsFamily:MTLGPUFamilyCommon3]) {
^~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.2.sdk/System/Library/Frameworks/Metal.framework/Headers/MTLDevice.h:154:28: note: 'MTLGPUFamily' has been marked as being introduced in iOS 13.0 here, but the deployment target is iOS 9.0.0
typedef NS_ENUM(NSInteger, MTLGPUFamily)
分析:
這個(gè)問(wèn)題很簡(jiǎn)單,就是在 FFmpeg-5.0以上中使用 “supportsFamily” ,而“supportsFamily” 只有在 iOS13.0以后才支持,因此低于 iOS13.0編譯會(huì)報(bào)錯(cuò)
解決方案:
所以我們只需要在 "build-ffmpeg.sh"中將 DEPLOYMENT_TARGET改為
="13.0" 或以上變可解決此問(wèn)題
4、FFmpeg-5.0以上編譯,iOS 版本設(shè)置為 13.0及以上 armv7、i386編譯報(bào)錯(cuò)
building armv7...
xcrun -sdk iphoneos clang is unable to create an executable file.
C compiler test failed.
分析:
因?yàn)閕OS 13.0以后不再使用armv7架構(gòu),所以在DEPLOYMENT_TARGET="13.0"
編譯 armv7架構(gòu)會(huì)報(bào)錯(cuò)。
解決方案:
在 "build-ffmpeg.sh"腳本中將
ARCHS="arm64 armv7 x86_64 i386 " 的 armv7 去掉變可解決此問(wèn)題
building i386...
xcrun -sdk iphonesimulator clang is unable to create an executable file.
C compiler test failed.
分析:
因?yàn)閕OS 13.0以后不再支持i386架構(gòu),所以在DEPLOYMENT_TARGET="13.0"
編譯 i386架構(gòu)會(huì)報(bào)錯(cuò)。
解決方案:
在 "build-ffmpeg.sh"腳本中將
ARCHS="arm64 armv7 x86_64 i386 " 的 i386 去掉變可解決此問(wèn)題