Skip to main content
Mediabunny uses two core classes to handle media files: Input for reading and Output for writing. These classes work together to provide a complete media processing pipeline.

Input class

The Input class represents an input media file and is the starting point for all read operations. It abstracts away the complexity of different file formats and provides a unified interface for reading media data.

Creating an Input

To create an Input, you need to specify:
  • formats: An array of supported input formats (MP4, WebM, etc.)
  • source: Where to read the data from (file, URL, buffer, etc.)

Reading media data

Once you have an Input, you can access its tracks and read packets:

Input metadata

The Input class provides access to file-level metadata:

Disposing resources

Always dispose of Input objects when you’re done to free resources:

Output class

The Output class orchestrates the creation of new media files. It manages tracks, encoders, and writes the final output to a target destination.

Creating an Output

To create an Output, specify:
  • format: The output format (MP4, WebM, etc.)
  • target: Where to write the data (buffer, stream, file, etc.)

Adding tracks

Before starting an output, you must add tracks and configure their sources:

Output lifecycle

An Output follows a specific lifecycle:
1

Create output

Instantiate the Output with a format and target.
2

Add tracks

Add video, audio, and subtitle tracks with their sources and metadata.
3

Start output

Call start() to begin accepting media data.
4

Add samples

Feed samples or packets to each track’s source.
5

Finalize

Call finalize() when all media data has been added.

Complete example

Here’s a complete example of reading from one file and writing to another:

Input and Output together

The power of Mediabunny comes from using Input and Output together:

Remuxing

Copy media data from one container format to another without re-encoding.

Transcoding

Decode media samples, process them, and re-encode to different codecs.

Editing

Extract, trim, or combine media from multiple sources.

Analysis

Read media data to analyze quality, extract metadata, or generate previews.

Error handling

Both Input and Output can throw errors during operation:
See the Error Handling guide for comprehensive error management strategies.