FFmpeg----linux环境下编译Android使用的so

FFmpeg 是很强大的视频编解码开源库,所以这里先带来如何使用FFmpeg 进行打包出Android各个arm的so。
我这里采用的linux为centos。

下载Android的NDK

  • 要想打包出so,必不可少的就是NDK,因此我们需要在linux下进行配置我们的NDK环境。
 wget -c https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip

当然你也可以通过ANDROID NDK DOWNLOAD PAGE进行下载zip后,再拖到你的linux里。

  • 下载完成后我们进行解压工作
unzip android-ndk-r15c-linux-x86_64.zip

当然这里我们是其解压到当前目录。

  • 接下来就是环境变量的配置(因为以后可能还要使用,就不采用临时环境)
# 首先查看当前android-ndk-r15c-linux-x86_64 的路径
pwd

# 进行设置我们的环境变量
vi /etc/profile
  • 我们打开编辑器后,我们需要在profile 的末尾增加以下内容,并进行保存。
export ANDROID_NDK=/root/ffmepg/ndk/android-ndk-r15c
export PATH=$ANDROID_NDK:$PATH
  • 使我们的环境变量立刻生效
source /etc/profile
  • 测试时候生效
echo $ANDROID_NDK

看到我们配置的路径后,就宣告配置成功。


FFmpeg的下载

首先我们进行下载我们的FFmpeg

 git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg

这里采用官方提供的方式(需要事先安装git环境yum install -y git

cd ffmpeg

# checkout出最新的版本
git checkout n3.3

你也可以选择你喜欢的版本。


FFmpeg的编译

  • 在编译钱我们需要修改FFmpeg的configure中的内容(因为默认打出的so 会追加版本号 例如:libavcodec.so.5.100.1),但是android 平台不认识 ,因此
    需要将下面内容进行更改,
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)'  
  • 在你的ffmpeg目录下创建FFmpeg-android.sh的脚本
# 清空上次的编译
make clean

# 设置你的NDK目录
export NDK=/root/ffmepg/ndk/android-ndk-r15c   
export PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt
# 设置你的android平台编译器的版本 这里采用Android4.0 
export PLATFORM=$NDK/platforms/android-14/arch-arm  
# 生成文件的目录
export PREFIX=$(pwd)/android/arm

# 函数 用来执行配置命令
build_one(){  
  ./configure --target-os=linux --prefix=$PREFIX \  
--enable-cross-compile \  
--enable-runtime-cpudetect \  
--disable-asm \  
--arch=arm \  
--cc=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi-gcc \  
--cross-prefix=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi- \  
--disable-stripping \  
--nm=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi-nm \  
--sysroot=$PLATFORM \  
--enable-gpl --enable-shared --disable-static --enable-small \  
--disable-ffprobe --disable-ffplay --disable-ffmpeg --disable-ffserver --disable-debug \  
--extra-cflags="-fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a"   
}  
# 调用函数
build_one  
# makefile进行编译  
make  
make install  
  • 进行赋予执行权限 并执行
chmod +x FFmpeg-android.sh

./FFmpeg-android.sh
  • 之后你就可以在你的ffmpeg的/android/arm目录下拿到我们要的arm平台的so。供您Android NDK的开发

或者你可以直接使用命令行


./configure --enable-shared --disable-static --disable-doc --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver --disable-avdevice --disable-symver --target-os=linux --arch=arm --enable-cross-compile --extra-cflags="-Os -fpic -marm" --cross-prefix=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi- --sysroot=$NDK/platforms/android-21/arch-arm/ --prefix=$(pwd)/android/arm &&make &&make install


编译时候出现的错误

  • WARNING: /root/ffmepg/ndk/android-ndk-r15c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-pkg-config not found, library detection may fail.
    这里写图片描述

不需要管,直接make进行编译就可以了。


  • C compiler test failed
    这个问题 可以进行cat /ffbuild/config.log进行查看到底是那个参数错误,导致的。更改为正确参数内容后,即可通过。

比如:
这里写图片描述

这里是因为我的环境变量配置错误,导致找不到这个路径下的这个文件,更正Android_NDK的环境变量后正常。


  • libavcodec/aaccoder.c: In function 'search_for_ms':
    libavcodec/aaccoder.c:803:25: error: expected identifier or '(' before numeric constant
    libavcodec/aaccoder.c:865:28: error: lvalue required as left operand of assignment
    libavcodec/aaccoder.c:866:25: error: 'B1' undeclared (first use in this function)
    libavcodec/aaccoder.c:866:25: note: each undeclared identifier is reported only once for each function it appears in
    ffbuild/common.mak:60: recipe for target 'libavcodec/aaccoder.o' failed
    make: *** [libavcodec/aaccoder.o] Error 1

这种问题是由于/usr/arm-linux-androideabi/include/asm/termbits.h文件中已经宏定义了。

#define B0 0000000  

导致int B0 = 0, B1 = 0;实际上变成了int 0000000 = 0, B1 = 0;导致的。

因此可以更改有问题的头文件:

ffmpeg/libavcodec/aaccoder.c
libavcodec/avcodec.h file includes:
libavutil/samplefmt.h file includes:
libavutil/avutil.h file includes:
libavutil/common.h file includes:
libavutil/internal.h file includes:
libavutil/timer.h file includes:

将B0改成bo

还有一种方法:由于选择的版本问题。因此你可以checkout低版本的FFmpeg来绕开这个问题。

猜你喜欢

转载自blog.csdn.net/qq_15807167/article/details/79379964