Skip to main content

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.

Target

The base class for targets, specifying where output files are written.

Properties

onwrite
((start: number, end: number) => unknown) | null
Called each time data is written to the target. Will be called with the byte range into which data was written.
This callback is extremely chatty and gets called very frequently. Use it to track the size of the output file as it grows.

BufferTarget

A target that writes data directly into an ArrayBuffer in memory. Great for performance, but not suitable for very large files. The buffer will be available once the output has been finalized.

Constructor

new BufferTarget()
No parameters required.

Properties

buffer
ArrayBuffer | null
Stores the final output buffer. Until the output is finalized, this will be null.

Example

const target = new BufferTarget();
const output = new Output(target);

// ... write data to the output

await output.finalize();

const arrayBuffer = target.buffer; // Access the final buffer

StreamTarget

This target writes data to a WritableStream, making it a general-purpose target for writing data anywhere. It is also compatible with FileSystemWritableFileStream for use with the File System Access API. The WritableStream can also apply backpressure, which will propagate to the output and throttle the encoders.

Constructor

new StreamTarget(writable: WritableStream<StreamTargetChunk>, options?: StreamTargetOptions)
writable
WritableStream<StreamTargetChunk>
required
The writable stream to write data to.
options
StreamTargetOptions
Optional configuration.

Options

options.chunked
boolean
default:"false"
When set to true, data created by the output will first be accumulated and only written out once it has reached sufficient size, using a default chunk size of 16 MiB. This is useful for reducing the total amount of writes, at the cost of latency.
options.chunkSize
number
default:"16777216"
When using chunked: true, this specifies the maximum size of each chunk. Defaults to 16 MiB.

StreamTargetChunk type

type
'write'
required
The operation type. Always 'write'. This ensures automatic compatibility with FileSystemWritableFileStream.
data
Uint8Array<ArrayBuffer>
required
The data to write.
position
number
required
The byte offset in the output file at which to write the data.

Example

const writable = new WritableStream({
  write(chunk) {
    console.log(`Writing ${chunk.data.length} bytes at position ${chunk.position}`);
  }
});

const target = new StreamTarget(writable, {
  chunked: true,
  chunkSize: 8 * 1024 * 1024 // 8 MiB chunks
});

FilePathTarget

A target that writes to a file at the specified path. Intended for server-side usage in Node, Bun, or Deno.
Writing is chunked by default. The internally held file handle will be closed when .finalize() or .cancel() are called on the corresponding Output.

Constructor

new FilePathTarget(filePath: string, options?: FilePathTargetOptions)
filePath
string
required
The path to the file to write to.
options
FilePathTargetOptions
Optional configuration. Same as StreamTargetOptions.

Options

options.chunked
boolean
default:"true"
When set to true, data created by the output will first be accumulated and only written out once it has reached sufficient size. Defaults to true for FilePathTarget.
options.chunkSize
number
default:"16777216"
The maximum size of each chunk. Defaults to 16 MiB.

Example

const target = new FilePathTarget('./output.mp4', {
  chunked: true,
  chunkSize: 32 * 1024 * 1024 // 32 MiB chunks
});

const output = new Output(target);

// ... write data to the output

await output.finalize(); // File handle is closed automatically

NullTarget

This target just discards all incoming data. It is useful for when you need an Output but extract data from it differently, for example through format-specific callbacks (onMoof, onMdat, …) or encoder events.

Constructor

new NullTarget()
No parameters required.

Example

const target = new NullTarget();
const output = new Output(target);

// Use format-specific callbacks instead of writing to a file
const muxer = new MP4Muxer({
  target,
  onMoof: (data, offset) => {
    // Handle moof box data
  },
  onMdat: (data, offset) => {
    // Handle mdat box data
  }
});

Build docs developers (and LLMs) love