原文地址:https://blog.youkuaiyun.com/peihexian/article/details/113603729
qtav集成使用备忘
1.使用qtav不需要从ffmpeg官网下载源码
qtav本身是封装操作ffmpeg的qt组件库,按道理来讲应该依赖ffmpeg,事实上也确实依赖,只是你要是喜欢从头自己编译ffmpeg源码的话可以从ffmpeg官方网站下载源码进行编译使用,如果想编译32位windows图像播放程序完全可以不用ffmpeg官方网站的任何东西,qtav的作者很贴心,把需要用到的东西都准备好了。
2.编译集成qtav过程
git clone https://github.com/wang-bin/QtAV.git
cd QtAV && git submodule update --init
执行效果如下图所示:
下面是我尝试折腾最耗时的部分,因为我想编译32位的ffmpeg应用,尝试从ffmpeg官网跳转的github下载包时没仔细看,文件都是按64位进行预编译的,导致我折腾32位qtav编译死活过不去,这也是为啥说qtav作者提供的东西就够我们用了,不用去ffmpeg官方网站下载源码进行编译的原因。
qtav的作者提供了ffmpeg的预编译版本,包括32位windows与64位windows版本和gcc版本,下载地址为:https://sourceforge.net/projects/qtav/files/depends/ ,下载QtAV-depends-windows-x86+x64.7z解压缩备用。
用QT Creator打开QtAV.pro工程,打开qtav.pro文件,在顶部增加以下配置:
INCLUDEPATH += D:/cpp_tools/QtAV-depends-windows-x86+x64/QtAV-depends-windows-x86+x64/include
LIBS+= -L D:/cpp_tools/QtAV-depends-windows-x86+x64/QtAV-depends-windows-x86+x64/lib
这里的D:\cpp_tools\QtAV-depends-windows-x86+x64\QtAV-depends-windows-x86+x64就是我解压缩qtav作者预编译ffmpeg库的路径,你们根据需要更换成自己的解压缩路径。
修改工程的编译环境变量,vc编译器的话修改INCLUDE\LIB\PATH三个变量,分别增加(注意只是增加以下路径,不是覆盖)预编译库的路径信息:
INCLUDE | D:\cpp_tools\QtAV-depends-windows-x86+x64\QtAV-depends-windows-x86+x64\include |
LIB | D:\cpp_tools\QtAV-depends-windows-x86+x64\QtAV-depends-windows-x86+x64\lib |
Path | D:\cpp_tools\QtAV-depends-windows-x86+x64\QtAV-depends-windows-x86+x64\bin |
如下:
编译项目,最终形成如下图所示的结果:
双击sdk_install.bat进行安装。
Hello World程序编写
新建一个mainwindow工程,修改工程的pro文件,增加以下内容:
QT += avwidgets
Release:LIBS += -L$$quote(D:\Qt\qt5_15_msvc_static_x86\lib) -L$$quote(D:\cpp_tools\QtAV-depends-windows-x86+x64\QtAV-depends-windows-x86+x64\lib) -lQt5AV -lQt5AVWidgets
Debug:LIBS +=-L$$quote(D:\Qt\qt5_15_msvc_static_x86\lib) -L$$quote(D:\cpp_tools\QtAV-depends-windows-x86+x64\QtAV-depends-windows-x86+x64\lib) -lQt5AVd -lQt5AVWidgetsd
D:\Qt\qt5_15_msvc_static_x86\lib 这个目录是我的qt5.15路径,也就是上一步sdk_install复制文件的目标路径,D:\cpp_tools\QtAV-depends-windows-x86+x64\QtAV-depends-windows-x86+x64\lib是qtav作者预编译ffmpeg库的位置,因为项目运行的时候还需要一个swresample.lib库文件,需要到预编译路径里面去找。
头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtAV>
#include <QtAVWidgets>
#include <QFile>
#include <QBuffer>
#include <QFileDialog>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QtAV::VideoOutput *m_vo;
QtAV::AVPlayer *m_player;
};
#endif // MAINWINDOW_H
cpp文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QtAV::Widgets::registerRenderers();
m_player = new QtAV::AVPlayer(this);
m_vo = new QtAV::VideoOutput(this);
m_player->setRenderer(m_vo);
setCentralWidget(m_vo->widget());
QString file = QFileDialog::getOpenFileName(0, tr("Open a video"));
if (file.isEmpty())
return;
m_player->play(file);
}
MainWindow::~MainWindow()
{
delete ui;
}
运行结果:
----------------------------------------------
补充,uos linux下面的配置过程记录:
1.安装依赖
sudo apt install libavcodec-dev libavfilter-dev libavresample-dev
sudo apt-get install libglfw3-dev libgles2-mesa-dev
2. 源码克隆
git clone https://github.com/wang-bin/QtAV.git
cd QtAV
sudo git submodule update --init
3.
用qt creator打开Qtav.pro,修改include路径
INCLUDEPATH += /home/data/qtav/QTAV/include
编译,运行编译结果里面的sdk_install.sh安装.