Skip to main content
Mediabunny uses Sources for reading data and Targets for writing data. These abstractions allow you to work with media files from various locations - memory, disk, network, or streams - using the same consistent API.

Sources for reading

A Source represents where you read media data from. All sources inherit from the abstract Source base class.
source.ts:42-90

BlobSource

Reads from a browser Blob or File object - perfect for file uploads and client-side processing.
source.ts:165-277

BufferSource

Reads from an ArrayBuffer or ArrayBufferView in memory - ideal for small files or pre-loaded data.
source.ts:97-146
BufferSource loads the entire file into memory. Use BlobSource or UrlSource for large files.

UrlSource

Reads from a remote URL using HTTP range requests - perfect for streaming from servers or CDNs.
source.ts:362-634
UrlSource uses intelligent prefetching to minimize latency and optimize for sequential access patterns.

FilePathSource

Reads from a file path on the server - for Node.js, Bun, or Deno environments.
source.ts:654-714
Always call input.dispose() to close the file handle when using FilePathSource.

StreamSource

A general-purpose, callback-driven source for custom reading logic.
source.ts:761-905

ReadableStreamSource

Reads from a ReadableStream<Uint8Array> - perfect for processing data as it arrives.
source.ts:939-1170
ReadableStreamSource is unsized - it doesn’t know the total length. This limits seeking and random access.

Targets for writing

A Target represents where you write media data to. All targets inherit from the abstract Target base class.
target.ts:24-38

BufferTarget

Writes to an ArrayBuffer in memory - great for small files or when you need the complete buffer.
target.ts:46-54
BufferTarget stores the entire output in memory. For large files, use StreamTarget or FilePathTarget.

StreamTarget

Writes to a WritableStream<StreamTargetChunk> - versatile target for streaming, files, or custom destinations.
target.ts:95-129

FilePathTarget

Writes to a file path on the server - for Node.js, Bun, or Deno.
target.ts:146-191
Use chunked: true (default) for better performance with FilePathTarget.

NullTarget

Discards all data - useful when extracting data through callbacks or events.
target.ts:199-204

Choosing the right source and target

Sources:
  • BlobSource - File uploads, drag-and-drop
  • UrlSource - Remote video streaming
  • BufferSource - Small files in memory
  • ReadableStreamSource - MediaRecorder output
Targets:
  • BufferTarget - Download files
  • StreamTarget - File System Access API, custom streams
  • NullTarget - Extract specific data via callbacks

Performance tips

Use appropriate caching

Configure maxCacheSize based on file size and access patterns. Larger caches reduce I/O but use more memory.

Enable chunking

Use chunked: true for StreamTarget and FilePathTarget to reduce write overhead.

Tune parallelism

For UrlSource, increase parallelism for faster downloads on high-bandwidth connections.

Choose prefetch profiles

Use 'network' for remote sources, 'fileSystem' for local files, 'none' for random access.

Monitoring reads and writes

Both sources and targets provide callbacks to monitor data flow:
These callbacks are called very frequently - avoid heavy processing inside them.

Next steps

Input and Output

Learn how to use sources and targets with Input and Output

Formats and Codecs

Understand which formats work with your sources