视频概述

视频特征

Qt Multimedia offers both high and low level C++ classes for playing and manipulating video data, and QML types for playback and control. Some of these classes also overlap with both camera and audio classes, which can be useful.

视频实现细节

在 C++ 中播放视频

可以使用 QMediaPlayer 类解码视频文件,和显示它使用 QVideoWidget , QGraphicsVideoItem ,或自定义类。

此处范例,使用 QVideoWidget :

player = new QMediaPlayer;
playlist = new QMediaPlaylist(player);
playlist->addMedia(QUrl("http://example.com/myclip1.mp4"));
playlist->addMedia(QUrl("http://example.com/myclip2.mp4"));
videoWidget = new QVideoWidget;
player->setVideoOutput(videoWidget);
videoWidget->show();
playlist->setCurrentIndex(1);
player->play();
					

和范例采用 QGraphicsVideoItem :

player = new QMediaPlayer(this);
QGraphicsVideoItem *item = new QGraphicsVideoItem;
player->setVideoOutput(item);
graphicsView->scene()->addItem(item);
graphicsView->show();
player->setMedia(QUrl("http://example.com/myclip4.ogv"));
player->play();
					
					

在 QML 中播放视频

可以使用 VideoOutput 渲染的内容提供通过 MediaPlayer Camera VideoOutput is a visual component that can be transformed or acted upon by shaders (as the QML 视频着色器效果范例 shows), while all media decoding and playback control is handled by the MediaPlayer .

Alternatively there is also a higher level 视频 type that acts as a single, visual element to play video and control playback.

处理低级视频帧

Qt Multimedia 提供了许多低级类以使处理视频帧变得更容易一些。这些类首要用于编写处理视频或摄像头帧的代码 (例如:检测条码或应用花哨晕影效果),或需要以其它不受支持的特殊方式显示视频。

The QVideoFrame class encapsulates a video frame and allows the contents to be mapped into system memory for manipulation or processing, while deriving a class from QAbstractVideoSurface 允许接收这些帧从 QMediaPlayer and QCamera .

class MyVideoSurface : public QAbstractVideoSurface
{
    QList<QVideoFrame::PixelFormat> supportedPixelFormats(
            QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const
    {
        Q_UNUSED(handleType);
        // Return the formats you will support
        return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_RGB565;
    }
    bool present(const QVideoFrame &frame)
    {
        Q_UNUSED(frame);
        // Handle the frame and do your processing
        return true;
    }
};
					

and with an instance of this surface, myVideoSurface , you can set the surface as the 视频输出 for QMediaPlayer .

player->setVideoOutput(myVideoSurface);
					

Several of the built-in Qt classes offer this functionality as well, so if you decode video in your application, you can present it to classes that offer a QVideoRendererControl class, and in QML you can set a custom object for the source of a VideoOutput with either a writable videoSurface property (that the instance will set it's internal video surface to) or a readable mediaObject property with a QMediaObject derived class that implements the QVideoRendererControl 接口。

The following snippet shows a class that has a writable videoSurface property and receives frames through a public slot onNewVideoContentReceived() . These frames are then presented on the surface set in setVideoSurface() .

class MyVideoProducer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QAbstractVideoSurface *videoSurface READ videoSurface WRITE setVideoSurface)
public:
    QAbstractVideoSurface* videoSurface() const { return m_surface; }
    void setVideoSurface(QAbstractVideoSurface *surface)
    {
        if (m_surface != surface && m_surface && m_surface->isActive()) {
            m_surface->stop();
        }
        m_surface = surface;
        if (m_surface)
            m_surface->start(m_format);
    }
    // ...
public slots:
    void onNewVideoContentReceived(const QVideoFrame &frame)
    {
        if (m_surface)
            m_surface->present(frame);
    }
private:
    QAbstractVideoSurface *m_surface;
    QVideoSurfaceFormat m_format;
};
					
					

录制视频

可以使用 QMediaRecorder class in conjunction with other classes to record video to disk. Primarily this is used with the camera, so consult the 摄像头概述 了解更多信息。

监视视频帧

可以使用 QVideoProbe class to access video frames as they flow through different parts of a media pipeline when using other classes like QMediaPlayer , QMediaRecorder or QCamera . After creating the high level media class, you can set the source of the video probe to that instance. This can be useful for performing some video processing tasks (like barcode recognition, or object detection) while the video is rendered normally. You can not affect the video frames using this class, and they may arrive at a slightly different time than they are being rendered.

Here's an example of installing a video probe while recording the camera:

camera = new QCamera;
viewfinder = new QCameraViewfinder();
camera->setViewfinder(viewfinder);
camera->setCaptureMode(QCamera::CaptureVideo);
videoProbe = new QVideoProbe(this);
if (videoProbe->setSource(camera)) {
    // Probing succeeded, videoProbe->isValid() should be true.
    connect(videoProbe, SIGNAL(videoFrameProbed(QVideoFrame)),
            this, SLOT(detectBarcodes(QVideoFrame)));
}
camera->start();
// Viewfinder frames should now also be emitted by
// the video probe, even in still image capture mode.
// Another alternative is to install the probe on a
// QMediaRecorder connected to the camera to get the
// recorded frames, if they are different from the
// viewfinder frames.
					
					

范例

存在 C++ 和 QML 两种可用范例。

C++ 范例

QML 范例

参考文档编制

C++ 类

QAbstractPlanarVideoBuffer 抽象平面视频数据
QAbstractVideoBuffer 抽象视频数据
QAbstractVideoFilter 表示应用于通过 VideoOutput 类型接收视频帧的过滤器
QAbstractVideoSurface 视频呈现表面的基类
QVideoFilterRunnable 表示拥有所有图形 计算资源,履行实际过滤 (或计算) 的过滤器实现
QVideoFrame 表示视频数据帧
QVideoProbe 允许监视视频帧播放或录制
QVideoSurfaceFormat 指定视频呈现表面的流格式

QML 类型

MediaPlayer 把媒体回放添加到场景
Playlist 针对要播放的指定媒体列表
PlaylistItem 定义播放列表项
视频 展示指定视频的方便类型
VideoOutput 渲染视频或摄像头取景器