window10_ffmpeg debugging environment construction - self compilation

Posted by clandestine555 on Fri, 25 Feb 2022 06:44:47 +0100

I recommend a free open course of zero sound college. Personally, I think the teacher speaks well. I share it with you: Linux, Nginx, ZeroMQ, MySQL, Redis, fastdfs, MongoDB, ZK, streaming media, CDN, P2P, K8S, Docker, TCP/IP, collaboration, DPDK and other technical contents, learn immediately

"Windows 10_ffmpeg debugging environment construction - speed version" has introduced debugging ffmpeg in qt creator C engineering. But the speed version of dll is ready-made. So this article explains how to compile the dll of ffmpeg.

There are two main compilation methods of ffmpeg in Windows 10 environment:

1,MSYS2 + MinGW

2,MSYS2 + MSVC

Since the native CMD command line of window cannot execute shell script and there are no commands such as make, MSYS2 software needs to be installed.

What is MSYS2? MSYS2 is actually a linux simulation environment. After installing MSYS2, you can run the configure compilation script of ffmpeg.

MinGW and MSVC are two different compilers. What are the advantages of MinGW Compiler? Look at the code below

/*linux api pthread_create()*/
#include <stdio.h>
#include <pthread.h>
void* PrintHello(void* data)
{
    printf("Hello from new thread\n");
    pthread_exit(NULL);
}
int main(int argc, char* argv[])
{

   pthread_t  thread_id;
   pthread_create(&thread_id, NULL, PrintHello, NULL);
   pthread_exit(NULL);
}

The above code uses a pthread in linux_ The create () thread function does not exist in the window environment. If it is compiled with MSVC compiler, an error will be reported.

However, if you use the MinGW compiler, you can compile pthread in the window environment_ Code for create().

The source code of fmpeg is common to linux, window s and other platforms. Pthread is not directly used in ffmpeg_ Create() and other linux specific api functions, so you can compile the source code of ffmpeg with MinGW or MSVC. The configure compilation script of ffmpeg executes different compilation logic according to different compilation methods. For example, if you use the compiler of MinGW, the compilation logic of MinGW will be used in configure. If you specify to compile with MSVC, configure will take another logic.

Different compilation tool chains can be specified by adding -- toolchain=xxx in front of configure. For example -- toolchain=msvc specifies msvc compilation tool chain. If -- toolchain is not specified, configure will automatically select MinGW for compilation in mingw32 environment. Our example does not specify -- toolchain.

Therefore, the code of ffmpeg is not so much cross platform as the shell script of configure, which implements the Compilation Rules of different platforms. Because the window native CMD command line cannot run shell scripts, MSYS2 is required.

This article mainly explains the compilation method of MSYS2 + MinGW. MSYS2 + MSVC will explain it in detail in a subsequent article.

Download address of MSYS2 official website: MSYS2

After MSYS2 installation, CMD enters the installation directory C:\msys64 and executes \msys2_shell.cmd - mingw32, open the 32-bit environment. Don't click "mingw32" directly Exe} enters the 32-bit environment. Try to use it \msys2_shell.cmd -mingw32. Because it will be changed later}msys2_shell.cmd file.

After running, you will enter the simulation environment of linux. linux commands such as ls, ps and so on can be used.

.\msys2_shell.cmd - mingw32# is a 64 bit environment. Ffmpeg compiled with this entry Exe and DLL are 64 bit by default. This paper first compiles 32-bit programs and 64 bit programs, followed by an article to explain.

The package management command in MSYS2 Linux simulation environment is pacman, similar to apt get, yum. Next, install some software for compiling FFmpeg with pacman. (pacman is slow to download and needs to switch the source)

# Refresh package data
pacman -Sy  
# Install mingw-w64. If an error signal not trust is reported, please see the following to solve it.
pacman -S mingw-w64-i686-toolchain 
pacman -S git
pacman -S make
pacman -S automake 
pacman -S autoconf
pacman -S perl
pacman -S mingw-w64-i686-SDL2
pacman -S libtool
pacman -S mingw-w64-i686-cmake 
pacman -S pkg-config 
pacman -S yasm
# nasm is required to compile x264
pacman -S nasm

Common errors:

  • mingw-w64-i686-toolchain , if the "signature not trust" error is reported, the certificate has expired, and the detection is directly disabled. Please refer to this article for solutions.   csdn article

The preparations have been completed.

FFmpeg engineering codec and other functions need to rely on some external libraries, such as x264 library, aac library and so on. Therefore, before compiling FFmpeg, you need to compile x264, acc and other projects into static libraries, and then Link x264 static libraries, acc static libraries and other links into FFmpeg DLL.

x264 project compilation:

For the following command, please change / home / token to your own directory.

If a final link failed: No space left on device error is reported during compilation, try restarting windows 10.

# Go back to the user directory
cd /home/loken
# Create ffmpeg directory and build directory for unified management
mkdir -p ffmpeg/build32  
# Enter ffmpeg directory
cd ffmpeg 
#Download x264 project code
git clone https://gitee.com/mirrors_addons/x264
# Enter x264 project directory
cd x264 
# Execute configure
./configure --prefix=/home/loken/ffmpeg/build32/libx264 \
--host=i686-w64-mingw32 --enable-static \
--extra-ldflags=-Wl,--output-def=libx264.def  
make -j8
make install

If the above command is correct, the static library libx264 of x264 will be compiled in ~ / ffmpeg/build32/libx264/lib directory a.

FDK AAC project compilation:

# Go back to ffmpeg directory
cd /home/loken/ffmpeg 
git clone --depth 1 https://gitee.com/mirrors/fdk-aac.git
cd fdk-aac
./autogen.sh
./configure --prefix=/home/loken/ffmpeg/build32/libfdk-aac --disable-shared \
--enable-static
make -j8
make install

If the above command is correct, the static library libfdk aac of aac will be compiled in ~ / ffmpeg / build32 / libfdk aac / lib directory a.

mp3 project compilation:

If the following command reports an error, please change the relative directory of "~" to the absolute directory

cd /home/loken/ffmpeg 
git clone --depth 1 https://gitee.com/hqiu/lame.git
cd lame
./configure --prefix=/home/loken/ffmpeg/build32/libmp3lame --disable-frontend \
--disable-shared --enable-static
make -j8
make install

If the above command is correct, the static library libmp3lame of mp3 will be compiled in ~ / ffmpeg/build32/libmp3lame/lib directory a.

libvpx project compilation:

cd /home/loken/ffmpeg 
git clone --depth 1 https://github.com/webmproject/libvpx.git
cd libvpx
./configure --prefix=/home/loken/ffmpeg/build32/libvpx --disable-examples \
--disable-unit-tests --enable-vp9-highbitdepth --as=yasm
make -j8
make install

If the above command is correct, the static library libvpx of vpx will be compiled in ~ / ffmpeg/build32/libvpx/lib directory a.

The external static libraries that the FFmpeg project depends on have been compiled. Now you can start compiling the FFmpeg project.

FFmpeg project compilation:

Download ffmpeg 4.2 source code, link: Baidu online disk, please enter the extraction code Extraction code: g3k8

Put the file ffmpeg-4.2 Zip to / home / token / ffmpeg / ffmpeg

cd /home/loken/ffmpeg/ffmpeg-4.2
./configure \
--prefix=/home/loken/ffmpeg/build32/ffmepg-4.2 \
--enable-gpl \
--enable-sdl2 \
--enable-zlib \
--enable-shared \
--enable-nonfree \
--enable-libx264 \
--enable-libfdk-aac \
--enable-libmp3lame \
--enable-libvpx \
--extra-cflags="-I/home/loken/ffmpeg/build32/libfdk-aac/include" \
--extra-ldflags="-L/home/loken/ffmpeg/build32/libfdk-aac/lib" \
--extra-cflags="-I/home/loken/ffmpeg/build32/libvpx/include" \
--extra-ldflags="-L/home/loken/ffmpeg/build32/libvpx/lib" \
--extra-cflags="-I/home/loken/ffmpeg/build32/libx264/include" \
--extra-ldflags="-L/home/loken/ffmpeg/build32/libx264/lib" \
--extra-cflags="-I/home/loken/ffmpeg/build32/libmp3lame/include" \
--extra-ldflags="-L/home/loken/ffmpeg/build32/libmp3lame/lib"
  
make -j8
make install

After compilation, the build32/ffmpeg-4.2 directory is as follows:

You can see ffmpeg Exe has been compiled. At this time, in the MSYS2 emulation linux command line, ffmpeg Exe can be run. As shown in the figure.

But if you run ffmpeg.com on the window CMD command line Exe, an error will be reported, indicating that libwinpthread-1 is missing DLL and other libraries.

C: \ msys64 \ mingw32 \ bin \ libwinpthread-1 dll to \ home \ token \ ffmpeg \ build32 \ ffmepg-4.2 \ bin. The same is true for other missing dll files.

After copying the missing dll file, run ffmpeg Exe will not report an error.

Important knowledge points:

  • How to check whether the EXE and dll are 32-bit or 64 bit? Answer: dumpbin exe /headers ffmpeg. exe

The compilation of ffmpeg in MSYS2 environment has been finished. Here is how QT calls the api function of ffmpeg.

Before compiling ffmpeg, configure specified -- enable shared, so the project generated several DLL dynamic libraries.

  • avcodec-58.dll codec API.
  • avdevice-58.dll device API.
  • avfilter-7.dll filter API
  • avformat-58.dll container API.
  • wait.

The following describes how QT projects add these DLLs. If you haven't installed QT and Visual Studio 2015 before, please see the first article.

1. Open Qt creator, click New File Or Project, select non QT project, and select Plain C Application.

2. Check the compilation environment kit, MinGW 32 and MSVC 2015 32bit.

3. Copy the entire directory of \ home \ token \ ffmpeg \ build32 to the ffmpeg QT version project directory.

4. Modify ffmpeg QT version Pro file:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
​
SOURCES += main.c
​
contains(QT_ARCH, i386) {
    message("32-bit")
    INCLUDEPATH += $$PWD/build32/ffmepg-4.2/include
    LIBS += $$PWD/build32/ffmepg-4.2/bin/avformat.lib \
            $$PWD/build32/ffmepg-4.2/bin/avcodec.lib \
            $$PWD/build32/ffmepg-4.2/bin/avdevice.lib \
            $$PWD/build32/ffmepg-4.2/bin/avfilter.lib \
            $$PWD/build32/ffmepg-4.2/bin/avutil.lib \
            $$PWD/build32/ffmepg-4.2/bin/postproc.lib \
            $$PWD/build32/ffmepg-4.2/bin/swresample.lib \
            $$PWD/build32/ffmepg-4.2/bin/swscale.lib
} else {
    message("64-bit")
}

5. Modify main C Documents:

#include <stdio.h>
#include "libavutil/avutil.h"
int main(){
    printf("Hello FFMPEG, version is %s\n", av_version_info());
    return 0;
}

6. Compile and run ffmpeg QT version project. You will be prompted that the dll is indeed missing.

Since the ffmpeg QT version project does not use pthread_ linux api functions such as create (), so there is no problem compiling with MinGW 32bit or MSVC2015 32bit.

7. Put build32 / ffmpeg-4.2 / bin / * DLL copy all DLLs to build ffmpeg QT version desktop_ XXXX debug \ debug directory. Then compile and run the ffmpeg QT version project again, and the version will be printed normally.

matters needing attention:

  • The project file path should not have Chinese, and QT may report an error.

Common errors:

  • If an error is reported and the file cannot be found, put qt build ffmpeg qt version desktop_ Delete the XXX debug directory because there may be a cache, and then re execute the above steps.

Important knowledge points:

  • The api function of ffmpeg {AV is called above_ version_ Info() prints the version number, because the ffmpeg QT version project does not use pthread_ linux api functions such as create (), so there is no problem compiling with MinGW 32bit or MSVC2015 32bit.
  • Although our avcodec-58 DLL is compiled by MinGW, but MSVC can be used to compile the main of other ffmpeg QT version C documents. In other words, the DLL compiled by MinGW can also be used in MSVC environment. My personal understanding is that DLL is already a window system level machine code and can certainly be called by MSVC. Add: This is ABI related knowledge, Differences between ABI and API
  • It should be noted that the gcc version of ffmpeg compiled by MSYS2 cannot be too different from the gcc version of MinGW in Qt creator. For example, the gcc of ffmpeg dll compiled in MSYS2 is version 11.0, while the gcc of MinGW of Qt creator is version 5.0. The ffmpeg dll cannot be used in the project, and an error will be reported. It should be that the gcc version gap is too large and the binary is incompatible.

Download the window dll officially released by ffmpeg:

Baidu online disk: Baidu online disk, please enter the extraction code Extraction code: n7dx

Ffmpeg-4.2.1-win32-shared README in zip records the configure option for the official compilation of ffmpeg. There are - enable LZMA -- enable zlib. Some online ffmpeg compilation tutorials do not add these two options. Zlib-1 should be copied every time Let's go. You can refer to the official configure option to directly compile zlib and other libraries into ffmpeg dll in the form of static library.

Relevant technical articles:

© Copyright: knowledge Planet: implication, QQ: 2338195090. Due to the limited level of the author and the fact that he has to participate in the development work while writing, it is inevitable that there will be some errors or inaccuracies in the article. I urge the readers to criticize and correct. If readers have any valuable comments, you can add me wechat Loken1.

Topics: Linux GNU