1.简介:
Qtav库是基于FFmpeg和Portaudio的基础上进行再次封装,方便使用的音视频播放库,因此在使用的使用必须依赖ffmpeg和protaudio.
2.材料准备:
1) 下载QTAV源码:https://github.com/wang-bin/QtAV
2) 下载ffmpeg和protaudio:下载FFmpeg和Portaudio库以及相应的头文件,因为QtAV库是基于这两个库的。FFmpeg库不用自己编译,去官网上下载即可.
本人CSDN上传有一份QTav编译需要的所有材料,不想麻烦的人也可以直接去下载为上次的材料 地址:http://download.csdn.net/download/u012532263/10193831
3. qt5.3.2环境变量:
使用qtcreate打开QtAV.pro工程文件
添加环境变量:然后按下图添加环境变量。注意Qt中斜杠的方向,有的博客上面写的方向跟我相反,如果编译有错的话,就换个方向试一下。然后就可以编译了(debug和realease两个版本都要配置)
4. 基于QtAv库实现简单的视频播放
为了方便大家学习,播放器例子我也上传DSCN了,有需要的朋友可以直接下载学习 地址:http://download.csdn.net/download/u012532263/10193906
源码我也贴一下,毕竟下载要分:
.h文件
/******************************************************************************
Simple Player: this file is part of QtAV examples
Copyright (C) 2012-2015 Wang Bin <[email protected]>
* This file is part of QtAV
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#ifndef PLAYERWINDOW_H
#define PLAYERWINDOW_H
#include <QWidget>
#include <QtAV/QtAV>
class QLabel;
class QSlider;
class QPushButton;
class PlayerWindow : public QWidget
{
Q_OBJECT
public:
explicit PlayerWindow(QWidget *parent = 0);
public Q_SLOTS:
void openMedia();
void seek(int);
void playPause();
void stop();
void imageCaptured(QImage);
private:
void InitQtAV();
void CreateUI();
QString SwitchTimes(const int&);
private Q_SLOTS:
void updateSlider();
void loadFinish();
private:
QtAV::VideoOutput* m_vo;
QtAV::AVPlayer* m_player;
QSlider* m_slider;
QPushButton* m_openBtn;
QPushButton* m_playBtn;
QPushButton* m_stopBtn;
QPushButton* m_captureBtn;
QLabel* m_TotalDurationLab;
QLabel* m_CurrentDurationLab;
};
#endif // PLAYERWINDOW_H
Simple Player: this file is part of QtAV examples
Copyright (C) 2012-2014 Wang Bin <[email protected]>
* This file is part of QtAV
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "playerwindow.h"
#include <QPushButton>
#include <QSlider>
#include <QLayout>
#include <QLabel>
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include <QtAV/VideoRenderer.h>
using namespace QtAV;
PlayerWindow::PlayerWindow(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("QtAV simple player example");
InitQtAV();
CreateUI();
}
void PlayerWindow::openMedia()
{
QString file = QFileDialog::getOpenFileName(0, "Open a video","","*.mp4 *.rmvb *.mkv *.avi");
if (file.isEmpty())
return;
m_player->play(file);
}
void PlayerWindow::seek(int pos)
{
if (!m_player->isPlaying())
return;
m_player->seek(pos*1000LL); // to msecs
}
void PlayerWindow::playPause()
{
if (!m_player->isPlaying()) {
m_player->play();
return;
}
m_player->pause(!m_player->isPaused());
}
void PlayerWindow::stop()
{
m_player->stop();
m_slider->setValue(0);
m_TotalDurationLab->setText("00:00");
m_CurrentDurationLab->setText("00:00");
}
void PlayerWindow::imageCaptured(QImage image)
{
QString savepath = QFileDialog::getSaveFileName(this,"Save Picture","captured","(*.png);; (*.jpg);; (*.jpge);; (*.bmp)");
if(!savepath.isEmpty())
image.save(savepath);
}
void PlayerWindow::InitQtAV()
{
//AV初始化
m_player = new AVPlayer(this);
m_vo = new VideoOutput(this);
if (!m_vo->widget()) {
QMessageBox::warning(0, "QtAV error", "Can not create video renderer");
return;
}
m_player->setRenderer(m_vo);
}
void PlayerWindow::CreateUI()
{
//控制按钮
m_openBtn = new QPushButton("Open");
m_playBtn = new QPushButton("Play/Pause");
m_stopBtn = new QPushButton("Stop");
m_captureBtn = new QPushButton("Capture");
QHBoxLayout* hlayout = new QHBoxLayout;
hlayout->addWidget(m_openBtn);
hlayout->addWidget(m_playBtn);
hlayout->addWidget(m_stopBtn);
hlayout->addWidget(m_captureBtn);
//时间进度条
m_slider = new QSlider();
m_slider->setOrientation(Qt::Horizontal);
m_TotalDurationLab = new QLabel("00:00");
m_CurrentDurationLab = new QLabel("00:00");
m_TotalDurationLab->setFixedHeight(25);
QHBoxLayout* hlayout1 = new QHBoxLayout;
hlayout1->addWidget(m_CurrentDurationLab);
hlayout1->addSpacing(3);
hlayout1->addWidget(m_slider);
hlayout1->addSpacing(3);
hlayout1->addWidget(m_TotalDurationLab);
QVBoxLayout* mainlayout = new QVBoxLayout(this);
mainlayout->addWidget(m_vo->widget());
mainlayout->addLayout(hlayout1);
mainlayout->addLayout(hlayout);
connect(m_slider, SIGNAL(sliderMoved(int)), SLOT(seek(int)));
connect(m_player, SIGNAL(positionChanged(qint64)), SLOT(updateSlider()));
connect(m_player, SIGNAL(started()), SLOT(updateSlider()));
connect(m_player, SIGNAL(loaded()), SLOT(loadFinish()));
connect(m_openBtn, SIGNAL(clicked()), SLOT(openMedia()));
connect(m_playBtn, SIGNAL(clicked()), SLOT(playPause()));
connect(m_stopBtn, SIGNAL(clicked()), SLOT(stop()));
connect(m_captureBtn, SIGNAL(clicked()),m_player->videoCapture(), SLOT(request()));
connect(m_player->videoCapture(), SIGNAL(imageCaptured(QImage)), SLOT(imageCaptured(QImage)));
}
QString PlayerWindow::SwitchTimes(const int &total)
{
QString hhs,mms,sss;
int hh = total/3600;
int mm = (total-hh*3600)/60;
int ss = (total-hh*3600)%60;
if(hh < 10)
hhs = QString("0%1").arg(hh);
else
hhs = QString("%1").arg(hh);
if(mm < 10)
mms = QString("0%1").arg(mm);
else
mms = QString("%1").arg(mm);
if(ss < 10)
sss = QString("0%1").arg(ss);
else
sss = QString("%1").arg(ss);
if(hh == 0)
return QString("%1:%2").arg(mms).arg(sss);
else
return QString("%1:%2:%3").arg(hhs).arg(mms).arg(sss);
}
void PlayerWindow::updateSlider()
{
m_slider->setRange(0, int(m_player->duration()/1000LL));
m_slider->setValue(int(m_player->position()/1000LL));
m_CurrentDurationLab->setText(SwitchTimes(m_player->position()/1000LL));
}
void PlayerWindow::loadFinish()
{
m_TotalDurationLab->setText(SwitchTimes(m_player->duration()/1000LL));
}