Skip to main content

canCopyAudioTrack()

Part of the @remotion/webcodecs package.

warning

Unstable API: This package is experimental. We might change the API at any time, until we remove this notice.

Given an AudioTrack, determine if it can be copied to the output without re-encoding.

You can obtain an AudioTrack using parseMedia() or during the conversion process using the onAudioTrack callback of convertMedia().

Examples

Check if an audio track can be copied
tsx
import {parseMedia} from '@remotion/media-parser';
import {canCopyAudioTrack} from '@remotion/webcodecs';
 
const {audioTracks} = await parseMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
fields: {
tracks: true,
},
});
 
for (const track of audioTracks) {
canCopyAudioTrack({
inputCodec: track.codecWithoutConfig,
outputCodec: 'opus',
container: 'webm',
}); // boolean
}
Check if an audio track can be copied
tsx
import {parseMedia} from '@remotion/media-parser';
import {canCopyAudioTrack} from '@remotion/webcodecs';
 
const {audioTracks} = await parseMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
fields: {
tracks: true,
},
});
 
for (const track of audioTracks) {
canCopyAudioTrack({
inputCodec: track.codecWithoutConfig,
outputCodec: 'opus',
container: 'webm',
}); // boolean
}
Copy an audio track to Opus, otherwise drop it
tsx
import {convertMedia, canCopyAudioTrack} from '@remotion/webcodecs';
 
await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
videoCodec: 'vp8',
audioCodec: 'opus',
onAudioTrack: async ({track}) => {
const canCopy = canCopyAudioTrack({
inputCodec: track.codecWithoutConfig,
outputCodec: 'opus',
container: 'webm',
});
 
if (canCopy) {
return {type: 'copy'};
}
 
// Just to keep the example brief, in reality, you would re-encode the track here
return {type: 'drop'};
},
});
Copy an audio track to Opus, otherwise drop it
tsx
import {convertMedia, canCopyAudioTrack} from '@remotion/webcodecs';
 
await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
videoCodec: 'vp8',
audioCodec: 'opus',
onAudioTrack: async ({track}) => {
const canCopy = canCopyAudioTrack({
inputCodec: track.codecWithoutConfig,
outputCodec: 'opus',
container: 'webm',
});
 
if (canCopy) {
return {type: 'copy'};
}
 
// Just to keep the example brief, in reality, you would re-encode the track here
return {type: 'drop'};
},
});

API

inputCodec

string MediaParserAudioCodec

The codec of the input audio track.

outputCodec

string ConvertMediaAudioCodec

The codec of the output audio track.

container

string ConvertMediaContainer

The container format of the output media.

Return value

Returns a boolean.

See also