我们有一个 QT 应用程序,它将以编程方式生成的 QPixmap 一张一张地呈现到显示器上,我们希望将此输出保存到视频文件中。
我知道过去人们建议使用 ffmpeg 或 opencv 和 Qt 来执行此操作。然而,在 Qt 5 中,新的 QtMultimedia 模块似乎公开了一些此类功能。
例如,现在可以使用 http://doc.qt.io/qt-5/qmediarecorder.html#details 中描述的 QMediaRecorder 在 Qt 5 中保存来自相机源的视频。
有了这个新功能,有什么方法可以使用 Qt 5 来保存我们以编程方式生成的视频,还是使用第三方库更好?
最佳答案
这实际上早在 Qt 4.7 就可以通过使用 QVideoFrame 和 QAbstractVideoSurface 实现。 Qt 甚至有这个很好的例子来创建一个可以显示编程构造的 QVideoFrames 的视频小部件:
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);
...当您想要更新视频小部件的当前帧时:
// 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);
.. 并结束播放...
videoWidget.stop();
关于c++ - 使用 Qt 5.0 以编程方式创建视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14566470/