FFmpeg library compilation and installation guide under Windows

        When I was learning FFmpeg before, I put together scattered knowledge points on the Internet without systematic study. I spent a lot of time recently, so I decided to start learning FFmpeg in depth, starting from compiling the source code by myself.

This article records the pits you stepped on during your own compilation process, as well as the complete compilation and installation process. It is recommended to read the last one in the pits you stepped on first.

Table of contents

Environmental preparation

Install MSYS2 and compile toolchain

FFmpeg source code download

x264 source code download

SDL download

compile and install

stepped on pit

reference article


Environmental preparation

  • Install and configure MYSY2
  • Download FFmpeg source code
  • Download x264 source code
  • Download SDL source code (optional, required when compiling FFplay.exe)

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. To put it simply, using MSYS2, you can compile software under Windows through various commands that you are very familiar with under Linux. Download the exe installation package from the MSYS2 official website , install it with administrator privileges, and there will be 6 shell programs after installation

 Start MSYS2 MINGW64, and enter the following command lines to start installing the mingw64 compilation chain and basic dependencies

sed -i "s#mirror.msys2.org/#mirrors.ustc.edu.cn/msys2/#g" /etc/pacman.d/mirrorlist*
pacman -Sy #提高下载速度,将下载源换成中科大的

pacman -S mingw-w64-x86_64-toolchain  # mingw64编译工具链,win下的gcc,用于编译64位的库
pacman -S mingw-w64-i686-toolchain # 用于编译32位的库
pacman -S base-devel    # 一些基本的编译工具
pacman -S yasm nasm     # 汇编器

FFmpeg source code download

Download the source code from the F Fmpeg official website

 

All three packages can be downloaded

x264 source code download

Go to x264 official website to download

SDL download

Go to SDL official website to download

 

compile and install

        Put the downloaded installation in the same directory ( no spaces or Chinese in the path ), unzip all files to the current directory, in order to distinguish the source code directory and the library installation directory, it is recommended to modify the name (not necessary)

 Create a new compilation and installation script file .sh, copy the following command, among them, the ./configure compilation parameter can be viewed by opening the configure file in the corresponding source code directory. The parameters here are for reference only, and the meaning of the parameters can be found in FFmpeg compilation parameter analysis- Nuggets

#!/bin/sh
#进入执行脚本所在目录
basepath=$(cd `dirname $0`;pwd)
echo ${basepath}

#进入h264源码目录
cd ${basepath}/x264-src   # 根据路径名称自行修改
pwd
#配置编译参数
./configure --prefix=${basepath}/x264-install --enable-shared
#开始16线程编译
make -j16
#将编译后的文件拷贝到--prefix参数配置目录
make install

#进入ffmpeg源码目录
cd ${basepath}/ffmpeg-5.1.2-src  # 根据路径名称自行修改
pwd
#指定PKG_CONFIG_PATH变量,告知编译器x264库的路径
export PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:${basepath}/x264-install/lib/pkgconfig
echo ${PKG_CONFIG_PATH}
#配置编译参数
./configure --prefix=${basepath}/ffmpeg-5.1.2-install \
--enable-gpl \
--enable-libx264 \
--disable-static \
--enable-shared \
--enable-sdl2 \
--enable-ffplay \
--enable-ffprobe \
--enable-ffmpeg \
--disable-vulkan \
--extra-cflags=-l${basepath}/x264-install/include \
--extra-ldflags=-L${basepath}/x264-install/lib

#开始16线程编译
make -j16
#将编译后的文件拷贝到--prefix参数配置目录
make install

Open the sdl2-config file in the SDL source code

Modify the prefix path, the specific path is determined according to the location of your file, you cannot directly copy the path in the file explorer, you need to modify it to the following format

prefix=/e/shell/SDL2-2.26.3/x86_64-w64-mingw32

Open the configure file in the FFmpeg source directory

Modify the SDL2_CONFIG path, the rules are the same as above

SDL2_CONFIG="/e/shell/SDL2-2.26.3/x86_64-w64-mingw32/bin/sdl2-config"

 

 Open MSYS2 MINGW64, enter the directory where the script is located, execute the script, wait for the compilation and installation to complete, about ten minutes

 cd /e/shell
./build-ffmpeg-5.1.2.sh

 After compiling and installing, you can see the library files in the install directory.

stepped on pit

1. The directory path where the source code is located contains Chinese, which will cause a prompt that the x264 library cannot be found when compiling FFmpeg, as shown below

2. If the sdl2-config file in the SDL source code and the SDL-related path in the FFmpeg source code configure file are not modified, it will prompt that the SDL library cannot be found when compiling FFmpeg, as shown in the figure below

3. When configuring FFmpeg compilation parameters, --disable-vulkan is not added, and an error will be reported during compilation. The reason is unknown. I don't know what the impact of disabling this module will be (I dug a hole for myself)

4. When compiling 32-bit, it prompts that the c compiler cannot be found. For the solution, refer to the No working C compiler found error encountered when compiling x264 under linux_Lao Yao---Lao Yao's Blog-CSDN Blog

 5. This article uses the mingw compiler to compile FFmpeg, so the compiled library can only be used in projects using the mingw compiler, otherwise there will be problems. I originally planned to use the VS+Qt+FFmpeg library to make a project Yes, after using the msvc compiler and introducing the library compiled by the above method, the program cannot start. After checking, I found out that the mingw compiler is not compatible with msvc, and VS cannot use the mingw compiler, so I had to find another way to compile with the msvc compiler The method of FFmpeg, fortunately, it didn't take long to find the solution . After reading the article of this big guy, I found out that the new version of FFmpeg comes with h264 codec, and there is no need to download the h264 source code to compile it yourself. In addition, if If you don’t need to compile ffplay.exe, you don’t need to download the SDL source code, so the compilation process is much simplified, and there are basically no pitfalls, and you can pass it in one pass. Later, I will sort out the process of compiling ffmpeg with msvc and send it out.

reference article

win10 compile ffmpeg and ffplay - Programmer Sought

Win10 compiles ffmpeg and ffplay - Programmer Sought (jianshu.com)

Analysis of FFmpeg compilation parameters - Nuggets

No working C compiler found error encountered when compiling x264 under linux_Lao Yao---Lao Yao's Blog-CSDN Blog

Solve the problem when Android NDK compiles the x86 cpu version of FFmpeg 4.2.2 – K-Res Blog

window10_ffmpeg-msys2-msvc compilation_h264_mf_Loken2020's Blog-CSDN Blog 

Guess you like

Origin blog.csdn.net/qq_36751491/article/details/129128533