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 behaviortsx
import {convertMedia ,defaultOnVideoTrackHandler } from '@remotion/webcodecs';awaitconvertMedia ({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 behaviorreturndefaultOnVideoTrackHandler (params );},});
Falling back to the default behaviortsx
import {convertMedia ,defaultOnVideoTrackHandler } from '@remotion/webcodecs';awaitconvertMedia ({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 behaviorreturndefaultOnVideoTrackHandler (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 toconvertMedia()
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 defaultOnAudioTrackHandlertsx
import {canCopyAudioTrack ,canReencodeAudioTrack ,AudioOperation ,ConvertMediaOnAudioTrackHandler ,getDefaultAudioCodec ,} from '@remotion/webcodecs';constDEFAULT_BITRATE = 128_000;export constdefaultOnAudioTrackHandler :ConvertMediaOnAudioTrackHandler =async ({track ,defaultAudioCodec ,logLevel ,container ,}):Promise <AudioOperation > => {constbitrate =DEFAULT_BITRATE ;constcanCopy =canCopyAudioTrack ({inputCodec :track .codecWithoutConfig ,container ,});if (canCopy ) {returnPromise .resolve ({type : 'copy'});}constaudioCodec =defaultAudioCodec ??getDefaultAudioCodec ({container });constcanReencode = awaitcanReencodeAudioTrack ({audioCodec ,track ,bitrate ,});if (canReencode ) {returnPromise .resolve ({type : 'reencode',bitrate ,audioCodec ,});}returnPromise .resolve ({type : 'fail'});};
Source code for defaultOnAudioTrackHandlertsx
import {canCopyAudioTrack ,canReencodeAudioTrack ,AudioOperation ,ConvertMediaOnAudioTrackHandler ,getDefaultAudioCodec ,} from '@remotion/webcodecs';constDEFAULT_BITRATE = 128_000;export constdefaultOnAudioTrackHandler :ConvertMediaOnAudioTrackHandler =async ({track ,defaultAudioCodec ,logLevel ,container ,}):Promise <AudioOperation > => {constbitrate =DEFAULT_BITRATE ;constcanCopy =canCopyAudioTrack ({inputCodec :track .codecWithoutConfig ,container ,});if (canCopy ) {returnPromise .resolve ({type : 'copy'});}constaudioCodec =defaultAudioCodec ??getDefaultAudioCodec ({container });constcanReencode = awaitcanReencodeAudioTrack ({audioCodec ,track ,bitrate ,});if (canReencode ) {returnPromise .resolve ({type : 'reencode',bitrate ,audioCodec ,});}returnPromise .resolve ({type : 'fail'});};