大网数据广告活动
Web 标准与前端开发

VideoEncoder: VideoEncoder() constructor

· 52CE 我爱测

The **`VideoEncoder()`** constructor creates a new VideoEncoder object with the provided `options.output` callback assigned as the output callback, the provided `options.error` callback as the error callback, and sets the VideoEncoder.state to `"unconfigured"`.

WebCodecs APIwindow_and_dedicated

The VideoEncoder() constructor creates a new VideoEncoder object with the provided options.output callback assigned as the output callback, the provided options.error callback as the error callback, and sets the VideoEncoder.state to "unconfigured".

Syntax

new VideoEncoder(options)

Parameters

  • options
    • : An object containing two required callbacks.
      • output
        • : A callback which takes an EncodedVideoChunk object as the first argument, and an optional metadata object as the second. The metadata object has three members:
          • decoderConfig
            • : An object containing:
              • codec
              • description
                • : An ArrayBuffer, a TypedArray, or a DataView containing a sequence of codec-specific bytes, commonly known as "extradata".
              • codedWidth
                • : An integer representing the width of the VideoFrame in pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments.
              • codedHeight
                • : An integer representing the height of the VideoFrame in pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments.
              • displayAspectWidth
                • : An integer representing the horizontal dimension of the VideoFrame's aspect ratio when displayed.
              • displayAspectHeight
                • : An integer representing the vertical dimension of the VideoFrame's aspect ratio when displayed.
              • colorSpace
                • : An object you pass to the VideoColorSpace constructor as the init argument, configuring the VideoFrame.colorSpace for VideoFrame associated with this decoderConfig object. If colorSpace exists, the provided values will override any in-band values from the bitstream.
              • hardwareAcceleration
                • : A string that configures hardware acceleration for this codec. Defaults to "no-preference". Options are:
                  • "no-preference"
                  • "prefer-hardware"
                  • "prefer-software"
              • optimizeForLatency
                • : A boolean representing whether the selected decoder should be configured to minimize the number of EncodedVideoChunk that have to be decoded before a VideoFrame is output.
          • svc
            • : An optional object with only one member: temporalLayerId, which is a number that identifies the temporal layer for the associated EncodedVideoChunk.
          • alphaSideData
            • : An ArrayBuffer, a TypedArray, or a DataView that contains the EncodedVideoChunk's extra alpha channel data.
      • error
        • : A callback which takes an Error object as its only argument.

Examples

In the following example a VideoEncoder is created with the two required callback functions, one to deal with the encoded frame and the other to handle errors.

const videoEncoder = new VideoEncoder({
  output(chunk, metadata) {
    console.log(chunk.timestamp);
    console.log(chunk.byteLength);
    console.log(JSON.stringify(metadata));
  },
  error(error) {
    console.log(error);
  },
});

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。