ffmpeg解码RTSP/TCP视频流H.264(QT界面显示视频画面)

您所在的位置:网站首页 ffmpeg解码udp视频流接口 ffmpeg解码RTSP/TCP视频流H.264(QT界面显示视频画面)

ffmpeg解码RTSP/TCP视频流H.264(QT界面显示视频画面)

2024-07-16 23:23| 来源: 网络整理| 查看: 265

我用的ffmpeg版本为 ffmpeg-2.1.8.tar.bz2  版本低了恐怕有些头文件和API找不到。  在Linux下解压后编译,Linux下编译很简单,我这里生成的动态库:  ./configure –enable-shared  make  就能找到各个so动态库文件。  移动位置后,记得手动链接 一下:

ln -s libavcodec.so.55 libavcodec.so ln -s libavdevice.so.55 libavdevice.so ln -s libavfilter.so.3 libavfilter.so ln -s libavformat.so.55 libavformat.so ln -s libavutil.so.52 libavutil.so ln -s libswscale.so.2 libswscale.so123456123456

QT pro文件中记得加入:  INCLUDEPATH += ffmpeg/include  // windows下用这几个  win32: LIBS += ffmpeg/lib/libavcodec.dll.a ffmpeg/lib/libavfilter.dll.a ffmpeg/lib/libavformat.dll.a ffmpeg/lib/libswscale.dll.a ffmpeg/lib/libavutil.dll.a  // Linux下用这几个  LIBS += -lavcodec -lavdevice -lavfilter -lavformat -lavutil -lswscale -L./ffmpeg/so

我这里对外提供三个接口:

void VideoStream::setUrl(QString url) { m_str_url = url; } void VideoStream::startStream() { videoStreamIndex=-1; av_register_all();//注册库中所有可用的文件格式和解码器 avformat_network_init();//初始化网络流格式,使用RTSP网络流时必须先执行 pAVFormatContext = avformat_alloc_context();//申请一个AVFormatContext结构的内存,并进行简单初始化 pAVFrame=av_frame_alloc(); if (this->Init()) { m_timerPlay->start(); } } void VideoStream::stopStream() { m_timerPlay->stop(); avformat_free_context(pAVFormatContext); av_frame_free(&pAVFrame); sws_freeContext(pSwsContext); }1234567891011121314151617181920212223242512345678910111213141516171819202122232425

里面与ffmpeg解码相关的私有变量:

QMutex mutex; AVPicture pAVPicture; AVFormatContext *pAVFormatContext; AVCodecContext *pAVCodecContext; AVFrame *pAVFrame; SwsContext * pSwsContext; AVPacket pAVPacket;12345671234567

在QT里,用一个QLabel的对象来显示解码后的视频画面:

connect(this,SIGNAL(GetImage(QImage)),this,SLOT(SetImageSlots(QImage))); ... void VideoStream::SetImageSlots(const QImage &image) { if (image.height()>0){ QPixmap pix = QPixmap::fromImage(image.scaled(m_i_w,m_i_h)); m_label->setPixmap(pix); } }1234567891012345678910

这里用的QTimer来进行一帧帧数据的解码,也可以用一个线程比如QThread来进行解码:

m_timerPlay = new QTimer; m_timerPlay->setInterval(10); connect(m_timerPlay,SIGNAL(timeout()),this,SLOT(playSlots())); m_i_frameFinished = 0; ... bool VideoStream::Init() { if(m_str_url.isEmpty()) return false; //打开视频流 int result=avformat_open_input(&pAVFormatContext, m_str_url.toStdString().c_str(),NULL,NULL); if (resultheight; avpicture_alloc(&pAVPicture,PIX_FMT_RGB24,videoWidth,videoHeight); AVCodec *pAVCodec; //获取视频流解码器 pAVCodec = avcodec_find_decoder(pAVCodecContext->codec_id); pSwsContext = sws_getContext(videoWidth,videoHeight,PIX_FMT_YUV420P,videoWidth,videoHeight,PIX_FMT_RGB24,SWS_BICUBIC,0,0,0); //打开对应解码器 result=avcodec_open2(pAVCodecContext,pAVCodec,NULL); if (result


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3