Media sources provide APIs for adding media data to an output file. Mediabunny offers multiple source types at different abstraction levels, allowing you to choose the right balance between convenience and control for your use case.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Vanilagy/mediabunny/llms.txt
Use this file to discover all available pages before exploring further.
Overview of media sources
Media sources can be organized into three abstraction levels:High-level sources
CanvasSource, AudioBufferSource, MediaStreamVideoTrackSource, MediaStreamAudioTrackSourceEasy to use, handles encoding automatically.Mid-level sources
VideoSampleSource, AudioSampleSourceWork with raw samples, handles encoding internally.Low-level sources
EncodedVideoPacketSource, EncodedAudioPacketSourceDirect packet control, you handle encoding.When to use each source
CanvasSource
Best for: Browser-based rendering, animations, games, data visualizationsCanvasSource is ideal when you’re rendering to a canvas element and want to capture that rendering as video. This is the most common use case for browser-based video creation.
src/media-source.ts
- Simplest API for canvas-based workflows
- Automatically creates VideoFrames from canvas
- Handles frame timing internally
VideoSampleSource
Best for: Direct VideoFrame manipulation, custom rendering pipelines, WebCodecs integration UseVideoSampleSource when you need fine-grained control over individual video frames or are working with VideoFrames from other sources.
src/media-source.ts
- Direct access to VideoFrame API
- Fine control over frame properties
- Can accept frames from any source
MediaStreamVideoTrackSource
Best for: Real-time capture (webcams, screen recording), live streaming to fileMediaStreamVideoTrackSource automatically captures from a MediaStreamTrack in real-time, making it perfect for recording user media.
src/media-source.ts
- Automatic real-time capture
- Built-in pause/resume support
- Handles timestamp synchronization across multiple tracks
errorPromise to catch asynchronous errors.
Source code: src/media-source.ts:1039-1267
EncodedVideoPacketSource
Best for: Custom encoding pipelines, remuxing without re-encoding, WebCodecs manual control Use this source when you need complete control over the encoding process or want to bypass encoding entirely.src/media-source.ts
- Complete control over encoding
- Can bypass encoding for remuxing
- Direct access to packet stream
- Must provide decoder config metadata
- Must handle B-frames correctly (decode order vs presentation order)
- Packets must be added in decode order
Audio sources
AudioBufferSource
Best for: Web Audio API integration, audio processing workflowssrc/media-source.ts
- Direct AudioBuffer support
- Automatic timestamp management
- Perfect for Web Audio API workflows
AudioSampleSource
Best for: Raw audio data, AudioData manipulation, custom audio processingsrc/media-source.ts
- Fine-grained control over audio samples
- Works with AudioData directly
- Precise timestamp control
MediaStreamAudioTrackSource
Best for: Microphone capture, live audio recordingsrc/media-source.ts
- Automatic real-time capture
- Synchronized with other MediaStream sources
- Pause/resume support
EncodedAudioPacketSource
Best for: Custom audio encoding, remuxing, direct packet controlsrc/media-source.ts
- Complete encoding control
- Bypass encoding for remuxing
- Direct packet access
Advanced patterns
Handling backpressure
All media sourceadd() methods return promises. Always await these to respect encoder and writer backpressure:
Closing sources early
Close sources as soon as you’re done adding data to improve performance:Managing video sample size changes
Control what happens when video frame dimensions change:Encoding alpha channels
Preserve transparency when encoding:Only certain codecs and containers support alpha channels. VP9 in WebM is the most common combination.
Custom key frame intervals
Control how frequently key frames are inserted:Pausing MediaStream sources
Temporarily pause capture without stopping the underlying stream:Best practices
Choose the right abstraction level
Start with high-level sources (CanvasSource, AudioBufferSource) unless you need the control of lower-level sources.
Always await add() calls
Respect backpressure by awaiting all
add() method calls to prevent memory issues.Close sources promptly
Call
close() on sources as soon as you’re done adding data to improve performance.Handle errorPromise for MediaStream sources
Always attach error handlers to
errorPromise when using MediaStream sources.Match codecs to containers
Ensure your chosen codec is supported by your output format (see supported formats).
See also
- Writing media files - Complete guide to creating output files
- Packets and samples - Understanding media data structures
- Custom coders - Implementing custom encoders and decoders