使用NDK20编译Android平台的ffmpeg

使用NDK编译Android平台的ffmpeg

编译环境

注意选择Linux版本的包下载

Ubuntu 16 下载
ndk20 下载
ffmpeg4.3.4 下载

安装

编译

参考:https://blog.csdn.net/qq_28478281/article/details/104000015

  1. 将ndk 以及ffmpeg 分别解压 路径随意,配置环境也不是必要的。
#!/bin/bash

# 这个对应的是ndk的解压路径
NDK=/opt/ndk/android-ndk-r20b

# darwin-x86_64是mos版本的路径,Linux 用linux-x86_64
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
API=21

function build_android
{
echo "Compiling FFmpeg for $CPU"
./configure \
    --prefix=$PREFIX \
    --libdir=$LIB_DIR \
    --enable-shared \
    --disable-static \
    --enable-jni \
    --disable-doc \
    --disable-symver \
    --disable-programs \
    --target-os=android \
    --arch=$ARCH \
    --cpu=$CPU \
    --cc=$CC \
    --cxx=$CXX \
    --enable-cross-compile \
    --sysroot=$SYSROOT \
    --extra-cflags="-Os -fpic $OPTIMIZE_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS" \
    --disable-asm \
    $COMMON_FF_CFG_FLAGS
make clean
make -j8
make install
echo "The Compilation of FFmpeg for $CPU is completed"
}

# armv8-a
source "config-env.sh"
OUTPUT_FOLDER="arm64-v8a"
ARCH=arm64
CPU="armv8-a"
TOOL_CPU_NAME=aarch64
TOOL_PREFIX="$TOOLCHAIN/bin/$TOOL_CPU_NAME-linux-android"

CC="$TOOL_PREFIX$API-clang"
CXX="$TOOL_PREFIX$API-clang++"
SYSROOT="$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot"
PREFIX="${PWD}/android/$OUTPUT_FOLDER"
LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
OPTIMIZE_CFLAGS="-march=$CPU"
build_android

# # armv7-a
# source "config-env.sh"
# OUTPUT_FOLDER="armeabi-v7a"
# ARCH="arm"
# CPU="armv7-a"
# TOOL_CPU_NAME=armv7a
# TOOL_PREFIX="$TOOLCHAIN/bin/arm-linux-androideabi"

# CC="$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang"
# CXX="$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++"
# SYSROOT="$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot"
# PREFIX="${PWD}/android/$OUTPUT_FOLDER"
# LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
# OPTIMIZE_CFLAGS="-march=$CPU"
# build_android

# x86
# source "config-env.sh"
# OUTPUT_FOLDER="x86"
# ARCH="x86"
# CPU="x86"
# TOOL_CPU_NAME="i686"
# TOOL_PREFIX="$TOOLCHAIN/bin/${TOOL_CPU_NAME}-linux-android"

# CC="$TOOL_PREFIX$API-clang"
# CXX="$TOOL_PREFIX$API-clang++"
# SYSROOT="$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot"
# PREFIX="${PWD}/android/$OUTPUT_FOLDER"
# LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
# OPTIMIZE_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
# build_android

# x86_64
# source "config-env.sh"
# OUTPUT_FOLDER="x86_64"
# ARCH="x86_64"
# CPU="x86-64"
# TOOL_CPU_NAME="x86_64"
# TOOL_PREFIX="$TOOLCHAIN/bin/${TOOL_CPU_NAME}-linux-android"

# CC="$TOOL_PREFIX$API-clang"
# CXX="$TOOL_PREFIX$API-clang++"
# SYSROOT="$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot"
# PREFIX="${PWD}/android/$OUTPUT_FOLDER"
# LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
# OPTIMIZE_CFLAGS="-march=$CPU"
# build_android

避坑

  1. 编译报错:……………………/bin/arm-linux-androideabi-gcc is unable to create an executable file.
    答疑:ndk版本太高,gcc编译在早期ndk版本支持,但是18b之后就弃用了,采用clang方式。编译配置中添加一下两行(两个属性 参照以上脚本填写参数 ):
	--cc=$CC \
	--cxx=$CXX \ 
  1. 编译显示
Unknown option "".
See ./configure --help for available options.

答疑:脚本的编码格式有错,建议别直接复制文件,可以新建一个sh脚本,再复制内容。

  1. 编译显示
 error: can't create dynamic relocation R_386_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

答疑:网上查到的资料比较少,我猜测是Ubuntu版本太高,我在使用ubuntu18的时候就出现这个问题,后来用16就没有。有知道原因的麻烦评论吱一声。

  1. 编译路径错误
Compiling FFmpeg for x86
/opt/ndk/android-ndk-r20b/toolchains/llvm/prebuilt/darwin-x86_64/bin/i686-linux-android21-clang is unable to create an executable file.
C compiler test failed.

If you think configure made a mistake, make sure you are using the latest
version from Git.  If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.libera.chat.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.

答疑: 这种情况最多的是ndk 版本和脚本中输入的路径不一致,导致找不到某个文件,第一次编译可能还会出现 config.mak 找不到的情况,具体可以根据脚本对照ndk是否有对应的路径。

  1. 编译中出现:strip: Unable to recognise the format of the input file " xxxx/libavdevice.so"
    答疑:strip是有关去除符号表的选项,可以添加–disable-stripping 选择项指明希望不去除符号表。也可以添加–disable-avdevice选项表示不编译libavdevice.so这个模块。 随后网上的参考解决方案是在ffbuild路径下的config.mak 修改 STRIP=xxx .这里的xxx是指ndk 目录下的android-ndk-r20b/toolchains/llvm/prebuilt/linux-x86_64/bin中的xxx-linux-androideabi-strip.

6 编译报错:

clang is unable to create an executable file.
If clang is a cross-compiler, use the --enable-cross-compile option.
Only do this if you know what cross compiling means.
C compiler test failed.

If you think configure made a mistake, make sure you are using the latest
version from Git.  If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.libera.chat.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.

答疑: 最朴素的原因是没有安装 gcc .但是一般不是。这这里遇到的情况是 在bash函数中不合理的穿插了了注释,例如:

function build_android
{
echo "Compiling FFmpeg for $CPU"
./configure \
    --prefix=$PREFIX \
    --libdir=$LIB_DIR \
    --enable-shared \
    --disable-static \
    --enable-jni \
    --disable-doc \
    --disable-symver \
    --disable-programs \
    --target-os=android \
#    --disable-stripping \  这这里的注释不能穿插在这里,会识别不出最好不要在./configure 指令中添加注释
    --disable-avdevice \
    --arch=$ARCH \
    --cpu=$CPU \
    --cc=$CC \
    --cxx=$CXX \
    --enable-cross-compile \
    --sysroot=$SYSROOT \
    --extra-cflags="-Os -fpic $OPTIMIZE_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS" \
    --disable-asm \
    $COMMON_FF_CFG_FLAGS
make clean
make -j8
make install
}

如何添加h264支持

参照这个:ffmpeg添加h264支持

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Griza_J

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值