Skip to main content
Mediabunny allows you to read media files with great control and efficiency. You can extract metadata like duration and resolution, read actual media data from video and audio tracks with frame-accurate timing, and work with many commonly used file formats. Files are always read partially (“lazily”), meaning only the bytes required to extract the requested information will be read, keeping performance high and memory usage low.

Creating an input

Reading media files in Mediabunny revolves around the Input class. One instance of Input represents one media file that you want to read.
1

Import the required classes

2

Create a new input

The source specifies where the Input reads data from. See the Streaming guide for available input sources.The formats field specifies which file formats the Input should support. Using ALL_FORMATS means you can load files of any format that Mediabunny supports, but requires including all format parsers. For smaller bundle sizes, specify only the formats you need:
Simply creating an instance of Input will perform zero reads and is practically free. The file will only be read once data is requested.

Reading file metadata

Once you have an Input, you can start reading file-level metadata.

Format and MIME type

Duration and timestamps

Methods prefixed with compute instead of get indicate that the library might need to do more work to retrieve the requested data.

Reading track information

You can extract the list of all media tracks in the file:

Common track properties

Video track properties

Audio track properties

Packet statistics

You can query aggregate statistics about a track’s encoded packets:
Pass a number to computePacketStats(50) to analyze only the first ~50 packets for faster estimation.

Reading media data

Mediabunny has the concept of media sinks for reading media data from tracks. Different sinks provide different levels of abstraction.

Reading encoded packets

Loop over all raw encoded packets of a track:

Reading decoded video samples

Iterate over decoded video frames:
You can also read specific time ranges:

Extracting thumbnails

Generate downscaled thumbnails from a video track:

Reading decoded audio samples

Loop over audio samples for playback:

Manual decoding with WebCodecs

For full control, combine encoded packets with WebCodecs:

Example: Extract metadata from a file

Disposing inputs

When you’re done with an Input, you can dispose of it to free up resources:
This cancels ongoing operations, closes decoders, and prevents future operations. You can also use the using keyword for automatic disposal:
When using FilePathSource, make sure to dispose the input to properly close the internal file handle.