Skip to content

🎥 Make videos programmatically with React

nice

Jonny Burger19723ddc49c7
diff --git a/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts b/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts
index bf2ac2e98e..3f29b65c2e 100644
--- a/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts
@@ -1,7 +1,6 @@
import type {BufferIterator} from '../../../buffer-iterator';
import {convertAudioOrVideoSampleToWebCodecsTimestamps} from '../../../convert-audio-or-video-sample';
import {getHasTracks, getTracks} from '../../../get-tracks';
-import {maySkipVideoData} from '../../../may-skip-video-data/may-skip-video-data';
import type {IsoBaseMediaStructure} from '../../../parse-result';
import type {ParserState} from '../../../state/parser-state';
import {getSamplePositionsFromTrack} from '../get-sample-positions-from-track';
@@ -14,15 +13,12 @@ export const parseMdatSection = async ({
}: {
iterator: BufferIterator;
state: ParserState;
-}): Promise<void> => {
+}): Promise<number | null> => {
const alreadyHas = getHasTracks(state.structure.getStructure(), state);
if (!alreadyHas) {
- if (maySkipVideoData({state})) {
+ if (state.supportsContentRange) {
const videoSection = state.videoSection.getVideoSection();
- iterator.discard(
- videoSection.size + videoSection.start - iterator.counter.getOffset(),
- );
- return;
+ return videoSection.size + videoSection.start;
}
throw new Error(
@@ -72,15 +68,15 @@ export const parseMdatSection = async ({
iterator.discard(
nextSample_.samplePosition.offset - iterator.counter.getOffset(),
);
- return;
+ return null;
}
// guess we reached the end!
- return;
+ return null;
}
if (iterator.bytesRemaining() < samplesWithIndex.samplePosition.size) {
- return;
+ return null;
}
const bytes = iterator.getSlice(samplesWithIndex.samplePosition.size);
@@ -142,4 +138,5 @@ export const parseMdatSection = async ({
}
iterator.removeBytesRead();
+ return null;
};
diff --git a/packages/media-parser/src/boxes/iso-base-media/parse-boxes.ts b/packages/media-parser/src/boxes/iso-base-media/parse-boxes.ts
index a98a2315b8..6ae547d9b9 100644
--- a/packages/media-parser/src/boxes/iso-base-media/parse-boxes.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/parse-boxes.ts
@@ -1,6 +1,7 @@
import type {BufferIterator} from '../../buffer-iterator';
import type {IsoBaseMediaStructure, ParseResult} from '../../parse-result';
import type {ParserState} from '../../state/parser-state';
+import {parseMdatSection} from './mdat/mdat';
import {processBox} from './process-box';
export const parseIsoBaseMedia = async ({
@@ -8,7 +9,6 @@ export const parseIsoBaseMedia = async ({
state,
}: {
iterator: BufferIterator;
-
state: ParserState;
}): Promise<ParseResult> => {
const continueParsing = () => {
@@ -18,6 +18,18 @@ export const parseIsoBaseMedia = async ({
});
};
+ if (state.videoSection.isInVideoSectionState(iterator) === 'in-section') {
+ await parseMdatSection({
+ iterator,
+ state,
+ });
+ return {
+ continueParsing,
+ skipTo: null,
+ status: 'incomplete',
+ };
+ }
+
const result = await processBox({
iterator,
state,
@@ -28,6 +40,19 @@ export const parseIsoBaseMedia = async ({
);
}
+ if (
+ iterator.counter.getOffset() === state.contentLength &&
+ state.iso.getShouldReturnToVideoSectionAfterEnd()
+ ) {
+ state.iso.setShouldReturnToVideoSectionAfterEnd(false);
+
+ return {
+ status: 'incomplete',
+ continueParsing,
+ skipTo: state.videoSection.getVideoSection().start,
+ };
+ }
+
if (result.skipTo !== null) {
if (!state.supportsContentRange) {
throw new Error(
diff --git a/packages/media-parser/src/boxes/iso-base-media/process-box.ts b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
index 99286b3870..6175817027 100644
--- a/packages/media-parser/src/boxes/iso-base-media/process-box.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
@@ -10,7 +10,6 @@ import {parseEsds} from './esds/esds';
import {parseFtyp} from './ftyp';
import {getIsoBaseMediaChildren} from './get-children';
import {makeBaseMediaTrack} from './make-track';
-import {parseMdatSection} from './mdat/mdat';
import {parseMdhd} from './mdhd';
import {parseHdlr} from './meta/hdlr';
import {parseIlstBox} from './meta/ilst';
@@ -54,7 +53,6 @@ export const processBox = async ({
type: 'void-box',
boxSize: 0,
},
- size: 4,
skipTo: null,
};
}
@@ -76,19 +74,11 @@ export const processBox = async ({
if (boxType === 'mdat') {
state.videoSection.setVideoSection({
- size: boxSize,
+ size: boxSize - 8,
start: iterator.counter.getOffset(),
});
- }
-
- if (bytesRemaining < boxSize) {
- if (boxType !== 'mdat') {
- throw new Error(
- `Expected box size of ${bytesRemaining}, got ${boxSize} for ${boxType}. Incomplete boxes are not allowed.`,
- );
- }
- // Check if the moov atom is at the end
+ // TODO: if content range is not supported we fall apart
const shouldSkip =
maySkipVideoData({state}) ||
(!getHasTracks(
@@ -98,25 +88,29 @@ export const processBox = async ({
state.supportsContentRange);
if (shouldSkip) {
+ state.iso.setShouldReturnToVideoSectionAfterEnd(true);
return {
skipTo: fileOffset + boxSize,
box: null,
- size: 0,
};
}
return {
box: null,
- size: 0,
skipTo: null,
};
}
+ if (bytesRemaining < boxSize) {
+ throw new Error(
+ `Expected box size of ${bytesRemaining}, got ${boxSize} for ${boxType}. Incomplete boxes are not allowed.`,
+ );
+ }
+
if (boxType === 'ftyp') {
const box = parseFtyp({iterator, size: boxSize, offset: fileOffset});
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -128,7 +122,6 @@ export const processBox = async ({
});
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -138,7 +131,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -148,7 +140,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -158,7 +149,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -168,7 +158,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -183,7 +172,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -197,7 +185,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -212,7 +199,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -226,7 +212,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -240,7 +225,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -254,7 +238,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -268,7 +251,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -283,7 +265,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -293,7 +274,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -303,7 +283,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -317,12 +296,19 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
if (boxType === 'moov') {
+ if (state.callbacks.tracks.hasAllTracks()) {
+ iterator.discard(boxSize - 8);
+ return {
+ box: null,
+ skipTo: null,
+ };
+ }
+
const box = await parseMoov({
iterator,
offset: fileOffset,
@@ -334,7 +320,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -357,7 +342,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -371,7 +355,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -384,7 +367,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -397,7 +379,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -411,7 +392,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -425,7 +405,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -439,7 +418,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
skipTo: null,
};
}
@@ -453,24 +431,6 @@ export const processBox = async ({
return {
box,
- size: boxSize,
- skipTo: null,
- };
- }
-
- if (boxType === 'mdat') {
- state.videoSection.setVideoSection({
- size: boxSize,
- start: iterator.counter.getOffset(),
- });
- await parseMdatSection({
- iterator,
- state,
- });
-
- return {
- box: null,
- size: boxSize,
skipTo: null,
};
}
@@ -501,7 +461,6 @@ export const processBox = async ({
children,
offset: fileOffset,
},
- size: boxSize,
skipTo: null,
};
}
@@ -516,7 +475,6 @@ export const processBox = async ({
children: [],
offset: fileOffset,
},
- size: boxSize,
skipTo: null,
};
};
diff --git a/packages/media-parser/src/parse-media.ts b/packages/media-parser/src/parse-media.ts
index eda253c06f..745e681d32 100644
--- a/packages/media-parser/src/parse-media.ts
+++ b/packages/media-parser/src/parse-media.ts
@@ -54,6 +54,12 @@ export const parseMedia: ParseMedia = async function <
contentLength ?? 1_000_000_000,
);
+ if (contentLength === null) {
+ throw new Error(
+ 'Media was passed with no content length. This is currently not supported. Ensure the media has a "Content-Length" HTTP header.',
+ );
+ }
+
const supportsContentRange =
readerSupportsContentRange &&
!(
diff --git a/packages/media-parser/src/parse-video.ts b/packages/media-parser/src/parse-video.ts
index 7b87417087..37566b3f08 100644
--- a/packages/media-parser/src/parse-video.ts
+++ b/packages/media-parser/src/parse-video.ts
@@ -18,7 +18,6 @@ import type {ParserState} from './state/parser-state';
export type BoxAndNext = {
box: IsoBaseMediaBox | null;
- size: number;
skipTo: number | null;
};
diff --git a/packages/media-parser/src/state/iso-state.ts b/packages/media-parser/src/state/iso-state.ts
new file mode 100644
index 0000000000..4e66aee0fe
--- /dev/null
+++ b/packages/media-parser/src/state/iso-state.ts
@@ -0,0 +1,11 @@
+export const isoBaseMediaState = () => {
+ let shouldReturnToVideoSectionAfterEnd = false;
+
+ return {
+ getShouldReturnToVideoSectionAfterEnd: () =>
+ shouldReturnToVideoSectionAfterEnd,
+ setShouldReturnToVideoSectionAfterEnd: (value: boolean) => {
+ shouldReturnToVideoSectionAfterEnd = value;
+ },
+ };
+};
diff --git a/packages/media-parser/src/state/parser-state.ts b/packages/media-parser/src/state/parser-state.ts
index 26dc1d0007..102c07781d 100644
--- a/packages/media-parser/src/state/parser-state.ts
+++ b/packages/media-parser/src/state/parser-state.ts
@@ -5,6 +5,7 @@ import type {Options, ParseMediaFields} from '../options';
import type {OnAudioTrack, OnVideoTrack} from '../webcodec-sample-types';
import {emittedState} from './emitted-fields';
import {imagesState} from './images';
+import {isoBaseMediaState} from './iso-state';
import {keyframesState} from './keyframes';
import {makeMp3State} from './mp3';
import {riffSpecificState} from './riff';
@@ -65,6 +66,7 @@ export const makeParserState = ({
riff: riffSpecificState(),
transportStream: transportStreamState(),
webm: webmState(),
+ iso: isoBaseMediaState(),
mp3Info,
callbacks: sampleCallback({
signal,
diff --git a/packages/media-parser/src/test/fast-mehmet.test.ts b/packages/media-parser/src/test/fast-mehmet.test.ts
index 2c14d21a97..141ae63ad1 100644
--- a/packages/media-parser/src/test/fast-mehmet.test.ts
+++ b/packages/media-parser/src/test/fast-mehmet.test.ts
@@ -9,7 +9,6 @@ test('mehmet video should be fast', async () => {
await parseMedia({
src: exampleVideos.iphonelivefoto,
reader: nodeReader,
- logLevel: 'trace',
fields: {
structure: true,
},
diff --git a/packages/media-parser/src/test/keyframes.test.ts b/packages/media-parser/src/test/keyframes.test.ts
index d377da65b7..6377b841fe 100644
--- a/packages/media-parser/src/test/keyframes.test.ts
+++ b/packages/media-parser/src/test/keyframes.test.ts
@@ -12,6 +12,7 @@ test('should be able to get keyframes from webm', async () => {
slowKeyframes: true,
internalStats: true,
},
+
reader: nodeReader,
});
expect(slowKeyframes).toEqual([