在windows平台上编译ffmpeg源码有些难度,我们这里采取简单方式,即直接引用ffmpeg官方编译好的库文件,将ffmpeg以第三方库的形式引入到QT中。
第一步 ffmpeg官网中下载文件,链接如下
Releases · BtbN/FFmpeg-Builds (github.com)
选择shared版本,下载解压后即可看见shared和dev两个文件夹,其中shared存放的为动态库和可ffmpeg,ffplay,ffprobe执行文件,dev文件夹是include头文件和lib链接库
第二步 新建QT项目,这里任意项目都可以,但注意编译器选择MinGW,注意64/32版本问题。
第三步 Pro文件中加入以下代码,我这里采用的是相对位置引用,绝对路径和相对路径都可以。
#ffmpeg外部库
INCLUDEPATH += $$PWD/ffmpeg-4.2.2-win64-dev/ffmpeg/include
LIBS += $$PWD/ffmpeg-4.2.2-win64-dev/ffmpeg/lib/avformat.lib\
$$PWD/ffmpeg-4.2.2-win64-dev/ffmpeg/lib/avcodec.lib\
$$PWD/ffmpeg-4.2.2-win64-dev/ffmpeg/lib/avdevice.lib\
$$PWD/ffmpeg-4.2.2-win64-dev/ffmpeg/lib/avfilter.lib\
$$PWD/ffmpeg-4.2.2-win64-dev/ffmpeg/lib/avutil.lib\
$$PWD/ffmpeg-4.2.2-win64-dev/ffmpeg/lib/postproc.lib\
$$PWD/ffmpeg-4.2.2-win64-dev/ffmpeg/lib/swresample.lib\
$$PWD/ffmpeg-4.2.2-win64-dev/ffmpeg/lib/swscale.lib
相对路径是把ffmpeg资源文件夹复制到QT项目路径下,绝对路径是ffmpeg资源文件夹随便放到电脑任意位置,引用时大概如下,
#ffmpeg外部库
INCLUDEPATH += D:........./ffmpeg-4.2.2-win64-dev/ffmpeg/include
LIBS += D:........./ffmpeg-4.2.2-win64-dev/ffmpeg/lib/avformat.lib\
D:........./ffmpeg-4.2.2-win64-dev/ffmpeg/lib/avcodec.lib\
D:........./ffmpeg-4.2.2-win64-dev/ffmpeg/lib/avdevice.lib\
D:........./ffmpeg-4.2.2-win64-dev/ffmpeg/lib/avfilter.lib\
D:........./ffmpeg-4.2.2-win64-dev/ffmpeg/lib/avutil.lib\
D:........./ffmpeg-4.2.2-win64-dev/ffmpeg/lib/postproc.lib\
D:........./ffmpeg-4.2.2-win64-dev/ffmpeg/lib/swresample.lib\
D:........./ffmpeg-4.2.2-win64-dev/ffmpeg/lib/swscale.lib
第四步 main文件中加入头文件,因为ffmpeg是C语言编写的,所以要加入extern “C”,具体如下
//引入C链接库
extern "C"
{
#include "libavdevice/avdevice.h"
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/avcodec.h"
#include "libswresample/swresample.h" //重采样头文件
}
main文件中加入
qDebug()<<"version"<<avcodec_version();
打印出如上结果则说明ffmpeg引入成功