preface

Ffmpeg is a set of open source computer programs that can be used to record, convert digital audio and video, and convert them into streams. It is very important in the field of audio and video. As an Android developer, compiling using FFMPEG is an essential step in getting your foot in the door of audio and video. The following takes you step by step to implement ffMPEG compilation. Before this, you need to prepare a Linux environment system, such as Ubuntu, Mac, you can get a cloud server, or you can install a virtual machine, the reader decides. NDK and FFMPEG are compiled using android-NDK-R16B and FFMPEG are compiled using FFMPEG-3.3.9. Readers are advised to use the version used in the above article, other versions will encounter other problems, and the compilation process is also different from this article.

1. Configure environment variables

After downloading the NDK and FFmpeg Linux versions, place the decompressed FFmpeg to the /lib directory, create the NDK directory under /lib, decompress the NDK to the /lib/ NDK directory, and configure the NDK environment variables.

To configure NDK environment variables, you need to configure them in /etc/profile

vegen@vegen-virtual-machine:/lib/ NDK /android-ndk-r16b$sudo vim /etc/profile [sudo] vegenCopy the code

Enable the file editing mode and configure the NDK path at the end

/etc/profile

Configure the NDK path at the end
export NDK_PATH=/lib/ndk/android-ndk-r16b
export PATH=$NDKPATH:$PATH
Copy the code

Press Esc and enter :wq to save the configuration

Then refresh the system environment and run the echo $NDK_PATH command to view the NDK path. If the command output is as follows, the configuration is successful

vegen@vegen-virtual-machine:/lib/ndk/android-ndk-r16b$ source /etc/profile
vegen@vegen-virtual-machine:/lib/ndk/android-ndk-r16b$ echo $NDK_PATH
/lib/ndk/android-ndk-r16b

Copy the code

2. Write the FFMPEG configuration script

Vegen @ vegen -- virtual machine: / lib/ffmpeg - 3.3.9 $vim ffmpeg_build. ShCopy the code

ffmpeg_build.sh

# Script for compiling android platform
#! /bin/bash
. /etc/profile

# Define several variables
ARCH=arm
CPU=armv7-a
PREFIX=$(pwd)/android/$ARCH/$CPU
ANDROID_TOOLCHAINS_PATH=$NDK_PATH/android-toolchains/android-19/arch-arm
CROSS_PREFIX=$ANDROID_TOOLCHAINS_PATH/bin/arm-linux-androideabi-
SYSROOT=$ANDROID_TOOLCHAINS_PATH/sysroot

 build() {Configure file
        ./configure --prefix=${PREFIX} \
        --enable-gpl \
        --disable-static \
        --enable-shared \
        --enable-small \
        --disable-programs \
        --disable-ffmpeg \
        --disable-ffplay \
        --disable-ffprobe \
        --disable-ffserver \
        --disable-doc \
        --arch=$ARCH \
        --cpu=$CPU \
        --cross-prefix=${CROSS_PREFIX} \
        --enable-cross-compile \
        --sysroot=$SYSROOT \
        --target-os=linux \
        --extra-cflags="-fpic" \

        The clean command in the makefile is executed
        make clean
        # run Makefile
        make
        Install to the specified prefix directory
        make install
        # make clean
}


Execute the build function
build

Copy the code

Ffmpeg engineering code is a lot of code, sometimes we just need some not too complicated audio and video processing, then we can choose to compile according to the needs.

3. Modify the configure file of ffmpeg

Vegen @ vegen -- virtual machine: / lib/ffmpeg - 3.3.9 $vim configureCopy the code

Enter /SLIBNAME_WITH_MAJOR= in configure to search and press n to find the following code segment

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

Modify the code snippet above to the following snippet

# 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

After saving and exiting, you need to install GCC and yASM

vegen@vegen-virtual-machine:/lib/ ffmPEG-3.3.9 $sudo apt install GCC vegen@vegen-virtual-machine:/lib/ ffmPEG-3.3.9 $sudo apt install yasmCopy the code

After the installation is complete, run the following command

Vegen @ vegen - virtual machine: / lib/ffmpeg - 3.3.9 $sudo. / configureCopy the code

4. Execute the compilation script

Vegen @ vegen -- virtual machine: / lib/ffmpeg - 3.3.9 $. / ffmpeg_build. ShCopy the code

At this point, ffmPEG compilation is complete, if successful, you should see the Android directory and the following SO libraries, then we’ll introduce the required SO libraries and include files into our project and have fun developing.

5. The latter

Compilation of FFMPEG process, may encounter a lot of problems, readers need to check the code is not wrong, the relevant procedures are not installed, such as Linux foundation, need to learn by yourself. If you have any questions, you can comment on the feedback below, behind will launch some opportunities FFMPEG audio and video processing articles, please look forward to.