一、背景
在iOS平臺下,使用FFmpeg tools(命令行)開發(fā)添加文字水印功能需要添加libfreetype、libfontconfig、libfribidi庫.?
為什么需要這3個庫?因為在ffmpeg官方說明文檔中:
11.60 drawtext
Draw a text string or text from a specified file on top of a video, using the libfreetype library.To enable compilation of this filter, you need to configure FFmpeg with?--enable-libfreetype. To enable default font fallback and the?font?option you need to configure FFmpeg with?--enable-libfontconfig. To enable the?text_shaping?option, you need to configure FFmpeg with?--enable-libfribidi.
翻譯過來意思是:
使用libfreetype庫在視頻指定文件中繪制文本字符串或文本。要啟用此過濾器的編譯,您需要使用--enable-libfreetype配置FFmpeg。要啟用默認字體后備和字體選項,您需要使用--enable-libfontconfig配置FFmpeg。要啟用text_shaping選項,您需要使用--enable-libfribidi配置FFmpeg。
所以,這里我們需要自行編譯libfreetype、libfontconfig、libfribidi庫。
二、編譯第三方庫
在ffmpeg中,加入第三方庫需要下載源碼并將它編譯。使用brew install libfreetype安裝是沒用的,因為這樣安裝出來的freetype只有x86架構(gòu),并沒有其它架構(gòu)。必須要像網(wǎng)上x264教程一樣,需要下載它的源碼,使用xcode編譯出它的靜態(tài)庫出來,再在FFmpeg編譯中添加參數(shù)--enable-libfreetype、--enable-libfontfig、--enable-libfribidi.這樣FFmpeg才會將這三個庫加進去一起編譯。
我使用的腳本是mobile-ffmpeg,在這個腳本中包含了編譯腳本與源碼:
1.源碼
freetype庫源碼位于mobile-ffmpeg/src/freetype
fontconfig庫源碼位于mobile-ffmpeg/src/fontconfig
fribidi庫源碼位于mobile-ffmpeg/src/fribidi
所有ffmpeg使用到的第三方庫都放到了mobile-ffmpeg/src目錄下,如有新版本或想更換源碼可以替換該路徑。
2.編譯腳本
freetype編譯腳本位于mobile-ffmpeg/build/ios-freetype.h?
fontconfig編譯腳本位于mobile-ffmpeg/build/ios-fontconfig.h
fribidi編譯腳本位于mobile-ffmpeg/build/ios-fontconfig.h
所有ffmpeg使用到的第三方庫的編譯腳本都在mobile-ffmpeg/build中。
3.編譯環(huán)境準備
編譯環(huán)境需要Xcode 8和Command Line Tools for Xcode8。(可見上一篇 iOS集成FFmpeg開發(fā))
4.編譯命令
cd mobile-ffmpeg
./ios.sh --disable-arm64e --disable-armv7s --enable-fontconfig --enable-freetype?--enable-fribidi
編譯完成后會在當前目錄生成一個prebuilt目錄,里面包含arm64、armv7、i386、x86_64架構(gòu)下已經(jīng)編譯的第三方庫與ffmpeg庫。靜態(tài)庫合并之后放在ios-universal中

第三方庫我們?nèi)xpat、fontconfig、freetype、fribidi、libpng、libuuid中的靜態(tài)庫,需要將他們加入到工程的鏈接庫中。
三、測試
1.新建Xcode工程
按照?iOS集成FFmpeg開發(fā)?的方法,直接替換ffmpeg庫和新增第三方庫,我們編寫以下命令作為測試代碼:
ffmpeg -i test.mp4 -vf?drawtext=fontfile=Songti.ttc:text='測試水印':x=10:y=10:fontsize=24:fontcolor=white test_output.mp4
運行后打開test_output.mp4成功看到文字水印。
編譯不再報錯:No such filter: 'drawtext' ,報沒有drawtext濾鏡的錯誤。
2.文字水印含有符號的情況
1).在文字水印中含有冒號:時,ffmpeg運行會報錯:
[Parsed_drawtext_0 @ 0x7f8d21702000] Cannot find a valid font for the family Sans
[AVFilterGraph @ 0x7f8d1e50e640] Error initializing filter 'drawtext' with args 'fontfile=/Library/Fonts/Songti.ttc:text=測試水印:90%:x=10:y=10:fontsize=24:fontcolor=white'
Error reinitializing filters!
Failed to inject frame into filter network: No such file or directory
Error while processing the decoded data for stream #0:0

原因是文字水印中的冒號:是ffmpeg中的分隔符,需要將它轉(zhuǎn)義.
2). 在文字水印中含有百分號%,ffmpeg運行會報錯:
[Parsed_drawtext_0 @ 0x7ffa57d0ecc0] Stray % near ''
? ? Last message repeated 1 times

原因是百分號%之后需要它要找大括號,因為%{...}格式是函數(shù)擴展模式,可見ffmpeg命令官網(wǎng)介紹

所以我們這里的解決方案就是增加命令expansion=none,指定擴展模式為逐個字符打印
ffmpeg -i test.mp4 -vf?drawtext=fontfile=Songti.ttc:text='測試水印\\:90%':x=10:y=10:fontsize=24:fontcolor=white:expansion=none test_output.mp4
四、分析編譯腳本
回過頭來,我們簡單分析一下mobile-ffmpeg腳本
第三方庫和ffmpeg源碼放在mobile-ffmpeg/src/* 中,編譯腳本放在mobile-ffmpeg/build/*中,
如果我們對ffmpeg編譯配置需要修改,可以打開mobile-ffmpeg/build/ios-ffmpeg.sh進行添加或修改。
最外層使用腳本位于mobile-ffmpeg/ios.sh,打開該腳本閱讀可以看到它的邏輯??蓪δ_本進行修改以滿足需求。
五、結(jié)束
文中Demo:
參考資料:https://github.com/tanersener/mobile-ffmpeg