Skip to main content

Overview

Mediabunny allows you to implement custom video and audio encoders and decoders that integrate seamlessly with the library’s encoding and decoding pipeline. This is useful when you need to support codecs not natively available in the browser or want to use alternative implementations.

Custom decoders

CustomVideoDecoder

Base class for custom video decoders.
Properties:
VideoCodec
The input video’s codec.
VideoDecoderConfig
The input video’s decoder config.
(sample: VideoSample) => unknown
The callback to call when a decoded VideoSample is available.
Methods:
(codec: VideoCodec, config: VideoDecoderConfig) => boolean
Returns true if and only if the decoder can decode the given codec configuration.
() => MaybePromise<void>
Called after decoder creation; can be used for custom initialization logic.
(packet: EncodedPacket) => MaybePromise<void>
Decodes the provided encoded packet.
() => MaybePromise<void>
Decodes all remaining packets and then resolves.
() => MaybePromise<void>
Called when the decoder is no longer needed and its resources can be freed.
Source: custom-coder.ts:20

CustomAudioDecoder

Base class for custom audio decoders.
Properties:
AudioCodec
The input audio’s codec.
AudioDecoderConfig
The input audio’s decoder config.
(sample: AudioSample) => unknown
The callback to call when a decoded AudioSample is available.
Methods:
(codec: AudioCodec, config: AudioDecoderConfig) => boolean
Returns true if and only if the decoder can decode the given codec configuration.
() => MaybePromise<void>
Called after decoder creation; can be used for custom initialization logic.
(packet: EncodedPacket) => MaybePromise<void>
Decodes the provided encoded packet.
() => MaybePromise<void>
Decodes all remaining packets and then resolves.
() => MaybePromise<void>
Called when the decoder is no longer needed and its resources can be freed.
Source: custom-coder.ts:50

Custom encoders

CustomVideoEncoder

Base class for custom video encoders.
Properties:
VideoCodec
The codec with which to encode the video.
VideoEncoderConfig
Config for the encoder.
(packet: EncodedPacket, meta?: EncodedVideoChunkMetadata) => unknown
The callback to call when an EncodedPacket is available.
Methods:
(codec: VideoCodec, config: VideoEncoderConfig) => boolean
Returns true if and only if the encoder can encode the given codec configuration.
() => MaybePromise<void>
Called after encoder creation; can be used for custom initialization logic.
(videoSample: VideoSample, options: VideoEncoderEncodeOptions) => MaybePromise<void>
Encodes the provided video sample.
() => MaybePromise<void>
Encodes all remaining video samples and then resolves.
() => MaybePromise<void>
Called when the encoder is no longer needed and its resources can be freed.
Source: custom-coder.ts:80

CustomAudioEncoder

Base class for custom audio encoders.
Properties:
AudioCodec
The codec with which to encode the audio.
AudioEncoderConfig
Config for the encoder.
(packet: EncodedPacket, meta?: EncodedAudioChunkMetadata) => unknown
The callback to call when an EncodedPacket is available.
Methods:
(codec: AudioCodec, config: AudioEncoderConfig) => boolean
Returns true if and only if the encoder can encode the given codec configuration.
() => MaybePromise<void>
Called after encoder creation; can be used for custom initialization logic.
(audioSample: AudioSample) => MaybePromise<void>
Encodes the provided audio sample.
() => MaybePromise<void>
Encodes all remaining audio samples and then resolves.
() => MaybePromise<void>
Called when the encoder is no longer needed and its resources can be freed.
Source: custom-coder.ts:110

Registration functions

registerDecoder

Registers a custom video or audio decoder. Registered decoders will automatically be used for decoding whenever possible.
typeof CustomVideoDecoder | typeof CustomAudioDecoder
required
The custom decoder class to register.
Source: custom-coder.ts:145

registerEncoder

Registers a custom video or audio encoder. Registered encoders will automatically be used for encoding whenever possible.
typeof CustomVideoEncoder | typeof CustomAudioEncoder
required
The custom encoder class to register.
Source: custom-coder.ts:175

Usage examples

Implementing a custom video decoder

Implementing a custom audio encoder

Using registered custom coders

Custom coders are checked before native browser encoders/decoders. If your custom coder’s supports() method returns true, it will be used instead of the native implementation.