c++ - 在 Qt5 中显示 FFmpeg 帧的最佳/最简单方法

标签 c++ image qt video ffmpeg

我需要在 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_BGR24AV_PIX_FMT_RGB24 .所有 ffmpeg 代码都来自流行的教程,并且可以很好地与 SDLPIX_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/

相关文章:

c++ - 将在 C++ 中创建的动态数组

ios - Objective-C 如何只压缩视频而不压缩视频和文本叠加层?

c++ - Qt 5.2.1如何重新配置​​?

c++ - n 个字节的低 7 位串联

c++ - 如何将没有构造函数参数的对象放置到 STL 容器中?

c++ - 从代码中添加作者、版本等信息

android - picasso 加载在 AsyncTask 中生成的图像

javascript - 当浏览器调整大小时,图像上的文本不起作用

c++ - 在 Visual Studio 2017 中使用 Qt 库的 CMake 项目 : can't find Qt dll when running the exe

c++ - 无法链接到qt中的boost文件系统