1. Configure the NDK environment

Download the NDK path: developer. The android. Google. Cn/the NDK/downloa…

Start configuring the environment (this tutorial is done on MAC OS) :

Open the terminal and enter the following commands to open the environment profile: bash_profile:

   touch ./.bash_profile
   open ./.bash_profile
Copy the code

After opening the.bash_profile file, configure the NDK environment variable as follows:

Export NDK_HOME = / Users/XHKJ/Library/Android/SDK/the NDK / 20.0.5594570 export PATH = ${PATH} : ${NDK_HOME}Copy the code

NDK_HOME: indicates the root directory of the NDK. I’m going to do it based on my actual path. After writing successfully, remember to save!!

Next, we need to configure the NDK environment to take effect. Therefore, we need to enter the following command line on the terminal:

source ./.bash_profile
Copy the code

At this point, the NDK environment is configured.

2. Start ffMPEG compilation

Let’s download ffMPEG source code from Github: github.com/FFmpeg/FFmp…

After downloading the source code, we need to modify some parameters in the configure file configuration. If left unmodified, the version number of the compiled dynamic library file name will appear after.so (for example, “libavcodec.so.5.100.1”). This name is not recognized on The Android platform, so you need to change it. Here, we go directly to the configure file in the FFmpeg root directory and make the following changes:

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)'Copy the code

And then there’s the tricky part… Build_android. sh file here to provide a sample, specific some of the contents of the inside is according to the configure file configuration decision, everyone later have what special needs to check configure themselves, there is what kind of position can unlock, depending on you! Hey hey

#! / bin/bash the NDK = / Users/XHKJ/Library/Android/SDK/the NDK / 20.0.5594570 TOOLCHAIN_ROOT_DIR = Darwin - x86_64 TOOLCHAIN = $the NDK/toolchains/LLVM/prebuilt / $TOOLCHAIN_ROOT_DIR/API = 29 # to compile ffmpeg content method 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 ARCH=arm64 ARCH= armv8-a CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++ SYSROOT=$NDK/toolchains/llvm/prebuilt/$TOOLCHAIN_ROOT_DIR/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/$TOOLCHAIN_ROOT_DIR/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/$TOOLCHAIN_ROOT_DIR/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/$TOOLCHAIN_ROOT_DIR/sysroot CROSS_PREFIX = $TOOLCHAIN/bin/x86_64 - Linux - android -- PREFIX = $(PWD)/android / $CPU OPTIMIZE_CFLAGS = "- march = $CPU - msse4.2 -mpopcnt -m64 - mtune=intel" build_androidCopy the code

⚠ ️ note: In addition to changing the NDK path, TOOLCHAIN can also be fixed, depending on the version of the NDK. Here is a more accurate way to create a new NDK project, and then check the CMakeCache. TXT file in the debug folder. It can be found as follows:

The red box is the relative path for the TOOLCHAIN variable.

The last step is to execute the build_android.sh script file on the terminal.

The resulting so library will be in the android folder in the current directory.

Now that ffMPEG compilation is complete, all we need to do is put the corresponding ABI’s so library and call h header files into the project, and we can have fun developing audio and video!