我需要在 Qt 小部件上显示 ffmpeg 帧。我知道 QtFFmpegWrapper,但它似乎已经过时了。我尝试使用 memcpy()
将数据从 RGB ffmpeg 帧复制到 QImage 并在其中出现未处理的异常。
QImage lastFrame;
lastFrame = QImage( screen_w, screen_h, QImage::Format_RGB888 );
for( int y = 0; y < screen_h; ++y )
memcpy( lastFrame.scanLine(y),
frameRGB -> data[0] + y * frameRGB -> linesize[0],
screen_w * 3 );
我在 ffmpeg 处理的所有部分都尝试了 sws_getContext()
和 sws_getCachedContext()
、AV_PIX_FMT_BGR24
和 AV_PIX_FMT_RGB24
.所有 ffmpeg 代码都来自流行的教程,并且可以很好地与 SDL
和 PIX_FMT_YUV420P
配合使用。
有什么想法吗? 也许这不是在 Qt 小部件上显示 ffmpeg 帧的最佳/最简单方法?
编辑。
好的,我将 Murat Şeker 的解决方案与 QImage::copy()
一起使用,但现在 QImage::isNull()
返回 true
。
我的一些 ffmpeg 代码:
out_buffer = (uint8_t*)av_malloc( avpicture_get_size( AV_PIX_FMT_RGB32,
codecCtx -> width, codecCtx -> height ));
avpicture_fill((AVPicture *)frameRGB, out_buffer, AV_PIX_FMT_RGB32,
codecCtx -> width, codecCtx -> height);
img_convert_ctx = sws_getContext( codecCtx -> width, codecCtx -> height,
codecCtx -> pix_fmt, codecCtx -> width,
codecCtx -> height, AV_PIX_FMT_RGB32,
SWS_BICUBIC, NULL, NULL, NULL );
/* ... */
if( got_picture ){
sws_scale( img_convert_ctx, (const uint8_t* const*)frame -> data,
frame -> linesize, 0, codecCtx -> height, frameRGB -> data,
frameRGB -> linesize );
QImage imgFrame = QImage( frameRGB -> data[0], frameRGB -> width,
frameRGB -> height, frameRGB -> linesize[0],
QImage::Format_RGB32 ).copy();
if( imgFrame.isNull()){} // true
// But I can write this frame to hard disk using BITMAPFILEHEADER
SaveBMP( frameRGB, codecCtx -> width, codecCtx -> height ); // it works
}
最佳答案
以下对我有用:
QImage frame = QImage(avFrame->data[0], avFrame->width, avFrame->height,
avFrame->linesize[0], QImage::Format_RGB32)
.copy();
关于c++ - 在 Qt5 中显示 FFmpeg 帧的最佳/最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30784549/