Skip to main content
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.

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 visualizations CanvasSource 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
Advantages:
  • Simplest API for canvas-based workflows
  • Automatically creates VideoFrames from canvas
  • Handles frame timing internally
Source code: src/media-source.ts:979-1028

VideoSampleSource

Best for: Direct VideoFrame manipulation, custom rendering pipelines, WebCodecs integration Use VideoSampleSource when you need fine-grained control over individual video frames or are working with VideoFrames from other sources.
src/media-source.ts
Advantages:
  • Direct access to VideoFrame API
  • Fine control over frame properties
  • Can accept frames from any source
Source code: src/media-source.ts:938-971

MediaStreamVideoTrackSource

Best for: Real-time capture (webcams, screen recording), live streaming to file MediaStreamVideoTrackSource automatically captures from a MediaStreamTrack in real-time, making it perfect for recording user media.
src/media-source.ts
Advantages:
  • Automatic real-time capture
  • Built-in pause/resume support
  • Handles timestamp synchronization across multiple tracks
Important: Always handle 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
Advantages:
  • Complete control over encoding
  • Can bypass encoding for remuxing
  • Direct access to packet stream
Requirements:
  • Must provide decoder config metadata
  • Must handle B-frames correctly (decode order vs presentation order)
  • Packets must be added in decode order
Source code: src/media-source.ts:172-202

Audio sources

AudioBufferSource

Best for: Web Audio API integration, audio processing workflows
src/media-source.ts
Advantages:
  • Direct AudioBuffer support
  • Automatic timestamp management
  • Perfect for Web Audio API workflows
Source code: src/media-source.ts:1833-1875

AudioSampleSource

Best for: Raw audio data, AudioData manipulation, custom audio processing
src/media-source.ts
Advantages:
  • Fine-grained control over audio samples
  • Works with AudioData directly
  • Precise timestamp control
Source code: src/media-source.ts:1792-1825

MediaStreamAudioTrackSource

Best for: Microphone capture, live audio recording
src/media-source.ts
Advantages:
  • Automatic real-time capture
  • Synchronized with other MediaStream sources
  • Pause/resume support
Source code: src/media-source.ts:1886-2035

EncodedAudioPacketSource

Best for: Custom audio encoding, remuxing, direct packet control
src/media-source.ts
Advantages:
  • Complete encoding control
  • Bypass encoding for remuxing
  • Direct packet access
Source code: src/media-source.ts:1297-1326

Advanced patterns

Handling backpressure

All media source add() 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:
Shorter key frame intervals improve seeking but increase file size. When using multiple video tracks, use the same interval for all tracks.

Pausing MediaStream sources

Temporarily pause capture without stopping the underlying stream:

Best practices

1

Choose the right abstraction level

Start with high-level sources (CanvasSource, AudioBufferSource) unless you need the control of lower-level sources.
2

Always await add() calls

Respect backpressure by awaiting all add() method calls to prevent memory issues.
3

Close sources promptly

Call close() on sources as soon as you’re done adding data to improve performance.
4

Handle errorPromise for MediaStream sources

Always attach error handlers to errorPromise when using MediaStream sources.
5

Match codecs to containers

Ensure your chosen codec is supported by your output format (see supported formats).

See also