Skip to main content

defaultOnAudioTrackHandler()

This is the default function if no onAudioTrack handler is provided to convertMedia().
You may use this function if you want to customize part of the track transformation logic, but fall back to the default behavior for the rest.

Falling back to the default behavior
tsx
import {convertMedia, defaultOnVideoTrackHandler} from '@remotion/webcodecs';
 
await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
onVideoTrack: (params) => {
// Custom logic for handling video tracks
// ...
 
// Fall back to the default behavior
return defaultOnVideoTrackHandler(params);
},
});
Falling back to the default behavior
tsx
import {convertMedia, defaultOnVideoTrackHandler} from '@remotion/webcodecs';
 
await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
onVideoTrack: (params) => {
// Custom logic for handling video tracks
// ...
 
// Fall back to the default behavior
return defaultOnVideoTrackHandler(params);
},
});

Algorithm

The default behavior is as follows:

  • Check if the track can be copied without re-encoding, if true, then do that.
  • Determine the audio codec to be used - either audioCodec which was passed to convertMedia() or the default codec for the container.
  • Check if the track can be re-encoded with the chosen audio codec and bitrate, if true, then do that.
  • If the track can be neither copied nor re-encoded, then fail the render.
    You may alternatively return {type: 'drop'} to remove the audio track, but still succeed the other tracks.

This is the source code for the default function. You may use this as a reference to create your own custom handler.

Source code for defaultOnAudioTrackHandler
tsx
import {
canCopyAudioTrack,
canReencodeAudioTrack,
AudioOperation,
ConvertMediaOnAudioTrackHandler,
getDefaultAudioCodec,
} from '@remotion/webcodecs';
 
const DEFAULT_BITRATE = 128_000;
 
export const defaultOnAudioTrackHandler: ConvertMediaOnAudioTrackHandler =
async ({
track,
defaultAudioCodec,
logLevel,
container,
}): Promise<AudioOperation> => {
const bitrate = DEFAULT_BITRATE;
 
const canCopy = canCopyAudioTrack({
inputCodec: track.codecWithoutConfig,
container,
});
 
if (canCopy) {
return Promise.resolve({type: 'copy'});
}
 
const audioCodec = defaultAudioCodec ?? getDefaultAudioCodec({container});
const canReencode = await canReencodeAudioTrack({
audioCodec,
track,
bitrate,
});
 
if (canReencode) {
return Promise.resolve({
type: 'reencode',
bitrate,
audioCodec,
});
}
 
return Promise.resolve({type: 'fail'});
};
Source code for defaultOnAudioTrackHandler
tsx
import {
canCopyAudioTrack,
canReencodeAudioTrack,
AudioOperation,
ConvertMediaOnAudioTrackHandler,
getDefaultAudioCodec,
} from '@remotion/webcodecs';
 
const DEFAULT_BITRATE = 128_000;
 
export const defaultOnAudioTrackHandler: ConvertMediaOnAudioTrackHandler =
async ({
track,
defaultAudioCodec,
logLevel,
container,
}): Promise<AudioOperation> => {
const bitrate = DEFAULT_BITRATE;
 
const canCopy = canCopyAudioTrack({
inputCodec: track.codecWithoutConfig,
container,
});
 
if (canCopy) {
return Promise.resolve({type: 'copy'});
}
 
const audioCodec = defaultAudioCodec ?? getDefaultAudioCodec({container});
const canReencode = await canReencodeAudioTrack({
audioCodec,
track,
bitrate,
});
 
if (canReencode) {
return Promise.resolve({
type: 'reencode',
bitrate,
audioCodec,
});
}
 
return Promise.resolve({type: 'fail'});
};

See also