本文主要讲述ffmpeg解码的视频如何现实到qml窗口上的过程,文末有代码地址。文章开始处先贴一张效果图
1、绘制类
通过继承QQuickPaintedItem可以使用QPainter接口将一帧解码QImage绘制到Qml上面。(XVideo是QQuickPaintedItem的子类)当接收到一帧(QImage)的时候直接调用update()更新。
void XVideo::paint(QPainter *pPainter)
{
if (!m_Frame.isNull())
{
qDebug()<<"painter";
pPainter->drawImage(QRect(0, 0, nWidth, nHeight), m_Frame);
}
}
2、解码类
使用ffmpeg接收视频流,将视频流解码成QImage然后传递给QQuickPaintedItem的子类(XVideoThread是QThread的子类,因为解码过程比较消耗资源如果放在主线程中会造成UI卡死)ffmpeg的转码过程可以参考雷神的博客。这边贴一段参考后的代码。
void XVideoThread::run()
{
m_pInFmtCtx = avformat_alloc_context();
if(avformat_open_input(&m_pInFmtCtx, m_strPath.c_str(), NULL, NULL))
{
qDebug()<<"get rtsp failed";
return;
}
else
{
qDebug()<<"get rtsp success";
}
if(avformat_find_stream_info(m_pInFmtCtx, NULL) < 0)
{
qDebug()<<"could not find stream information";
return;
}
int nVideoIndex = -1;
for(int i = 0; i < m_pInFmtCtx->nb_streams; i++)
{
if(m_pInFmtCtx->streams[i]->codec->codec_type =&#