Skip to main content
Mediabunny’s custom coder API allows you to extend the library with your own encoding and decoding implementations. This is useful for adding support for codecs not natively supported by browsers, implementing specialized encoders/decoders, or polyfilling missing WebCodecs support.

When to use custom coders

Missing codec support

Add support for codecs not available in the browser’s WebCodecs implementation (e.g., MP3, AC3).

Polyfilling

Provide fallback implementations when WebCodecs is unavailable or doesn’t support a specific codec.

Specialized encoding

Implement custom encoding logic with specific quality/performance characteristics.

Hardware integration

Interface with custom hardware encoders/decoders or external encoding services.

Architecture

Custom coders integrate seamlessly into Mediabunny’s encoding and decoding pipeline: When you register a custom coder, Mediabunny automatically uses it when:
  1. The custom coder’s supports() method returns true
  2. A native WebCodecs implementation is unavailable or doesn’t support the configuration

Custom video encoder

Create a custom video encoder by extending CustomVideoEncoder:
src/custom-coder.ts
Key points:
  • supports() determines when your encoder is used
  • Access codec and config via readonly properties
  • Call this.onPacket() for each encoded packet
  • Include decoder config metadata in the first onPacket() call
Source code: src/custom-coder.ts:80-102

Custom video decoder

Create a custom video decoder by extending CustomVideoDecoder:
src/custom-coder.ts
Key points:
  • Call this.onSample() for each decoded sample
  • Create VideoFrames with proper format and timestamps
  • Handle B-frames correctly (buffering may be needed)
Source code: src/custom-coder.ts:20-42

Custom audio encoder

Create a custom audio encoder by extending CustomAudioEncoder:
src/custom-coder.ts
Source code: src/custom-coder.ts:110-132

Custom audio decoder

Create a custom audio decoder by extending CustomAudioDecoder:
src/custom-coder.ts
Source code: src/custom-coder.ts:50-72

Registration

Register your custom coders before creating outputs or inputs:
Register custom coders before creating Output or Input instances to ensure they’re available when needed.
Source code:

Polyfilling missing codecs

Custom coders are perfect for polyfilling codecs not supported by a browser:

Real-world examples

Mediabunny extensions use custom coders:

MP3 Encoder

WASM-based MP3 encoder using LAME@mediabunny/mp3-encoder

AC-3 Decoder

WASM-based AC-3/E-AC-3 decoder@mediabunny/ac3
See the extension source code for complete implementations.

Best practices

1

Implement supports() carefully

The supports() method determines when your coder is used. Return false if native support is better.
2

Handle errors gracefully

Throw descriptive errors from your methods - they’ll be caught and surfaced to the user.
3

Manage resources

Always clean up in close(). This includes WASM memory, worker threads, etc.
4

Provide metadata

Include complete decoder config in the first onPacket() call for encoders.
5

Use async operations

Methods can return void or Promise<void>. Use async when needed for WASM or workers.
6

Test thoroughly

Test with various configurations, frame sizes, and edge cases.

API reference

CustomVideoEncoder

CustomVideoDecoder

CustomAudioEncoder

CustomAudioDecoder

See also