VideoEncoder: VideoEncoder() constructor

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Dedicated Web Workers.

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

js
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 Optional

An object containing:

codec

A string containing a valid codec string.

description Optional

An ArrayBuffer, a TypedArray, or a DataView containing a sequence of codec-specific bytes, commonly known as "extradata".

codedWidth Optional

An integer representing the width of the VideoFrame in pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments.

codedHeight Optional

An integer representing the height of the VideoFrame in pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments.

displayAspectWidth Optional

An integer representing the horizontal dimension of the VideoFrame's aspect ratio when displayed.

displayAspectHeight Optional

An integer representing the vertical dimension of the VideoFrame's aspect ratio when displayed.

colorSpace Optional

An object you pass to the VideoColorSpace constructor as the init argument, configuring the VideoFrame.colorSpace for VideoFrames associated with this decoderConfig object. If colorSpace exists, the provided values will override any in-band values from the bitstream.

hardwareAcceleration Optional

A string that configures hardware acceleration for this codec. Defaults to "no-preference". Options are:

  • "no-preference"
  • "prefer-hardware"
  • "prefer-software"
optimizeForLatency Optional

A boolean representing whether the selected decoder should be configured to minimize the number of EncodedVideoChunks that have to be decoded before a VideoFrame is output.

svc Optional

An optional object with only one member: temporalLayerId, which is a number that identifies the temporal layer for the associated EncodedVideoChunk.

alphaSideData Optional

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.

js
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

Specification
WebCodecs
# dom-videoencoder-videoencoder

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
VideoEncoder() constructor
Chrome – Full support
Chrome 94 (Release date: 2021-09-21)
footnote Full support
Edge – Full support
Edge 94 (Release date: 2021-09-24)
footnote Full support
Firefox – Full support
Firefox 130 (Release date: 2024-09-03)
footnote Full support
Opera – Full support
Opera 80 (Release date: 2021-10-05)
footnote Full support
Safari – Full support
Safari 16.4 (Release date: 2023-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 94 (Release date: 2021-09-21)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 66 (Release date: 2021-12-15)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 17 (Release date: 2022-05-04)
footnote Full support
WebView Android – Full support
WebView Android 94 (Release date: 2021-09-21)
footnote Full support
WebView on iOS – Full support
WebView on iOS 16.4 (Release date: 2023-03-27)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support