Skip to content

🎥 Make videos programmatically with React

force seek to be inside video section

Jonny Burger828533120eb7
diff --git a/packages/media-parser/src/containers/flac/parse-flac.ts b/packages/media-parser/src/containers/flac/parse-flac.ts
index 167e25dc76..2959b31e40 100644
--- a/packages/media-parser/src/containers/flac/parse-flac.ts
+++ b/packages/media-parser/src/containers/flac/parse-flac.ts
@@ -21,7 +21,8 @@ export const parseFlac = ({
iterator: BufferIterator;
state: ParserState;
}): Promise<ParseResult> => {
- const videoSectionState = state.videoSection.isInVideoSectionState(iterator);
+ const videoSectionState =
+ state.videoSection.isCurrentByteInVideoSection(iterator);
if (videoSectionState === 'in-section') {
if (maySkipVideoData({state})) {
return Promise.resolve(makeSkip(state.contentLength));
diff --git a/packages/media-parser/src/containers/iso-base-media/parse-boxes.ts b/packages/media-parser/src/containers/iso-base-media/parse-boxes.ts
index b1c8a94bc8..04aa80f34d 100644
--- a/packages/media-parser/src/containers/iso-base-media/parse-boxes.ts
+++ b/packages/media-parser/src/containers/iso-base-media/parse-boxes.ts
@@ -6,7 +6,7 @@ import {processBox} from './process-box';
export const parseIsoBaseMedia = async (
state: ParserState,
): Promise<ParseResult> => {
- const videoSectionState = state.videoSection.isInVideoSectionState(
+ const videoSectionState = state.videoSection.isCurrentByteInVideoSection(
state.iterator,
);
diff --git a/packages/media-parser/src/containers/riff/parse-riff-body.ts b/packages/media-parser/src/containers/riff/parse-riff-body.ts
index 802500b509..4dd0953bbe 100644
--- a/packages/media-parser/src/containers/riff/parse-riff-body.ts
+++ b/packages/media-parser/src/containers/riff/parse-riff-body.ts
@@ -9,7 +9,8 @@ export const parseRiffBody = async (
state: ParserState,
): Promise<ParseResult> => {
if (
- state.videoSection.isInVideoSectionState(state.iterator) === 'in-section'
+ state.videoSection.isCurrentByteInVideoSection(state.iterator) ===
+ 'in-section'
) {
if (
maySkipVideoData({
diff --git a/packages/media-parser/src/containers/wav/parse-wav.ts b/packages/media-parser/src/containers/wav/parse-wav.ts
index ee07e6731f..343f44caa4 100644
--- a/packages/media-parser/src/containers/wav/parse-wav.ts
+++ b/packages/media-parser/src/containers/wav/parse-wav.ts
@@ -10,7 +10,8 @@ import {parseVideoSection} from './parse-video-section';
export const parseWav = (state: ParserState): Promise<ParseResult> => {
const {iterator} = state;
- const insideVideoSection = state.videoSection.isInVideoSectionState(iterator);
+ const insideVideoSection =
+ state.videoSection.isCurrentByteInVideoSection(iterator);
if (insideVideoSection === 'in-section') {
return parseVideoSection({state});
}
diff --git a/packages/media-parser/src/perform-seek.ts b/packages/media-parser/src/perform-seek.ts
index 0ee5dd293f..4f8a43ae6a 100644
--- a/packages/media-parser/src/perform-seek.ts
+++ b/packages/media-parser/src/perform-seek.ts
@@ -12,7 +12,21 @@ export const performSeek = async ({
state: ParserState;
userInitiated: boolean;
}): Promise<void> => {
- const {iterator, logLevel, mode, contentLength, seekInfiniteLoop} = state;
+ const {
+ iterator,
+ logLevel,
+ mode,
+ contentLength,
+ seekInfiniteLoop,
+ videoSection,
+ } = state;
+
+ const byteInVideoSection = videoSection.isByteInVideoSection(seekTo);
+ if (byteInVideoSection !== 'in-section') {
+ throw new Error(
+ `Cannot seek to a byte that is not in the video section: ${seekTo}`,
+ );
+ }
seekInfiniteLoop.registerSeek(seekTo);
diff --git a/packages/media-parser/src/seek-signal.ts b/packages/media-parser/src/seek-signal.ts
index 160c57fe04..1d3279039c 100644
--- a/packages/media-parser/src/seek-signal.ts
+++ b/packages/media-parser/src/seek-signal.ts
@@ -15,12 +15,7 @@ type SeekToByte = {
byte: number;
};
-type ForceSeekToByte = {
- type: 'force-seek-to-byte';
- byte: number;
-};
-
-export type Seek = SeekToTime | SeekToByte | ForceSeekToByte;
+export type Seek = SeekToTime | SeekToByte;
export const makeSeekSignal = (emitter: MediaParserEmitter): SeekSignal => {
let seek: Seek | undefined;
diff --git a/packages/media-parser/src/state/video-section.ts b/packages/media-parser/src/state/video-section.ts
index 525be482b6..ce893b06c6 100644
--- a/packages/media-parser/src/state/video-section.ts
+++ b/packages/media-parser/src/state/video-section.ts
@@ -26,17 +26,16 @@ export const videoSectionState = () => {
return videoSection;
};
- const isInVideoSectionState = (
- iterator: BufferIterator,
+ const isByteInVideoSection = (
+ byte: number,
): 'no-section-defined' | 'in-section' | 'outside-section' => {
if (!videoSection) {
return 'no-section-defined';
}
- const offset = iterator.counter.getOffset();
if (
- offset >= videoSection.start &&
- offset < videoSection.start + videoSection.size
+ byte >= videoSection.start &&
+ byte < videoSection.start + videoSection.size
) {
return 'in-section';
}
@@ -44,9 +43,17 @@ export const videoSectionState = () => {
return 'outside-section';
};
+ const isCurrentByteInVideoSection = (
+ iterator: BufferIterator,
+ ): 'no-section-defined' | 'in-section' | 'outside-section' => {
+ const offset = iterator.counter.getOffset();
+ return isByteInVideoSection(offset);
+ };
+
return {
setVideoSection,
getVideoSection,
- isInVideoSectionState,
+ isCurrentByteInVideoSection,
+ isByteInVideoSection,
};
};
diff --git a/packages/media-parser/src/test/seeking/seek-to-byte.test.ts b/packages/media-parser/src/test/seeking/seek-to-byte.test.ts
new file mode 100644
index 0000000000..b2f2e2b355
--- /dev/null
+++ b/packages/media-parser/src/test/seeking/seek-to-byte.test.ts
@@ -0,0 +1,40 @@
+import {exampleVideos} from '@remotion/example-videos';
+import {expect, test} from 'bun:test';
+import {hasBeenAborted} from '../../errors';
+import {mediaParserController} from '../../media-parser-controller';
+import {nodeReader} from '../../node';
+import {parseMedia} from '../../parse-media';
+import type {AudioOrVideoSample} from '../../webcodec-sample-types';
+
+test('should be able to set the start seek', async () => {
+ const controller = mediaParserController();
+
+ let firstSample: AudioOrVideoSample | undefined;
+
+ try {
+ controller._experimentalSeek({
+ type: 'byte',
+ byte: 5,
+ });
+
+ await parseMedia({
+ src: exampleVideos.bigBuckBunny,
+ controller,
+ reader: nodeReader,
+ onVideoTrack: () => {
+ return (s) => {
+ firstSample = s;
+ controller.abort();
+ };
+ },
+ acknowledgeRemotionLicense: true,
+ });
+ throw new Error('should not complete');
+ } catch (err) {
+ console.log({err});
+ expect(hasBeenAborted(err)).toBe(true);
+ const timeInSeconds =
+ (firstSample?.timestamp ?? 0) / (firstSample?.timescale ?? 1);
+ expect(timeInSeconds).toBe(10.5);
+ }
+});
diff --git a/packages/media-parser/src/work-on-seek-request.ts b/packages/media-parser/src/work-on-seek-request.ts
index 71b854f2f6..470629803f 100644
--- a/packages/media-parser/src/work-on-seek-request.ts
+++ b/packages/media-parser/src/work-on-seek-request.ts
@@ -21,7 +21,16 @@ const turnSeekIntoByte = (seek: Seek, state: ParserState): SeekResolution => {
};
}
- throw new Error(`Cannot process seek request ${JSON.stringify(seek)}`);
+ if (seek.type === 'byte') {
+ return {
+ type: 'do-seek',
+ byte: seek.byte,
+ };
+ }
+
+ throw new Error(
+ `Cannot process seek request for ${seek}: ${JSON.stringify(seek)}`,
+ );
};
export const workOnSeekRequest = async (state: ParserState) => {