QMediaRecorder 類用於編碼和錄製捕獲會話。 更多...
| 頭: |
#include <QMediaRecorder>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Multimedia)
target_link_libraries(mytarget PRIVATE Qt6::Multimedia)
|
| qmake: |
QT += multimedia
|
| 在 QML: | MediaRecorder |
| 繼承: | QObject |
| enum | EncodingMode { ConstantQualityEncoding, ConstantBitRateEncoding, AverageBitRateEncoding, TwoPassEncoding } |
| enum | Error { NoError, ResourceError, FormatError, OutOfSpaceError, LocationNotWritable } |
| enum | Quality { VeryLowQuality, LowQuality, NormalQuality, HighQuality, VeryHighQuality } |
| enum | RecorderState { StoppedState, RecordingState, PausedState } |
|
|
| QMediaRecorder (QObject * parent = nullptr) | |
| virtual | ~QMediaRecorder () override |
| QUrl | actualLocation () const |
| void | addMetaData (const QMediaMetaData & metaData ) |
| int | audioBitRate () const |
| int | audioChannelCount () const |
| int | audioSampleRate () const |
| bool | autoStop () const |
| QMediaCaptureSession * | captureSession () const |
| qint64 | duration () const |
| QMediaRecorder::EncodingMode | encodingMode () const |
| QMediaRecorder::Error | error () const |
| QString | errorString () const |
| bool | isAvailable () const |
| QMediaFormat | mediaFormat () const |
| QMediaMetaData | metaData () const |
| QIODevice * | outputDevice () const |
| QUrl | outputLocation () const |
| QMediaRecorder::Quality | quality () const |
| QMediaRecorder::RecorderState | recorderState () const |
| void | setAudioBitRate (int bitRate ) |
| void | setAudioChannelCount (int channels ) |
| void | setAudioSampleRate (int sampleRate ) |
| void | setAutoStop (bool autoStop ) |
| void | setEncodingMode (QMediaRecorder::EncodingMode mode ) |
| void | setMediaFormat (const QMediaFormat & format ) |
| void | setMetaData (const QMediaMetaData & metaData ) |
| void | setOutputDevice (QIODevice * device ) |
| void | setOutputLocation (const QUrl & location ) |
| void | setQuality (QMediaRecorder::Quality quality ) |
| void | setVideoBitRate (int bitRate ) |
| void | setVideoFrameRate (qreal frameRate ) |
| void | setVideoResolution (const QSize & size ) |
| void | setVideoResolution (int width , int height ) |
| int | videoBitRate () const |
| qreal | videoFrameRate () const |
| QSize | videoResolution () const |
| void | pause () |
| void | record () |
| void | stop () |
| void | actualLocationChanged (const QUrl & location ) |
| void | audioBitRateChanged () |
| void | audioChannelCountChanged () |
| void | audioSampleRateChanged () |
| void | autoStopChanged () |
| void | durationChanged (qint64 duration ) |
| void | encodingModeChanged () |
| void | errorChanged () |
| void | errorOccurred (QMediaRecorder::Error error , const QString & errorString ) |
| void | mediaFormatChanged () |
| void | metaDataChanged () |
| void | qualityChanged () |
| void | recorderStateChanged (QMediaRecorder::RecorderState state ) |
| void | videoBitRateChanged () |
| void | videoFrameRateChanged () |
| void | videoResolutionChanged () |
Use the QMediaRecorder class to encode and record media generated in QMediaCaptureSession . You can generate:
To record media, connect a generator to a corresponding media capture session.
Performance of video encoding and recording is limited by the hardware, the operating system, the installed graphic drivers, and the input video format. If
QCamera
,
QScreenCapture
,或
QWindowCapture
produces video frames faster than
QMediaRecorder
can encode and record them, the recorder may drop some frames. This is likely to occur if the input frame resolution is high, 4K for example, and hardware-accelerated encoding is unavailable. If you generate input video via
QVideoFrameInput
, the method
QVideoFrameInput::sendVideoFrame
will do nothing and return
false
whenever this limitation is reached and the internal frame queue is full. Rely on the signal
QVideoFrameInput::readyToSendVideoFrame
to know when the recorder is ready to receive new frames again. If you cannot change the rate of video frame generation and dropping frames is undesirable, we recommend implementing your own frame queue on top of
QVideoFrameInput
, considering the memory limitations of the hardware.
QMediaCaptureSession session; QAudioInput audioInput; session.setAudioInput(&input); QMediaRecorder recorder; session.setRecorder(&recorder); recorder.setQuality(QMediaRecorder::HighQuality); recorder.setOutputLocation(QUrl::fromLocalFile("test.mp3")); recorder.record();
枚舉編碼模式。
| 常量 | 值 | 描述 |
|---|---|---|
QMediaRecorder::ConstantQualityEncoding
|
0
|
編碼的目標是擁有常量品質,調節比特率以擬閤。 |
QMediaRecorder::ConstantBitRateEncoding
|
1
|
編碼將使用常量比特率,調節品質以擬閤。 |
QMediaRecorder::AverageBitRateEncoding
|
2
|
編碼將試著保持平均比特率設置,但會根據需要使用更多 (或更少)。 |
QMediaRecorder::TwoPassEncoding
|
3
|
The media will first be processed to determine the characteristics, and then processed a second time allocating more bits to the areas that need it. |
| 常量 | 值 | 描述 |
|---|---|---|
QMediaRecorder::NoError
|
0
|
沒有錯誤。 |
QMediaRecorder::ResourceError
|
1
|
設備未就緒 (或不可用)。 |
QMediaRecorder::FormatError
|
2
|
不支持當前格式。 |
QMediaRecorder::OutOfSpaceError
|
3
|
設備沒有剩餘空間。 |
QMediaRecorder::LocationNotWritable
|
4
|
輸齣位置不可寫。 |
枚舉品質編碼級彆。
| 常量 | 值 |
|---|---|
QMediaRecorder::VeryLowQuality
|
0
|
QMediaRecorder::LowQuality
|
1
|
QMediaRecorder::NormalQuality
|
2
|
QMediaRecorder::HighQuality
|
3
|
QMediaRecorder::VeryHighQuality
|
4
|
| 常量 | 值 | 描述 |
|---|---|---|
QMediaRecorder::StoppedState
|
0
|
錄製器不活動。 |
QMediaRecorder::RecordingState
|
1
|
請求錄製。 |
QMediaRecorder::PausedState
|
2
|
錄製器暫停。 |
[read-only]
actualLocation
: const
QUrl
此特性保持最後媒體內容的實際位置。
The actual location is reset when a new
outputLocation
or a non-null
outputDevice
is assigned. When
record
() is invoked and
outputDevice
is
null
or not writable, the recorder generates the actual location basing on the following rules.
outputLocation
is empty, a directory, or a file without an extension, the recorder generates the appropriate extension based on the selected media format and system MIME types.
outputLocation
is a directory, the recorder generates a new file name within it.
outputLocation
is empty, the recorder generates a new file name in the system-specific directory for audio or video.
recorderStateChanged(RecordingState)
.
訪問函數:
| QUrl | actualLocation () const |
通知程序信號:
| void | actualLocationChanged (const QUrl & location ) |
This property holds the bit rate of the compressed audio stream in bits per second.
訪問函數:
| int | audioBitRate () const |
| void | setAudioBitRate (int bitRate ) |
通知程序信號:
| void | audioBitRateChanged () |
This property holds the number of audio channels.
訪問函數:
| int | audioChannelCount () const |
| void | setAudioChannelCount (int channels ) |
通知程序信號:
| void | audioChannelCountChanged () |
This property holds the audio sample rate in Hz.
訪問函數:
| int | audioSampleRate () const |
| void | setAudioSampleRate (int sampleRate ) |
通知程序信號:
| void | audioSampleRateChanged () |
This property controls whether the media recorder stops automatically when all media inputs have reported the end of the stream or have been deactivated.
The end of the stream is reported by sending an empty media frame, which you can send explicitly via QVideoFrameInput or QAudioBufferInput .
Video inputs, specificly,
QCamera
,
QScreenCapture
and
QWindowCapture
, can be deactivated via the function
setActive
.
默認為
false
.
QMediaRecorder::autoStop is only supported with the FFmpeg backend.
訪問函數:
| bool | autoStop () const |
| void | setAutoStop (bool autoStop ) |
通知程序信號:
| void | autoStopChanged () |
另請參閱 QCamera , QScreenCapture ,和 QWindowCapture .
[read-only]
duration
: const
qint64
此特性保持錄製媒體的持續時間 (以毫秒為單位)。
訪問函數:
| qint64 | duration () const |
通知程序信號:
| void | durationChanged (qint64 duration ) |
This property holds the encoding mode.
訪問函數:
| QMediaRecorder::EncodingMode | encodingMode () const |
| void | setEncodingMode (QMediaRecorder::EncodingMode mode ) |
通知程序信號:
| void | encodingModeChanged () |
另請參閱 EncodingMode .
[read-only]
error
: const
QMediaRecorder::Error
返迴當前錯誤狀態。
訪問函數:
| QMediaRecorder::Error | error () const |
通知程序信號:
| void | errorChanged () |
另請參閱 errorString ().
[read-only]
errorString
: const
QString
返迴描述當前錯誤狀態的字符串。
訪問函數:
| QString | errorString () const |
通知程序信號:
| void | errorChanged () |
另請參閱 error ().
This property holds the current QMediaFormat of the recorder.
The value of this property may change when invoking record (). If this happens, the mediaFormatChanged() signal will be emitted. This will always happen if the QMediaFormat::audioCodec or QMediaFormat::fileFormat properties are set to unspecified. If a video source ( QCamera , QScreenCapture ,或 QVideoFrameInput ) is connected to the QMediaCaptureSession , QMediaFormat::videoCodec must also be specified. The QMediaFormat::audioCodec and QMediaFormat::videoCodec property values may also change if the media backend does not support the selected file format or codec.
The
QMediaFormat::fileFormat
property value may also change to an
audio
only format if a video format was requested, but
QMediaCaptureSession
does not have a video source connected. For example, if
QMediaFormat::fileFormat
被設為
QMediaFormat::MPEG4
, it may be changed to
QMediaFormat::Mpeg4Audio
.
Applications can determine if
mediaFormat
will change before recording starts by calling the
QMediaFormat::isSupported
() function. When recording without any video inputs,
record
() will not be changed the
QMediaFormat
if the following is true:
true
When recording with video input,
mediaFormat
will not be changed if the following is true:
true
注意: The QMediaRecorder does not take the file name extension from the outputLocation property into account when determining the QMediaFormat::fileFormat , and will not adjust the extension of the outputLocation QUrl to match the selected file format if an extension is specified. Applications should therefore make sure to set the QMediaRecorder::mediaFormat::fileFormat to match the file extension, or not specify a file extension. If no file extension is specified, the actualLocation file extension will be updated to match the file format used for recording.
訪問函數:
| QMediaFormat | mediaFormat () const |
| void | setMediaFormat (const QMediaFormat & format ) |
通知程序信號:
| void | mediaFormatChanged () |
另請參閱 QMediaFormat::isSupported () 和 actualLocation .
返迴錄製關聯的 metaData。
訪問函數:
| QMediaMetaData | metaData () const |
| void | setMetaData (const QMediaMetaData & metaData ) |
通知程序信號:
| void | metaDataChanged () |
此特性保持媒體內容的目的地位置。
Setting the location can fail, for example when the service supports only local file system locations but a network URL was passed. If the operation fails, the errorOccurred () 信號發射。
The output location is ignored if a writable
outputDevice
has been assigned to the recorder. This behavior may change in the future, so we recommend setting only one output, either
outputLocation
or
outputDevice
.
The output location can be empty, a directory, or a file. The path to a directory or file can be relative or absolute. The record () method generates the actual location according to the specified output location and system-specific settings. Refer to the actualLocation property description for details.
訪問函數:
| QUrl | outputLocation () const |
| void | setOutputLocation (const QUrl & location ) |
另請參閱 actualLocation and outputDevice ().
返迴錄製品質。
訪問函數:
| QMediaRecorder::Quality | quality () const |
| void | setQuality (QMediaRecorder::Quality quality ) |
通知程序信號:
| void | qualityChanged () |
[read-only]
recorderState
: const
QMediaRecorder::RecorderState
此特性保持媒體錄製器的當前狀態。
The state property represents the user request and is changed synchronously during record (), pause () 或 stop () calls. Recorder state may also change asynchronously when recording fails.
訪問函數:
| QMediaRecorder::RecorderState | recorderState () const |
通知程序信號:
| void | recorderStateChanged (QMediaRecorder::RecorderState state ) |
Constructs a media recorder. The media recorder is a child of parent .
[override virtual noexcept]
QMediaRecorder::
~QMediaRecorder
()
銷毀媒體錄製器對象。
[signal]
void
QMediaRecorder::
actualLocationChanged
(const
QUrl
&
location
)
Signals that the actual location of the recorded media has changed. This signal is usually emitted when recording starts.
注意: 通知程序信號對於特性 actualLocation .
添加 metaData 到錄製媒體。
Returns the bit rate of the compressed audio stream in bits per second.
注意: getter 函數對於特性 audioBitRate。
另請參閱 setAudioBitRate ().
[signal]
void
QMediaRecorder::
audioBitRateChanged
()
Signals when the recording audio bit rate changes.
注意: 通知程序信號對於特性 audioBitRate .
Returns the number of audio channels.
注意: Getter function for property audioChannelCount.
另請參閱 setAudioChannelCount ().
[signal]
void
QMediaRecorder::
audioChannelCountChanged
()
Signals when the recording audio channel count changes.
注意: 通知程序信號對於特性 audioChannelCount .
Returns the audio sample rate in Hz.
注意: Getter function for property audioSampleRate.
另請參閱 setAudioSampleRate ().
[signal]
void
QMediaRecorder::
audioSampleRateChanged
()
Signals when the recording audio sample rate changes.
注意: 通知程序信號對於特性 audioSampleRate .
Returns the media capture session.
[signal]
void
QMediaRecorder::
durationChanged
(
qint64
duration
)
發射信號,當 duration of the recorded media has changed.
注意: 通知程序信號對於特性 duration .
Returns the encoding mode.
注意: Getter function for property encodingMode.
另請參閱 setEncodingMode () 和 EncodingMode .
[signal]
void
QMediaRecorder::
encodingModeChanged
()
Signals when the encoding mode changes.
注意: 通知程序信號對於特性 encodingMode .
[signal]
void
QMediaRecorder::
errorOccurred
(
QMediaRecorder::Error
error
, const
QString
&
errorString
)
發射信號,當 error has occurred, with errorString containing a description of the error.
返迴
true
if media recorder service ready to use.
[signal]
void
QMediaRecorder::
metaDataChanged
()
Signals that a media object's meta-data has changed.
If multiple meta-data elements are changed metaDataChanged() is emitted once.
注意: 通知程序信號對於特性 metaData .
Returns the output IO device for media content.
另請參閱 setOutputDevice ().
[slot]
void
QMediaRecorder::
pause
()
暫停錄製。
The recorder state is changed to QMediaRecorder::PausedState .
Depending on the platform, pausing recording may be not supported. In this case the recorder state is unchanged.
[signal]
void
QMediaRecorder::
qualityChanged
()
Signals when the recording quality changes.
注意: 通知程序信號對於特性 quality .
[slot]
void
QMediaRecorder::
record
()
Starts recording.
While the recorder state is changed immediately to c{ QMediaRecorder::RecordingState }, recording may start asynchronously.
若錄製失敗
error
() signal is emitted with recorder state being reset back to
QMediaRecorder::StoppedState
.
This method updates actualLocation according to its generation rules.
注意: On mobile devices, recording will happen in the orientation the device had when calling record and is locked for the duration of the recording. To avoid artifacts on the user interface, we recommend to keep the user interface locked to the same orientation as long as the recording is ongoing using the contentOrientation property of QWindow and unlock it again once the recording is finished.
Returns the current media recorder state.
注意: getter 函數對於特性 recorderState。
另請參閱 QMediaRecorder::RecorderState .
[signal]
void
QMediaRecorder::
recorderStateChanged
(
QMediaRecorder::RecorderState
state
)
Signals that a media recorder's state 已改變。
注意: 通知程序信號對於特性 recorderState .
設置音頻 bitRate 以位每秒為單位。
注意: setter 函數對於特性 audioBitRate .
另請參閱 audioBitRate ().
Sets the number of audio channels .
A value of -1 indicates the recorder should make an optimal choice based on what is available from the audio source and the limitations of the codec.
注意: setter 函數對於特性 audioChannelCount .
另請參閱 audioChannelCount ().
設置音頻 sampleRate (以 Hz 為單位)。
值
-1
indicates the recorder should make an optimal choice based on what is available from the audio source, and the limitations of the codec.
注意: setter 函數對於特性 audioSampleRate .
另請參閱 audioSampleRate ().
設置編碼 mode 設置。
若 ConstantQualityEncoding is set, the quality encoding parameter is used and bit rates are ignored, otherwise the bitrates are used.
注意: setter 函數對於特性 encodingMode .
另請參閱 encodingMode () 和 EncodingMode .
將元數據設為 metaData .
注意: To ensure that meta-data is set correctly, it should be set before starting the recording. Once the recording is started, any meta-data set will be attached to the next recording.
注意: setter 函數對於特性 metaData .
另請參閱 metaData ().
Set the output IO device for media content.
The device 必須已打開以 WriteOnly or ReadWrite modes before the recording starts.
The media recorder doesn't take ownership of the specified
device
. If the recording has been started, the device must be kept alive and open until the signal
recorderStateChanged(StoppedState)
is emitted.
This method resets
actualLocation
immediately unless the specified
device
is
null
.
If a writable output device is assigned to the recorder,
outputLocation
is ignored, and
actualLocation
is not generated when recording starts. This behavior may change in the future, so we recommend setting only one output, either
outputLocation
or
outputDevice
.
QMediaRecorder::setOutputDevice
is only supported with the FFmpeg backend.
另請參閱 outputDevice () 和 outputLocation .
設置視頻 bitRate 以位每秒為單位。
注意: setter 函數對於特性 videoBitRate .
另請參閱 videoBitRate ().
設置視頻 frameRate .
A value of 0 indicates the recorder should make an optimal choice based on what is available from the video source and the limitations of the codec.
注意: setter 函數對於特性 videoFrameRate .
另請參閱 videoFrameRate ().
Sets the resolution of the encoded video to size .
傳遞空 QSize to make the recorder choose an optimal resolution based on what is available from the video source and the limitations of the codec.
注意: setter 函數對於特性 videoResolution .
另請參閱 videoResolution ().
設置 width and height of the resolution of the encoded video.
注意: setter 函數對於特性 videoResolution .
這是重載函數。
[slot]
void
QMediaRecorder::
stop
()
The recorder will stop the recording. Processing pending video and audio data might however still take some time. The recording is finished, once the state of the media recorder changes to QMediaRecorder::StoppedState .
Returns the bit rate of the compressed video stream in bits per second.
注意: getter 函數對於特性 videoBitRate。
另請參閱 setVideoBitRate ().
[signal]
void
QMediaRecorder::
videoBitRateChanged
()
Signals when the recording video bit rate changes.
注意: 通知程序信號對於特性 videoBitRate .
返迴視頻幀速率。
注意: getter 函數對於特性 videoFrameRate。
另請參閱 setVideoFrameRate ().
[signal]
void
QMediaRecorder::
videoFrameRateChanged
()
Signals when the recording video frame rate changes.
注意: 通知程序信號對於特性 videoFrameRate .
Returns the resolution of the encoded video.
注意: getter 函數對於特性 videoResolution。
另請參閱 setVideoResolution ().
[signal]
void
QMediaRecorder::
videoResolutionChanged
()
Signals when the video recording resolution changes.
注意: 通知程序信號對於特性 videoResolution .