使用Qt 5.0以编程方式创建视频

Joe*_*oey 14 c++ qt opencv ffmpeg

我们有一个QT应用程序,它将以编程方式生成的QPixmaps逐个呈现给显示器,我们希望将此输出保存到视频文件中.

我知道在过去人们建议使用ffmpeg或opencv与Qt这样做.然而,在Qt 5中,新的QtMultimedia模块似乎暴露了一些此类功能.

例如,现在可以使用QdiaRecorder在Qt 5中保存来自摄像机源的视频,如http://doc.qt.io/qt-5/qmediarecorder.html#details中所述.

有了这个新功能,有没有办法使用Qt 5来保存我们以编程方式生成的视频,或者我最好还是使用第三方库?

Sir*_*sar 5

这实际上可以在Qt 4.7中通过使用QVideoFrame和QAbstractVideoSurface来实现.Qt甚至有一个很好的例子来创建一个可以显示编程构造的QVideoFrames的Video Widget:

http://qt-project.org/doc/qt-4.8/multimedia-videowidget.html

您可以将此窗口小部件与QVideoFrame的映射功能结合使用,以使用格式正确的数据填充各个视频帧.这应该是这样的:

实例化您的videoWidget:

VideoWidgetSurface * videoWidget = new VideoWidgetSurface();
QSize videoSize(500,500); // supplement with your video dimensions

// look at VideoWidgetSurface::supportedPixelFormats for supported formats
QVideoSurfaceFormat format( videoSize, QVideoFrame::Format_RGB32, QAbstractVideoBuffer::QPixmapHandle)

// possibly fill with initial frame?

videoWidget->start(format);
Run Code Online (Sandbox Code Playgroud)

...当您想要更新视频小部件的当前帧时:

// If you don't need the data in any past frames you can probably just create one frame
// and just use it repeadtly (as VideoWidgetSurface only keeps track of one frame at a time)
QVideoFrame aFrame(32 * format.frameWidth()  * format.frameHeight(),format.frameSize(), 32 * format.frameWidth(),format.pixelFormat());

aFrame.map(QAbstractVideoBuffer::WriteOnly);

QRgb * pixels = aFrame.bits();

// perform pixel manipulation here...

aFrame.unmap();

videoWidget->present(aFrame);
Run Code Online (Sandbox Code Playgroud)

..并结束播放...

videoWidget.stop();
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的详细回复.我看到这就是在视频窗口小部件中显示以编程方式生成的帧的方式,但我的问题是不同的.我想将视频保存到文件(.avi,.mp4等).你知道这是否可行?在我看来,Qt5使用QMediaRecorder将视频保存到文件,但我没有看到如何将QVideoFrame放入QMediaRecorder. (3认同)
  • 我一直在使用[qtffmpegwrapper](https://code.google.com/p/qtffmpegwrapper/QtFFMpegWrapper).它不那么简单,内置的Qt类,不处理音频,但它做的工作. (2认同)