Qt+ffmpeg解码视频

7 篇文章 1 订阅
3 篇文章 0 订阅

Qt+ffmpeg解码视频


http://www.oschina.net/code/snippet_1466652_44272



1. [文件] main.cpp ~ 176B     下载(0)     

?
1
2
3
4
5
6
7
8
9
10
11
#include "video.h"
#include <QApplication>
 
int main( int argc, char *argv[])
{
     QApplication a(argc, argv);
     Video w;
     w.show();
 
     return a.exec();
}

2. [文件] video.h ~ 597B     下载(0)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef VIDEO_H
#define VIDEO_H
 
 
#include <QThread>
#include <QWidget>
#include <QDebug>
#include <QImage>
#include <QPixmap>
#include "videodec.h"
class VideoDec;
namespace Ui {
class Video;
}
 
class Video : public QWidget
{
     Q_OBJECT
 
public :
     explicit Video(QWidget *parent = 0);
     ~Video();
     QThread *VideoDecThread;
     VideoDec *OVideoDec;
signals:
     void init_s();
     void play_s();
public slots:
     void showVideo( const QImage &img);
private slots:
     void on_VideoPlay_clicked();
 
private :
     Ui::Video *ui;
};
 
#endif // VIDEO_H

3. [文件] video.cpp ~ 888B     下载(0)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "video.h"
#include "ui_video.h"
 
Video::Video(QWidget *parent) :
     QWidget(parent),
     ui( new Ui::Video)
{
     ui->setupUi( this );
     VideoDecThread = new QThread( this );
     OVideoDec = new VideoDec;
     OVideoDec->moveToThread(VideoDecThread);
     VideoDecThread->start();
     connect( this ,SIGNAL(init_s()),OVideoDec,SLOT(init()));
     connect( this ,SIGNAL(play_s()),OVideoDec,SLOT(play()));
     //connect(OVideoDec,SIGNAL(SendImage(QImage)),this,SLOT(showVideo(QImage)));
     connect(OVideoDec,SIGNAL(SendImage(QImage)), this ,SLOT(showVideo(QImage)));
}
 
Video::~Video()
{
     delete ui;
}
 
 
void Video::on_VideoPlay_clicked()
{
     emit init_s();
     //emit play_s();
}
void Video::showVideo( const QImage &img)
{
 
     QPixmap pix;
     pix = pix.fromImage(img).scaledToWidth(ui->VideoShow->width());
     ui->VideoShow->setPixmap(pix);
}

4. [文件] videodec.h ~ 790B     下载(0)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef VIDEODEC_H
#define VIDEODEC_H
extern "C" {
     #include "libavcodec/avcodec.h"
     #include "libavformat/avformat.h"
     #include "libavutil/avutil.h"
     #include "libavutil/mem.h"
     #include "libavutil/fifo.h"
     #include "libswscale/swscale.h"
}
#include <QObject>
#include <QImage>
#include <QThread>
#include <QDebug>
 
class VideoDec : public QObject
{
     Q_OBJECT
public :
     explicit VideoDec(QObject *parent = 0);
     int videoindex;
     AVFormatContext *pFormatCtx;
     AVCodecContext *pCodecCtx;
     AVCodec *pCodec;
     AVPacket *packet;
     AVFrame *pFrame,*pFrameRGB;
     char *FileName = "E:/mv/1.mp4" ;
 
 
 
signals:
     void SendImage(QImage img);
 
public slots:
     void init();
     void play();
 
};
 
#endif // VIDEODEC_H

5. [文件] videodec.cpp ~ 2KB     下载(0)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "videodec.h"
 
VideoDec::VideoDec(QObject *parent) :
     QObject(parent)
{
}
void VideoDec::init()
{
     qDebug()<< "init" ;
     avcodec_register_all();
     av_register_all();
     pFormatCtx = avformat_alloc_context();
     if (avformat_open_input(&pFormatCtx,FileName,NULL,NULL)!=0){
         qDebug()<< "OpenFail" ;
     }
     if (avformat_find_stream_info(pFormatCtx,NULL)<0){
         qDebug()<< "FindFail" ;
     }
     videoindex = -1;
     for ( int i=0;pFormatCtx->nb_streams;i++){
         if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
             videoindex = i;
             break ;
         }
     }
     if (videoindex == -1){
         qDebug()<< "Din't find video stream" ;
     }
     pCodecCtx = pFormatCtx->streams[videoindex]->codec;
     pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
     if (pCodec == NULL){
         qDebug()<< "codec not find" ;
     }
     if (avcodec_open2(pCodecCtx,pCodec,NULL)<0){
         qDebug()<< "can't open codec" ;
     }
     pFrame = avcodec_alloc_frame();
     pFrameRGB = avcodec_alloc_frame();
     play();
}
void VideoDec::play()
{
     int got_picture;
 
     int y_size = pCodecCtx->width*pCodecCtx->height;
     packet=(AVPacket *) malloc ( sizeof (AVPacket));
     av_new_packet(packet, y_size);
 
     uint8_t *out_buffer;
     out_buffer = new uint8_t[avpicture_get_size(PIX_FMT_RGB32, pCodecCtx->width, pCodecCtx->height)]; //分配AVFrame所需内存
     avpicture_fill((AVPicture *)pFrameRGB, out_buffer, PIX_FMT_RGB32, pCodecCtx->width, pCodecCtx->height); //填充AVFrame
     while (av_read_frame(pFormatCtx,packet)>=0){
         if (packet->stream_index == videoindex){
             int rec = avcodec_decode_video2(pCodecCtx,pFrame,&got_picture,packet);
             if (rec>0){
                 SwsContext *convertCtx = sws_getContext(pCodecCtx->width,pCodecCtx->height
                                                         ,pCodecCtx->pix_fmt,pCodecCtx->width,pCodecCtx->height
                                                         ,PIX_FMT_RGB32,SWS_BICUBIC,NULL,NULL,NULL);
                 sws_scale(convertCtx,( const uint8_t*  const *)pFrame->data,pFrame->linesize,0
                           ,pCodecCtx->height,pFrameRGB->data,pFrameRGB->linesize);
                 QImage Img((uchar *)pFrameRGB->data[0],pCodecCtx->width,pCodecCtx->height,QImage::Format_RGB32);
                 emit SendImage(Img);
             }
         }
         av_free_packet(packet);
         QThread::msleep(4);
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值