Android使用NDK(r20)交叉编译FFmpeg

参考https://blog.csdn.net/listener51/article/details/81605472
参考https://juejin.im/post/5d831333f265da03c61e8a28?utm_source=gold_browser_extension

可以在Ubuntu下编译,也可以在Windows下编译,大同小异,主要是编译脚本。我是在Windows下用msys2编译的FFmpeg。

  1. 下载msys2,官网地址http://www.msys2.org/

  2. FFmpeg官网下载源码,然后在源码文件夹下编写build_android.sh脚本

#!/bin/bash
NDK=F:/Sdk/ndk-bundle
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/windows-x86_64
API=29

function build_android
{
echo "Compiling FFmpeg for $CPU"
./configure \
    --prefix=$PREFIX \
    --disable-neon \
    --disable-hwaccels \
    --disable-gpl \
    --disable-postproc \
    --enable-shared \
    --enable-jni \
    --disable-mediacodec \
    --disable-decoder=h264_mediacodec \
    --disable-static \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-avdevice \
    --disable-doc \
    --disable-symver \
    --cross-prefix=$CROSS_PREFIX \
    --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" \
    $ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
echo "The Compilation of FFmpeg for $CPU is completed"
}

#armv8-a
ARCH=arm64
CPU=armv8-a
CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=$CPU"
build_android

#armv7-a
ARCH=arm
CPU=armv7-a
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
CROSS_PREFIX=$TOOLCHAIN/bin/arm-linux-androideabi-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
build_android

#x86
ARCH=x86
CPU=x86
CC=$TOOLCHAIN/bin/i686-linux-android$API-clang
CXX=$TOOLCHAIN/bin/i686-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/i686-linux-android-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
build_android

#x86_64
ARCH=x86_64
CPU=x86-64
CC=$TOOLCHAIN/bin/x86_64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/x86_64-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/x86_64-linux-android-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=$CPU -msse4.2 -mpopcnt -m64 -mtune=intel"
build_android
  1. 运行build_android.sh文件


    clipboard.png

2020-03-23补充

交叉编译环境:

  1. Ubuntu18.0.4
  2. ndkr21
  3. Ffmpeg4.2.2

由于自己只能看懂简单得C++语法,而且不同NDK版本编译FFmpeg有差异,不考虑优化编译,编译参数尽量使用默认,编译出Android能使用的库

参考https://www.jianshu.com/p/94bb964b4305

image.png

image.png

发现clang不但和系统架构有关系,还和android版本有关系。而ar等命令只和系统架构有关系,所以我们单独配置--cc

./configure --prefix=abc 
--enable-cross-compile 
--target-os=android 
--arch=arm 
--extra-cflags=-D__ANDROID_API__=21
--cc=/home/guozengjie/Android/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi29-clang 
--cross-prefix=/home/guozengjie/Android/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-

需要配置-D__ANDROID_API__=21,否则报错找不到stderr
导入Android Studio

image.png

image.png

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}")
target_link_libraries( # Specifies the target library.
        native-lib
        avutil avformat avcodec avfilter  swresample swscale
        ${log-lib})

ffmpeg各个库文件的顺序不同可能会报错,因为各个库之间有依赖

另外可以参考ninja文件中的编译参数


image.png

总结:编译过程肯定不是最优,只是记录自己交叉编译的过程。以后遇到问题,再记录。

2020/4/18更新

编译成功

#!/bin/bash

API=29

ARCH=x86

PREFIX=./ABC

TOOLCHAIN=/home/guozengjie/Android/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64

build()
{
./configure \
--prefix=$PREFIX \
--disable-static \
--enable-shared \
--enable-small \
--disable-doc \
--disable-programs \
--disable-avdevice \
--enable-cross-compile \
--target-os=android \
--arch=$ARCH \
--pkg-config=/usr/bin/pkg-config \
--cc=$TOOLCHAIN/bin/i686-linux-android$API-clang \
--cross-prefix=$TOOLCHAIN/bin/i686-linux-android- \
--disable-asm

make clean
make
make install

}

build

导入AS

#设置头文件目录
include_directories(include)
#设置库文件目录
link_directories(../jniLibs/${ANDROID_ABI})
target_link_libraries( # Specifies the target library.
        native-lib
        avformat avcodec avfilter avutil swresample swscale
        log
        z)
externalNativeBuild {
            cmake {
                cppFlags ""
                abiFilters "x86"
            }
        }

若报错
has text relocations
增加编译选项

--disable-asm

你可能感兴趣的:(Android使用NDK(r20)交叉编译FFmpeg)