Qt 集成 FFmpeg 实现颜色格式转换

目录
1. Qt 集成
1.1 下载
1.2 Qt 集成
1.2.1 修改 .pro 文件
1.2.2 放入dll 文件
1.2.3 代码中使用
2. 图像格式转换
3. 预览
4. 项目地址
项目需要,写个小工具来实现图像颜色格式的转换,主要的如下:
支持输入输出图像格式选择:支持对输入图像进行缩放;支持对输入输出图像预览;
支持的图像格式转换如下:
由于输入输出为纯图片文件,所以需要指定图像的宽高信息;
1. Qt 集成
Qt 中集成的文章非常多,大家可以自行搜索,这里简单的提一下集成的过程;
上,集成的方式有 2 种:
1. 下载的库,代码中引用头文件来使用 ;
2. 下载源码,自行构建;
这里选择第一种,下载的 DLL 来集成;
1.1 下载
【Qt 集成 FFmpeg 实现颜色格式转换】下载地址:
选择,下面有 2 个 build,随便选一个,我选择的是第一个;
进入后,最先看到的是的 build,咱们选个的稳定版的,代表是动态链接库,我们选他:
解压后如下:
我们要 3 部分,头文件、动态库、和导入库:
动态库在:-5.1.2--\bin
头文件在:-5.1.2--\
导入库在:-5.1.2--\\lib
1.2 Qt 集成1.2.1 修改 .pro 文件
在 Qt 的工程文件中,指定导入的头文件以及库的地方
#-------------------------------------------------## Project created by QtCreator 2022-10-16T17:35:35##-------------------------------------------------QT+= core guiRC_ICONS = logo.icogreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = YUVEyesTEMPLATE = app# The following define makes your compiler emit warnings if you use# any feature of Qt which has been marked as deprecated (the exact warnings# depend on your compiler). Please consult the documentation of the# deprecated API in order to know how to port your code away from it.DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if you use deprecated APIs.# In order to do so, uncomment the following line.# You can also select to disable deprecated APIs only up to a certain version of Qt.#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000# disables all the APIs deprecated before Qt 6.0.0CONFIG += c++11SOURCES += \main.cpp \mainwindow.cpp \qtffmpegutils.cppHEADERS += \mainwindow.h \qtffmpegutils.h \error_code.hFORMS += \mainwindow.ui# Default rules for deployment.qnx: target.path = /tmp/$${TARGET}/binelse: unix:!android: target.path = /opt/$${TARGET}/bin!isEmpty(target.path): INSTALLS += targetwin32: {FFMPEG_HOME=G:\MediaCodeC\ffmpeg-5.1.2-full_build-shared#设置 ffmpeg 的头文件INCLUDEPATH += $$FFMPEG_HOME/include#设置导入库的目录一边程序可以找到导入库# -L :指定导入库的目录# -l :指定要导入的 库名称LIBS +=-L$$FFMPEG_HOME/lib \-lavcodec\-lavdevice\-lavfilter\-lavformat\-lavutil\-lpostproc\-lswresample\-lswscale}RESOURCES += \src.qrc
1.2.2 放入dll 文件
Qt 编译的时候,会生成一个编译的文件夹,将我们用到的 dll 扔进去:
好了,基本环境准备完毕;
1.2.3 代码中使用
在代码中,包含的头文件即可使用的库了:
打印的版本号:
2. 图像格式转换
图像格式转换可以使用的函数进行,该函数的原型为:
能够支持不同的像素格式转换,并且能够进行缩放功能;
这里定义一个专门使用的类 .h,来进行转换工作:
#ifndef QTFFMPEGUTILS_H#define QTFFMPEGUTILS_Hextern "C"{#include "libswscale/swscale.h"#include "libavutil/opt.h"#include "libavutil/imgutils.h"}#include "error_code.h"typedef struct {const char *filename;int width;int height;AVPixelFormat format;} RawVideoFile;typedef struct {char *pixels;int width;int height;AVPixelFormat format;} RawVideoFrame;class QtFFmpegUtils{public:QtFFmpegUtils();static int convertRawVideo(RawVideoFile &in, RawVideoFile &out);static int convertToRGB888(RawVideoFile &in, uint8_t *rgb888_data);static int getBufferSize(AVPixelFormat pix_fmt, int width, int height);//static void convertRawFrame(RawVideoFrame &in, RawVideoFrame &out);};#endif // QTFFMPEGUTILS_H