1. Selection of development library
(1) Audio and video development library
Each mainstream platform basically has its own audio and video development library (API) to process audio and video data, such as:
iOS: AVFoundation, AudioUnit, etc.
Android: MediaPlayer, MediaCodec, etc.
Windows: DirectShow, etc.
But there is a cross-platform library: FFmpeg library.
In fact, as long as you have mastered FFmpeg, you can also quickly get started with other audio and video development libraries, because the underlying principles are the same, and you end up operating the same data, such as MP3 and MP4 files.
2) GUI interface development library
In the same way, we also choose QT , a GUI interface development library that can be developed across platforms .
- Windows:MFC
2. Download and install the environment
(1) The first way to install ffmpeg
: download the source code, and add all the source code of ffmpeg to our project. (This method is not recommended, because the source code is too huge, and there are a lot of codes in it that we don't need.)
Download address: Download FFmpeg
snapshot: snapshot version, not the official version.
releases: The release version is the official version, we can click release.
1. We are currently newbies, and the compilation may not pass .
2. So we directly use the dynamic link library compiled by others . (The official website provides a compiled dynamic library, which is a bit larger)
3. After our technology has been achieved, we can tailor the source code by ourselves, and then compile our own dynamic library .
The first link: (I use this link to download)
- 7z, zip: The compression algorithm is different, and the compression size is different. The contents are the same.
- essentials: Necessary components, things may not be complete.
- full: Comprehensive, the library files inside are relatively complete.
- shared: There is a dynamic link library. (we pick this one)
- shared : Represents the applications inside: the dynamic link library used by ffmpeg, ffplay, and ffprobe.
- Without the shared suffix: it means that the dynamic link library is not used.
- gpl, lgpl: represent different open source agreements.
3. Integrate the function of ffmpeg into the QT project
In Windows, we finally operate audio and video data by calling functions in the FFmpeg dynamic library (dll). One of the ways to use dll requires the use of three types of files:
.h: header file (Header File)
Contains the declaration of the function
Import the corresponding header file
.dll through *#include*: Dynamic Link Library (Dynamic Link Library)
Contains the specific implementation code of the function.
The Windows program will dynamically call the function .lib or .dll.a in the dll during the running process
: (Dynamic Link Library) Import Library (Import Library)
.lib: used in MSVC compiler (Microsoft compiler)
.dll.a: used in MinGW compiler (g++ compiler)
contains the entry of the function in the dll, used to assist in finding and calling the function in the dll, inside Only index information is stored. In the end, it needs to be linked into the Windows program (such as merged into the exe file)
Essence:! ! ! ! It is a static library file, and this part is to be put into the exe file. Then guide the exe program to find the function body in the dll file.
It is worth mentioning that in Windows, the extension of the static link library (Static Link Library) is also .lib, .dll.a. The difference between a static link library and an import library is:
Static link library: contains the specific implementation code of the function
Import library: does not contain the specific implementation code of the function (the specific implementation code of the function is stored in the dll)
(0) The search order of the dll (when the exe is running)
- Instruct whether the corresponding dynamic link library can be found when the windows program is running .
When the exe is looking for dll files, it probably looks for them in the following order of priority (only the approximate search paths are listed here, not all of them):
The path in the environment variable Path
So you can consider configuring the %FFMPEG_HOME%/bin directory in the Path variable
and place it on how to configure the Windows environment variable Path. This is basic development common sense, so I won’t explain it anymore.
2) .pro file (when exe is compiled)
Different from the role of the previous configuration environment variable, this is to guide the linking process of the program , whether the corresponding dynamic library can be found.
.pro
file is the main configuration file for the Qt project. (Equivalent to files in linux makefile
, used to guide compilation)
# 包含了core、gui两个模块
QT += core gui
# 高于4版本,就包含widgets模块
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# 源代码
SOURCES += \
main.cpp \
mainwindow.cpp
# 头文件
HEADERS += \
mainwindow.h
# ui文件
FORMS += \
mainwindow.ui
The configuration is as follows
Modify the .pro file so that our project supports ffmpeg library functions
The configuration of this .pro file is to guide the program to find the corresponding dynamic library when compiling. The environment variable configured before is to guide Windows to find the corresponding dynamic library when running the program.
INCLUDEPATH, LIBS, are environment variables defined by QT itself. Used to store header file paths and library file paths.
FFMPEG_HOME: A variable defined by ourselves to mark our home directory.
win32: {
FFMPEG_HOME=D:\SF\ffmpeg\MJ\ffmpeg-4.3.2-2021-02-27-full_build-shared
#设置 ffmpeg 的头文件
INCLUDEPATH += $$FFMPEG_HOME/include
#设置导入库的目录一边程序可以找到导入库
# -L :指定导入库的目录
# -l :指定要导入的 库名称
LIBS += -L$$FFMPEG_HOME/lib \
-lavcodec \
-lavdevice \
-lavfilter \
-lavformat \
-lavutil \
-lpostproc \
-lswresample \
-lswscale
}
The content after the # sign is a comment
$$FFMPEG_HOME represents a reference to the value of the environment variable, equivalent to in-place expansion.
-L: Set the directory of the import library so that the compiler can find the import library. (The exe program only needs to include the xxx.dll.a static import library when compiling, and the xxx.dll file is only needed when running)
-l: Set the name of the import library that needs to be linked
The name of the imported library needs to remove the lib in front of the file name, for example, libavcodec.dll.a is written as avcodec
- Print the version of av_version_info :
#include "mainwindow.h"
#include <QApplication>
#include <QDebug> // 利用打印函数
// ffmpeg 是纯 C 语言的代码,在 C++ 当中不能直接进行 include
extern "C" {
#include <libavcodec/avcodec.h>
}
int main(int argc, char *argv[])
{
qDebug() << "yeahhh"<< av_version_info();
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Console output: