preface
Having looked at some C/C++ basics and some compilation basics, now it’s time to get into the game and compile FFmpeg in general.
The body of the
Here side operation, side record it, to see the famous FFmpeg library.
FFmpeg definition
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, FFmpeg provides a rich audio and video processing call interface for developers.
FFmpeg provides a variety of media formats to encapsulate and unencapsulate, including a variety of audio and video encoding and decoding, a variety of protocol streaming media, a variety of color format conversion, a variety of sampling rate conversion, a variety of code rate conversion and so on.
At the same time, FFmpeg framework provides a variety of rich plug-in modules, including encapsulation and unencapsulation plug-in, encoding and decoding plug-in.
Some of the specific concepts here, such as what is encapsulation and unencapsulation, we will go to the audio and video related knowledge points to understand, here is a general understanding.
The FFmpeg module is introduced
The FFmpeg code is easy to download, either from the official website or from GitHub, but IT was slow to download on Linux, so I downloaded it directly from GitHub on Windows and copied it to a Linux VIRTUAL machine. Here is the full directory:
The FFmpeg framework directory is quite clear, including several modules, here is a brief introduction:
- libavcodec
Some codecs are licensed. FFmpeg is not added by default, such as libx264, FDK-aAC, Lame, etc. However, FFmpeg is like a platform that can be added as plug-ins by other third party coDecs. Provide a unified interface for developers.
- libavformat
File formats and Protocol libraries encapsulate the Protocol layer and Demuxer and Muxer layers, making protocols and formats transparent to developers.
- libavfilter
Audio and video filter library, this module contains the processing of audio and video effects, in the process of using FFmpeg API encoding and decoding, you can use this module to efficiently do special processing for audio and video data.
- libavdevice
Input/output device libraries, such as ffplay, which needs to be compiled to play sound or video, need to be enabled and need to be pre-compiled by libsdl, which uses the libsDL library to play sound and video.
- libavutil
Core tool library, one of the most basic modules, other modules will rely on this module to do some basic audio and video processing operations.
- libswresample
It is used for audio and video sampling, which can convert audio channel number, data format, sampling rate and other basic information.
- libswscale
The module is used for image format conversion, can reduce YUV data into RGB data
- libpostproc
This module is used for post-processing, when we use filter, we need to open this module, filter will use some basic functions of this module.
In addition, the library also includes the X264 library for H.264/MPEG-4 AVC video encoding, which is a very common lossy video encoder. It supports CBR and VBR modules, and can directly change the bit rate setting in the process of encoding. It is very applicable in the live broadcast scene, and can do the bit rate adaptive function.
Some of the most basic introduction is above, specific is what meaning, we say in detail again later.
Compile FFmpeg
The wget command
Wget command can be used in Linux to download files, this wget is quite a download, but it has recently found that many websites use wGET download is particularly slow, especially some foreign websites, Windows hung agent is still not good, the Internet to find some, recommend using MWGET.
Mwget command
Mwget download is a multithreaded download application that is faster than WGET download. Here is a script you can install directly:
#! http://jaist.dl.sourceforge.net/project/kmphpfm/mwget/0.1/mwget_0.1.0.orig.tar.bz2 / bin/bashwget yum install bzip2 GCC -c++ openssl-devel intltool -y bzip2 -d mwget_0.1.0.org.tar. bz2 tar -xvf mwget_0.1.0.org.tar CD mwget_0.1.0.orig You can control the installation by adding parameters to configure./configure # /configure -- prefix=/usr =/usr =/usrCopy the code
This script will not work on Ubuntu, for example, the yum command will not work on Ubuntu. Here is a brief introduction to apt and yum.
Apt and yun
In general, well-known Linux systems fall into two broad categories,
-
RedHat series: RedHat, Centos, and Fedora
-
Debian series: Debian, Ubuntu, etc
Yum, DKPG, apt, etc.
Yum install bzip2
apt install bzip2 gcc-c++ openssl-devel intltool -y
However, you will find that only one bzip2 can be installed successfully, and the others can not be found. What is this ghost?
Ubuntu installation mwget
To install mwGET on Ubuntu, you need to check the following links:
# Ubuntu install mwget
If you want to run the mwGET shell script, you need to install 3 software, and then unzip the bz2 file, then run the./configure script, and then execute make and make install. Then you will find that many CPP files lack header files, so you need to modify and add header files one by one.
Download FFmpeg
Assuming the mwGET installation is successful, you need to download and unzip FFmpeg.
Mwget https://ffmpeg.org/releases/ffmpeg-4.2.2.tar.bz2 tar - JXVF ffmpeg - 4.2.2. Tar..bz2Copy the code
Now that you have the directories described above, you can run the configure script, which is used to configure FFmpeg. It contains a lot of configuration items, so you can run it directly:
FFmpeg uses assembler instructions to improve efficiency, so it needs to install assembler compiler:
sudo apt-get install yasm
Copy the code
Then the configure script will execute successfully.
Compile FFmpeg
Configure configure for FFmpeg, configure for FFmpeg, configure for FFmpeg
#! / bin/bash echo "> > > > > > > > > note: the compiler environment currently only in NDK17c + ffmpeg4.2.2 tested < < < < < < < <" echo "> > > > > > > > > note: The compiler environment currently only in NDK17c + ffmpeg4.2.2 tested < < < < < < < < "echo" > > > > > > > > > note: The build environment is currently only tested in NDK17c + ffmpeg4.2.2 <<<<<<<<" #NDK_ROOT variable pointing to NDK directory NDK_ROOT=$NDK_HOME # Specifies android API version ANDROID_API=21 Function build_ffmpeg() {echo "start compiling $PREFIX_CPU" start compiling $PREFIX" echo" start compiling $TOOLCHAIN" ./configure \ --prefix=$PREFIX \ --enable-small \ --disable-programs \ --disable-avdevice \ --disable-encoders \ --disable-muxers \ --disable-filters \ --enable-cross-compile \ --cross-prefix=$CROSS_PREFIX \ --disable-shared \ --enable-static \ --sysroot=$NDK_ROOT/platforms/android-$ANDROID_API/arch-$ARCH \ --extra-cflags="$CFLAGES" \ --arch=$arch \ --target-os=android # Make clean make make install echo "$PREFIX_CPU "$PREFIX_CPU "echo "$PREFIX_CPU" echo "$PREFIX_CPU "echo "$PREFIX_CPU" . # armeabi - v7a PREFIX = / result/armeabi - v7a TOOLCHAIN = $NDK_ROOT toolchains/arm - Linux - androideabi - 4.9 / prebuilt/Linux - x86_64 ARCH=arm CROSS_PREFIX=$TOOLCHAIN/bin/arm-linux-androideabi- CFLAGES="-isysroot $NDK_ROOT/sysroot -isystem $NDK_ROOT/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=$ANDROID_API -U_FILE_OFFSET_BITS -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fPIC" build_ffmpeg #arm64-v8a PREFIX =. / result/arm64 - v8a TOOLCHAIN = $NDK_ROOT toolchains/aarch64 - Linux - android - 4.9 / prebuilt/Linux - x86_64 ARCH = arm64 CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android- CFLAGES="-isysroot $NDK_ROOT/sysroot -isystem $NDK_ROOT/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=$ANDROID_API -U_FILE_OFFSET_BITS -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -werror =format-security -o0-fpic "build_ffmpeg" build_ffmpeg #Copy the code
Above is the compiled script, there are a few points need to explain, for this kind of compiled script, or strive to understand, or a problem can not find.
- For example, the variables PREFIX, TOOLCHAIN, and ARCH are either newly created directories or strings.
- Function parameters are also important here, such as toolchain, CPU architecture version, and so on.
- Here you can use functions in a shell script, where just as you can run a shell script and pass parameters directly after the name of the script, you can define the build_ffmpeg() function directly and call it later.
- The build_ffmpeg function calls the FFmpeg configure script and passes in a list of parameters to configure the script.
- After running configure, a makefile is generated and the script can be executed using the make directive.
Configure Common parameters
Configure configures the following configuration scripts: configure configures the following configuration scripts: configure configures
- –prefix= prefix: Sets the location of the static and dynamic libraries. This must be set.
- –enable-small: the library size can be optimized.
- Disable-programs: You don’t need to run an.exe program on Windows, you only use it in code.
- — disable-avDevice: controls our camera, not supported in Android.
- –disable-encoders: Specifies whether to enable the code.
- –disable-muxers: Whether mixed encapsulation can be enabled or disabled if not.
- –disable-filters: Specifies whether filter effects are enabled. If not, disable them.
- –enable-cross-compile: enables cross-compilation.
- –cross-prefix=$CROSS_PREFIX: cross-compiled script, which is the compiled script in the NDK set up earlier.
- –disable-shared: indicates whether compilation into a dynamic library is enabled.
- –enable-static: indicates whether to enable compiling to static libraries.
- — sysroot = NDKROOT/platforms/android – NDK_ROOT/platforms/android – NDKROOT/platforms/android – ANDROID_API/arch – $arch: Some configuration for cross-compilation.
- –extra-cflags=”$CFLAGES” : other configurations.
- Arch =$arch: CPU architecture for the desired runtime environment
- –target-os= Android: Expected to run on Android.
Compilation problems
First of all, the compilation wasn’t smooth sailing. It took about 20 minutes for my Ubuntu VIRTUAL machine to compile due to a lot of code. There were a few major errors, which are described below.
- C compiler test failed: FFmpeg 4.2.2 uses clang for compilation by default. This requires you to find the code in configure to modify the default configuration:
//1. Configure vim //2. GCC if test "$target_OS "= android; then # cc_default="clang" cc_default="gcc" fiCopy the code
-
Yasm not found: cannot find assembly compiler, need to install.
-
How to find keywords in VIm, this is an operation of Vim, in command mode, enter/XXX to find XXX, and can be highlighted, want to see the next n key that is next to see the next XXX keyword, this is very important in finding long script files.
conclusion
Compiling FFmpeg is still very complicated, which is normal, after all FFmpeg has so many functions, audio and video learning road is still very long, learning side summary, come on.