Skip to main content
Mediabunny provides powerful streaming capabilities for both reading and writing media files. This enables memory-efficient operations on large files by processing data in chunks rather than loading entire files into memory.

Input sources

Input sources determine where an Input reads data from. All sources support lazy loading - only the bytes needed for the requested operation are read.

BufferSource

Reads from an in-memory ArrayBuffer.
Pros: Fastest source available Cons: Requires entire file in memory

BlobSource

Reads from a Blob or File.
Pros: Perfect for reading files from disk in the browser Cons: Browser-only

UrlSource

Fetches data from a remote URL over the network.
The server must support range requests (HTTP 206 responses). For cross-origin requests, ensure CORS is properly configured.
Pros: Intelligently prefetches data based on access patterns Cons: Requires network access and CORS configuration

Retry logic

Customize retry behavior when requests fail:
Default behavior:
  • Infinite exponential backoff, capped at 16 seconds
  • No retries if a CORS error is suspected

FilePathSource

Reads from a file path. Requires Node.js, Bun, or Deno.
Make sure to call input.dispose() when done to properly close the internal file handle.
Pros: Direct file system access in server environments Cons: Server-side only

StreamSource

A general-purpose, callback-driven source for reading data from anywhere.

Prefetch profiles

No prefetching - only requested data is loaded. Use for random access patterns.
Pros: Maximum flexibility - read from any data source Cons: Requires manual implementation

ReadableStreamSource

Reads from a ReadableStream of Uint8Array for incrementally streaming files.
This source is unsized - calls to .getSize() will throw. Only use with sequential access patterns like reading all packets or doing conversions.
Pros: Stream in files while they’re being created Cons: Limited to sequential access patterns

Using with MediaRecorder

Combine MediaRecorder with ReadableStreamSource to stream recorded data into Mediabunny:

Monitoring reads

All sources support an onread callback to inspect which areas of the file are being read:

Output targets

Output targets determine where an Output writes data.

BufferTarget

Writes all data to a single in-memory ArrayBuffer.
Pros: Simple and fast for small files Cons: Not suitable for very large files (may cause memory exhaustion)

StreamTarget

Writes data to a WritableStream in chunks.
Some byte regions may be written to multiple times. You must write each chunk at the specified byte offset position in the order chunks arrive - don’t just concatenate them.Some output formats support append-only mode where simple concatenation works. Check the format documentation.

Chunked mode

Enable chunked mode to reduce write frequency:
Data is accumulated in memory until chunks reach the specified size before being emitted.

Backpressure

The output automatically respects backpressure applied by the WritableStream:

Using with File System Access API

StreamTargetChunk is compatible with FileSystemWritableFileStream:
Pros: Memory-efficient for large files, supports backpressure Cons: More complex to use

FilePathTarget

Writes to a file at the specified path. Requires Node.js, Bun, or Deno.
The file handle is automatically closed when finalize() or cancel() is called. Pros: Simple API for writing to disk in server environments Cons: Server-side only

NullTarget

Discards all data. Useful when extracting data through other means (format callbacks, encoder events).
Pros: Zero overhead when you don’t need the final file Cons: No output file produced

Monitoring writes

All targets support an onwrite callback:
This callback is called extremely frequently. Use it carefully.

Example: Process large file without loading into memory