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来保存我们以编程方式生成的视频,或者我最好还是使用第三方库?
这实际上可以在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)