Skip to content

🎥 Make videos programmatically with React

also make it work for webm

Jonny Burger9bc39386f9ad
diff --git a/packages/media-parser/src/boxes/webm/parse-webm-header.ts b/packages/media-parser/src/boxes/webm/parse-webm-header.ts
index cf869225a0..9b4f529e5c 100644
--- a/packages/media-parser/src/boxes/webm/parse-webm-header.ts
+++ b/packages/media-parser/src/boxes/webm/parse-webm-header.ts
@@ -1,4 +1,5 @@
import type {BufferIterator} from '../../buffer-iterator';
+import type {Options, ParseMediaFields} from '../../options';
import type {
MatroskaParseResult,
MatroskaStructure,
@@ -30,10 +31,15 @@ const continueAfterMatroskaResult = (
};
// Parsing according to https://darkcoding.net/software/reading-mediarecorders-webm-opus-output/
-export const parseWebm = async (
- counter: BufferIterator,
- parserContext: ParserContext,
-): Promise<ParseResult<MatroskaStructure>> => {
+export const parseWebm = async ({
+ counter,
+ parserContext,
+ fields,
+}: {
+ counter: BufferIterator;
+ parserContext: ParserContext;
+ fields: Options<ParseMediaFields>;
+}): Promise<ParseResult<MatroskaStructure>> => {
const structure: MatroskaStructure = {type: 'matroska', boxes: []};
const results = await expectChildren({
iterator: counter,
@@ -41,6 +47,8 @@ export const parseWebm = async (
children: structure.boxes,
parserContext,
startOffset: counter.counter.getOffset(),
+ fields,
+ topLevelStructure: structure,
});
return continueAfterMatroskaResult(results, structure);
};
diff --git a/packages/media-parser/src/boxes/webm/segments.ts b/packages/media-parser/src/boxes/webm/segments.ts
index a8481313eb..1ff08fcffb 100644
--- a/packages/media-parser/src/boxes/webm/segments.ts
+++ b/packages/media-parser/src/boxes/webm/segments.ts
@@ -1,8 +1,10 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import type {BufferIterator} from '../../buffer-iterator';
+import type {Options, ParseMediaFields} from '../../options';
import type {
ExpectSegmentParseResult,
MatroskaParseResult,
+ MatroskaStructure,
} from '../../parse-result';
import type {ParserContext} from '../../parser-context';
import {parseEbml, postprocessEbml} from './parse-ebml';
@@ -59,11 +61,15 @@ export const expectSegment = async ({
parserContext,
offset,
children,
+ fields,
+ topLevelStructure,
}: {
iterator: BufferIterator;
parserContext: ParserContext;
offset: number;
children: PossibleEbml[];
+ fields: Options<ParseMediaFields>;
+ topLevelStructure: MatroskaStructure;
}): Promise<ExpectSegmentParseResult> => {
iterator.counter.decrement(iterator.counter.getOffset() - offset);
@@ -76,6 +82,8 @@ export const expectSegment = async ({
parserContext,
offset,
children,
+ fields,
+ topLevelStructure,
});
},
segment: null,
@@ -94,6 +102,8 @@ export const expectSegment = async ({
parserContext,
offset,
children,
+ fields,
+ topLevelStructure,
});
},
segment: null,
@@ -109,7 +119,14 @@ export const expectSegment = async ({
return {
status: 'incomplete',
continueParsing: () => {
- return expectSegment({iterator, parserContext, offset, children});
+ return expectSegment({
+ iterator,
+ parserContext,
+ offset,
+ children,
+ fields,
+ topLevelStructure,
+ });
},
segment: null,
};
@@ -131,6 +148,8 @@ export const expectSegment = async ({
children: newSegment.value,
parserContext,
startOffset: iterator.counter.getOffset(),
+ fields,
+ topLevelStructure,
});
if (main.status === 'incomplete') {
@@ -161,7 +180,14 @@ export const expectSegment = async ({
status: 'incomplete',
segment: null,
continueParsing: () => {
- return expectSegment({iterator, parserContext, offset, children});
+ return expectSegment({
+ iterator,
+ parserContext,
+ offset,
+ children,
+ fields,
+ topLevelStructure,
+ });
},
};
}
diff --git a/packages/media-parser/src/boxes/webm/segments/parse-children.ts b/packages/media-parser/src/boxes/webm/segments/parse-children.ts
index 955db89bb0..90fe720005 100644
--- a/packages/media-parser/src/boxes/webm/segments/parse-children.ts
+++ b/packages/media-parser/src/boxes/webm/segments/parse-children.ts
@@ -1,7 +1,10 @@
import type {BufferIterator} from '../../../buffer-iterator';
+import {hasAllInfo} from '../../../has-all-info';
+import type {Options, ParseMediaFields} from '../../../options';
import type {
ExpectSegmentParseResult,
MatroskaParseResult,
+ MatroskaStructure,
} from '../../../parse-result';
import type {ParserContext} from '../../../parser-context';
import type {ParserState} from '../../../state/parser-state';
@@ -13,13 +16,24 @@ const processParseResult = ({
parseResult,
children,
state,
+ fields,
+ topLevelStructure,
}: {
children: MatroskaSegment[];
parseResult: ExpectSegmentParseResult;
state: ParserState;
+ fields: Options<ParseMediaFields>;
+ topLevelStructure: MatroskaStructure;
}): ExpectSegmentParseResult => {
if (parseResult.segment && !children.includes(parseResult.segment)) {
children.push(parseResult.segment);
+ if (hasAllInfo({fields, state, structure: topLevelStructure})) {
+ return {
+ status: 'done',
+ segment: parseResult.segment,
+ };
+ }
+
if (parseResult.segment.type === 'Tracks') {
state.tracks.setIsDone();
}
@@ -36,6 +50,8 @@ const processParseResult = ({
children,
parseResult: newParseResult,
state,
+ fields,
+ topLevelStructure,
});
},
};
@@ -52,22 +68,30 @@ export const expectAndProcessSegment = async ({
parserContext,
offset,
children,
+ fields,
+ topLevelStructure,
}: {
iterator: BufferIterator;
parserContext: ParserContext;
offset: number;
children: PossibleEbml[];
+ fields: Options<ParseMediaFields>;
+ topLevelStructure: MatroskaStructure;
}) => {
const segment = await expectSegment({
iterator,
parserContext,
offset,
children,
+ fields,
+ topLevelStructure,
});
return processParseResult({
children,
parseResult: segment,
state: parserContext.parserState,
+ fields,
+ topLevelStructure,
});
};
@@ -78,6 +102,8 @@ const continueAfterSegmentResult = async ({
parserContext,
iterator,
startOffset,
+ fields,
+ topLevelStructure,
}: {
result: ExpectSegmentParseResult;
length: number;
@@ -85,6 +111,8 @@ const continueAfterSegmentResult = async ({
parserContext: ParserContext;
iterator: BufferIterator;
startOffset: number;
+ fields: Options<ParseMediaFields>;
+ topLevelStructure: MatroskaStructure;
}): Promise<MatroskaParseResult> => {
if (result.status === 'done') {
throw new Error('Should not continue after done');
@@ -102,6 +130,8 @@ const continueAfterSegmentResult = async ({
length,
parserContext,
startOffset,
+ fields,
+ topLevelStructure,
});
},
skipTo: null,
@@ -118,6 +148,8 @@ const continueAfterSegmentResult = async ({
length,
parserContext,
startOffset,
+ fields,
+ topLevelStructure,
});
},
skipTo: null,
@@ -130,12 +162,16 @@ export const expectChildren = async ({
children,
parserContext,
startOffset,
+ fields,
+ topLevelStructure,
}: {
iterator: BufferIterator;
length: number;
children: MatroskaSegment[];
parserContext: ParserContext;
startOffset: number;
+ fields: Options<ParseMediaFields>;
+ topLevelStructure: MatroskaStructure;
}): Promise<MatroskaParseResult> => {
while (iterator.counter.getOffset() < startOffset + length) {
if (iterator.bytesRemaining() === 0) {
@@ -148,8 +184,22 @@ export const expectChildren = async ({
parserContext,
offset: currentOffset,
children,
+ fields,
+ topLevelStructure,
});
+ if (
+ hasAllInfo({
+ fields,
+ state: parserContext.parserState,
+ structure: topLevelStructure,
+ })
+ ) {
+ return {
+ status: 'done',
+ };
+ }
+
if (child.status === 'incomplete') {
return {
status: 'incomplete',
@@ -161,6 +211,8 @@ export const expectChildren = async ({
length: length - (currentOffset - startOffset),
parserContext,
startOffset: currentOffset,
+ fields,
+ topLevelStructure,
});
},
skipTo: null,
diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.ts
index ad41a44110..fce60b576a 100644
--- a/packages/media-parser/src/get-tracks.ts
+++ b/packages/media-parser/src/get-tracks.ts
@@ -119,7 +119,7 @@ export const hasTracks = (
if (structure.type === 'matroska') {
const mainSegment = getMainSegment(structure.boxes);
if (!mainSegment) {
- throw new Error('No main segment found');
+ return false;
}
return getTracksSegment(mainSegment) !== null;
diff --git a/packages/media-parser/src/parse-video.ts b/packages/media-parser/src/parse-video.ts
index 505fd55385..fdad2b2bd4 100644
--- a/packages/media-parser/src/parse-video.ts
+++ b/packages/media-parser/src/parse-video.ts
@@ -64,7 +64,7 @@ export const parseVideo = ({
if (iterator.isWebm()) {
Log.verbose(logLevel, 'Detected Matroska container');
- return parseWebm(iterator, options);
+ return parseWebm({counter: iterator, parserContext: options, fields});
}
if (iterator.isMp3()) {
diff --git a/packages/media-parser/src/state/parser-state.ts b/packages/media-parser/src/state/parser-state.ts
index cf54a9e454..f1f460f4dc 100644
--- a/packages/media-parser/src/state/parser-state.ts
+++ b/packages/media-parser/src/state/parser-state.ts
@@ -16,7 +16,10 @@ import type {
import {makeCanSkipTracksState} from './can-skip-tracks';
import {makeTracksSectionState} from './has-tracks-section';
-export type InternalStats = {};
+export type InternalStats = {
+ skippedBytes: number;
+ finalCursorOffset: number;
+};
export type SpsAndPps = {
sps: AvcProfileInfo;
@@ -220,7 +223,7 @@ export const makeParserState = ({
getAvcProfile: () => {
return avcProfile;
},
- getInternalStats: () => ({
+ getInternalStats: (): InternalStats => ({
skippedBytes,
finalCursorOffset: getIterator()?.counter.getOffset() ?? 0,
}),
diff --git a/packages/media-parser/src/test/parse-mp4-partially.test.ts b/packages/media-parser/src/test/parse-mp4-partially.test.ts
index 7a2ee7e730..dda4e8521a 100644
--- a/packages/media-parser/src/test/parse-mp4-partially.test.ts
+++ b/packages/media-parser/src/test/parse-mp4-partially.test.ts
@@ -58,7 +58,7 @@ test('Should read the whole file', async () => {
expect(parsed.container).toBe('mp4');
expect(parsed.internalStats).toEqual({
- finalCursorOffset: parsed.size,
+ finalCursorOffset: parsed.size as number,
skippedBytes: 0,
});
});
diff --git a/packages/media-parser/src/test/parse-webm-partially.test.ts b/packages/media-parser/src/test/parse-webm-partially.test.ts
new file mode 100644
index 0000000000..d46dbf5363
--- /dev/null
+++ b/packages/media-parser/src/test/parse-webm-partially.test.ts
@@ -0,0 +1,53 @@
+import {exampleVideos} from '@remotion/example-videos';
+import {expect, test} from 'bun:test';
+import {parseMedia} from '../parse-media';
+import {nodeReader} from '../readers/from-node';
+
+test('Parse only header of WebM', async () => {
+ const {internalStats} = await parseMedia({
+ src: exampleVideos.stretchedVp8,
+ fields: {
+ size: true,
+ internalStats: true,
+ },
+ reader: nodeReader,
+ });
+
+ expect(internalStats).toEqual({
+ finalCursorOffset: 43,
+ skippedBytes: 13195316,
+ });
+});
+
+test('Parse WebM partially', async () => {
+ const {internalStats} = await parseMedia({
+ src: exampleVideos.stretchedVp8,
+ fields: {
+ tracks: true,
+ internalStats: true,
+ },
+ reader: nodeReader,
+ });
+
+ expect(internalStats).toEqual({
+ finalCursorOffset: 4562,
+ skippedBytes: 13190797,
+ });
+});
+
+test('Parse WebM fully', async () => {
+ const {internalStats} = await parseMedia({
+ src: exampleVideos.stretchedVp8,
+ fields: {
+ tracks: true,
+ internalStats: true,
+ },
+ onVideoTrack: () => () => undefined,
+ reader: nodeReader,
+ });
+
+ expect(internalStats).toEqual({
+ finalCursorOffset: 13195359,
+ skippedBytes: 0,
+ });
+});
diff --git a/packages/media-parser/src/test/skipped-avi.test.ts b/packages/media-parser/src/test/skipped-avi.test.ts
index e0e8527a80..ee69219d3b 100644
--- a/packages/media-parser/src/test/skipped-avi.test.ts
+++ b/packages/media-parser/src/test/skipped-avi.test.ts
@@ -75,7 +75,7 @@ test('Should not skip if just a video track is requested', async () => {
expect(internalStats).toEqual({
skippedBytes: 0,
- finalCursorOffset: size,
+ finalCursorOffset: size as number,
});
expect(dimensions).toEqual({height: 270, width: 480});
expect(size).toEqual(742478);
diff --git a/packages/media-parser/src/test/stream-remote.test.ts b/packages/media-parser/src/test/stream-remote.test.ts
index edc6e539bc..2ca044543a 100644
--- a/packages/media-parser/src/test/stream-remote.test.ts
+++ b/packages/media-parser/src/test/stream-remote.test.ts
@@ -30,6 +30,7 @@ test(
fps: true,
dimensions: true,
videoCodec: true,
+ internalStats: true,
},
});
expect(result.durationInSeconds).toBe(634.571);
@@ -40,6 +41,7 @@ test(
height: 720,
});
expect(result.videoCodec).toEqual('vp9');
+ expect(result.internalStats.skippedBytes).toBeGreaterThan(120000000);
},
{timeout: 10000},
);
@@ -51,9 +53,11 @@ test('Should handle redirect', async () => {
fps: true,
videoCodec: true,
durationInSeconds: true,
+ internalStats: true,
},
});
expect(result.videoCodec).toEqual('h264');
expect(result.fps).toEqual(24);
expect(result.durationInSeconds).toEqual(596.4733333333334);
+ expect(result.internalStats.skippedBytes).toBeGreaterThan(100000000);
});