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 {
canReencodeAudioTrack,
AudioOperation,
ConvertMediaOnAudioTrackHandler,
getDefaultAudioCodec,
} from '@remotion/webcodecs';
 
const DEFAULT_BITRATE = 128_000;
 
export const defaultOnAudioTrackHandler: ConvertMediaOnAudioTrackHandler =
async ({
track,
defaultAudioCodec,
logLevel,
inputContainer,
outputContainer,
canCopyTrack,
}): Promise<AudioOperation> => {
const bitrate = DEFAULT_BITRATE;
 
if (canCopyTrack) {
return Promise.resolve({type: 'copy'});
}
 
// In the future, we might support containers that don't support audio
// (like GIF, animated WebP, etc.) - in that case, we should drop the audio
if (defaultAudioCodec === null) {
return Promise.resolve({type: 'drop'});
}
 
const canReencode = await canReencodeAudioTrack({
audioCodec: defaultAudioCodec,
track,
bitrate,
});
 
if (canReencode) {
return Promise.resolve({
type: 'reencode',
bitrate,
audioCodec: defaultAudioCodec,
});
}
 
return Promise.resolve({type: 'fail'});
};
Source code for defaultOnAudioTrackHandler
tsx
import {
canReencodeAudioTrack,
AudioOperation,
ConvertMediaOnAudioTrackHandler,
getDefaultAudioCodec,
} from '@remotion/webcodecs';
 
const DEFAULT_BITRATE = 128_000;
 
export const defaultOnAudioTrackHandler: ConvertMediaOnAudioTrackHandler =
async ({
track,
defaultAudioCodec,
logLevel,
inputContainer,
outputContainer,
canCopyTrack,
}): Promise<AudioOperation> => {
const bitrate = DEFAULT_BITRATE;
 
if (canCopyTrack) {
return Promise.resolve({type: 'copy'});
}
 
// In the future, we might support containers that don't support audio
// (like GIF, animated WebP, etc.) - in that case, we should drop the audio
if (defaultAudioCodec === null) {
return Promise.resolve({type: 'drop'});
}
 
const canReencode = await canReencodeAudioTrack({
audioCodec: defaultAudioCodec,
track,
bitrate,
});
 
if (canReencode) {
return Promise.resolve({
type: 'reencode',
bitrate,
audioCodec: defaultAudioCodec,
});
}
 
return Promise.resolve({type: 'fail'});
};

See also