How to convert 'QVideoFrame' with YUV data to 'QVideoframe' with RGBA32 data in Qt?

前端 未结 3 1314
一个人的身影
一个人的身影 2021-01-15 13:33

I receive QVideoFrames from webcam, and they contain image data in YUV format (QVideoFrame::Format_YUV420P). How can I convert one

3条回答
  •  生来不讨喜
    2021-01-15 14:27

    I found a solution that is built into Qt5, but UNSUPPORTED BY Qt.

    Here is how to go about:

    1. Put QT += multimedia-private into your qmake .pro file
    2. Put #include "private/qvideoframe_p.h" into your code to make the function available.
    3. You now have access to a function with the following signature: QImage qt_imageFromVideoFrame(const QVideoFrame &frame);
    4. Use the function to convert the QVideoFrame to a temporay QImage and then create the output QVideoFrame from that image.

    Here is my example usage:

    QVideoFrame convertFormat(const QVideoFrame &inputframe, QVideoFrame::PixelFormat outputFormat)
        {
            inputframe->map(QAbstractVideoBuffer::ReadOnly);
            QImage tempImage=qt_imageFromVideoFrame(inputframe);
            inputframe->unmap();
            QVideoFrame outputFrame=QVideoFrame(tempImage);
            return outputFrame;
        }
    

    Again, the warning copied from the header reads as follows:

    //
    //  W A R N I N G
    //  -------------
    //
    // This file is not part of the Qt API.  It exists purely as an
    // implementation detail.  This header file may change from version to
    // version without notice, or even be removed.
    //
    // We mean it.
    //
    

    This does not matter in my project since it is a personal toy product. If it ever gets serious I will just track down the implementation of that function and copy it into my project or something.

提交回复
热议问题