Skip to content

🎥 Make videos programmatically with React

Change .seek() API to be much simpler

Jonny Burger8a0dce70034b
diff --git a/packages/docs/docs/media-parser/media-parser-controller.mdx b/packages/docs/docs/media-parser/media-parser-controller.mdx
index 14ad447d42..c110138bbc 100644
--- a/packages/docs/docs/media-parser/media-parser-controller.mdx
+++ b/packages/docs/docs/media-parser/media-parser-controller.mdx
@@ -48,6 +48,10 @@ Resumes the download and parsing process.
Aborts the download and parsing process.
+### `seek(timeInSeconds: number)`
+
+[Seeks to the best keyframe](/docs/media-parser/seeking) that comes before the time you specified.
+
### `getSeekingHints()`
Returns a promise that resolves to the [seeking hints](/docs/media-parser/seeking-hints).
@@ -62,7 +66,7 @@ See events below.
## Events
-You can attach event listeners to the `mediaParserController` object.
+You can attach event listeners to the object returned by `mediaParserController()`.
```tsx twoslash title="Use events"
import {mediaParserController, parseMedia} from '@remotion/media-parser';
@@ -85,7 +89,7 @@ controller.removeEventListener('pause', onPause);
controller.removeEventListener('resume', onResume);
```
-The `mediaParserController` object emits the following events:
+It also emits the following events:
### `pause`
diff --git a/packages/docs/docs/media-parser/seeking.mdx b/packages/docs/docs/media-parser/seeking.mdx
index b5c7af7e9a..28ce44c5f8 100644
--- a/packages/docs/docs/media-parser/seeking.mdx
+++ b/packages/docs/docs/media-parser/seeking.mdx
@@ -17,10 +17,7 @@ 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,
-});
+controller.seek(5); // Seek to 5 seconds
await parseMedia({
src: 'https://example.com/video.mp4',
@@ -28,21 +25,16 @@ await parseMedia({
onVideoTrack: () => {
return () => {
// You can also seek inmidst a parse.
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10,
- });
+ controller.seek(10); // Seek to 10 seconds
};
},
});
```
-## Types of seek
-
-### `keyframe-before-time`
+Calling [`.seek()`](/docs/media-parser/media-parser-controller#seek) will **seek to the best keyframe** that comes before the time you specified.
-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.
+If there is a keyframe at exactly this time, it may seek to this one.
+There is no guarantee that this is the last keyframe before the time you specified.
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.
@@ -51,10 +43,7 @@ import {mediaParserController} from '@remotion/media-parser';
const controller = mediaParserController();
-controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5,
-});
+controller.seek(5);
```
## When you cannot seek
diff --git a/packages/media-parser/src/containers/m3u/first-sample-in-m3u-chunk.ts b/packages/media-parser/src/containers/m3u/first-sample-in-m3u-chunk.ts
index ffec1753b7..2af0427f3a 100644
--- a/packages/media-parser/src/containers/m3u/first-sample-in-m3u-chunk.ts
+++ b/packages/media-parser/src/containers/m3u/first-sample-in-m3u-chunk.ts
@@ -40,19 +40,13 @@ export const considerSeekBasedOnChunk = async ({
chunkIndex > 0
) {
m3uState.setNextSeekShouldSubtractChunks(playlistUrl, subtractChunks + 1);
- parentController.seek({
- type: 'keyframe-before-time',
- timeInSeconds: pendingSeek.targetTime,
- });
+ parentController.seek(pendingSeek.targetTime);
return;
}
// We are good, we have not gone too far! Don't emit sample and seek and clear pending seek
- childController.seek({
- type: 'keyframe-before-time',
- timeInSeconds: pendingSeek.targetTime,
- });
+ childController.seek(pendingSeek.targetTime);
m3uState.setNextSeekShouldSubtractChunks(playlistUrl, 0);
m3uState.setSeekToSecondsToProcess(playlistUrl, null);
};
diff --git a/packages/media-parser/src/controller/emitter.ts b/packages/media-parser/src/controller/emitter.ts
index e05f7694ff..fd2fa1c416 100644
--- a/packages/media-parser/src/controller/emitter.ts
+++ b/packages/media-parser/src/controller/emitter.ts
@@ -1,11 +1,10 @@
import {withResolvers} from '../with-resolvers';
-import type {Seek} from './seek-signal';
type MediaParserEventMap = {
pause: undefined;
resume: undefined;
abort: {reason?: unknown};
- seek: {seek: Seek};
+ seek: {seek: number};
};
export type MediaParserEventTypes = keyof MediaParserEventMap;
@@ -84,7 +83,7 @@ export class MediaParserEmitter {
});
};
- dispatchSeek = (seek: Seek) => {
+ dispatchSeek = (seek: number) => {
this.readyPromise = this.readyPromise.then(() => {
this.dispatchEvent('seek', {seek});
});
diff --git a/packages/media-parser/src/controller/seek-signal.ts b/packages/media-parser/src/controller/seek-signal.ts
index 06cf29877c..7f3c318c38 100644
--- a/packages/media-parser/src/controller/seek-signal.ts
+++ b/packages/media-parser/src/controller/seek-signal.ts
@@ -1,20 +1,13 @@
import type {MediaParserEmitter} from './emitter';
export type SeekSignal = {
- seek: (seek: Seek) => void;
- getSeek: () => Seek | undefined;
- clearSeekIfStillSame: (previousSeek: Seek) => {hasChanged: boolean};
+ seek: (timeInSeconds: number) => void;
+ getSeek: () => number | null;
+ clearSeekIfStillSame: (previousSeek: number) => {hasChanged: boolean};
};
-type SeekToTime = {
- type: 'keyframe-before-time';
- timeInSeconds: number;
-};
-
-export type Seek = SeekToTime;
-
export const makeSeekSignal = (emitter: MediaParserEmitter): SeekSignal => {
- let seek: Seek | undefined;
+ let seek: number | null = null;
return {
seek: (seekRequest) => {
@@ -25,9 +18,9 @@ export const makeSeekSignal = (emitter: MediaParserEmitter): SeekSignal => {
return seek;
},
// In the meanwhile a new seek could have been queued
- clearSeekIfStillSame(previousSeek: Seek) {
+ clearSeekIfStillSame(previousSeek: number) {
if (seek === previousSeek) {
- seek = undefined;
+ seek = null;
return {hasChanged: false};
}
diff --git a/packages/media-parser/src/internal-parse-media.ts b/packages/media-parser/src/internal-parse-media.ts
index 4c2a584f14..a364ebb9d9 100644
--- a/packages/media-parser/src/internal-parse-media.ts
+++ b/packages/media-parser/src/internal-parse-media.ts
@@ -173,7 +173,7 @@ export const internalParseMedia: InternalParseMedia = async function <
throw state.errored;
}
- if (state.controller._internals.seekSignal.getSeek()) {
+ if (state.controller._internals.seekSignal.getSeek() !== null) {
throw new Error('Should not finish while a seek is pending');
}
diff --git a/packages/media-parser/src/parse-loop.ts b/packages/media-parser/src/parse-loop.ts
index d0506f28c1..1017f706c0 100644
--- a/packages/media-parser/src/parse-loop.ts
+++ b/packages/media-parser/src/parse-loop.ts
@@ -177,13 +177,13 @@ export const parseLoop = async ({
state.samplesObserved.setLastSampleObserved();
// After the last sample, you might queue a last seek again.
- if (state.controller._internals.seekSignal.getSeek()) {
+ if (state.controller._internals.seekSignal.getSeek() !== null) {
Log.verbose(
state.logLevel,
'Reached end of samples, but there is a pending seek. Trying to seek...',
);
await workOnSeekRequest(getWorkOnSeekRequestOptions(state));
- if (state.controller._internals.seekSignal.getSeek()) {
+ if (state.controller._internals.seekSignal.getSeek() !== null) {
throw new Error(
'Reached the end of the file even though a seek was requested. This is likely a bug in the parser. You can report this at https://remotion.dev/report and we will fix it as soon as possible.',
);
diff --git a/packages/media-parser/src/parse-media-on-worker-entry.ts b/packages/media-parser/src/parse-media-on-worker-entry.ts
index 4f947e1215..f2a18f4a27 100644
--- a/packages/media-parser/src/parse-media-on-worker-entry.ts
+++ b/packages/media-parser/src/parse-media-on-worker-entry.ts
@@ -1,4 +1,3 @@
-import type {Seek} from './controller/seek-signal';
import type {AllOptions, Options, ParseMediaFields} from './fields';
import type {ParseMediaOptions, ParseMediaResult} from './options';
import type {SeekingHints} from './seeking-hints';
@@ -134,7 +133,7 @@ export const parseMediaOnWorkerImplementation = async <
post(worker, {type: 'request-pause'});
};
- const onSeek = ({detail: {seek}}: {detail: {seek: Seek}}) => {
+ const onSeek = ({detail: {seek}}: {detail: {seek: number}}) => {
post(worker, {type: 'request-seek', payload: seek});
controller?._internals.seekSignal.clearSeekIfStillSame(seek);
};
diff --git a/packages/media-parser/src/state/sample-callbacks.ts b/packages/media-parser/src/state/sample-callbacks.ts
index a2898cdb86..40656ac8b9 100644
--- a/packages/media-parser/src/state/sample-callbacks.ts
+++ b/packages/media-parser/src/state/sample-callbacks.ts
@@ -88,7 +88,7 @@ export const callbacksState = ({
if (audioSample.data.length > 0) {
// If we emit samples with data length 0, Chrome will fail
if (callback) {
- if (seekSignal.getSeek()) {
+ if (seekSignal.getSeek() !== null) {
Log.trace(
logLevel,
'Not emitting sample because seek is processing',
@@ -115,7 +115,7 @@ export const callbacksState = ({
const callback = videoSampleCallbacks[trackId];
// If we emit samples with data 0, Chrome will fail
if (callback) {
- if (seekSignal.getSeek()) {
+ if (seekSignal.getSeek() !== null) {
Log.trace(
logLevel,
'Not emitting sample because seek is processing',
diff --git a/packages/media-parser/src/test/seeking/basic-mp4.test.ts b/packages/media-parser/src/test/seeking/basic-mp4.test.ts
index c149a8d43d..a22aa18417 100644
--- a/packages/media-parser/src/test/seeking/basic-mp4.test.ts
+++ b/packages/media-parser/src/test/seeking/basic-mp4.test.ts
@@ -17,10 +17,7 @@ test('should process a basic seek request', async () => {
controller,
reader: nodeReader,
onVideoTrack: () => {
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10.6,
- });
+ controller.seek(10.6);
return (s) => {
firstSample = s;
controller.abort();
@@ -39,10 +36,7 @@ test('should process a basic seek request', async () => {
test('should not be able to seek into a negative time', async () => {
const controller = mediaParserController();
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: -1,
- });
+ controller.seek(-1);
try {
await parseMedia({
@@ -57,8 +51,6 @@ test('should not be able to seek into a negative time', async () => {
throw new Error('should not complete');
} catch (err) {
- expect((err as Error).message).toBe(
- 'Cannot seek to a negative time: {"type":"keyframe-before-time","timeInSeconds":-1}',
- );
+ expect((err as Error).message).toBe('Cannot seek to a negative time: -1');
}
});
diff --git a/packages/media-parser/src/test/seeking/no-samples-twice.test.ts b/packages/media-parser/src/test/seeking/no-samples-twice.test.ts
index de830c1e93..11681d8d83 100644
--- a/packages/media-parser/src/test/seeking/no-samples-twice.test.ts
+++ b/packages/media-parser/src/test/seeking/no-samples-twice.test.ts
@@ -20,10 +20,7 @@ test('should not count samples twice', async () => {
return () => {
samples++;
if (samples === 100) {
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 0,
- });
+ controller.seek(0);
}
};
},
diff --git a/packages/media-parser/src/test/seeking/seek-aac.test.ts b/packages/media-parser/src/test/seeking/seek-aac.test.ts
index 88662c7925..252d78a1b2 100644
--- a/packages/media-parser/src/test/seeking/seek-aac.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-aac.test.ts
@@ -6,10 +6,7 @@ import {parseMedia} from '../../parse-media';
test('seek aac', async () => {
const controller = mediaParserController();
- controller.seek({
- timeInSeconds: 3,
- type: 'keyframe-before-time',
- });
+ controller.seek(3);
await parseMedia({
src: exampleVideos.aac,
@@ -22,26 +19,17 @@ test('seek aac', async () => {
samples++;
if (samples === 1) {
expect(s.timestamp / s.timescale).toBe(2.995374149659864);
- controller.seek({
- timeInSeconds: 10,
- type: 'keyframe-before-time',
- });
+ controller.seek(10);
}
if (samples === 2) {
expect(s.timestamp / s.timescale).toBe(9.984580498866213);
- controller.seek({
- timeInSeconds: 5,
- type: 'keyframe-before-time',
- });
+ controller.seek(5);
}
if (samples === 3) {
expect(s.timestamp / s.timescale).toBe(4.992290249433107);
- controller.seek({
- timeInSeconds: 1000,
- type: 'keyframe-before-time',
- });
+ controller.seek(1000);
}
if (samples === 4) {
diff --git a/packages/media-parser/src/test/seeking/seek-avi-without-idx.test.ts b/packages/media-parser/src/test/seeking/seek-avi-without-idx.test.ts
index 905e6ca6fc..1e76f0e371 100644
--- a/packages/media-parser/src/test/seeking/seek-avi-without-idx.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-avi-without-idx.test.ts
@@ -35,10 +35,7 @@ const expectedSeekingHints: SeekingHints = {
test('seek avi', async () => {
const controller = mediaParserController();
- controller.seek({
- timeInSeconds: 10,
- type: 'keyframe-before-time',
- });
+ controller.seek(10);
let samples = 0;
@@ -54,10 +51,7 @@ test('seek avi', async () => {
if (samples === 1) {
expect(sample.timestamp / sample.timescale).toBe(0);
expect(sample.type).toBe('key');
- controller.seek({
- timeInSeconds: 20,
- type: 'keyframe-before-time',
- });
+ controller.seek(20);
}
if (samples === 2) {
@@ -69,10 +63,7 @@ test('seek avi', async () => {
expect(sample.timestamp / sample.timescale).toBe(9.933333333333334);
expect(sample.type).toBe('delta');
- controller.seek({
- timeInSeconds: 10,
- type: 'keyframe-before-time',
- });
+ controller.seek(10);
}
if (samples === 301) {
@@ -126,10 +117,7 @@ test('should be able to use seeking hints', async () => {
const controller = mediaParserController();
let samples = 0;
- controller.seek({
- timeInSeconds: 10,
- type: 'keyframe-before-time',
- });
+ controller.seek(10);
await parseMedia({
src: exampleVideos.aviWithoutIdx,
@@ -143,10 +131,7 @@ test('should be able to use seeking hints', async () => {
if (samples === 1) {
expect(sample.timestamp / sample.timescale).toBe(8.333333333333334);
expect(sample.type).toBe('key');
- controller.seek({
- timeInSeconds: 20,
- type: 'keyframe-before-time',
- });
+ controller.seek(20);
}
};
},
diff --git a/packages/media-parser/src/test/seeking/seek-avi.test.ts b/packages/media-parser/src/test/seeking/seek-avi.test.ts
index 32315953c5..83505fd412 100644
--- a/packages/media-parser/src/test/seeking/seek-avi.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-avi.test.ts
@@ -8,10 +8,7 @@ import {parseMedia} from '../../parse-media';
test('seek avi', async () => {
const controller = mediaParserController();
- controller.seek({
- timeInSeconds: 10,
- type: 'keyframe-before-time',
- });
+ controller.seek(10);
let samples = 0;
@@ -27,10 +24,7 @@ test('seek avi', async () => {
if (samples === 1) {
expect(sample.timestamp / sample.timescale).toBe(8.333333333333334);
expect(sample.type).toBe('key');
- controller.seek({
- timeInSeconds: 20,
- type: 'keyframe-before-time',
- });
+ controller.seek(20);
}
if (samples === 2) {
@@ -43,20 +37,14 @@ test('seek avi', async () => {
if (samples === 3) {
expect(sample.timestamp / sample.timescale).toBe(16.7);
expect(sample.type).toBe('delta');
- controller.seek({
- timeInSeconds: 0,
- type: 'keyframe-before-time',
- });
+ controller.seek(0);
}
if (samples === 4) {
expect(sample.timestamp / sample.timescale).toBe(0);
expect(sample.type).toBe('key');
- controller.seek({
- timeInSeconds: 40,
- type: 'keyframe-before-time',
- });
+ controller.seek(40);
}
if (samples === 5) {
@@ -68,10 +56,7 @@ test('seek avi', async () => {
expect(sample.timestamp / sample.timescale).toBe(30);
expect(sample.type).toBe('delta');
- controller.seek({
- timeInSeconds: 100,
- type: 'keyframe-before-time',
- });
+ controller.seek(100);
}
if (samples === 156) {
diff --git a/packages/media-parser/src/test/seeking/seek-backwards.test.ts b/packages/media-parser/src/test/seeking/seek-backwards.test.ts
index fd27a5a525..0eca95e5c2 100644
--- a/packages/media-parser/src/test/seeking/seek-backwards.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-backwards.test.ts
@@ -11,10 +11,7 @@ test('should be able to seek forward and then backwards', async () => {
let samples = 0;
try {
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10.6,
- });
+ controller.seek(10.6);
await parseMedia({
src: exampleVideos.bigBuckBunny,
@@ -28,10 +25,7 @@ test('should be able to seek forward and then backwards', async () => {
expect((sample?.timestamp ?? 0) / (sample?.timescale ?? 1)).toBe(
10.5,
);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 0,
- });
+ controller.seek(0);
}
if (samples === 2) {
diff --git a/packages/media-parser/src/test/seeking/seek-before.test.ts b/packages/media-parser/src/test/seeking/seek-before.test.ts
index 43d4b6e6f5..8b01eec5bd 100644
--- a/packages/media-parser/src/test/seeking/seek-before.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-before.test.ts
@@ -12,10 +12,7 @@ test('should be able to set the start seek', async () => {
let firstSample: MediaParserVideoSample | undefined;
try {
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10.6,
- });
+ controller.seek(10.6);
await parseMedia({
src: exampleVideos.bigBuckBunny,
diff --git a/packages/media-parser/src/test/seeking/seek-flac.test.ts b/packages/media-parser/src/test/seeking/seek-flac.test.ts
index c2a683e2ed..7caabbf394 100644
--- a/packages/media-parser/src/test/seeking/seek-flac.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-flac.test.ts
@@ -6,10 +6,7 @@ import {parseMedia} from '../../parse-media';
test('seek flac', async () => {
const controller = mediaParserController();
- controller.seek({
- timeInSeconds: 3,
- type: 'keyframe-before-time',
- });
+ controller.seek(3);
let samples = 0;
await parseMedia({
@@ -20,26 +17,17 @@ test('seek flac', async () => {
samples++;
if (samples === 1) {
expect(sample.timestamp / sample.timescale).toBe(2.972154195011338);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5,
- });
+ controller.seek(5);
}
if (samples === 2) {
expect(sample.timestamp / sample.timescale).toBe(4.922630385487528);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 2,
- });
+ controller.seek(2);
}
if (samples === 3) {
expect(sample.timestamp / sample.timescale).toBe(1.9504761904761905);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 90,
- });
+ controller.seek(90);
}
if (samples === 4) {
diff --git a/packages/media-parser/src/test/seeking/seek-m3u-mp4.test.ts b/packages/media-parser/src/test/seeking/seek-m3u-mp4.test.ts
index 0a25fc48b4..59f0c7679b 100644
--- a/packages/media-parser/src/test/seeking/seek-m3u-mp4.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-m3u-mp4.test.ts
@@ -12,10 +12,7 @@ import type {
test('seek m3u, only video', async () => {
const controller = mediaParserController();
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 4.5,
- });
+ controller.seek(4.5);
let samples = 0;
try {
@@ -28,26 +25,17 @@ test('seek m3u, only video', async () => {
return (sample) => {
if (samples === 0) {
expect(sample.dts / sample.timescale).toBe(4.021666666666667);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 2,
- });
+ controller.seek(2);
}
if (samples === 1) {
expect(sample.dts / sample.timescale).toBe(1.18);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 2.05,
- });
+ controller.seek(2.05);
}
if (samples === 2) {
expect(sample.dts / sample.timescale).toBe(2.0283333333333333);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 2.0,
- });
+ controller.seek(2);
}
if (samples === 3) {
@@ -72,10 +60,7 @@ test('seek m3u, only video', async () => {
test('seek m3u, video and audio', async () => {
const controller = mediaParserController();
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5.5,
- });
+ controller.seek(5.5);
let samples = 0;
@@ -91,19 +76,13 @@ test('seek m3u, video and audio', async () => {
if (samples === 1) {
expect(mediaType).toBe('audio');
expect(sample.dts / sample.timescale).toBe(5.482666666666666);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 100,
- });
+ controller.seek(100);
}
if (samples === 2) {
expect(mediaType).toBe('video');
expect(sample.dts / sample.timescale).toBe(9.148333333333333);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 1,
- });
+ controller.seek(1);
}
if (samples === 3) {
diff --git a/packages/media-parser/src/test/seeking/seek-m3u-ts.test.ts b/packages/media-parser/src/test/seeking/seek-m3u-ts.test.ts
index e7cd9659d3..90f37033e0 100644
--- a/packages/media-parser/src/test/seeking/seek-m3u-ts.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-m3u-ts.test.ts
@@ -6,10 +6,7 @@ import {parseMedia} from '../../parse-media';
test('seek m3u, ts segments', async () => {
const controller = mediaParserController();
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10.2,
- });
+ controller.seek(10.2);
let samples = 0;
@@ -28,37 +25,25 @@ test('seek m3u, ts segments', async () => {
if (samples === 0) {
expect(sample.dts / sample.timescale).toBe(10);
expect(sample.type).toBe('key');
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 39,
- });
+ controller.seek(39);
}
if (samples === 1) {
expect(sample.dts / sample.timescale).toBe(30);
expect(sample.type).toBe('key');
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5,
- });
+ controller.seek(5);
}
if (samples === 2) {
expect(sample.dts / sample.timescale).toBe(10);
expect(sample.type).toBe('key');
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 25,
- });
+ controller.seek(25);
}
if (samples === 3) {
expect(sample.dts / sample.timescale).toBe(20);
expect(sample.type).toBe('key');
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10000,
- });
+ controller.seek(10000);
}
if (samples === 4) {
diff --git a/packages/media-parser/src/test/seeking/seek-moof.test.ts b/packages/media-parser/src/test/seeking/seek-moof.test.ts
index 0117fa87f7..06dd7c64c5 100644
--- a/packages/media-parser/src/test/seeking/seek-moof.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-moof.test.ts
@@ -10,10 +10,7 @@ const controller1 = mediaParserController();
test('seek moof, should make use of the mfra atom if available', async () => {
const video = await getRemoteExampleVideo('fragmentedMoofTrickyDuration');
- controller1.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 20,
- });
+ controller1.seek(20);
let samples = 0;
try {
@@ -36,17 +33,11 @@ test('seek moof, should make use of the mfra atom if available', async () => {
if (sample.cts === 19533333.333333336) {
expect(sample.type).toBe('key');
- controller1.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 0,
- });
+ controller1.seek(0);
}
if (sample.dts === 0) {
- controller1.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10,
- });
+ controller1.seek(10);
}
if (sample.dts === 9700000) {
@@ -115,10 +106,7 @@ test('seek moof, should make use of the mfra atom if available', async () => {
test('should use seeking hints from previous parse', async () => {
const controller2 = mediaParserController();
- controller2.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 20,
- });
+ controller2.seek(20);
const video = await getRemoteExampleVideo('fragmentedMoofTrickyDuration');
const hints = await controller1.getSeekingHints();
diff --git a/packages/media-parser/src/test/seeking/seek-mp3-cbr.test.ts b/packages/media-parser/src/test/seeking/seek-mp3-cbr.test.ts
index 7c0052295b..b0a69d481f 100644
--- a/packages/media-parser/src/test/seeking/seek-mp3-cbr.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-mp3-cbr.test.ts
@@ -6,10 +6,7 @@ import {parseMedia} from '../../parse-media';
test('seek mp3 cbr', async () => {
const controller = mediaParserController();
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 60,
- });
+ controller.seek(60);
let samples = 0;
@@ -23,10 +20,7 @@ test('seek mp3 cbr', async () => {
samples++;
if (samples === 1) {
expect(sample.timestamp / sample.timescale).toBe(59.95102);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10000000,
- });
+ controller.seek(10000000);
}
if (samples === 2) {
diff --git a/packages/media-parser/src/test/seeking/seek-mp3.test.ts b/packages/media-parser/src/test/seeking/seek-mp3.test.ts
index 0466133172..39c808f31a 100644
--- a/packages/media-parser/src/test/seeking/seek-mp3.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-mp3.test.ts
@@ -6,10 +6,7 @@ import {parseMedia} from '../../parse-media';
test('seek mp3', async () => {
const controller = mediaParserController();
- controller.seek({
- timeInSeconds: 10,
- type: 'keyframe-before-time',
- });
+ controller.seek(10);
let samples = 0;
@@ -24,36 +21,24 @@ test('seek mp3', async () => {
if (samples === 1) {
expect(sample.timestamp / sample.timescale).toBe(9.978776);
expect(sample.type).toBe('key');
- controller.seek({
- timeInSeconds: 25,
- type: 'keyframe-before-time',
- });
+ controller.seek(25);
}
if (samples === 2) {
expect(sample.timestamp / sample.timescale).toBe(24.999184);
- controller.seek({
- timeInSeconds: 30,
- type: 'keyframe-before-time',
- });
+ controller.seek(30);
}
if (samples === 3) {
expect(sample.timestamp / sample.timescale).toBe(29.988571);
expect(sample.type).toBe('key');
- controller.seek({
- timeInSeconds: 0.0,
- type: 'keyframe-before-time',
- });
+ controller.seek(0);
}
if (samples === 4) {
expect(sample.timestamp / sample.timescale).toBe(0);
expect(sample.type).toBe('key');
- controller.seek({
- timeInSeconds: 100,
- type: 'keyframe-before-time',
- });
+ controller.seek(100);
}
if (samples === 5) {
diff --git a/packages/media-parser/src/test/seeking/seek-on-worker.test.ts b/packages/media-parser/src/test/seeking/seek-on-worker.test.ts
index 3a96968387..4b3df3abb9 100644
--- a/packages/media-parser/src/test/seeking/seek-on-worker.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-on-worker.test.ts
@@ -3,18 +3,15 @@ import {expect, test} from 'bun:test';
import {mediaParserController} from '../../controller/media-parser-controller';
import {hasBeenAborted} from '../../errors';
import {parseMediaOnServerWorker} from '../../server-worker.module';
-import type {AudioOrVideoSample} from '../../webcodec-sample-types';
+import type {MediaParserVideoSample} from '../../webcodec-sample-types';
test('seek should also work on worker', async () => {
const controller = mediaParserController();
- let firstSample: AudioOrVideoSample | undefined;
+ let firstSample: MediaParserVideoSample | undefined;
try {
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10.6,
- });
+ controller.seek(10.6);
await parseMediaOnServerWorker({
src: exampleVideos.bigBuckBunny,
@@ -45,10 +42,7 @@ test('should be able to seek forward and then backwards', async () => {
let samples = 0;
try {
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10.6,
- });
+ controller.seek(10.6);
await parseMediaOnServerWorker({
src: exampleVideos.bigBuckBunny,
@@ -61,10 +55,7 @@ test('should be able to seek forward and then backwards', async () => {
expect((sample?.timestamp ?? 0) / (sample?.timescale ?? 1)).toBe(
10.5,
);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 0,
- });
+ controller.seek(0);
}
if (samples === 2) {
diff --git a/packages/media-parser/src/test/seeking/seek-transportstream.test.ts b/packages/media-parser/src/test/seeking/seek-transportstream.test.ts
index 2b3ff37eec..d2956f7fe2 100644
--- a/packages/media-parser/src/test/seeking/seek-transportstream.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-transportstream.test.ts
@@ -17,10 +17,7 @@ test('should seek transport stream', async () => {
return (sample) => {
samples++;
if (samples === 20) {
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10,
- });
+ controller.seek(10);
expect(sample.dts).toBe(316666.6666666666);
}
diff --git a/packages/media-parser/src/test/seeking/seek-xing.test.ts b/packages/media-parser/src/test/seeking/seek-xing.test.ts
index 3457214174..46cd5fb94b 100644
--- a/packages/media-parser/src/test/seeking/seek-xing.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-xing.test.ts
@@ -7,10 +7,7 @@ import {parseMedia} from '../../parse-media';
test('seek-xing', async () => {
const controller = mediaParserController();
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10,
- });
+ controller.seek(10);
await parseMedia({
src: exampleVideos.mp3vbr,
@@ -33,18 +30,12 @@ test('seek-xing', async () => {
if (samples === 2) {
expect(sample.timestamp / sample.timescale).toBe(10.005549);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 20,
- });
+ controller.seek(20);
}
if (samples === 3) {
expect(sample.timestamp / sample.timescale).toBe(19.99599);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 30,
- });
+ controller.seek(30);
}
if (samples === 4) {
diff --git a/packages/media-parser/src/test/seeking/seeking-wav.test.ts b/packages/media-parser/src/test/seeking/seeking-wav.test.ts
index dd92639b36..047c6098ee 100644
--- a/packages/media-parser/src/test/seeking/seeking-wav.test.ts
+++ b/packages/media-parser/src/test/seeking/seeking-wav.test.ts
@@ -7,10 +7,7 @@ import {parseMedia} from '../../parse-media';
test('seeking in a wav', async () => {
const controller = mediaParserController();
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10,
- });
+ controller.seek(10);
let samples = 0;
@@ -27,27 +24,18 @@ test('seeking in a wav', async () => {
samples++;
if (samples === 1) {
expect(sample.timestamp).toBe(10000000);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 0,
- });
+ controller.seek(0);
}
if (samples === 2) {
expect(sample.timestamp).toBe(0);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 28,
- });
+ controller.seek(28);
}
if (samples === 3) {
expect(sample.timestamp).toBe(28000000);
// out of bounds
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 40,
- });
+ controller.seek(40);
}
if (samples === 4) {
diff --git a/packages/media-parser/src/test/seeking/slow-fields-forward-seek.test.ts b/packages/media-parser/src/test/seeking/slow-fields-forward-seek.test.ts
index 8ff81568eb..4bc8837b96 100644
--- a/packages/media-parser/src/test/seeking/slow-fields-forward-seek.test.ts
+++ b/packages/media-parser/src/test/seeking/slow-fields-forward-seek.test.ts
@@ -7,10 +7,7 @@ import {parseMedia} from '../../parse-media';
test('should not calculate slow fps, slow duration, slow keyframes etc. if there was seeking inbetween', async () => {
const controller = mediaParserController();
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 30,
- });
+ controller.seek(30);
try {
await parseMedia({
diff --git a/packages/media-parser/src/test/seeking/throw-on-infinite-loop.test.ts b/packages/media-parser/src/test/seeking/throw-on-infinite-loop.test.ts
index 0f4f5d59c6..a625b5851e 100644
--- a/packages/media-parser/src/test/seeking/throw-on-infinite-loop.test.ts
+++ b/packages/media-parser/src/test/seeking/throw-on-infinite-loop.test.ts
@@ -19,10 +19,7 @@ test('handle seek infinite loop better', async () => {
},
onVideoTrack: () => {
return () => {
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10,
- });
+ controller.seek(10);
};
},
acknowledgeRemotionLicense: true,
diff --git a/packages/media-parser/src/test/seeking/webm-cues.test.ts b/packages/media-parser/src/test/seeking/webm-cues.test.ts
index c5df36fa94..f6b5c28eff 100644
--- a/packages/media-parser/src/test/seeking/webm-cues.test.ts
+++ b/packages/media-parser/src/test/seeking/webm-cues.test.ts
@@ -42,10 +42,7 @@ test('get webm cues', async () => {
const controller1 = mediaParserController();
test('should use them for seeking', async () => {
- controller1.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10,
- });
+ controller1.seek(10);
let samples = 0;
@@ -61,34 +58,22 @@ test('should use them for seeking', async () => {
const timeInSeconds = s.timestamp / s.timescale;
if (samples === 1) {
expect(timeInSeconds).toBe(9.603);
- controller1.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5,
- });
+ controller1.seek(5);
}
if (samples === 2) {
expect(timeInSeconds).toBe(4.803);
- controller1.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 0,
- });
+ controller1.seek(0);
}
if (samples === 3) {
expect(timeInSeconds).toBe(0.003);
- controller1.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5,
- });
+ controller1.seek(5);
}
if (samples === 4) {
expect(timeInSeconds).toBe(4.803);
- controller1.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5,
- });
+ controller1.seek(5);
controller1.abort();
}
};
@@ -129,10 +114,7 @@ test('should be able to use precomputed seeking hints', async () => {
let samples = 0;
const controller2 = mediaParserController();
- controller2.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10,
- });
+ controller2.seek(10);
try {
await parseMedia({
@@ -147,34 +129,22 @@ test('should be able to use precomputed seeking hints', async () => {
const timeInSeconds = s.timestamp / s.timescale;
if (samples === 1) {
expect(timeInSeconds).toBe(9.603);
- controller2.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5,
- });
+ controller2.seek(5);
}
if (samples === 2) {
expect(timeInSeconds).toBe(4.803);
- controller2.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 0,
- });
+ controller2.seek(0);
}
if (samples === 3) {
expect(timeInSeconds).toBe(0.003);
- controller2.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5,
- });
+ controller2.seek(5);
}
if (samples === 4) {
expect(timeInSeconds).toBe(4.803);
- controller2.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5,
- });
+ controller2.seek(5);
controller2.abort();
}
};
@@ -213,10 +183,7 @@ test('should be able to use precomputed seeking hints', async () => {
test('should work if there are no cues', async () => {
const controller = mediaParserController();
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 10,
- });
+ controller.seek(10);
let samples = 0;
@@ -229,19 +196,13 @@ test('should work if there are no cues', async () => {
onVideoTrack: () => {
return (s) => {
if (samples === 213) {
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 5,
- });
+ controller.seek(5);
expect(s.timestamp / s.timescale).toBe(7.227);
}
if (samples === 214) {
expect(s.timestamp / s.timescale).toBe(3.408);
- controller.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 0,
- });
+ controller.seek(0);
}
if (samples === 215) {
diff --git a/packages/media-parser/src/test/ts-multiple-keyframes.test.ts b/packages/media-parser/src/test/ts-multiple-keyframes.test.ts
index d85fbe92be..38bef22cd7 100644
--- a/packages/media-parser/src/test/ts-multiple-keyframes.test.ts
+++ b/packages/media-parser/src/test/ts-multiple-keyframes.test.ts
@@ -23,19 +23,13 @@ test('Should be able to seek back based on already observed keyframes', async ()
samples++;
const timeInSeconds = sample.timestamp / sample.timescale;
if (timeInSeconds === 5.8058) {
- controller1.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 3,
- });
+ controller1.seek(3);
}
if (samples === 176) {
expect(timeInSeconds).toBe(2.9029);
expect(sample.type).toBe('key');
- controller1.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 2,
- });
+ controller1.seek(2);
}
if (samples === 177) {
@@ -80,10 +74,7 @@ test('should be able to use seeking hints from previous parse', async () => {
expect(hints?.type).toEqual('transport-stream-seeking-hints');
const controller2 = mediaParserController();
- controller2.seek({
- type: 'keyframe-before-time',
- timeInSeconds: 3,
- });
+ controller2.seek(3);
try {
await parseMedia({
diff --git a/packages/media-parser/src/work-on-seek-request.ts b/packages/media-parser/src/work-on-seek-request.ts
index cbbd2bb97a..52d770eb6b 100644
--- a/packages/media-parser/src/work-on-seek-request.ts
+++ b/packages/media-parser/src/work-on-seek-request.ts
@@ -1,5 +1,4 @@
import type {MediaParserController} from './controller/media-parser-controller';
-import type {Seek} from './controller/seek-signal';
import type {PrefetchCache} from './fetch';
import type {AllOptions, ParseMediaFields} from './fields';
import {getSeekingByte} from './get-seeking-byte';
@@ -51,7 +50,7 @@ const turnSeekIntoByte = async ({
aacState,
m3uState,
}: {
- seek: Seek;
+ seek: number;
mediaSectionState: MediaSectionState;
logLevel: MediaParserLogLevel;
iterator: BufferIterator;
@@ -79,58 +78,50 @@ const turnSeekIntoByte = async ({
};
}
- if (seek.type === 'keyframe-before-time') {
- if (seek.timeInSeconds < 0) {
- throw new Error(
- `Cannot seek to a negative time: ${JSON.stringify(seek)}`,
- );
- }
-
- const seekingHints = getSeekingHints({
- riffState,
- samplesObserved,
- structureState,
- mediaSectionState,
- isoState,
- transportStream,
- tracksState,
- keyframesState: keyframes,
- webmState,
- flacState,
- mp3State,
- contentLength,
- aacState,
- m3uPlaylistContext,
- });
-
- if (!seekingHints) {
- Log.trace(logLevel, 'No seeking info, cannot seek yet');
- return {
- type: 'valid-but-must-wait',
- };
- }
+ if (seek < 0) {
+ throw new Error(`Cannot seek to a negative time: ${JSON.stringify(seek)}`);
+ }
- const seekingByte = await getSeekingByte({
- info: seekingHints,
- time: seek.timeInSeconds,
- logLevel,
- currentPosition: iterator.counter.getOffset(),
- isoState,
- transportStream,
- webmState,
- mediaSection: mediaSectionState,
- m3uPlaylistContext,
- structure: structureState,
- riffState,
- m3uState,
- });
+ const seekingHints = getSeekingHints({
+ riffState,
+ samplesObserved,
+ structureState,
+ mediaSectionState,
+ isoState,
+ transportStream,
+ tracksState,
+ keyframesState: keyframes,
+ webmState,
+ flacState,
+ mp3State,
+ contentLength,
+ aacState,
+ m3uPlaylistContext,
+ });
- return seekingByte;
+ if (!seekingHints) {
+ Log.trace(logLevel, 'No seeking info, cannot seek yet');
+ return {
+ type: 'valid-but-must-wait',
+ };
}
- throw new Error(
- `Cannot process seek request for ${seek}: ${JSON.stringify(seek)}`,
- );
+ const seekingByte = await getSeekingByte({
+ info: seekingHints,
+ time: seek,
+ logLevel,
+ currentPosition: iterator.counter.getOffset(),
+ isoState,
+ transportStream,
+ webmState,
+ mediaSection: mediaSectionState,
+ m3uPlaylistContext,
+ structure: structureState,
+ riffState,
+ m3uState,
+ });
+
+ return seekingByte;
};
export type WorkOnSeekRequestOptions = {
@@ -225,7 +216,7 @@ export const workOnSeekRequest = async (options: WorkOnSeekRequestOptions) => {
m3uState,
} = options;
const seek = controller._internals.seekSignal.getSeek();
- if (!seek) {
+ if (seek === null) {
return;
}
diff --git a/packages/media-parser/src/worker/worker-types.ts b/packages/media-parser/src/worker/worker-types.ts
index 9012065986..97102e8f4e 100644
--- a/packages/media-parser/src/worker/worker-types.ts
+++ b/packages/media-parser/src/worker/worker-types.ts
@@ -6,7 +6,6 @@ import type {
SelectM3uAssociatedPlaylistsFnOptions,
SelectM3uStreamFnOptions,
} from '../containers/m3u/select-stream';
-import type {Seek} from '../controller/seek-signal';
import type {ImageType} from '../errors';
import type {Options, ParseMediaFields} from '../fields';
import type {MediaParserDimensions} from '../get-dimensions';
@@ -83,7 +82,7 @@ type RequestPause = {
type RequestSeek = {
type: 'request-seek';
- payload: Seek;
+ payload: number;
};
type RequestResume = {