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

VideoEncoder: encode() method

· 52CE 我爱测

The **`encode()`** method of the VideoEncoder interface asynchronously encodes a VideoFrame. Encoded data (EncodedVideoChunk) or an error will eventually be returned via the callbacks provided to the VideoEncoder constructor.

WebCodecs APIwindow_and_dedicated

The encode() method of the VideoEncoder interface asynchronously encodes a VideoFrame. Encoded data (EncodedVideoChunk) or an error will eventually be returned via the callbacks provided to the VideoEncoder constructor.

Syntax

encode(frame)
encode(frame, options)

Parameters

  • frame
    • : A VideoFrame object.
  • options
    • : An object containing the following members:
      • keyFrame
        • : A Boolean, defaulting to false giving the user agent flexibility to decide if this frame should be encoded as a key frame. If true this indicates that the given frame must be encoded as a key frame.
      • vp9
        • : Encode options for the VP9 codec.
          • quantizer
            • : Frame quantizer value 0 to 63. Only effective if VideoEncoder was configured with quantizer bitrate mode.
      • av1
        • : Encode options for the AV1 codec.
          • quantizer
            • : Frame quantizer value 0 to 63. Only effective if VideoEncoder was configured with quantizer bitrate mode.
      • avc
        • : Encode options for the AVC (H.264) codec.
          • quantizer
            • : Frame quantizer value 0 to 51. Only effective if VideoEncoder was configured with quantizer bitrate mode.
      • hevc
        • : Encode options for the HEVC (H.265) codec.
          • quantizer
            • : Frame quantizer value 0 to 51. Only effective if VideoEncoder was configured with quantizer bitrate mode.

Return value

None (undefined).

Exceptions

  • InvalidStateError DOMException
    • : Thrown if the VideoEncoder.state is not "configured".
  • DataError DOMException
    • : Thrown if the given frame object's rotation and flip do not match the rotation and flip of the first VideoFrame passed to encode() (the "active orientation").

Examples

In the following example encode is passed a VideoFrame, and the options parameter indicating that this frame should be considered a keyframe.

encoder.encode(frame, { keyFrame: true });

Setting per-frame QP value for encoding individual frames.

const encoder = new VideoEncoder(init);
const encoderConfig = {
  codec: "vp09.00.10.08",
  width: 800,
  height: 600,
  bitrateMode: "quantizer",
  framerate: 30,
  latencyMode: "realtime",
};
encoder.configure(encoderConfig);

const encodeOptions = { keyFrame: false };
const qp = calculateQp(codec, frame);

if (codec.includes("vp09")) {
  encodeOptions.vp9 = { quantizer: qp };
} else if (codec.includes("av01")) {
  encodeOptions.av1 = { quantizer: qp };
} else if (codec.includes("avc")) {
  encodeOptions.avc = { quantizer: qp };
} else if (codec.includes("hvc1") || codec.includes("hev1")) {
  encodeOptions.hevc = { quantizer: qp };
}

encoder.encode(frame, encodeOptions);

Specifications

Browser compatibility

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

资料来源与适用范围

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

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

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