Audio and video development three: FFmpeg compilation and installation in Windows environment

Introduction

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 . Adopt LGPL or GPL license. It provides a complete solution for recording, converting and streaming audio and video. It includes libavcodec, a very advanced audio/video codec library.

FFmpeg is developed under the Linux platform, but it can also be compiled and run in other operating system environments, including Windows , [Mac OS X](https://baike.baidu.com/item/Mac OS X/470629?fromModule= lemma_inlink), etc.

Users can use the command line to directly operate audio and video (CLI), or call its open source library for personalized function development (SDK).

If you want to use ffmpeg in your own program, then using its SDK is the best choice. Currently ffmpeg contains the following libraries:

  • libavcodec : code/decode
  • libavfilter : frame-level operations (such as adding filters)
  • libavformat : file I/O and packing/unpacking
  • libavdevice : Encapsulation/decapsulation of device video files
  • libavutil : a collection of common tools
  • libswresample : audio resampling, formatting, mixing, etc.
  • libpostproc : preprocessing
  • libswscale : color processing and scaling

FFmpeg compile

Why compile it?

​ Since ffmpeg currently does not provide precompiled library files, it needs to download the source code and compile it by itself, and can customize configuration and optimization, which is more flexible than directly downloading the precompiled binary version.

Compile and install preparation

Under Windows, the compilation needs to be prepared as follows:

  • Install and configure MYSY2
  • Install git (optional)
  • ffmpeg source code

Install MSYS2 and compile toolchain

MSYS2 is a set of compilation suites under Windows, which can simulate the compilation environment under Linux in Windows systems, such as using shell to run commands, using pacman to install software packages, and using gcc (MinGW) to compile code, etc. Simply put, using MSYS2, you can compile software under Windows through various commands that you are very familiar with under Linux.

The installation of MSYS2 is also very worry-free, you only need to download the installation package from the MSYS2 official website , and run the installation as an administrator..exe

Note that the installation disk must be NTFS, and the path must use ASCII characters without spaces . It is recommended to install it in the default location. If you don’t want to install it in the C drive, you can directly change the drive letter and install it in the root directory of other drives.

After the installation is complete, the following programs will appear in the start menu:

Clicking them will start a shell window, Just like on Linux! This shell program is Mintty by default, similar to the bash shell in Linux systems .

MSYS2 supports multiple compiler toolchains, and their environments are independent (you can check it in the installation folder),

Open MSYS2 MSYS in the start menu , use the command pacman -Syuto upgrade all libraries

Before compiling and installing FFmpeg, install the dependencies and tools required for FFmpeg compilation

pacman -S mingw-w64-x86_64-toolchain  # mingw64编译工具链,win下的gcc
pacman -S base-devel    # 一些基本的编译工具
pacman -S yasm nasm     # 汇编器
pacman -S mingw-w64-x86_64-SDL2 # SDL2 是ffmpeg依赖的,因为没有它ffpaly不能用

After the installation is complete, you can enter gcc -vto view the gcc version:

Finally, the environment variable needs to be added. Right-click This Computer -> Properties -> Advanced System Settings -> Environment Variables -> Select Path(Both User Variables and System Variables) -> Edit -> New, add the path of the compilation toolchain:

2022-12-09-11-07-29

The step of adding environment variables is to find the compilation toolchain of MINGW64 when using other editors or IDEs in the future. Compiling ffmpeg itself in MSYS2 does not need to add Windows environment variables.

ffmpeg source code download

Download the source code from ffmpeg official website , the latest version is 5.1.2. Be careful not to download executable files, that is a CLI program that can be run directly.

All three downloads are available, but the format of the compressed package is different:

After downloading, extract it to an easy-to-find folder.

In the shell of MSYS2, open the folder where the source code is located.

Execute the compile and install command

We compile and install ffmpeg and choose to use the mingw64 terminal (if you are also a 64-bit system).

  1. ./configure --prefix=/usr/local/ffmpeg --enable-shared --enable-sdl2 --disable-static

    Values ​​in parentheses are optional.

    configure说明
     --prefix=/usr/local/ffmpeg        # 安装位置
     --enable-debug                    # 启动debug调试
     --enable-sdl2					   # 引入SDL2库,ffplay编译必须;
     --enable-static                   # 编译静态库(默认开启)
     --enable-shared                   # 编译动态库(默认关闭)
     --enable-libvpx                   # VP8/VP9 视频编码器
     --enable-libvmaf                  # VMAF视频质量评估工具
     --disable-x86asm                  # 未知
     --enable-gpl                      # 允许使用GPL(默认关闭)
     --enable-nonfree                  # 允许使用非免费的代码, 产生的库和二进制程序将是不可再发行的
     --enable-libx264                  # 启用H.264编码(默认关闭)
     --enable-libfdk-aac               # 使能aac编码(默认关闭)
     --disable-optimizations           # 禁用编译器优化
     --disable-asm                     # 禁用全部汇编程序优化
     --enable-librtmp                  # 使用librtmp拉流(默认关闭)
    

    More detailed instructions can be foundffmpeg ./configure参数说明

  2. Compile: make -j 4

    4 means compiling requires 4 threads.

  3. Install sudo make install

This step is all installed, execute .\ffmpeg.exe -version in the bin directory under the installation directory

set environment variables

In order to be able to execute the ffmpeg command every time it is opened. Add this command at the end of the configuration file export PATH=$PATH:/d/repos/ffmpeg/ffmpeg_5.2.1_install/bin. The configuration file is the installation location of msys . This file is similar /msys64/etc/profileto the file under Linux ./etc/profile

ffmpeg integrated x264

libx264 compile and install

ffmpeg provides quite a lot of external library support, but if you want to use it, you need to compile the external library yourself.

libx264 is released by the VideoLAN organization, it is a set of better performance soft encoder implementation of H.264 (decoder not included). H.264 (AVC) is currently the most commonly used video encoding, and the successor of H.264, H.265 (HEVC), is gradually becoming mature. VideoLAN also has a set of libx265.

  1. Enter the x264 official website page to download

    The official recommendation is to use git to download the source code (downloading the compressed package and decompressing it should be the same):

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

  2. Enter the source code directory

  3. Execute the following commands to compile and install

    The compilation and installation process is similar to ffmpeg.

./configure --prefix=安装路径 --enable-shared
make -j 4
make install 

– enable-shared, and generate a shared library in the system at the same time, which is convenient for other programs to call.

  1. Check if libx264 is installed successfully

    Go to the executable file directory of x264, execute ./x264.exe --versionand print information similar to the following, and the installation is successful.

Compile ffmpeg library and integrate libx264

  1. Check if the PKG_CONFIG_PATH environment variable is configured correctly

    The pkg-config tool looks for pkg-config configuration files for libraries through the PKG_CONFIG_PATH environment variable. ibx264's pkg-config is a tool for managing library dependencies. It helps developers automatically include library files when compiling and linking source code, and provides necessary compiler and linker options. In addition, it can also be integrated with other development tools, such as Autotools and CMake, for a more complete and automated build process.

    You can execute the following command to confirm whether PKG_CONFIG_PATH contains the pkg-config configuration file path of libx264:

    echo $PKG_CONFIG_PATH
    

    If not included, you can add libx264's pkg-config configuration file path to PKG_CONFIG_PATH with the following command:

     export PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:/f/software/install/ffmpeg/x264_install/lib/pkgconfig
    
  2. Compile and install ffmpeg

    ./configure --prefix=/f/software/install/ffmpeg/ffmpeg_install \
    --enable-shared --enable-sdl2 --disable-static  --enable-gpl --enable-libx264 \
    --extra-cflags=-I/f/softweg/x264_install/include \
    --extra-ldflags=-L/f/software/install/ffmpeg/x264_install/lib
    
    make -j 4
    
    make install
    

    The following parameters are used here:

    • join --enable-libx264parameters
  • Add --enable-gplparameters. Otherwise it will promptlibx264 is gpl and --enable-gpl is not specified

  • –extra-cflags="-I/f/softweg/x264_install/include": Specify the x264 header file path as /f/softweg/x264_install/include.

    • –extra-ldflags="-L/f/software/install/ffmpeg/x264_install/lib": Specify the x264 library file path as /f/software/install/ffmpeg/x264_install/lib, and link to the libx264.so dynamic library document.

Note: There is a pitfall that after the compilation and installation are completed, the execution of ffmpeg, ffplay and other commands does not respond. Or the double-section ffmpeg.exe prompts that the code cannot continue to be executed because libx264-164.dll cannot be found. Reinstalling the program may resolve this issue . This is because the installation paths of our libx264 and ffmpeg are not in the same place. The libx264-164.dll under the bin folder of the libx264 installation directory needs to be placed in the bin directory of the ffmpeg installation path.

reference

Fmpeg library compilation installation and getting started guide (Windows)

Guess you like

Origin blog.csdn.net/qq_38056514/article/details/129827722