Skip to main content
Mediabunny enables you to create media files with fine-grained control. You can add multiple video, audio, and subtitle tracks to a media file and precisely control the timing of media data. Using output targets, you can decide whether to build the entire file in memory or stream it out in chunks as it’s being created, allowing you to create very large files efficiently.

Creating an output

Media file creation in Mediabunny revolves around the Output class. One instance of Output represents one media file you want to create.
1

Import the required classes

2

Create a new output

The format determines the container format of the output file (MP4, WebM, etc.).The target determines where the data will be written (memory, disk, stream, etc.). See the Streaming guide for available targets.

Adding tracks

Before starting an output, you need to add tracks to it. Each track requires a media source that provides the media data.

Adding a video track

Adding an audio track

Track metadata options

The frameRate option snaps all timestamps and durations to the specified frame rate. To achieve fractional frame rates precisely, use their exact fractional forms:
  • 23.976 → 24000/1001
  • 29.97 → 30000/1001
  • 59.94 → 60000/1001

Setting metadata tags

You can write descriptive metadata tags to the output file:
Metadata tags must be set before calling output.start().
See the Metadata guide for all available metadata fields.

Starting an output

After adding all tracks, you need to start the output:
This spins up the writing process and prevents adding new tracks. After this, you can start sending media data to the output file.

Adding media data

After starting an output, use the media sources to pipe data to the output file:
The API differs for each media source type - check the media sources documentation for details.

Finalizing an output

Once all media data has been added, finalize the output:
After calling finalize(), adding more media data will result in an error.

Output state

You can check the current state of an output:
  • 'pending' - Not started yet; tracks can be added
  • 'started' - Ready to receive media data; no more tracks can be added
  • 'finalizing' - finalize() has been called but hasn’t completed
  • 'finalized' - Output is complete
  • 'canceled' - Output was canceled

Canceling an output

To cancel an ongoing output:
This frees up resources like encoders and prevents adding more data.

Example: Record canvas and microphone

Getting the MIME type

To retrieve the full MIME type of the output file (including codec strings):
This promise only resolves once codec strings for all tracks are known, which requires encoders to be initialized. Don’t await this before adding media data or you’ll create a deadlock.

Packet buffering

Some output formats require packet buffering for multi-track outputs. The output must wait for data from all tracks for a given timestamp before writing.
To minimize memory usage, add media data in an interleaved way. For example, add 10 seconds of video, then 10 seconds of audio, then repeat - instead of adding all video first, then all audio.