How to build FFmpeg for Mac

This article mainly introduces the compilation of FFmpeg to generate so library in Mac environment.

Introduction of FFmpeg

FFmpeg is not only an audio and video codec tool, but also a set of audio and video codec development kit. As a codec development kit, it provides developers with rich audio and video processing call interfaces. FFmpeg provides a variety of media formats to encapsulate and unencapsulate, including a variety of audio and video coding, a variety of protocols for streaming media, a variety of color format conversion, a variety of sampling rate conversion, a variety of code rate conversion. FFmpeg framework provides a variety of rich plug-in modules, including encapsulation and unencapsulation plug-in, encoding and decoding plug-in, etc.

The development environment

The environment for this compilation is as follows:

  • MacOS High Sierra (10.13.5)
  • FFmpeg source (3.3.7)
  • ndk(android-ndk-r14b)

Download the NDK first. It is recommended not to use the ndK-bundle that comes with Android Studio. Then configure the NDK global environment and add the following configuration to.bash_profile:

export ANDROID_NDK_ROOT=/Users/jiangshuaijie/android-ndk-r14b/build
export PATH=${PATH}:${ANDROID_NDK_ROOT}
Copy the code

Then, enter the COMMAND line interface (CLI) to check whether the test environment is configured successfully.

Download FFmpeg source code

FFmpeg source code can be downloaded at https://ffmpeg.org/download.html#releases, Can also use the git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg cloning to the local. It is recommended to download the 3.3.x version of the library, the latest library compilation problems are more, various paths are not normal and other problems.

Modifying the configure file

After downloading the FFmpeg source code, you first need to modify the configure file in the source code. Because the compiled dynamic library file name has a version number after. So (for example, “libavcodec.so.5.100.1”) and the Android platform does not recognize such a file name, you need to change the file name. Find the following lines of code in the configure file:

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

replace

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

Write scripts to generate class libraries

Create a build_android.sh script in ffmpeg with the following permissions:

#! /bin/bash

make clean
#The PATH of the NDK can be set based on the installation locationExport TMPDIR = / Users/jiangshuaijie/ffmpeg - 3.3.7 / ffmpeg_install export the NDK = / Users/jiangshuaijie/android - the NDK - r14b export SYSROOT=$NDK/platforms/android-21/arch-arm/ export TOOLCHAIN = $the NDK/toolchains/arm - Linux - androideabi - 4.9 / prebuilt/Darwin - x86_64 export = arm CPU export PREFIX=$(pwd)/android/$CPU export ADDI_CFLAGS="-marm" function build_one { ./configure \ --prefix=$PREFIX \ --target-os=linux \ --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \ --arch=arm \ --sysroot=$SYSROOT \ --extra-cflags="-Os -fpic $ADDI_CFLAGS" \ --extra-ldflags="$ADDI_LDFLAGS" \ --cc=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \ --nm=$TOOLCHAIN/bin/arm-linux-androideabi-nm \ --enable-shared \ --enable-runtime-cpudetect \ --enable-gpl \ --enable-small \ --enable-cross-compile \ --disable-debug \ --disable-static  \ --disable-doc \ --disable-asm \ --disable-ffmpeg \ --disable-ffplay \ --disable-ffprobe \ --disable-ffserver \ --enable-postproc \ --enable-avdevice \ --disable-symver \ --disable-stripping \$ADDITIONAL_CONFIGURE_FLAG
sed -i '' 's/HAVE_LRINT 0/HAVE_LRINT 1/g' config.h
sed -i '' 's/HAVE_LRINTF 0/HAVE_LRINTF 1/g' config.h
sed -i '' 's/HAVE_ROUND 0/HAVE_ROUND 1/g' config.h
sed -i '' 's/HAVE_ROUNDF 0/HAVE_ROUNDF 1/g' config.h
sed -i '' 's/HAVE_TRUNC 0/HAVE_TRUNC 1/g' config.h
sed -i '' 's/HAVE_TRUNCF 0/HAVE_TRUNCF 1/g' config.h
sed -i '' 's/HAVE_CBRT 0/HAVE_CBRT 1/g' config.h
sed -i '' 's/HAVE_RINT 0/HAVE_RINT 1/g' config.h
make clean
#Here is the definition of compiling on several cpus. I used 4 and generally compiled in less than 5 minutes
make -j4
make install
}
build_one

Copy the code

Among them:

  • TMPDIR is the directory where temporary files generated by compilation are stored
  • SYSROOT is the platform directory of the lowest Android version supported by the so file
  • CPU is the platform supported by the specified SO file
  • PREFIX is the directory for storing the generated so file
  • TOOLCHAIN is the directory of toolchains used for compilation
  • Cross-prefix is the tool-chain file used for compilation
  • Enable and disable specify the items to compile
  • Target-os indicates the target operating system.

Compile FFmpeg

In the FFmpeg directory, execute the terminal command:

./build_android.sh
Copy the code

Compile and wait for the so file to be generated.

The relevant data

Lei Xiaohua’s blog