用ffmpeg源碼編譯適用Android平臺的庫

一、linux環境編譯 (ubuntu1604_64)

1)下載FFmpeg源碼

    https://ffmpeg.org/download.html

    下載最新的版本,ffmpeg-x.x.x.tar.gz,再通過命令解壓:tar -xjvf ffmpeg-x.x.x.tar.bz2

    或者git拉下來,git clone "https://git.ffmpeg.org/ffmpeg.git" ffmpeg

2)下載NDK

    https://developer.android.google.cn/ndk/downloads/index.html

    可以點擊下載,也可以終端:wget https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip

    最後都用uzip命令解壓到當前目錄。

    可以下低一點的版本比如r14b,但是編譯時有問題,我編譯的時候下載的是17c,但是還是會遇到問題,後面會一一列舉。

    更高版本的就不知道了,沒試過最新的怎麼編。

PS:爲了方便管理我都是把解壓好放到一個目錄下,比如:

         ls ~/ffmpeg

         android-ndk-r17c  ffmpeg-4.2.1

3)修改配置

     目的是編譯android可用的動態庫so。

    1.進入ffmpeg目錄

        cd ~/ffmpeg/ffmpeg-4.2.1/

     2.修改configure

SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR) $(SLIBNAME)'

        修改成

SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'

        這樣生成的動態庫命名方式可以被Android平臺識別。

    3.創建build_android.sh腳本

       在configure同級目錄下,新建腳本build_android.sh,加入如下內容:

#!/bin/bash
set -x
API=28
NDK=D:/NDK/android-ndk-r17c
SYSROOT=$NDK/platforms/android-$API/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
function function_one
{
./configure \
  --prefix=$PREFIX \
  --disable-shared \
  --enable-static \
  --disable-doc \
  --disable-ffplay \
  --disable-ffprobe \
  --disable-symver \
  --disable-ffmpeg \
  --cc=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \
  --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
  --target-os=linux \
  --arch=arm \
  --enable-cross-compile \
  --sysroot=$SYSROOT \
  --extra-cflags="-I$NDK/sysroot/usr/include/arm-linux-androideabi -isysroot $NDK/sysroot -fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a" \
  --enable-neon

make clean all
make 
make install
}
CPU=armv7-a
PREFIX=$(pwd)/android/$CPU
function_one

改完之後,修改build_android.sh權限爲777:chmod 777 build_android.sh

然後運行腳本build_android.sh:./build_android.sh

 

二、windows環境編譯(win10)

    1)下載FFmpeg源碼

        https://ffmpeg.org/download.html

    2)下載NDK

        https://developer.android.google.cn/ndk/downloads/index.html

    3)下載MinGW

         https://sourceforge.net/projects/mingw/files/

        FFmpeg和DNK的下載和linux一樣的,MinGW是爲了代替cmd。可以類似linux的環境執行命令。

        下載後解壓出來,再放到同一個目錄下,比如我的都放D盤。

    4)配置系統環境參數

        在我的電腦--屬性--高級系統設置--環境變量--系統變量--Path,添加以下幾個路徑:

        D:\MinGW\bin

        D:\MinGW\msys\1.0\bin

        D:\NDK\android-ndk-r17c

        一路確定返回。win+R,輸入cmd,回車調出終端。分別輸入ndk-build和gcc -v來驗證配置成功。

    5)修改配置

       1. 和linux的一樣,進入ffmpeg的目錄,修改configure和新增build_android.sh

       2.進入目錄D:\MinGW\msys\1.0,雙擊打開msys.bat,就能像linux那樣執行bash命令;

            cd /d/ffmpeg/ffmpeg-4.2.1

            chmod 777 build_android.sh

            ./build_android.sh

 

↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ 上面是下載源文件和配置 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

 

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 下面是編譯時遇到的問題和解決方法 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

1.提示type.h找不到

/NDK/android-ndk-r17c/sysroot/usr/include/linux/types.h:21:23: fatal error: asm/types.h: No such file or directory
 #include <asm/types.h>

查看中build_android.sh中--extra-cflags=中有沒有加上了 -isysroot $NDK/sysroot 

 

2.提示變量名B0出問題

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中的所有變量B0都改成b0

 

3.提示y0000000' undeclared

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文件的所有變量B0改成b0,xB0改成xb0,yB0改成yb0

 

4.提示變量名B0出問題

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中所有變量B0都改成b0

 

基本經歷過這幾個錯誤之後就能正常編完了,編好的庫在ffmpeg-4.2.1\android\armv7-a\lib下。

 

參考:

https://blog.csdn.net/qq_38261174/article/details/83210458

https://blog.csdn.net/qq_34902522/article/details/87879145

http://alientechlab.com/how-to-build-ffmpeg-for-android/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章