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:- The custom coder’s
supports()method returnstrue - A native WebCodecs implementation is unavailable or doesn’t support the configuration
Custom video encoder
Create a custom video encoder by extendingCustomVideoEncoder:
src/custom-coder.ts
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
Custom video decoder
Create a custom video decoder by extendingCustomVideoDecoder:
src/custom-coder.ts
- Call
this.onSample()for each decoded sample - Create VideoFrames with proper format and timestamps
- Handle B-frames correctly (buffering may be needed)
Custom audio encoder
Create a custom audio encoder by extendingCustomAudioEncoder:
src/custom-coder.ts
Custom audio decoder
Create a custom audio decoder by extendingCustomAudioDecoder:
src/custom-coder.ts
Registration
Register your custom coders before creating outputs or inputs:registerEncoder: src/custom-coder.ts:175-197registerDecoder: src/custom-coder.ts:145-167
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-encoderAC-3 Decoder
WASM-based AC-3/E-AC-3 decoder
@mediabunny/ac3Best 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
Properties
Properties
Methods
Methods
CustomVideoDecoder
Properties
Properties
Methods
Methods
CustomAudioEncoder
Properties
Properties
Methods
Methods
CustomAudioDecoder
Properties
Properties
Methods
Methods
See also
- Extensions - Pre-built codec extensions
- Media sources - Using custom encoders with media sources
- Media sinks - Using custom decoders with media sinks
- Packets and samples - Understanding media data structures