Use this file to discover all available pages before exploring further.
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.
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.
Once you have an Input, you can access its tracks and read packets:
// Get all tracksconst tracks = await input.getTracks();// Get specific track typesconst videoTracks = await input.getVideoTracks();const audioTracks = await input.getAudioTracks();// Get primary tracksconst videoTrack = await input.getPrimaryVideoTrack();const audioTrack = await input.getPrimaryAudioTrack();// Read packets from a trackfor await (const packet of videoTrack.readPackets()) { // Process packet}
Always dispose of Input objects when you’re done to free resources:
input.dispose();// Or use explicit resource management (ECMAScript 2023)using input = new Input({ formats: [MP4], source });// Automatically disposed at end of scope
target: Where to write the data (buffer, stream, file, etc.)
import { Output, Mp4OutputFormat, BufferTarget } from 'mediabunny';const output = new Output({ format: new Mp4OutputFormat(), target: new BufferTarget()});
Both Input and Output can throw errors during operation:
try { const input = new Input({ formats: [MP4], source }); const format = await input.getFormat();} catch (error) { if (error.message.includes('unsupported or unrecognizable format')) { console.error('File format not supported'); } else if (error instanceof InputDisposedError) { console.error('Input was disposed before operation completed'); }}
See the Error Handling guide for comprehensive error management strategies.