Skip to main content

canCopyVideoTrack()

Part of the @remotion/webcodecs package.

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

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

You can obtain a VideoTrack using parseMedia() or during the conversion process using the onVideoTrack callback of convertMedia().

Examples​

Check if a video tracks can be copied
tsx
import {parseMedia} from '@remotion/media-parser';
import {canCopyVideoTrack} from '@remotion/webcodecs';
 
const {videoTracks, container} = await parseMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
fields: {
tracks: true,
container: true,
},
});
 
for (const track of videoTracks) {
canCopyVideoTrack({
outputContainer: 'webm',
inputContainer: container,
inputCodec: track.codecWithoutConfig,
inputRotation: track.rotation,
rotationToApply: 0,
}); // boolean
}
Check if a video tracks can be copied
tsx
import {parseMedia} from '@remotion/media-parser';
import {canCopyVideoTrack} from '@remotion/webcodecs';
 
const {videoTracks, container} = await parseMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
fields: {
tracks: true,
container: true,
},
});
 
for (const track of videoTracks) {
canCopyVideoTrack({
outputContainer: 'webm',
inputContainer: container,
inputCodec: track.codecWithoutConfig,
inputRotation: track.rotation,
rotationToApply: 0,
}); // boolean
}
Copy a video track to VP8, otherwise drop it
tsx
import {convertMedia, canCopyVideoTrack} from '@remotion/webcodecs';
 
await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
videoCodec: 'vp8',
audioCodec: 'opus',
onVideoTrack: async ({track, inputContainer, outputContainer}) => {
const canCopy = canCopyVideoTrack({
inputCodec: track.codecWithoutConfig,
outputContainer,
inputContainer,
inputRotation: track.rotation,
rotationToApply: 0,
});
 
if (canCopy) {
return {type: 'copy'};
}
 
// In reality, you would re-encode the track here
return {type: 'drop'};
},
});
Copy a video track to VP8, otherwise drop it
tsx
import {convertMedia, canCopyVideoTrack} from '@remotion/webcodecs';
 
await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
videoCodec: 'vp8',
audioCodec: 'opus',
onVideoTrack: async ({track, inputContainer, outputContainer}) => {
const canCopy = canCopyVideoTrack({
inputCodec: track.codecWithoutConfig,
outputContainer,
inputContainer,
inputRotation: track.rotation,
rotationToApply: 0,
});
 
if (canCopy) {
return {type: 'copy'};
}
 
// In reality, you would re-encode the track here
return {type: 'drop'};
},
});

API​

inputCodec​

string MediaParserVideoCodec

The codec of the input video track.

inputRotation​

number

The number of degrees the input video track is rotated.

rotationToApply​

number

The number of degrees to rotate the video track.

inputContainer​

string ParseMediaContainer

The container format of the input media.

outputContainer​

string ConvertMediaContainer

The container format of the output media.

Rotation behavior​

Any rotationToApply is in addition to an auto-rotation that is applied by default to fix the orientation of the video track.

If rotationToApply is not the same amount of rotation as inputRotation, this function will always return false, because rotation cannot be performed without re-encoding.

See: Rotating a video

Return value​

Returns a boolean.

See also​