Skip to main content

Fast and slow operations

@remotion/media-parser allows you to specify the information that you want to obtain.
It then reads as little data as possible to achieve the goal.

There are three types of fields:

  • Header-only: Only requires the first few bits of the file to be read.
  • Metadata-only: Only requires the metadata section to be parsed
  • Full parse required: The entire file is read and processed.

Obviously, processing less of the file is faster and you should aim to read only the information you require.

Full parsing operations

The following fields require the full file to be read:

Also, if an onVideoTrack or onAudioTrack handler is passed, and the handler function returns an callback function for each sample, full parsing is required.

Also, if convertMedia() is used, full parsing is always required.

Metadata-only operations

The following fields require only the metadata section of the video to be parsed:

Also, if an onVideoTrack or onAudioTrack handler is passed, only the parsing of the metadata section is required if null is returned from the handler function.

Header-only operations

The following fields require only the first few bytes of the media to be parsed:

Seeking required

If you load videos from a URL, make sure that they support the Range header.
Otherwise, @remotion/media-parser has no choice but to read the full file if the metadata is at the end of it.

Example

Reading header only
tsx
// Some fields only require the first few bytes of the file to be read:
const result = await parseMedia({
src: 'https://example.com/my-video.mp4',
fields: {
size: true,
container: true,
internalStats: true,
},
});
 
console.log(result.internalStats.finalCursorOffset); // 12
 
// Reading the metadata of the video will only require the metadata section to be parsed.
// You can also use onVideoTrack() and return null to retrieve track information but to not get the samples.
const result2 = await parseMedia({
src: 'https://example.com/my-video.mp4',
fields: {
durationInSeconds: true,
dimensions: true,
internalStats: true,
},
onVideoTrack: ({track}) => {
console.log(track);
return null;
},
});
 
console.log(result2.internalStats.finalCursorOffset); // 4000
console.log(result2.dimensions);
 
// Asking for all video samples requires parsing the whole file
const result3 = await parseMedia({
src: 'https://example.com/my-video.mp4',
fields: {
internalStats: true,
},
onVideoTrack: () => {
return (videoSample) => console.log(videoSample);
},
});
 
console.log(result3.internalStats.finalCursorOffset); // 1870234802
Reading header only
tsx
// Some fields only require the first few bytes of the file to be read:
const result = await parseMedia({
src: 'https://example.com/my-video.mp4',
fields: {
size: true,
container: true,
internalStats: true,
},
});
 
console.log(result.internalStats.finalCursorOffset); // 12
 
// Reading the metadata of the video will only require the metadata section to be parsed.
// You can also use onVideoTrack() and return null to retrieve track information but to not get the samples.
const result2 = await parseMedia({
src: 'https://example.com/my-video.mp4',
fields: {
durationInSeconds: true,
dimensions: true,
internalStats: true,
},
onVideoTrack: ({track}) => {
console.log(track);
return null;
},
});
 
console.log(result2.internalStats.finalCursorOffset); // 4000
console.log(result2.dimensions);
 
// Asking for all video samples requires parsing the whole file
const result3 = await parseMedia({
src: 'https://example.com/my-video.mp4',
fields: {
internalStats: true,
},
onVideoTrack: () => {
return (videoSample) => console.log(videoSample);
},
});
 
console.log(result3.internalStats.finalCursorOffset); // 1870234802

See also