Qt Multimedia

"image of multimedia icons, courtesy of misirlou from the Noun Project"

Qt Multimedi 是提供了一组丰富 QML 类型和 C++ 类,以处理多媒体内容的附件模块。它包含易于使用的 API,用于回放音频和视频文件并在屏幕上渲染这些,及用于从系统摄像头和麦克风录制音频和视频的综合 API。

该模块功能分为下列子模块:

Qt Multimedia 提供用于特定多媒体用例的 API。
Qt Multimedia Widgets 提供基于 Widget 的多媒体 API。
Qt Spatial Audio 提供用于在 3D 空间实现声场的 API。

快速入门

若要从 Qt 5 移植到 Qt 6,见 Qt Multimedia 的变化 .

若是 Qt Multimedia 新手,QML 类型可以 imported 进应用程序使用以下语句在 .qml 文件。

import QtMultimedia
					

要链接到 C++ 库,添加以下到工程 CMakeLists.txt 文件。代入 my_project 采用工程名称。

find_package(Qt6 REQUIRED COMPONENTS Multimedia)
target_link_libraries(my_project PRIVATE Qt6::Multimedia)
					

Overviews and important topics

QML 类型

下表提纲了一些重要 QML 类型。

类型 描述
MediaPlayer 将音频/视频回放功能添加到场景。
CaptureSession 创建用于捕获音频/视频的会话。
Camera 访问连接到系统的摄像头。
AudioInput 访问连接到系统的音频输入 (麦克风)。
AudioOutput 访问连接到系统的音频输出 (扬声器、耳机)。
VideoOutput 显示视频内容。
MediaRecorder 录制音频/视频从 CaptureSession .
ImageCapture 从摄像头捕获静止图像。
Video 将视频回放功能添加到场景。使用 MediaPlayer and VideoOutput 类型以提供视频回放功能。
ScreenCapture Captures a screen.
WindowCapture Captures a top-level window.

C++ 类

The following table outlines some important C++ classes

描述
QMediaPlayer 从源回放媒体。
QVideoWidget 显示来自媒体播放器 (或捕获会话) 的视频。
QMediaCaptureSession 捕获音频/视频。
QCamera 访问连接到系统的摄像头
QAudioInput 访问连接到系统的音频输入 (麦克风)。
QAudioOutput 访问连接到系统的音频输出 (扬声器、耳机)。
QImageCapture 以摄像头捕获静止图像。
QMediaRecorder 从捕获会话录制媒体内容。
QVideoSink 访问和渲染单个视频帧。
QAudioSink 将原生音频数据发送给音频输出设备。
QScreenCapture Captures a screen.
QWindowCapture Captures a top-level window.

对于回放 QMediaPlayer , QAudioOutput 和 QVideoOutput 包含所有要求功能。其它类用于捕获音频和视频内容,其中 QMediaCaptureSession 是管理整个捕获/录制过程的中心类。

许可和归属

Qt Multimedia 模块在商业许可下是可用的来自 Qt 公司 。此外,它在自由软件许可下也是可用的。从 Qt 5.6 起,这些自由软件许可是 GNU LGPL (次一般公共许可) 第 3 版 ,或 GNU GPL (一般公共许可) 第 2 版 。见 Qt 许可 进一步了解细节。

FFmpeg 后端使用 FFmpeg 框架 . FFmpeg is licensed under LGPLv2.1, GPLv2, or later versions of the licenses. Some optional components of FFmpeg are only available under GPL. The FFmpeg backend shipped with the Qt binary packages is configured to not contain any of the components that are available under GPLv2 only.

FFmpeg 许可页面 进一步了解细节。

Note that video compression standards, such as the H.264 media compression standard, may be covered by patents and can incur royalty fees. This can apply to any implementation, also if the implementation is provided as an operating system service, through a third party library, or through any of Qt Multimedia's backends. Such fees are not covered by the Qt licenses.

目标平台和后端注意事项

We aim to align the behavior on all the platforms but there are some issues to consider.

Backends

在大多数平台,有 2 种不同后端可以用于 Qt Multimedia。

FFmpeg as the default backend

In this release the FFmpeg 框架 is set as the default backend on Windows, macOS, Android, and Linux except Yocto distribution.

The version shipped with Qt binary packages is FFmpeg 6.0 and is tested by the maintainers.

注意: 许可和归属 regarding what components are removed in the package shipped by Qt.

Native backends

These are:

  • gstreamer on Linux
  • AVFoundation on macOS
  • WMF Windows
  • MediaCodec framework on Android

注意: These are still available but with limited 支持。

Backend support

Maintainers will strive to fix critical issues with the native backends but don't guarantee fixing minor issues and won't implement new features for the native backends. Furthermore, even some major issues with the gstreamer backend (on Linux) won't be fixed since gstreamer is difficult to debug and is further complicated as it is dependent on Linux distributions.

We aim to align the behavior on all the platforms, especially, with the FFmpeg backend. Despite this fact we still have platform-dependent issues with formats, codecs, advanced camera features, and screen capturing due to the Qt Multimedia API relying on target platform APIs. For example, with FFmpeg, there are specific problems with hardware acceleration on Linux targets with ARM architectures.

Backend-dependent limitations will be documented and their status maintained in the respective classes.

Changing backends

In the case of issues with the default FFmpeg backend, we suggest testing with a native backend. You can switch to native backends by setting the QT_MEDIA_BACKEND 环境变量到 windows , gstreamer (在 Linux), darwin (在 macOS),或 android :

export QT_MEDIA_BACKEND=darwin
					

In order to force assign FFmpeg as the used backend, set the variable to ffmpeg :

export QT_MEDIA_BACKEND=ffmpeg
					

在 Qt Multimedia 编译阶段,可以配置默认媒体后端凭借 cmake 变量 QT_DEFAULT_MEDIA_BACKEND .

Target platform notes

The following pages list issues for specific target platforms that are not related to the multimedia backed.

Permissions

Starting from Qt 6.6, the Qt Multimedia module uses new QPermission API to handle camera and microphone permissions. This means that Qt itself no longer queries for these permissions, so this needs to be done directly from the client application.

请参考 应用程序权限 page for an example of how to integrate the new QPermission API into the application.

Reference and examples