ffmpeg compilation notes: ubuntu18.04 cross compile win-ffmpeg, import libx264, libx265, libfdk-aac, libfreetype and other third-party libraries

Foreword:

For the cross-compilation environment of ubuntu18.04, please see my other blog: https://blog.csdn.net/linyibin_123/article/details/131214295.
If you don't want to compile, you can directly download the library I compiled: https://download.csdn.net/download/linyibin_123/87966470

1. Compile the third-party library

1.1 X264 compilation:

1) Download:

git clone https://code.videolan.org/videolan/x264.git

2) Configuration:

PREFIX_X264=/home/yibin/ffmpeg/build/libx264-build-x86
./configure --cross-prefix=i686-w64-mingw32- --host=i686-w64-mingw32  --enable-pic --enable-static --prefix=/home/yibin/ffmpeg/build/libx264-build-x86

The libx264 library may be configured for the first time: Found no assembler problem.
The solution is: add --disable-asm to the command line, or install nasm

3) Compile:
make && make install

1.2 X265 compilation:

1.2.1 Download:

http://ftp.videolan.org/pub/videolan/x265/

1.2.2 cmake installation:

Compiling libx265 requires cmake, follow the command below to install the cmake environment:

sudo apt install cmake
sudo apt-get install cmake-curses-gui
1.2.3 Cross-compilation process:

Reference: https://blog.csdn.net/m0_37554445/article/details/123727198

1) Unzip:

tar zxvf x265_3.2.1.tar.gz

2) Create a new folder in the x265_3.2/build directory: build_i686, and then create a file in the newly created file: build.cmake, edit the content as follows:

#!cmake
# this one is important
SET(CMAKE_SYSTEM_NAME Windows)

SET(cross_prefix i686-w64-mingw32-)

# specify the cross compiler
SET(CMAKE_C_COMPILER   ${cross_prefix}gcc)
SET(CMAKE_CXX_COMPILER ${cross_prefix}g++)
SET(CMAKE_RC_COMPILER  ${cross_prefix}windres)
SET(CMAKE_ASM_NASM_COMPILER nasm)

SET(CMAKE_CXX_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")
SET(CMAKE_C_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")
SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")

3) Command input:

PREFIX_X265=/home/yibin/ffmpeg/build/libx265-build-x86
cmake -DCMAKE_TOOLCHAIN_FILE=build.cmake -DCMAKE_INSTALL_PREFIX=${PREFIX_X265} -DENABLE_SHARED=0 -DENABLE_PIC=1 ../../source 
make 
make install

4) Modify the generated x265.pc, add -lstdc++ -lgcc -lgcc -static

Libs: -L${libdir} -lx265 -lstdc++ -lgcc -lgcc -static
Libs.private: -lstdc++ -lgcc -lgcc

1.3 libfdk-aac

    libfaac is an aac audio encoding library, which can encode the collected PCM data into aac data. Compared with MP3, AAC is an audio coding format with higher coding effect and better coding sound quality. The three encoders that FFMpeg can form AAC are: aac: FFmpeg's own AAC encoding implementation, libaac: third-party AAC encoder, libfdk_aac: third-party AAC encoder. The FDK-AAC library is the highest quality AAC encoding library among the third-party encoding libraries supported by FFMpeg.

1) Download
git clone https://github.com/mstorsjo/fdk-aac.git

2) Configuration

./configure --host=i686-w64-mingw32 --prefix=/home/yibin/ffmpeg/build/libfdkaac-build-x86 --enable-static --disable-shared --with-mp4v2=no

3) Compile
make && make install

1.4 libfreetype (required for ffmpeg filter function)

    The open source font library is an open source, high-quality, scalable, customizable, and portable font engine. It provides a unified interface to access multiple font format files, including dot matrix, TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF, etc. libfreetype is used for ffmpeg filter function.
    The development packages that freetype2 depends on are zlib, bzip2, libpng, harfbuzz, so these libraries must be installed first

1.4.1 zlib installation

1) Download:
http://www.zlib.net/
2) Configure:
export CC=i686-w64-mingw32-gcc
./configure --prefix=/home/yibin/ffmpeg/build/zlib-build-x86 - -static
3) Compile:
make -j8 && make install
export CC=""

1.4.2 libpng installation
  1. Download:
    https://sourceforge.net/projects/libpng/files/libpng16/1.6.39/
  2. 配置:
    ./configure --host=i686-w64-mingw32 --prefix=/home/yibin/ffmpeg/build/libpng-build-x86 LIBS=-L/home/yibin/ffmpeg/build/zlib-build-x86/lib CPPFLAGS=-I/home/yibin/ffmpeg/build/zlib-build-x86/include --disable-shared
  3. Compilation:
    make -j8 && make install
    (the make -j option means parallel compilation. make -j8, let make allow up to 8 compilation commands to be executed at the same time, which can make more efficient use of CPU resources)
1.4.3 libfreetype installation

1) Download: http://download.savannah.gnu.org/releases/freetype/

2)配置:
export PKG_CONFIG_PATH=/home/yibin/ffmpeg/build/libpng-build-x86/lib/pkgconfig:/home/yibin/ffmpeg/build/zlib-build-x86/lib/pkgconfig

./configure --host=i686-w64-mingw32 CC=i686-w64-mingw32-gcc --prefix=/home/yibin/ffmpeg/build/freetype2-build-x86

insert image description here
3) Compile:
make -j8 && make install

1.5 libfontconfig (required for ffmpeg filter function)

libfontconfig is a font configuration and customization software library.
Depends on expat.

1.5.1 expat installation:

1) Download:
http://distfiles.macports.org/expat/
2) Configure and compile:

./configure --without-tests --without-examples --prefix=/home/yibin/ffmpeg/build/expat-build-x86 --host=mingw32 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++

make && make install
1.5.2 libfontconfig installation

1) Download
https://www.freedesktop.org/software/fontconfig/release/
2) Configure and compile:

export CC=i686-w64-mingw32-gcc

./configure --host=i686-w64-mingw32   --prefix=/home/yibin/ffmpeg/build/libfontconfig-build-x86 FREETYPE_LIBS='-lfreetype -L/home/yibin/ffmpeg/build/freetype2-build-x86/lib' FREETYPE_CFLAGS=-I/home/yibin/ffmpeg/build/freetype2-build-x86/include/freetype2

1.6 libfribidi (required for ffmpeg filter function)

1) Download
https://www.linuxfromscratch.org/blfs/view/svn/general/fribidi.html
2) Configure and compile:

export CC=i686-w64-mingw32-gcc
./configure --host=i686-w64-mingw32   --prefix=/home/yibin/ffmpeg/build/libfribidi-build-x86 --enable-static --disable-shared

2. Compile ffmpeg and introduce third-party libraries

2.1 Configure environment variables:

export PKG_CONFIG_PATH=/home/yibin/ffmpeg/build/libx264-build-x86/lib/pkgconfig:/home/yibin/ffmpeg/build/libx265-build-x86/lib/pkgconfig:/home/yibin/ffmpeg/build/libfdkaac-build-x86/lib/pkgconfig:/home/yibin/ffmpeg/build/zlib-build-x86/lib/pkgconfig:/home/yibin/ffmpeg/build/libpng-build-x86/lib/pkgconfig:/home/yibin/ffmpeg/build/freetype2-build-x86/lib/pkgconfig:/home/yibin/ffmpeg/build/libfribidi-build-x86/lib/pkgconfig:/home/yibin/ffmpeg/build/expat-build-x86/lib/pkgconfig:/home/yibin/ffmpeg/build/libfontconfig-build-x86/pkgconfig

2.2 Configuration command:

    Note: Adding libx264 and libx265 will not enable the compilation of ffplay, which may be caused by the conflict between the SDL library and the libx264 and libx265 libraries

./configure --enable-static --enable-shared --enable-version3 --enable-gpl --enable-libx264 --extra-cflags="-I/home/yibin/ffmpeg/build/libx264-build-x86/include" --extra-ldflags="-L/home/yibin/ffmpeg/build/libx264-build-x86/lib -static"  --enable-libx265 --extra-cflags="-I/home/yibin/ffmpeg/build/libx265-build-x86/include" --extra-ldflags="-L/home/yibin/ffmpeg/build/libx265-build-x86/lib" --enable-asm --enable-nonfree --enable-openssl --enable-protocols  --enable-protocol=https --extra-cflags="-I/home/yibin/ffmpeg/build/openssl-build-x86/include/openssl" --extra-ldflags='-static-libgcc' --extra-ldflags="-L/home/yibin/ffmpeg/build/openssl-build-x86/lib" --disable-pthreads --enable-w32threads --enable-cross-compile --target-os=mingw32 --arch=x86 --enable-small --prefix=/home/yibin/ffmpeg/build/ffmpeg-build-x86 --extra-version=avbuild  --disable-doc --enable-runtime-cpudetect --disable-ptx-compression --enable-mediafoundation --disable-vulkan  --disable-postproc --disable-htmlpages --cross-prefix=i686-w64-mingw32- --disable-indevs --disable-outdevs --disable-debug    --enable-hwaccels --enable-dxva2   --enable-libfreetype --extra-cflags="-I/home/yibin/ffmpeg/build/freetype2-build-x86/include" --extra-ldflags="-L/home/yibin/ffmpeg/build/freetype2-build-x86/lib"  --enable-libfdk-aac --extra-cflags="-I/home/yibin/ffmpeg/build/libfdkaac-build-x86/include" --extra-ldflags="-L/home/yibin/ffmpeg/build/libfdkaac-build-x86/lib" --enable-libfontconfig --extra-cflags="-I/home/yibin/ffmpeg/build/libfontconfig-build-x86/include" --extra-ldflags="-L/home/yibin/ffmpeg/build/libfontconfig-build-x86/lib" --enable-libfribidi --extra-cflags="-I/home/yibin/ffmpeg/build/libfribidi-build-x86/include/fribidi" --extra-ldflags="-L/home/yibin/ffmpeg/build/libfribidi-build-x86/lib" --pkg-config-flags="--static" 

insert image description here
The above configuration instructions:
1) –extra-ldflags="-L/home/yibin/ffmpeg/build/libx264-build-x86/lib-static"

ffmpeg默认链接的是动态库,所以会出现该编译错误:
libx264.o : error LNK2019: 无法解析的外部符号 __imp__x264_param_default,函数 _X264_init 中引用了该符号
libx264.o : error LNK2019: 无法解析的外部符号 __imp__x264_param_parse,函数 _X264_init 中引用了该符号
libx264.o : error LNK2019: 无法解析的外部符号 __imp__x264_param_cleanup,函数 _X264_close 中引用了该符号
libx264.o : error LNK2019: 无法解析的外部符号 __imp__x264_param_default_preset,函数 _X264_init 中引用了该符号

在–extra-ldflags中加了“-static”可以解决。

2)–pkg-config-flags=“–static”

Adding --pkg-config-flags="--static" can solve the problem of ffmpeg statically linking third-party libraries.

2.3 Compile:

make && make install

generate:
insert image description here

3. Test function

View FFmpeg installed encoders:
ffmpeg -encoders

3.1 ffmpeg uses libx264 encoding

ffmpeg -i input-h265.mp4 -c:v libx264 -x264-params threads=1:br:1000:maxrate=1000:minrate=1000:bufsize=1000 out.mp4

insert image description here

3.2 ffmpeg uses libx265 encoding

ffmpeg -i input-h264.mp4 -c:v libx265 -x265-params threads=1:br:1000:maxrate=1000:minrate=1000:bufsize=1000 out.mp4

insert image description here

3.3 ffmpeg uses libfdk-aac encoding

ffmpeg -i iuput.wav -c:a libfdk_aac -b:a 128k output.m4a
FFmpeg uses libfdk_aac to convert input.wav to a constant bit rate of 128kbit/s and encode it into an AAC output.m4a audio file.
See: https: //zhuanlan.zhihu.com/p/605496943

3.4 Use of ffmpeg filters

You can use: ffmpeg --help filter=drawtext to view the filter function parameters.

If you want to use drawtext, you need to add --enable-libfreetype when compiling.
If you want to choose multiple fonts, you need to add --enable-libfontconfig.
If you also need font deformation, you need to add --enable-libfribidi.

1) Add text:

ffmpeg -i input-h264.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':x=20:y=20" output.mp4

insert image description here

2) Add pictures:

ffmpeg -i input.mp4 -vf "movie=logo.png[wm]; [in][wm]overlay=30:10[out]" output.mp4

insert image description here

Reference:
https://blog.csdn.net/migu123/article/details/129314333 https://blog.csdn.net/u014552102/article/details/104442111

3. Description of other third-party libraries

3.1 librtmp

librtmp is an open source C language-based library that provides an API for connecting to RTMP servers, sending and receiving RTMP streams. It can be used to develop streaming media player, webcast and other applications. Its main features are fast, stable and low latency.
librtmp supports multiple protocols such as RTMP, RTMPS, RTMPE and RTMPTE encryption protocols. Besides that, it also supports proxy server protocols of PProxy, PPlayer and appspot. Under Linux, applications developed using librtmp can support multiple system platforms such as Windows and Android.
The core code of librtmp is implemented based on the libavformat library in ffmpeg. It depends on third-party libraries such as libz, libssl, libcrypto, etc., and needs to be compiled and installed by itself.

Download:
git clone git://git.ffmpeg.org/rtmpdump

编译:
make CROSS_COMPILE=i686-w64-mingw32- INC=“-I/home/yibin/ffmpeg/build/openssl-build-x86/include -I/home/yibin/ffmpeg/build/zlib-build-x86/include” XLDFLAGS=“-L/home/yibin/ffmpeg/build/openssl-build-x86/lib -L/home/yibin/ffmpeg/build/zlib-build-x86/lib” SYS=mingw32 prefix=/home/yibin/ffmpeg/build/rtmp-build-x86 install

3.2 libmp3lame

mp3 encoding library

CFLAGS=-DFORCEINLINE= ./configure --host=i686-w64-mingw32 --prefix=/home/×××/Desktop/ffmpeg282/opensdk --enable-static --disable-shared --disable-frontend
make
make install

3.3 libspeex

"The libspeex package of the Speex codec includes all speech encoding and decoding functions. The libspeexdsp static library contains preprocessor, acoustic echo canceller, resampler, adaptive jitter buffer and other related functions. testdenoise : Tests noise suppression. testecho: Tests acoustic echo cancellation.

3.4 libxvid

Encapsulation of Xvid MPEG-4 Part 2

3.5 libtheora

libtheora options affect quality and bitrate

3.6 libvorbis

"libvorbis is an audio compression and decompression encoder developed by Xiph. Generally, Vorbis data is encapsulated into ogg files. This library is used to decode and encode Vorbis audio data. However, libogg is also required to convert the encoded audio data from ogg file, that is to say, it needs to be used together with libogg and libvorbis to decode the audio data in the ogg file

3.7 libopenjpeg

./configure --host=i686-w64-mingw32 --prefix=/home/haibindev/opensdk --enable-static --disable-shared
"OpenJPEG is an open source codec based on C language for compression and decompression Compress JPEG2000 images. Unlike other image formats, JPEG2000 has the characteristics of high compression rate, high quality, multi-resolution and lossless compression, and is widely used in medical image processing, satellite remote sensing image processing, high-definition video transmission and other fields. OpenJPEG not only provides In addition to basic codec functions, it also supports multiple advanced functions such as data streaming, various compression options, and conversion with other formats.

3.8 libogg

The full name of Ogg is OGGVobis (oggVorbis) is an audio compression format, similar to music formats such as MP3. Ogg is completely free, open and patent-free. OggVorbis files have the extension ".ogg". The Ogg file format can be continuously improved in size and sound quality without affecting older encoders or players.

./configure --host=i686-w64-mingw32 --prefix=/home/×××/Desktop/ffmpeg282/opensdk --enable-static --disable-shared
make
make install

3.9 libmfx qsv hard solution function

Refer to:
https://blog.csdn.net/haiyangyunbao813/article/details/107829583

https://github.com/lu-zero/mfx_dispatch
autoreconf -i
./configure --host=i686-w64-mingw32
make -j$(nproc)
make install DESTDIR=/usr/x86_64-w64-mingw32

./configure --cross-prefix=i686-w64-mingw32- --host=i686-w64-mingw32 --prefix=/home/yibin/ffmpeg/build/libmfx-build-x86 --enable-static --disable-shared

https://github.com/Intel-Media-SDK/MediaSDK/releases

    qsv depends on the mfx library, libmfx is Intel's proprietary library for using Quick Sync hardware on Linux and Windows. On Windows, it is the main way to access more advanced features than using DXVA2/D3D11VA, especially encoding. On Linux, it has a somewhat different feature set that helps certain use cases where maximum throughput is required.

Guess you like

Origin blog.csdn.net/linyibin_123/article/details/131462858