Skip to main content

Seekingv4.0.291

In parseMedia(), parseMediaOnWebWorker() and parseMediaOnServerWorker(), you can seek to a different position in the media file.

Basic seeking
tsx
import {parseMedia, mediaParserController} from '@remotion/media-parser';
 
const controller = mediaParserController();
 
// You can make a seek even before starting a parse
// This will be considered before emitting the first sample
controller.seek({
type: 'keyframe-before-time',
timeInSeconds: 5,
});
 
await parseMedia({
src: 'https://example.com/video.mp4',
controller,
onVideoTrack: () => {
return () => {
// You can also seek inmidst a parse.
controller.seek({
type: 'keyframe-before-time',
timeInSeconds: 10,
});
};
},
});

Types of seek

keyframe-before-time

This will seek to the last keyframe that comes before the time you specified.
If there is a keyframe at exactly this time, it will seek to this one.

Remember that if you want to decode a video, you always have to start at a keyframe. If you are not interested in the frames before time, you still need to decode them, but you may discard them.

Seeking to a keyframe
tsx
import {mediaParserController} from '@remotion/media-parser';
 
const controller = mediaParserController();
 
controller.seek({
type: 'keyframe-before-time',
timeInSeconds: 5,
});

When you cannot seek

It is not allowed to seek forward if any of the following fields are required:

The reason for this is that all samples need to be iterated over for these fields to be calculated. By seeking forward, some samples would be skipped which would skew the results.

An error will be thrown if you attempt to do this.

How smart is seeking?

The efficiency of seeking depends on the container format.

  • ISO Base Media: Will use observed keyframes, stsd atoms and mfra atoms in case of fragmented files.
  • WebM: Will use Cues and observed keyframes.
  • WAV: Will predict the sample number based on the sample rate and block align.
  • Transport Stream: Not smart - can only use the previously observed PES headers.