web api instance method
VideoEncoder: encode() method
Secure contextAvailable in workers
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
VideoFrameobject.
- : A
optionsOptional- : An object containing the following members:
keyFrameOptional- : A
Boolean, defaulting tofalsegiving the user agent flexibility to decide if this frame should be encoded as a key frame. Iftruethis indicates that the given frame must be encoded as a key frame.
- : A
vp9Optional- : Encode options for the VP9 codec.
quantizer- : Frame quantizer value 0 to 63. Only effective if
VideoEncoderwas configured withquantizerbitrate mode.
- : Frame quantizer value 0 to 63. Only effective if
- : Encode options for the VP9 codec.
av1Optional- : Encode options for the AV1 codec.
quantizer- : Frame quantizer value 0 to 63. Only effective if
VideoEncoderwas configured withquantizerbitrate mode.
- : Frame quantizer value 0 to 63. Only effective if
- : Encode options for the AV1 codec.
avcOptional- : Encode options for the AVC (H.264) codec.
quantizer- : Frame quantizer value 0 to 51. Only effective if
VideoEncoderwas configured withquantizerbitrate mode.
- : Frame quantizer value 0 to 51. Only effective if
- : Encode options for the AVC (H.264) codec.
hevcOptional- : Encode options for the HEVC (H.265) codec.
quantizer- : Frame quantizer value 0 to 51. Only effective if
VideoEncoderwas configured withquantizerbitrate mode.
- : Frame quantizer value 0 to 51. Only effective if
- : Encode options for the HEVC (H.265) codec.
- : An object containing the following members:
Return value
None (undefined).
Exceptions
InvalidStateErrorDOMException- : Thrown if the
stateis not"configured".
- : Thrown if the
DataErrorDOMException- : Thrown if the given
frameobject’s rotation and flip do not match the rotation and flip of the firstVideoFramepassed toencode()(the “active orientation”).
- : Thrown if the given
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
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.