通过流媒体服务器(live555或VLC)发送视频流,FFmpeg接收RTSP视频流解码为RGB数据,QT界面同步显示1路或3路视频数据并实现截图等功能!
源码
#include "qffmpeg.h"
#include <QDateTime>
#include <QDebug>
QFFmpeg::QFFmpeg(QObject *parent) :
QObject(parent)
{
videoStreamIndex=-1;
av_register_all();//注册库中所有可用的文件格式和解码器
avformat_network_init();//初始化网络流格式,使用RTSP网络流时必须先执行
pAVFormatContext = avformat_alloc_context();//申请一个AVFormatContext结构的内存,并进行简单初始化
pAVFrame=av_frame_alloc();
}
QFFmpeg::~QFFmpeg()
{
avformat_free_context(pAVFormatContext);
av_frame_free(&pAVFrame);
sws_freeContext(pSwsContext);
}
bool QFFmpeg::Init()
{
//打开视频流
int result=avformat_open_input(&pAVFormatContext, url.toStdString().c_str(),NULL,NULL);
if (result<0){
// qDebug()<<"打开视频流失败";
return false;
}
//获取视频流信息
result=avformat_find_stream_info(pAVFormatContext,NULL);
if (result<0){
//qDebug()<<"获取视频流信息失败";
return false;
}
//获取视频流索引
videoStreamIndex = -1;
for (uint i = 0; i < pAVFormatContext->nb_streams; i++) {
if (pAVFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex==-1){
// qDebug()<<"获取视频流索引失败";
return false;
}
//获取视频流的分辨率大小
pAVCodecContext = pAVFormatContext->streams[videoStreamIndex]->codec;
videoWidth=pAVCodecContext->width;
videoHeight=pAVCodecContext->height;
avpicture_alloc(&pAVPicture, AV_PIX_FMT_RGB24, videoWidth, videoHeight);
AVCodec *pAVCodec;
//获取视频流解码器
pAVCodec = avcodec_find_decoder(pAVCodecContext->codec_id);
pSwsContext = sws_getContext(videoWidth, videoHeight, AV_PIX_FMT_YUV440P, videoWidth, videoHeight, AV_PIX_FMT_RGB24, SWS_BICUBIC, 0, 0, 0);
//打开对应解码器
result=avcodec_open2(pAVCodecContext,pAVCodec,NULL);
if (result<0){
//qDebug()<<"打开解码器失败";
return false;
}
qDebug()<<"sucess"; //初始化视频流成功
return true;
}
void QFFmpeg::Play()
{
//一帧一帧读取视频
int frameFinished=0;
while (true){
if (av_read_frame(pAVFormatContext, &pAVPacket) >= 0){
if(pAVPacket.stream_index==videoStreamIndex){
qDebug() << "start" << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"); //开始解码
avcodec_decode_video2(pAVCodecContext, pAVFrame, &frameFinished, &pAVPacket);
if (frameFinished){
mutex.lock();
sws_scale(pSwsContext,(const uint8_t* const *)pAVFrame->data,pAVFrame->linesize,0,videoHeight,pAVPicture.data,pAVPicture.linesize);
//发送获取一帧图像信号
QImage image(pAVPicture.data[0],videoWidth,videoHeight,QImage::Format_RGB888);
emit GetImage(image);
mutex.unlock();
}
}
}
av_free_packet(&pAVPacket);//释放资源,否则内存会一直上升
}
}
#include "frmmain.h"
#include "ui_frmmain.h"
#include "qffmpeg.h"
#include "rtspthread.h"
#include <QDebug>
#include <QPixmap> //截图
#include <QDesktopWidget>
frmMain::frmMain(QWidget *parent) :
QWidget(parent),
ui(new Ui::frmMain)
{
ui->setupUi(this);
tempWidth=320;
tempHeight=180;
video1Max=false;
video2Max=false;
video3Max=false;
all=false;
ui->labVideo1->installEventFilter(this);
ui->labVideo2->installEventFilter(this);
ui->labVideo3->installEventFilter(this);
}
frmMain::~frmMain()
{
delete ui;
}
//处理用户双击对应通道最大化处理
bool frmMain::eventFilter(QObject *obj, QEvent *event)
{
if (event->type()==QEvent::MouseButtonDblClick){
if (obj==ui->labVideo1){
if (video1Max){
tempWidth=320;
tempHeight=180;
ui->labVideo2->setVisible(true);
ui->labVideo3->setVisible(true);
ui->labImage->setVisible(true);
}else{
tempWidth=645;
tempHeight=370;
ui->labVideo2->setVisible(false);
ui->labVideo3->setVisible(false);
ui->labImage->setVisible(false);
}
video1Max=!video1Max;
}else if (obj==ui->labVideo2){
if (video2Max){
tempWidth=320;
tempHeight=180;
ui->labVideo1->setVisible(true);
ui->labVideo3->setVisible(true);
ui->labImage->setVisible(true);
}else{
tempWidth=645;
tempHeight=370;
ui->labVideo1->setVisible(false);
ui->labVideo3->setVisible(false);
ui->labImage->setVisible(false);
}
video2Max=!video2Max;
}else if (obj==ui->labVideo3){
if (video3Max){
tempWidth=320;
tempHeight=180;
ui->labVideo1->setVisible(true);
ui->labVideo2->setVisible(true);
ui->labImage->setVisible(true);
}else{
tempWidth=645;
tempHeight=370;
ui->labVideo1->setVisible(false);
ui->labVideo2->setVisible(false);
ui->labImage->setVisible(false);
}
video3Max=!video3Max;
}
}
return QObject::eventFilter(obj,event);
}
void frmMain::on_btnOpen_clicked()
{
QFFmpeg *ffmpeg=new QFFmpeg(this);
connect(ffmpeg,SIGNAL(GetImage(QImage)),this,SLOT(SetImage(QImage)));
ffmpeg->SetUrl(ui->txtUrl->text());
if (ffmpeg->Init()){
RtspThread *rtsp=new RtspThread(this);
rtsp->setffmpeg(ffmpeg);
rtsp->start();
}
}
void frmMain::on_btnGetImage_clicked()
{
ui->labImage->clear();
int index=ui->cboxVideo->currentIndex();
if (index==0){
if (ui->labVideo1->pixmap()!=0x0)
ui->labImage->setPixmap(*ui->labVideo1->pixmap());
}else if (index==1){
if (ui->labVideo2->pixmap()!=0x0)
ui->labImage->setPixmap(*ui->labVideo2->pixmap());
}else if (index==2){
if (ui->labVideo3->pixmap()!=0x0)
ui->labImage->setPixmap(*ui->labVideo3->pixmap());
}
//截图
QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), pos().x(), pos().y(),/*frameGeometry().width(),frameGeometry().height()*/100, 100);
pixmap.save("1.png", "png");
}
void frmMain::SetImage(const QImage &image)
{
if (image.height()>0){
QPixmap pix = QPixmap::fromImage(image.scaled(tempWidth,tempHeight));
ui->labVideo1->setPixmap(pix);
if (all){//启用三通道同步
ui->labVideo2->setPixmap(pix);
ui->labVideo3->setPixmap(pix);
}
}
}
void frmMain::on_ckAll_stateChanged(int arg1)
{
all=arg1!=0?true:false;
}
运行结果
![](https://i-blog.csdnimg.cn/blog_migrate/d912a9fbf2a0b1e2fd3b3cab69b73546.jpeg)
下载
https://download.csdn.net/download/u010872301/10542828