The original article was first published on wechat public account: Audio and video development progress

A few days ago, I posted an article about FFmpeg calling Android MediaCodec for hard decoding. The technical points above are not too difficult, and it is also a common interface operation to call FFmpeg. The important thing is that FFmpeg version selection and compilation options require MediaCodec to be enabled.

On FFmpeg compilation, is a platial topic, many beginners will be stuck in how to compile the dynamic library so, which is actually a big obstacle for Android development to audio and video, a line of FFmpeg code has not had time to write it, will have to do STH for a long time to compile the problem.

Of course, the compilation trouble is FFmpeg’s fault. As it continues to be updated from 2.x to 4.x, the call interface has changed and the build options have changed a lot. However, the various Android So dynamic library articles on the web have not been updated. Some tutorials are still in the 2.x version. There are bound to be compatibility issues.

A few examples:

  • FFmpeg source inside the file to modify?

Early builds required some source code modifications in FFmpeg, the most common being the following code:


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)'

Copy the code

It used to be necessary to change the concatenation of compiled library names, but this is not the case in the latest release (which refers to the RELEASE /4.4 branch of FFMPEG).

In addition, there is no need to modify any FFmpeg source code in the actual compilation.

  • Which version of Android NDK should I use?

This is also a common compatibility issue.

The NDK r18B version of the NDK is the same as the NDK r18B version of the NDK. The NDK r18B version is the same as the NDK r18B version of the NDK.

NDK revision history

So now the latest dynamic library compilation is done in Clang, so to keep up with The Times, you should not use the previous NDK version and go straight to the latest one.


To avoid the hassle of FFmpeg compilation, just give a Github Repo and put all the compiled scripts in that repository.

The address is as follows:

Github.com/glumes/ffmp…

FFmpeg source code is loaded as a submodule of the warehouse in the form of the Submodules, in the download should pay attention to:


git clone --recursive https://github.com/glumes/ffmpeg_android

Copy the code

Once downloaded, go to the build_android.sh file and replace the NDK with your own path, preferably with the R20B version as well, to be consistent.

#! / bin/bash # you the NDK path the NDK = / Users/glumes/Downloads/android - the NDK - r20b TOOLCHAIN = $the NDK/toolchains LLVM/prebuilt/Darwin - x86_64 API=21

Copy the code

The compiled dynamic libraries are in the ffmpeg_library folder. So far, only armeabi-v7A has been compiled.

Android folder in the corresponding load so android project, is also FFmpeg call Android MediaCodec source.

The project directory also does not need to modify FFmpeg, and many of the FFmpeg compilation options are placed in the config-env.sh directory, which can be changed in the file if necessary without affecting the main compilation script.

By the way, post the source code:

Here is the function that executes the compilation. Some of the parameters used in the function are defined outside:


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 #"The Compilation of FFmpeg for $CPU is completed"

}

Copy the code

Now that you have defined the relevant parameters, you can execute:


// The compiled configure executable is in the ffmpeg source directory

cd ffmpeg

// Define the architecture of the compilation

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/darwin-x86_64/sysroot"

PREFIX="${PWD}/.. /ffmpeg_library/android/$OUTPUT_FOLDER"

LIB_DIR="${PWD}/.. /ffmpeg_library/android/libs/$OUTPUT_FOLDER"

OPTIMIZE_CFLAGS="-march=$CPU"

build_android

Copy the code

Since our compiled script is not in the FFmpeg source directory, we need to make some changes to the path so that absolutely nothing in FFmpeg is changed.

If you have any problems during the compilation process, please feel free to contact me in the comments section. Please make adjustments to ensure that the compilation process is easy.