
剛接觸 FFmpeg 時,我翻了不少編譯教學卻遲遲無法成功。前幾天在 GitHub 上找到一支腳本(https://github.com/kewlbear/FFmpeg-iOS-build-script),終於能一次編譯通過,這裡把過程與踩到的坑記錄下來。
下載 https://github.com/libav/gas-preprocessor,把 gas-preprocessor.pl 複製到 /usr/sbin 下,並調整權限:chmod 777 /usr/local/bin/gas-preprocessor.pl
安裝 yasm
1. 下載腳本https://github.com/kewlbear/FFmpeg-iOS-build-script
2. 解壓縮,找到 build-ffmpeg.sh。
3. 執行腳本。若沒有先裝好 yasm,會看到類似以下的錯誤:
$ ./build-ffmpeg.sh\nYasm not found\nHomebrew not found. Trying to install...\nWhoops, the Homebrew installer has moved! Please instead run:\n ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"\nTrying to install Yasm...\n./build-ffmpeg.sh: line 67: brew: command not found
意思是缺 yasm,而 Homebrew 的安裝網址已搬移。依提示執行 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 安裝 Homebrew,再用它把 yasm 裝好,重跑腳本即可。
4. 合併靜態庫。編譯完成後,在 FFmpeg-iOS-build-script 目錄下執行 ./build-ffmpeg.sh lipo,lipo 會把多個架構的 .a 檔合併成一個通用庫。
5. 匯入專案。把 ffmpeg-iOS 目錄加入你的工程。
6. 寫入最小驗證程式碼。加入標頭檔 #include "avformat.h",並呼叫一次 av_register_all();。由於 FFmpeg 是 C 語言庫,需要把對應的 .m 檔副檔名改為 .mm 以開啟 Objective-C++ 混編模式。執行專案若不報錯,即代表編譯成功。
編譯時若出現 'libavcodec/avcodec.h' file not found,請修正 Header Search Paths 中的路徑:
$(PROJECT_DIR)/FFmpeg-iOS/include
另外別忘了加入所需的外部庫:
libz.dylib\nlibbz2.dylib\nlibiconv.dylib
完成上述設定後,我自己寫了一個 demo 驗證可用性(原文另附下載連結)。整體來說,只要把 gas-preprocessor 與 yasm 這兩個前置條件備齊,後續流程其實相當順利。