🎥 Make videos programmatically with React
`@remotion/media-parser`: Seeking support for .wav
Jonny Burgera54dc8220c41
diff --git a/packages/media-parser/src/containers/iso-base-media/get-seeking-info-from-mp4.ts b/packages/media-parser/src/containers/iso-base-media/get-seeking-info-from-mp4.tsindex de478c192f..ce62fe639f 100644--- a/packages/media-parser/src/containers/iso-base-media/get-seeking-info-from-mp4.ts+++ b/packages/media-parser/src/containers/iso-base-media/get-seeking-info-from-mp4.ts@@ -1,5 +1,5 @@import type {IsoBaseMediaStructure} from '../../parse-result';-import type {SeekingInfo} from '../../seeking-info';+import type {IsoBaseMediaSeekingInfo} from '../../seeking-info';import type {IsoBaseMediaState} from '../../state/iso-base-media/iso-state';import type {StructureState} from '../../state/structure';import type {VideoSectionState} from '../../state/video-section';@@ -15,7 +15,7 @@ export const getSeekingInfoFromMp4 = ({isoState: IsoBaseMediaState;mp4HeaderSegment: IsoBaseMediaStructure | null;videoSectionState: VideoSectionState;-}): SeekingInfo | null => {+}): IsoBaseMediaSeekingInfo | null => {const structure = structureState.getIsoStructure();const moovAtom = getMoovBoxFromState({isoState,diff --git a/packages/media-parser/src/containers/wav/get-seeking-byte.ts b/packages/media-parser/src/containers/wav/get-seeking-byte.tsnew file mode 100644index 0000000000..5feed4d4cc--- /dev/null+++ b/packages/media-parser/src/containers/wav/get-seeking-byte.ts@@ -0,0 +1,28 @@+import type {WavSeekingInfo} from '../../seeking-info';+import type {SeekResolution} from '../../work-on-seek-request';++export const WAVE_SECONDS_PER_SAMPLE = 1;++export const getSeekingByteFromWav = ({+ info,+ time,+}: {+ info: WavSeekingInfo;+ time: number;+}): Promise<SeekResolution> => {+ const bytesPerSecond = info.sampleRate * info.blockAlign;+ const videoSectionLength = info.videoSection.size;+ const durationInSeconds = videoSectionLength / bytesPerSecond;++ const timeRoundedDown =+ Math.floor(+ Math.min(time, durationInSeconds - 0.0000001) / WAVE_SECONDS_PER_SAMPLE,+ ) * WAVE_SECONDS_PER_SAMPLE;++ const byteOffset = bytesPerSecond * timeRoundedDown;++ return Promise.resolve({+ type: 'do-seek',+ byte: byteOffset + info.videoSection.start,+ });+};diff --git a/packages/media-parser/src/containers/wav/get-seeking-info.ts b/packages/media-parser/src/containers/wav/get-seeking-info.tsnew file mode 100644index 0000000000..adc58c983b--- /dev/null+++ b/packages/media-parser/src/containers/wav/get-seeking-info.ts@@ -0,0 +1,31 @@+import type {WavSeekingInfo} from '../../seeking-info';+import type {VideoSectionState} from '../../state/video-section';+import type {WavFmt, WavStructure} from './types';++export const getSeekingInfoFromWav = ({+ structure,+ videoSectionState,+}: {+ structure: WavStructure;+ videoSectionState: VideoSectionState;+}): WavSeekingInfo | null => {+ const fmtBox = structure.boxes.find((box) => box.type === 'wav-fmt') as+ | WavFmt+ | undefined;++ if (!fmtBox) {+ return null;+ }++ const videoSection = videoSectionState.getVideoSections();+ if (videoSection.length !== 1) {+ return null;+ }++ return {+ type: 'wav-seeking-info',+ sampleRate: fmtBox.sampleRate,+ blockAlign: fmtBox.blockAlign,+ videoSection: videoSection[0],+ };+};diff --git a/packages/media-parser/src/containers/wav/parse-video-section.ts b/packages/media-parser/src/containers/wav/parse-video-section.tsindex 17092605e1..75130cf9a1 100644--- a/packages/media-parser/src/containers/wav/parse-video-section.ts+++ b/packages/media-parser/src/containers/wav/parse-video-section.ts@@ -2,8 +2,8 @@ import {convertAudioOrVideoSampleToWebCodecsTimestamps} from '../../convert-audiimport {emitAudioSample} from '../../emit-audio-sample';import type {ParseResult} from '../../parse-result';import type {ParserState} from '../../state/parser-state';-import {getCurrentVideoSection} from '../../state/video-section';import {getWorkOnSeekRequestOptions} from '../../work-on-seek-request';+import {WAVE_SECONDS_PER_SAMPLE} from './get-seeking-byte';import type {WavFmt} from './types';export const parseVideoSection = async ({@@ -14,13 +14,7 @@ export const parseVideoSection = async ({const {iterator} = state;const structure = state.structure.getWavStructure();- const videoSection = getCurrentVideoSection({- offset: iterator.counter.getOffset(),- videoSections: state.videoSection.getVideoSections(),- });- if (!videoSection) {- throw new Error('No video section defined');- }+ const videoSection = state.videoSection.getVideoSectionAssertOnlyOne();const maxOffset = videoSection.start + videoSection.size;const maxRead = maxOffset - iterator.counter.getOffset();@@ -33,11 +27,9 @@ export const parseVideoSection = async ({throw new Error('Expected fmt box');}- const secondsToRead = 1;-const toRead = Math.min(maxRead,- fmtBox.sampleRate * fmtBox.blockAlign * secondsToRead,+ fmtBox.sampleRate * fmtBox.blockAlign * WAVE_SECONDS_PER_SAMPLE,);const duration = toRead / (fmtBox.sampleRate * fmtBox.blockAlign);diff --git a/packages/media-parser/src/get-seeking-info.ts b/packages/media-parser/src/get-seeking-info.tsindex b95c394956..af166cd794 100644--- a/packages/media-parser/src/get-seeking-info.ts+++ b/packages/media-parser/src/get-seeking-info.ts@@ -1,5 +1,7 @@import {getSeekingByteFromIsoBaseMedia} from './containers/iso-base-media/get-seeking-from-mp4';import {getSeekingInfoFromMp4} from './containers/iso-base-media/get-seeking-info-from-mp4';+import {getSeekingByteFromWav} from './containers/wav/get-seeking-byte';+import {getSeekingInfoFromWav} from './containers/wav/get-seeking-info';import type {LogLevel} from './log';import type {IsoBaseMediaStructure} from './parse-result';import type {SeekingInfo} from './seeking-info';@@ -34,7 +36,13 @@ export const getSeekingInfo = ({});}- return null;+ if (structure.type === 'wav') {+ return getSeekingInfoFromWav({structure, videoSectionState});+ }++ throw new Error(+ `Seeking is not supported for this format: ${structure.type}`,+ );};export const getSeekingByte = ({@@ -60,5 +68,12 @@ export const getSeekingByte = ({});}- throw new Error(`Unknown seeking info type: ${info.type as never}`);+ if (info.type === 'wav-seeking-info') {+ return getSeekingByteFromWav({+ info,+ time,+ });+ }++ throw new Error(`Unknown seeking info type: ${info as never}`);};diff --git a/packages/media-parser/src/seeking-info.ts b/packages/media-parser/src/seeking-info.tsindex e57df81759..50781784b0 100644--- a/packages/media-parser/src/seeking-info.ts+++ b/packages/media-parser/src/seeking-info.ts@@ -11,4 +11,11 @@ export type IsoBaseMediaSeekingInfo = {videoSections: VideoSection[];};-export type SeekingInfo = IsoBaseMediaSeekingInfo;+export type WavSeekingInfo = {+ type: 'wav-seeking-info';+ sampleRate: number;+ blockAlign: number;+ videoSection: VideoSection;+};++export type SeekingInfo = IsoBaseMediaSeekingInfo | WavSeekingInfo;diff --git a/packages/media-parser/src/test/seeking/seeking-wav.test.ts b/packages/media-parser/src/test/seeking/seeking-wav.test.tsnew file mode 100644index 0000000000..8bc1778a61--- /dev/null+++ b/packages/media-parser/src/test/seeking/seeking-wav.test.ts@@ -0,0 +1,82 @@+import {exampleVideos} from '@remotion/example-videos';+import {expect, test} from 'bun:test';+import {mediaParserController} from '../../controller/media-parser-controller';+import {nodeReader} from '../../node';+import {parseMedia} from '../../parse-media';++test('seeking in a wav', async () => {+ const controller = mediaParserController();++ controller._experimentalSeek({+ type: 'keyframe-before-time-in-seconds',+ time: 10,+ });++ let samples = 0;++ const {internalStats} = await parseMedia({+ src: exampleVideos.chirp,+ controller,+ reader: nodeReader,+ acknowledgeRemotionLicense: true,+ fields: {+ internalStats: true,+ },+ onAudioTrack: () => {+ return (sample) => {+ samples++;+ if (samples === 1) {+ expect(sample.timestamp).toBe(10000000);+ controller._experimentalSeek({+ type: 'keyframe-before-time-in-seconds',+ time: 0,+ });+ }++ if (samples === 2) {+ expect(sample.timestamp).toBe(0);+ controller._experimentalSeek({+ type: 'keyframe-before-time-in-seconds',+ time: 28,+ });+ }++ if (samples === 3) {+ expect(sample.timestamp).toBe(28000000);+ // out of bounds+ controller._experimentalSeek({+ type: 'keyframe-before-time-in-seconds',+ time: 40,+ });+ }++ if (samples === 4) {+ expect(sample.timestamp).toBe(29000000);+ }+ };+ },+ });++ expect(samples).toBe(4);+ expect(+ controller._internals.performedSeeksSignal.getPerformedSeeks(),+ ).toEqual([+ {+ from: 44,+ to: 882044,+ type: 'user-initiated',+ },+ {+ from: 970244,+ to: 44,+ type: 'user-initiated',+ },+ {+ from: 88244,+ to: 2469644,+ type: 'user-initiated',+ },+ ]);+ expect(internalStats.finalCursorOffset).toBe(2646150);+ expect(internalStats.skippedBytes).toBe(0);+});