packages/docs/docs/media-parser/samples.mdx
:::warning We are phasing out Media Parser and are moving to Mediabunny! :::
With parseMedia(), you can extract video and audio samples from a variety of media formats.
Use onVideoTrack and/or onAudioTrack to get information about a video track.
import {parseMedia} from '@remotion/media-parser';
const samples = await parseMedia({
src: 'https://remotion.media/video.mp4',
onVideoTrack: ({track, container}) => {
console.log(track.codecEnum);
// ^?
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.
If you return a callback from onVideoTrack and/or onAudioTrack, you can get samples from the track.
import {parseMedia} from '@remotion/media-parser';
const samples = await parseMedia({
src: 'https://remotion.media/video.mp4',
onVideoTrack: ({track, container}) => {
return (sample) => {
console.log(sample);
// ^?
};
},
});
See the type definitions for MediaParserVideoSample and MediaParserAudioSample for more information.
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.
import {parseMedia} from '@remotion/media-parser';
const samples = await parseMedia({
src: 'https://remotion.media/video.mp4',
onVideoTrack: ({track, container}) => {
return (sample) => {
return () => {
console.log(sample, 'is the last sample');
};
};
},
});
In all types of callbacks, you can pause, resume, seek and abort.
import {parseMedia, mediaParserController} from '@remotion/media-parser';
const controller = mediaParserController();
const samples = await parseMedia({
src: 'https://remotion.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.