🎥 Make videos programmatically with React
vibe seeking
Jonny Burger4e0b6dfdec80
diff --git a/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts b/packages/media-parser/src/containers/iso-base-media/mdat/mdat.tsindex 9d93fcc7fc..c4e0b58d6f 100644--- a/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts+++ b/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts@@ -13,6 +13,10 @@ export const parseMdatSection = async (state: ParserState,): Promise<Skip | null> => {const videoSection = state.videoSection.getVideoSection();+ if (!videoSection) {+ throw new Error('No video section defined');+ }+const endOfMdat = videoSection.size + videoSection.start;// don't need mdat at all, can skipdiff --git a/packages/media-parser/src/containers/riff/parse-movi.ts b/packages/media-parser/src/containers/riff/parse-movi.tsindex 526f1993b0..7d2de6d7c9 100644--- a/packages/media-parser/src/containers/riff/parse-movi.ts+++ b/packages/media-parser/src/containers/riff/parse-movi.ts@@ -144,6 +144,10 @@ export const parseMovi = async ({await handleChunk({state, ckId, ckSize});const videoSection = state.videoSection.getVideoSection();+ if (!videoSection) {+ throw new Error('No video section defined');+ }+const maxOffset = videoSection.start + videoSection.size;// Discard added zeroesdiff --git a/packages/media-parser/src/containers/riff/parse-riff-body.ts b/packages/media-parser/src/containers/riff/parse-riff-body.tsindex 4dd0953bbe..69384ec561 100644--- a/packages/media-parser/src/containers/riff/parse-riff-body.ts+++ b/packages/media-parser/src/containers/riff/parse-riff-body.ts@@ -19,6 +19,9 @@ export const parseRiffBody = async (state.riff.getAvcProfile()) {const videoSection = state.videoSection.getVideoSection();+ if (!videoSection) {+ throw new Error('No video section defined');+ }// only skipping forward in query modereturn Promise.resolve(makeSkip(videoSection.start + videoSection.size));diff --git a/packages/media-parser/src/containers/wav/parse-video-section.ts b/packages/media-parser/src/containers/wav/parse-video-section.tsindex 1292364c74..a192bdcddc 100644--- a/packages/media-parser/src/containers/wav/parse-video-section.ts+++ b/packages/media-parser/src/containers/wav/parse-video-section.ts@@ -13,6 +13,10 @@ export const parseVideoSection = async ({const structure = state.getWavStructure();const videoSection = state.videoSection.getVideoSection();+ if (!videoSection) {+ throw new Error('No video section defined');+ }+const maxOffset = videoSection.start + videoSection.size;const maxRead = maxOffset - iterator.counter.getOffset();const offset = iterator.counter.getOffset();diff --git a/packages/media-parser/src/perform-seek.ts b/packages/media-parser/src/perform-seek.tsindex 4f8a43ae6a..679fba7bdf 100644--- a/packages/media-parser/src/perform-seek.ts+++ b/packages/media-parser/src/perform-seek.ts@@ -22,9 +22,14 @@ export const performSeek = async ({} = state;const byteInVideoSection = videoSection.isByteInVideoSection(seekTo);- if (byteInVideoSection !== 'in-section') {+ if (byteInVideoSection !== 'in-section' && userInitiated) {+ const section = videoSection.getVideoSection();+ const sectionString =+ section === null+ ? 'not yet defined'+ : `start: ${section.start}, end: ${section.size + section.start}`;throw new Error(- `Cannot seek to a byte that is not in the video section: ${seekTo}`,+ `Cannot seek to a byte that is not in the video section. Seeking to: ${seekTo}, section: ${sectionString}`,);}diff --git a/packages/media-parser/src/state/video-section.ts b/packages/media-parser/src/state/video-section.tsindex ce893b06c6..dcee915908 100644--- a/packages/media-parser/src/state/video-section.ts+++ b/packages/media-parser/src/state/video-section.ts@@ -20,7 +20,7 @@ export const videoSectionState = () => {const getVideoSection = () => {if (!videoSection) {- throw new Error('No video section defined');+ return null;}return videoSection;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.tsindex b2f2e2b355..b1feab641c 100644--- a/packages/media-parser/src/test/seeking/seek-to-byte.test.ts+++ b/packages/media-parser/src/test/seeking/seek-to-byte.test.ts@@ -4,13 +4,10 @@ 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 () => {+test('should not be able to seek to a byte too early', async () => {const controller = mediaParserController();- let firstSample: AudioOrVideoSample | undefined;-try {controller._experimentalSeek({type: 'byte',@@ -21,9 +18,64 @@ test('should be able to set the start seek', async () => {src: exampleVideos.bigBuckBunny,controller,reader: nodeReader,+ acknowledgeRemotionLicense: true,+ fields: {+ dimensions: true,+ },+ });+ throw new Error('should not complete');+ } catch (err) {+ expect((err as Error).message).toBe(+ 'Cannot seek to a byte that is not in the video section. Seeking to: 5, section: start: 48, end: 14282315',+ );+ }+});++test('should not be able to seek to a byte beyond the file', async () => {+ const controller = mediaParserController();++ try {+ controller._experimentalSeek({+ type: 'byte',+ byte: 1_000_000_000,+ });++ await parseMedia({+ src: exampleVideos.bigBuckBunny,+ controller,+ reader: nodeReader,+ acknowledgeRemotionLicense: true,+ fields: {+ dimensions: true,+ },+ });+ throw new Error('should not complete');+ } catch (err) {+ expect((err as Error).message).toBe(+ 'Cannot seek to a byte that is not in the video section. Seeking to: 1000000000, section: start: 48, end: 14282315',+ );+ }+});++test('should be able to seek to a valid byte', async () => {+ const controller = mediaParserController();++ try {+ controller._experimentalSeek({+ type: 'byte',+ byte: 3224491,+ });++ await parseMedia({+ src: exampleVideos.bigBuckBunny,+ controller,+ reader: nodeReader,+ fields: {+ dimensions: true,+ },onVideoTrack: () => {- return (s) => {- firstSample = s;+ return (sample) => {+ expect(sample.offset).toBe(3224491);controller.abort();};},@@ -31,10 +83,6 @@ test('should be able to set the start seek', async () => {});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);+ expect(hasBeenAborted(err as Error)).toBe(true);}});diff --git a/packages/media-parser/src/test/seeking/seek-todos.test.ts b/packages/media-parser/src/test/seeking/seek-todos.test.tsdeleted file mode 100644index b821f14354..0000000000--- a/packages/media-parser/src/test/seeking/seek-todos.test.ts+++ /dev/null@@ -1,4 +0,0 @@-import {test} from 'bun:test';--test.todo('may not use skip to skip over metadata that is requested');-test.todo('may not skip to a section that is outside the video section');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.tsindex d69b2051ca..9b2956cb8c 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@@ -30,7 +30,7 @@ test('should not calculate slow fps, slow duration, slow keyframes etc. if therethrow new Error('should not get this far');} catch (error) {expect((error as Error).message).toInclude(- `Forward seeking is not allowed when the following fields are requested from parseMedia(): slowDurationInSeconds, slowFps. Either don't seek forward, or don't request these fields.`,+ `Forward seeking is not allowed when the following fields are requested from parseMedia(): slowDurationInSeconds, slowFps. Seek was from 0x3ba to 0x480672. Either don't seek forward, or don't request these fields.`,);}});diff --git a/packages/media-parser/src/work-on-seek-request.ts b/packages/media-parser/src/work-on-seek-request.tsindex 470629803f..181f6c6e24 100644--- a/packages/media-parser/src/work-on-seek-request.ts+++ b/packages/media-parser/src/work-on-seek-request.ts@@ -5,6 +5,13 @@ import type {Seek} from './seek-signal';import type {ParserState} from './state/parser-state';const turnSeekIntoByte = (seek: Seek, state: ParserState): SeekResolution => {+ const videoSection = state.videoSection.getVideoSection();+ if (!videoSection) {+ return {+ type: 'valid-but-must-wait',+ };+ }+if (seek.type === 'keyframe-before-time-in-seconds') {const seekingInfo = getSeekingInfo(state);if (!seekingInfo) {@@ -54,6 +61,8 @@ export const workOnSeekRequest = async (state: ParserState) => {);await workOnSeekRequest(state);}++ return;}if (resolution.type === 'invalid') {@@ -61,6 +70,13 @@ export const workOnSeekRequest = async (state: ParserState) => {`The seek request ${JSON.stringify(seek)} cannot be processed`,);}++ if (resolution.type === 'valid-but-must-wait') {+ Log.trace(+ state.logLevel,+ 'Seek request is valid but cannot be processed yet',+ );+ }};type SeekResolution =