Skip to main content

Overview

The encoding module provides types and functions for configuring and checking media encoding capabilities.

Types

VideoEncodingConfig

Configuration object that controls video encoding.
VideoCodec
required
The video codec that should be used for encoding the video samples (frames).
number | Quality
required
The target bitrate for the encoded video, in bits per second. Alternatively, a subjective Quality can be provided.
number
default:"5"
The interval, in seconds, of how often frames are encoded as a key frame. Frequent key frames improve seeking behavior but increase file size. When using multiple video tracks, you should give them all the same key frame interval.
'deny' | 'passThrough' | 'fill' | 'contain' | 'cover'
default:"'deny'"
Video frames may change size over time. This field controls the behavior in case this happens.
  • 'deny' (default): Throw an error, requiring all frames to have the exact same dimensions
  • 'passThrough': Allow the change and directly pass the frame to the encoder
  • 'fill': Stretch the image to fill the entire original box, potentially altering aspect ratio
  • 'contain': Contain the entire image within the original box while preserving aspect ratio (may cause letterboxing)
  • 'cover': Scale the image until the entire original box is filled, while preserving aspect ratio
'discard' | 'keep'
default:"'discard'"
What to do with alpha data contained in the video samples.
  • 'discard' (default): Only the samples’ color data is kept; the video is opaque
  • 'keep': The samples’ alpha data is also encoded as side data. Pair this with a container format that supports transparency (WebM or Matroska)
'constant' | 'variable'
default:"'variable'"
Configures the bitrate mode.
'quality' | 'realtime'
default:"'quality'"
The latency mode used by the encoder; controls the performance-quality tradeoff.
  • 'quality' (default): The encoder prioritizes quality over latency, and no frames can be dropped
  • 'realtime': The encoder prioritizes low latency over quality, and may drop frames if overloaded
(packet: EncodedPacket, meta?: EncodedVideoChunkMetadata) => unknown
Called for each successfully encoded packet. Both the packet and the encoding metadata are passed.
(config: VideoEncoderConfig) => unknown
Called when the internal encoder config is created.
Source: encode.ts:33

AudioEncodingConfig

Configuration object that controls audio encoding.
AudioCodec
required
The audio codec that should be used for encoding the audio samples.
number | Quality
The target bitrate for the encoded audio, in bits per second. Alternatively, a subjective Quality can be provided. Required for compressed audio codecs, unused for PCM codecs.
'constant' | 'variable'
Configures the bitrate mode.
(packet: EncodedPacket, meta?: EncodedAudioChunkMetadata) => unknown
Called for each successfully encoded packet. Both the packet and the encoding metadata are passed.
(config: AudioEncoderConfig) => unknown
Called when the internal encoder config is created.
Source: encode.ts:230

Quality

Represents a subjective media quality level.
Source: encode.ts:339

Quality constants

QUALITY_VERY_LOW

Represents a very low media quality.
Source: encode.ts:420

QUALITY_LOW

Represents a low media quality.
Source: encode.ts:426

QUALITY_MEDIUM

Represents a medium media quality.
Source: encode.ts:432

QUALITY_HIGH

Represents a high media quality.
Source: encode.ts:438

QUALITY_VERY_HIGH

Represents a very high media quality.
Source: encode.ts:444

Functions

canEncode

Checks if the browser is able to encode the given codec.
MediaCodec
required
The media codec to check for encoding support.
Promise<boolean>
A promise that resolves to true if the codec can be encoded, false otherwise.
Source: encode.ts:451

canEncodeVideo

Checks if the browser is able to encode the given video codec with the given parameters.
VideoCodec
required
The video codec to check.
object
Optional encoding parameters to test.
Promise<boolean>
A promise that resolves to true if the video codec can be encoded with the given parameters.
Source: encode.ts:468

canEncodeAudio

Checks if the browser is able to encode the given audio codec with the given parameters.
AudioCodec
required
The audio codec to check.
object
Optional encoding parameters to test.
Promise<boolean>
A promise that resolves to true if the audio codec can be encoded with the given parameters.
Source: encode.ts:585

canEncodeSubtitles

Checks if the browser is able to encode the given subtitle codec.
SubtitleCodec
required
The subtitle codec to check.
Promise<boolean>
A promise that resolves to true if the subtitle codec can be encoded.
Source: encode.ts:656

getEncodableCodecs

Returns the list of all media codecs that can be encoded by the browser.
Promise<MediaCodec[]>
Array of all encodable video, audio, and subtitle codecs.
Source: encode.ts:669

getEncodableVideoCodecs

Returns the list of all video codecs that can be encoded by the browser.
VideoCodec[]
default:"VIDEO_CODECS"
Array of codecs to check. Defaults to all video codecs.
object
Optional parameters for encoding capability testing.
Promise<VideoCodec[]>
Array of encodable video codecs from the checked list.
Source: encode.ts:684

getEncodableAudioCodecs

Returns the list of all audio codecs that can be encoded by the browser.
AudioCodec[]
default:"AUDIO_CODECS"
Array of codecs to check. Defaults to all audio codecs.
object
Optional parameters for encoding capability testing.
Promise<AudioCodec[]>
Array of encodable audio codecs from the checked list.
Source: encode.ts:701

getEncodableSubtitleCodecs

Returns the list of all subtitle codecs that can be encoded by the browser.
SubtitleCodec[]
default:"SUBTITLE_CODECS"
Array of codecs to check. Defaults to all subtitle codecs.
Promise<SubtitleCodec[]>
Array of encodable subtitle codecs from the checked list.
Source: encode.ts:718

getFirstEncodableVideoCodec

Returns the first video codec from the given list that can be encoded by the browser.
Promise<VideoCodec | null>
The first encodable codec from the list, or null if none are encodable.
Source: encode.ts:730

getFirstEncodableAudioCodec

Returns the first audio codec from the given list that can be encoded by the browser.
Promise<AudioCodec | null>
The first encodable codec from the list, or null if none are encodable.
Source: encode.ts:752

getFirstEncodableSubtitleCodec

Returns the first subtitle codec from the given list that can be encoded by the browser.
SubtitleCodec[]
required
Array of subtitle codecs to check.
Promise<SubtitleCodec | null>
The first encodable codec from the list, or null if none are encodable.
Source: encode.ts:774

Usage examples

Check encoding capabilities

Get available codecs

Configure encoding with quality presets