Skip to main content
The Conversion class provides a high-level API for converting one media file into another. It supports transcoding, resizing, rotating, trimming, and processing video and audio tracks.

Static methods

Conversion.init()

Initializes a new conversion process without starting the conversion. This method analyzes the input file, determines which tracks can be converted, and prepares the output configuration.
ConversionOptions
required
Configuration for the conversion

Properties

Input
The input file.
Output
The output file.
boolean
Whether this conversion, as it has been configured, is valid and can be executed. If this field is false, check the discardedTracks field for reasons.
InputTrack[]
The list of tracks that are included in the output file.
DiscardedTrack[]
The list of tracks from the input file that have been discarded, alongside the discard reason.
(progress: number) => unknown
A callback that is fired whenever the conversion progresses. Returns a number between 0 and 1, indicating the completion of the conversion. Must be set before execute() is called for progress to be computed.

Methods

execute()

Executes the conversion process. Resolves once conversion is complete. Will throw if isValid is false.

cancel()

Cancels the conversion process, causing any ongoing execute call to throw a ConversionCanceledError. Does nothing if the conversion is already complete.

Video options

ConversionVideoOptions

boolean
If true, all video tracks will be discarded and will not be present in the output.
number
The desired width of the output video in pixels, defaulting to the video’s natural display width. If height is not set, it will be deduced automatically based on aspect ratio.
number
The desired height of the output video in pixels, defaulting to the video’s natural display height. If width is not set, it will be deduced automatically based on aspect ratio.
'fill' | 'contain' | 'cover'
The fitting algorithm in case both width and height are set:
  • 'fill' will stretch the image to fill the entire box, potentially altering aspect ratio.
  • 'contain' will contain the entire image within the box while preserving aspect ratio. This may lead to letterboxing.
  • 'cover' will scale the image until the entire box is filled, while preserving aspect ratio.
Rotation
The angle in degrees to rotate the input video by, clockwise. Must be 0, 90, 180, or 270. Rotation is applied before cropping and resizing. This rotation is in addition to the natural rotation of the input video.
boolean
default:"true"
When enabled, Mediabunny will use the rotation metadata in the output file to perform video rotation whenever possible. Set this field to false if you want to ensure the output file does not make use of rotation metadata.
object
Specifies the rectangular region of the input video to crop to
number
The desired frame rate of the output video, in hertz. If not specified, the original input frame rate will be used (which may be variable).
VideoCodec
The desired output video codec.
number | Quality
The desired bitrate of the output video.
'discard' | 'keep'
default:"'discard'"
Whether to discard or keep the transparency information of the input video. Note that for 'keep' to produce a transparent video, you must use an output config that supports it, such as WebM with VP9.
number
default:"5"
The interval, in seconds, of how often frames are encoded as a key frame. Frequent key frames improve seeking behavior but increase file size.
'no-preference' | 'prefer-hardware' | 'prefer-software'
default:"'no-preference'"
A hint that configures the hardware acceleration method used when transcoding.
boolean
When true, video will always be re-encoded instead of directly copying over the encoded samples.
Function
Allows for custom user-defined processing of video frames, e.g. for applying overlays, color transformations, or timestamp modifications. Will be called for each input video sample after transformations and frame rate corrections.
number
An optional hint specifying the width of video samples returned by the process function, for better encoder configuration.
number
An optional hint specifying the height of video samples returned by the process function, for better encoder configuration.

Audio options

ConversionAudioOptions

boolean
If true, all audio tracks will be discarded and will not be present in the output.
number
The desired channel count of the output audio.
number
The desired sample rate of the output audio, in hertz.
AudioCodec
The desired output audio codec.
number | Quality
The desired bitrate of the output audio.
boolean
When true, audio will always be re-encoded instead of directly copying over the encoded samples.
Function
Allows for custom user-defined processing of audio samples, e.g. for applying audio effects, transformations, or timestamp modifications. Will be called for each input audio sample after remixing and resampling.
number
An optional hint specifying the channel count of audio samples returned by the process function, for better encoder configuration.
number
An optional hint specifying the sample rate of audio samples returned by the process function, for better encoder configuration.

Example

Per-track options

You can provide different options for each track by passing a function instead of an object: