资讯详情

使用NDK20编译Android平台的ffmpeg

使用NDK编译Android平台的ffmpeg

  • 编译环境
    • 安装
    • 编译
    • 避坑
    • 如何添加h264支持

编译环境

注意选择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=KaTeX parse error: Undefined control sequence: \ at position 4: CC \̲ ̲ --cxx=CXX \ 两个属性 并参照以上脚本填写参数

  2. 编译显示

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支持

标签: 6187r20kl1单圈电位器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台