preface

Audio and video development is almost inseparable from FFmpeg, engaged in audio and video development also has a period of time, recently finally have time to reorganize the relevant knowledge, the first article from FFmpeg compilation began. What a pity for Thor. God is jealous of talent. While we are busy with our work and study, we should also pay attention to our health.

The preparatory work

Compile environment:

  • Ubuntu 20.04.2 LTS
  • Ffmpeg – 4.2.2
  • android-ndk-r21e-linux-x86_64

compile

Once the FFmpeg and NDK have been downloaded and unzipped, you can officially begin the compilation process.

  • The first step

    Switch to the ffmpeg directory

CD ffmpeg - 4.2.2Copy the code

You can see the source directory structure of FFmpeg:

  • The second step

    Compile the build_android_clang.sh script:

#! /bin/bashExport the NDK = / usr/work/the NDK/android - the NDK - r21e # here your the NDK directory path configuration TOOLCHAIN = $the NDK/toolchains LLVM/prebuilt/Linux - x86_64 function build_android { ./configure \ --prefix=$PREFIX \ --enable-neon \ --enable-hwaccels \ --disable-postproc \ --disable-debug \ --enable-small \ --enable-jni \ --enable-mediacodec \ --enable-decoder=h264_mediacodec \ --enable-static \ --enable-shared \ --disable-doc \ --enable-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" make clean make -j16 make install echo "============================ build android $CPU success ==========================" }
#arm64-v8a
ARCH=arm64
CPU=armv8-a
API=21
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
API=21
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
Copy the code

The various compilation parameters can be viewed using./configure –help. I have compiled the dynamic library and the static library separately, and you can change them according to your needs *–enable-static and –enable-shared*

  • The third step

    Add executable permission to the step first

    chmod +x build_android_clang.sh
    Copy the code

    To start running the compile script:

    ./build_android_clang.sh
    Copy the code

    Wait for some time, and when the compilation is complete, it looks like this:

    At this point we CD to the Android directory and see the generated library file:

    At this point, the FFmpeg compilation process is basically over, and you can officially start audio and video development.

  • other

    The current compiled result is multiple separate library files, as shown below:

    You can also use libffmpeg.so to package multiple static libraries into one dynamic library and modify the compile script:

    --enable-static
    --disable-shared
    Copy the code

    The package script is as follows:

    Export NDK=/usr/work/ NDK /android-ndk-r21e PLATFORM = $the NDK/platforms/android - 21 / arch - arm TOOLCHAIN = $the NDK/toolchains/arm - Linux - androideabi - 4.9 / prebuilt/Linux - x86_64 PREFIX=$(pwd)
    $TOOLCHAIN/bin/arm-linux-androideabi-ld \
    -rpath-link=$PLATFORM/usr/lib \
    -L$PLATFORM/usr/lib \
    -L$PREFIX/lib \
    -soname libffmpeg.so -shared -nostdlib -Bsymbolic --whole-archive --no-undefined -o \
    $PREFIX/libffmpeg.so \
        libavcodec.a \
        libavfilter.a \
        libswresample.a \
        libavformat.a \
        libavutil.a \
        libpostproc.a \
        libswscale.a \
        -lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker \
        $TOOLCHAIN/ lib/GCC/arm - Linux - androideabi / 4.9 x/libgcc \ aEcho "finish compiling ffMPEG so"Copy the code

    Then go to the corresponding static library directory to execute the packaging script, and I won’t go into detail. The libffmpeg.so dynamic library file is generated.

reference

www.jianshu.com/p/0792f6bbc… Blog.csdn.net/u010231682/…