Skip to main content

Extract samples

With parseMedia(), you can extract video and audio samples from a variety of media formats.

Getting tracks

Use onVideoTrack and/or onAudioTrack to get information about a video track.

Extract tracks from a video
tsx
import {parseMedia} from '@remotion/media-parser';
 
const samples = await parseMedia({
src: 'https://parser.media/video.mp4',
onVideoTrack: ({track, container}) => {
console.log(track.codecEnum)
(property) codecEnum: MediaParserVideoCodec
return null
}
});

See the type definitions for MediaParserVideoTrack and MediaParserAudioTrack for more information.

Return null if you are not interested in getting samples from the track.

Getting samples

If you return a callback from onVideoTrack and/or onAudioTrack, you can get samples from the track.

Extract samples from a video
tsx
import {parseMedia} from '@remotion/media-parser';
 
const samples = await parseMedia({
src: 'https://parser.media/video.mp4',
onVideoTrack: ({track, container}) => {
return (sample) => {
console.log(sample)
(parameter) sample: MediaParserVideoSample
}
}
});

See the type definitions for MediaParserVideoSample and MediaParserAudioSample for more information.

Check if a sample was the last onev4.0.307

If you would like to execute code when the last sample of a track has been parsed, you can return a callback from the sample callback that will be called when the last sample has been parsed.

Execute code when the last sample of a track has been parsed
tsx
import {parseMedia} from '@remotion/media-parser';
 
const samples = await parseMedia({
src: 'https://parser.media/video.mp4',
onVideoTrack: ({track, container}) => {
return (sample) => {
return () => {
console.log(sample, 'is the last sample')
}
}
}
});

Seeking in callbacks

In all types of callbacks, you can pause, resume, seek and abort.

Looping the parse
tsx
import {parseMedia, mediaParserController} from '@remotion/media-parser';
 
const controller = mediaParserController()
 
const samples = await parseMedia({
src: 'https://parser.media/video.mp4',
controller,
onVideoTrack: ({track, container}) => {
return (sample) => {
return () => {
// When it's the last sample, seek to the beginning
controller.seek(0)
}
}
}
});

See: mediaParserController() and Seeking.