網站首頁 編程語言 正文
一、準備交叉編譯環(huán)境
我的環(huán)境如下:
- 系統(tǒng):ubuntu20.04
- NDK版本:r17c
參考教程及NDK構建交叉工具鏈:https://www.jb51.net/article/92161.htm
下載之后解壓,我的NDK解壓路徑為:/work/android/sdk/ndk/android-ndk-r17c/
安裝獨立工具鏈需要安裝python,如果沒有,安裝下:
lorien@ubuntu-20: sudo apt-get install python
然后執(zhí)行工具鏈構建腳本:
lorien@ubuntu-20: cd /work/android/sdk/ndk/android-ndk-r17c/build/tools/ lorien@ubuntu-20: ./make_standalone_toolchain.py \ --arch arm64 \ --api 21 \ --install-dir /work/tmp/android-toolchain
–install-dir參數(shù)指定了交叉工具鏈的路徑,進入該目錄看下:
lorien@ubuntu-20: cd /work/tmp/android-toolchain/ lorien@ubuntu-20: ls aarch64-linux-android COPYING COPYING.LIB lib manifest_4691093.xml MODULE_LICENSE_MIT repo.prop test AndroidVersion.txt COPYING3 COPYING.RUNTIME lib64 MODULE_LICENSE_BSD_LIKE NOTICE share bin COPYING3.LIB include libexec MODULE_LICENSE_GPL prebuilt_include sysroot
幾個比較重要的目錄:
- bin目錄:里面是編譯、鏈接可執(zhí)行工具
- sysroot:里面是基礎庫的頭文件和lib
二、交叉編譯ffmpeg+libx264+libfdk-aac
1. 交叉編譯libfdk-aac
下載libfdk-aac源碼:https://sourceforge.net/projects/opencore-amr/files/fdk-aac/
編寫配置configure腳本config-arm64.sh,將文件放入libfdk-aac源碼跟目錄中,腳本內容如下:
#!/bin/bash export PATH=$PATH:/work/tmp/android-toolchain/bin SYSROOT=/work/tmp/android-toolchain/sysroot target_host=aarch64-linux-android export AR=$target_host-ar export AS=$target_host-clang export CC=$target_host-clang export CXX=$target_host-clang++ export LD=$target_host-ld export STRIP=$target_host-strip export RANLIB=$target_host-ranlib # 注意要加-D__ANDROID_API__選項,否則可能會遇到undefined reference to 'stderr'錯誤 export CFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include" export CPPFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include" export CXXFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include" export LDFLAGS="-L$SYSROOT/usr/lib" ./configure --host=aarch64-linux-android \ --enable-static \ --disable-shared \ --prefix=$(pwd)/install/
執(zhí)行腳本:
lorien@ubuntu-20: cd /work/android/media/fdk-aac-2.0.2/ lorien@ubuntu-20: ./config-arm64.sh
可能會報錯:
/work/tmp/android-toolchain/bin/clang60: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
是缺少 libtinfo5導致的,需要安裝下:
lorien@ubuntu-20: sudo apt-get install libtinfo5
再重新執(zhí)行config-arm64.sh腳本,即可配置成功。
接下來,編譯安裝
lorien@ubuntu-20: make lorien@ubuntu-20: make install
編譯期間可能會碰到一些錯誤:
錯誤1:
libSBRdec/src/lpp_tran.cpp:122:10: fatal error: ‘log/log.h’ file not found
解決:
刪除代碼 include “l(fā)og/log.h”
錯誤2:
libSBRdec/src/lpp_tran.cpp:341:5: error: use of undeclared identifier ‘android_errorWriteLog’
解決:
刪除調用代碼即可
錯誤處理后再重新編譯&安裝,成功后進入/work/android/media/fdk-aac-2.0.2/install 目錄下可看到編譯產物:
lorien@ubuntu-20: cd install/ include lib
include目錄下是產生頭文件,lib目錄下是一些.a的庫文件。
2. 交叉編譯libx264
下載libx264源碼:git clone https://code.videolan.org/videolan/x264.git
和上面類似,進入x264源碼根目錄,編寫config腳本config-arm64.sh:
#!/bin/bash export PATH=$PATH:/work/tmp/android-toolchain/bin SYSROOT=/work/tmp/android-toolchain/sysroot target_host=aarch64-linux-android export CFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include" export CPPFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include" export CXXFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include" export LDFLAGS="-L$SYSROOT/usr/lib" ./configure --host=aarch64-linux-android \ --cross-prefix=$target_host- \ --sysroot=$SYSROOT \ --enable-static \ --enable-shared \ --prefix=$(pwd)/install/
執(zhí)行腳本,然后make&make install即可。根據(jù)-prefix選項配置,編譯產物會輸出到 /work/android/media/x264/install目錄下面。
3. 交叉編譯ffmpeg
fdk-aac和x264編譯完成之后,我們可以編譯ffmpeg了,ffmpeg會使用上面編譯出來的兩個庫。
首先,下載ffmpeg源碼:
lorien@ubuntu-20: git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
進入ffmpeg源碼根目錄,編寫配置腳本config-arm64.sh:
#!/bin/bash export PATH=$PATH:/work/tmp/android-toolchain/bin SYSROOT=/work/tmp/android-toolchain/sysroot target_host=aarch64-linux-android export CFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include -I$(pwd)/../fdk-aac-2.0.2/install/include -I$(pwd)/../x264/install/include" export LDFLAGS="-L$SYSROOT/usr/lib -L$(pwd)/../fdk-aac-2.0.2/install/lib -L$(pwd)/../x264/install/lib" ./configure --cross-prefix=$target_host- \ --enable-cross-compile \ --sysroot=$SYSROOT \ --arch=aarch64 \ --target-os=linux \ --enable-static \ --disable-shared \ --enable-gpl \ --enable-nonfree \ --disable-ffmpeg \ --disable-ffplay \ --disable-ffprobe \ --disable-doc \ --disable-devices \ --disable-avdevice \ --disable-postproc \ --disable-avfilter \ --disable-filters \ --disable-protocols \ --enable-libx264 \ --enable-libfdk-aac \ --prefix=$(pwd)/install/
在配置ffmpeg時,添加*–enable-libx264選項和–enable-libfdk-aac* 即可使ffmpeg打開x264和fdk-aac。需要注意的是,我們要在CFLAGS編譯選項中添加*-I參數(shù)來指明ffmpeg編譯x264和fdk-aac庫使用兩個外部庫的頭文件目錄,以及通過-L* 選項指明ffmpeg在鏈接外部庫時庫文件目錄。
配置完之后,我們通過make & make install進行編譯安裝。
編譯可能遇到錯誤:
錯誤一:
libavcodec/aaccoder.c: In function 'search_for_ms':
libavcodec/aaccoder.c:803:25: error: expected identifier or '(' before numeric constant
? ? ? ? ? ? ? ? ? ? ?int B0 = 0, B1 = 0;
? ? ? ? ? ? ? ? ? ? ? ? ?^
libavcodec/aaccoder.c:865:28: error: lvalue required as left operand of assignment
? ? ? ? ? ? ? ? ? ? ? ? ?B0 += b1+b2;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ^
libavcodec/aaccoder.c:866:25: error: 'B1' undeclared (first use in this function)
? ? ? ? ? ? ? ? ? ? ? ? ?B1 += b3+b4;
libavcodec/aaccoder.c:866:25: note: each undeclared identifier is reported only once for each function it appears in
make: *** [libavcodec/aaccoder.o] Error 1
解決:將libavcodec/aaccoder.c文件B0變量替換成b0
錯誤二:
libavcodec/hevc_mvs.c: In function 'derive_spatial_merge_candidates':
libavcodec/hevc_mvs.c:208:15: error: 'y0000000' undeclared (first use in this function)
? ? ? ? ? ? ?((y ## v) >> s->ps.sps->log2_min_pu_size))
? ? ? ? ? ? ? ?^
libavcodec/hevc_mvs.c:204:14: note: in definition of macro 'TAB_MVF'
? ? ?tab_mvf[(y) * min_pu_width + x]
? ? ? ? ? ? ? ^
libavcodec/hevc_mvs.c:274:16: note: in expansion of macro 'TAB_MVF_PU'
? ? ?(cand && !(TAB_MVF_PU(v).pred_flag == PF_INTRA))
? ? ? ? ? ? ? ? ^
libavcodec/hevc_mvs.c:368:23: note: in expansion of macro 'AVAILABLE'
? ? ?is_available_b0 = AVAILABLE(cand_up_right, B0) &&
? ? ? ? ? ? ? ? ? ? ? ?^
libavcodec/hevc_mvs.c:208:15: note: each undeclared identifier is reported only once for each function it appears in
解決: 將libavcodec/hevc_mvs.c文件的變量B0改成b0,xB0改成xb0,yB0改成yb0
錯誤三:
libavcodec/opus_pvq.c: In function ‘quant_band_template’:
libavcodec/opus_pvq.c:498:9: error: expected identifier or ‘(’ before
numeric constant
int B0 = blocks;
^ libavcodec/opus_pvq.c:559:12: error: lvalue required as left operand of assignment
B0 = blocks;
^ make: *** [libavcodec/opus_pvq.o] Error 1
解決:將libavcodec/opus_pvq.c文件的變量B0改成b0
解完所有錯誤后,重新編譯、安裝。看下編譯產物
lorien@ubuntu-20: cd /work/android/media/ffmpeg/install/ lorien@ubuntu-20: ls include lib share lorien@ubuntu-20: cd lib lorien@ubuntu-20: ls libavcodec.a libavformat.a libavutil.a libswresample.a libswscale.a pkgconfig
可以看到在lib下有一系列靜態(tài)庫文件。.a文件有點多,下面我們將所有.a文件打包一個so文件,方便后面引入,打包腳本如下:
#!/bin/bash export PATH=$PATH:/work/tmp/android-toolchain/bin SYSROOT=/work/tmp/android-toolchain/sysroot aarch64-linux-android-ld -rpath-link=-L$SYSROOT/usr/lib \ -L$SYSROOT/usr/lib \ -L$(pwd)/x264 \ -lc -lm -lz -ldl -lx264 \ -soname libffmpeg.so \ -shared \ -nostdlib \ -Bsymbolic \ --whole-archive \ --no-undefined \ -o libffmpeg.so \ libavcodec.a libavformat.a libavutil.a libswresample.a libswscale.a \ $(pwd)/fdk-aac/libfdk-aac.a \ $SYSROOT/../lib/gcc/aarch64-linux-android/4.9.x/libgcc.a
通過ld命令,將所有.a文件打包成libffmpeg.so文件。接下來再用strip命令優(yōu)化下so
lorien@ubuntu-20: aarch64-linux-android-strip libffmpeg.so
至此,libffmpeg.so庫文件就已經制作好了。
原文鏈接:https://blog.csdn.net/H_Zhang/article/details/123775275
相關推薦
- 2023-01-17 用Python實現(xiàn)的等差數(shù)列方式_python
- 2023-07-13 react中useState的基本用法
- 2022-09-07 python?sklearn?畫出決策樹并保存為PDF的實現(xiàn)過程_python
- 2022-06-23 .bat文件中start、pause、goto及rem的用法示例_DOS/BAT
- 2022-09-14 R語言ggplot2圖例標簽、標題、順序修改和刪除操作實例_R語言
- 2022-10-11 Spring Boot 使用@Scheduled注解實現(xiàn)定時任務
- 2023-04-12 Python?issubclass和isinstance函數(shù)的具體使用_python
- 2023-07-02 如何遠程使用服務器上的Jupyter?notebook_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支