Skip to content

🎥 Make videos programmatically with React

video parsers

Jonny Burgereaf383ceb821
diff --git a/packages/browser-renderer/src/video-parser.ts b/packages/browser-renderer/src/video-parser.ts
index 978f3152e6..5199672269 100644
--- a/packages/browser-renderer/src/video-parser.ts
+++ b/packages/browser-renderer/src/video-parser.ts
@@ -1,7 +1,32 @@
import {parseMedia} from '@remotion/media-parser';
import {webFileReader} from '@remotion/media-parser/web-file';
+import {createDecoder} from './create-decoder';
export const parseVideo = async (file: File) => {
+ const {dimensions} = await parseMedia({
+ src: file,
+ readerInterface: webFileReader,
+ fields: {
+ dimensions: true,
+ },
+ });
+
+ const {decoder} = createDecoder({
+ onFrame: (frame) => {
+ console.log('frame', frame);
+ },
+ onProgress: (decoded) => {
+ console.log('progress', decoded);
+ },
+ });
+
+ decoder.configure({
+ codec: 'avc1.640020',
+ codedHeight: dimensions.height,
+ codedWidth: dimensions.width,
+ hardwareAcceleration: 'prefer-hardware',
+ });
+
const result = await parseMedia({
src: file,
readerInterface: webFileReader,
diff --git a/packages/example/src/EncoderDemo.tsx b/packages/example/src/EncoderDemo.tsx
index 7f1b92f985..0c633830cd 100644
--- a/packages/example/src/EncoderDemo.tsx
+++ b/packages/example/src/EncoderDemo.tsx
@@ -1,11 +1,11 @@
-import {parseVideo} from '@remotion/browser-renderer';
+import {reencodeVideo} from '@remotion/browser-renderer/src/reencode-video';
import React from 'react';
export const EncoderDemo: React.FC = () => {
return (
<input
type="file"
- onChange={(e) => parseVideo(e.target.files?.[0] as File)}
+ onChange={(e) => reencodeVideo(e.target.files?.[0] as File)}
/>
);
};
diff --git a/packages/media-parser/boxes.json b/packages/media-parser/boxes.json
new file mode 100644
index 0000000000..e1cb6a6002
--- /dev/null
+++ b/packages/media-parser/boxes.json
@@ -0,0 +1,633 @@
+[
+ {
+ "type": "ftyp-box",
+ "majorBrand": "isom",
+ "minorVersion": 512,
+ "compatibleBrands": [
+ "isom",
+ "iso2",
+ "avc1",
+ "mp41"
+ ],
+ "offset": 0,
+ "boxSize": 32
+ },
+ {
+ "offset": 32,
+ "boxSize": 1589,
+ "type": "moov-box",
+ "children": [
+ {
+ "creationTime": null,
+ "modificationTime": null,
+ "timeScale": 1000,
+ "durationInUnits": 337,
+ "durationInSeconds": 0.337,
+ "rate": 1,
+ "volume": 1,
+ "matrix": [
+ 65536,
+ 0,
+ 0,
+ 0,
+ 65536,
+ 0,
+ 0,
+ 0,
+ 1073741824
+ ],
+ "nextTrackId": 3,
+ "type": "mvhd-box",
+ "boxSize": 108,
+ "offset": 40
+ },
+ {
+ "offset": 148,
+ "boxSize": 712,
+ "type": "trak-box",
+ "children": [
+ {
+ "offset": 156,
+ "boxSize": 92,
+ "type": "tkhd-box",
+ "creationTime": null,
+ "modificationTime": null,
+ "trackId": 1,
+ "duration": 334,
+ "layer": 0,
+ "alternateGroup": 0,
+ "volume": 0,
+ "matrix": [
+ 65536,
+ 0,
+ 0,
+ 0,
+ 65536,
+ 0,
+ 0,
+ 0,
+ 1073741824
+ ],
+ "width": 1080,
+ "height": 1080,
+ "version": 0
+ },
+ {
+ "type": "regular-box",
+ "boxType": "edts",
+ "boxSize": 36,
+ "children": [],
+ "offset": 248
+ },
+ {
+ "type": "regular-box",
+ "boxType": "mdia",
+ "boxSize": 576,
+ "children": [
+ {
+ "type": "mdhd-box",
+ "duration": 5120,
+ "timescale": 15360,
+ "version": 0,
+ "language": 21956,
+ "quality": 0
+ },
+ {
+ "type": "regular-box",
+ "boxType": "hdlr",
+ "boxSize": 45,
+ "children": [],
+ "offset": 324
+ },
+ {
+ "type": "regular-box",
+ "boxType": "minf",
+ "boxSize": 491,
+ "children": [
+ {
+ "type": "regular-box",
+ "boxType": "vmhd",
+ "boxSize": 20,
+ "children": [],
+ "offset": 377
+ },
+ {
+ "type": "regular-box",
+ "boxType": "dinf",
+ "boxSize": 36,
+ "children": [],
+ "offset": 397
+ },
+ {
+ "type": "regular-box",
+ "boxType": "stbl",
+ "boxSize": 427,
+ "children": [
+ {
+ "type": "stsd-box",
+ "boxSize": 199,
+ "offset": 441,
+ "numberOfEntries": 1,
+ "samples": [
+ {
+ "format": "avc1",
+ "offset": 457,
+ "dataReferenceIndex": 1,
+ "version": 0,
+ "revisionLevel": 0,
+ "vendor": [
+ 0,
+ 0,
+ 0,
+ 0
+ ],
+ "size": 183,
+ "type": "video",
+ "width": 1080,
+ "height": 1080,
+ "horizontalResolutionPpi": 72,
+ "verticalResolutionPpi": 72,
+ "spacialQuality": 0,
+ "temporalQuality": 0,
+ "dataSize": 0,
+ "frameCountPerSample": 1,
+ "compressorName": [
+ 21,
+ 76,
+ 97,
+ 118,
+ 99,
+ 54,
+ 48,
+ 46,
+ 51,
+ 49,
+ 46,
+ 49,
+ 48,
+ 50,
+ 32,
+ 108,
+ 105,
+ 98,
+ 120,
+ 50,
+ 54,
+ 52,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0
+ ],
+ "depth": 24,
+ "colorTableId": -1,
+ "descriptors": [
+ {
+ "type": "avcc-box",
+ "data": {
+ "0": 1,
+ "1": 100,
+ "2": 0,
+ "3": 32,
+ "4": 255,
+ "5": 225,
+ "6": 0,
+ "7": 32,
+ "8": 103,
+ "9": 100,
+ "10": 0,
+ "11": 32,
+ "12": 172,
+ "13": 217,
+ "14": 64,
+ "15": 68,
+ "16": 2,
+ "17": 39,
+ "18": 150,
+ "19": 92,
+ "20": 5,
+ "21": 168,
+ "22": 16,
+ "23": 16,
+ "24": 45,
+ "25": 40,
+ "26": 0,
+ "27": 0,
+ "28": 3,
+ "29": 0,
+ "30": 8,
+ "31": 0,
+ "32": 0,
+ "33": 3,
+ "34": 1,
+ "35": 224,
+ "36": 120,
+ "37": 193,
+ "38": 140,
+ "39": 176,
+ "40": 1,
+ "41": 0,
+ "42": 6,
+ "43": 104,
+ "44": 235,
+ "45": 224,
+ "46": 140,
+ "47": 178,
+ "48": 44,
+ "49": 253,
+ "50": 248,
+ "51": 248,
+ "52": 0
+ }
+ },
+ {
+ "type": "regular-box",
+ "boxType": "pasp",
+ "boxSize": 16,
+ "children": [],
+ "offset": 604
+ },
+ {
+ "type": "regular-box",
+ "boxType": "btrt",
+ "boxSize": 20,
+ "children": [],
+ "offset": 620
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "type": "stts-box",
+ "sampleDistribution": [
+ {
+ "sampleCount": 10,
+ "sampleDelta": 512
+ }
+ ]
+ },
+ {
+ "type": "regular-box",
+ "boxType": "stss",
+ "boxSize": 20,
+ "children": [],
+ "offset": 664
+ },
+ {
+ "type": "regular-box",
+ "boxType": "ctts",
+ "boxSize": 24,
+ "children": [],
+ "offset": 684
+ },
+ {
+ "type": "stsc-box",
+ "boxSize": 40,
+ "offset": 708,
+ "version": 0,
+ "flags": [
+ 0,
+ 0,
+ 0
+ ],
+ "entryCount": 2,
+ "entries": [
+ {
+ "firstChunk": 1,
+ "samplesPerChunk": 2
+ },
+ {
+ "firstChunk": 2,
+ "samplesPerChunk": 1
+ }
+ ]
+ },
+ {
+ "type": "stsz-box",
+ "boxSize": 60,
+ "offset": 748,
+ "version": 0,
+ "flags": [
+ 0,
+ 0,
+ 0
+ ],
+ "sampleCount": 10,
+ "countType": "variable",
+ "entries": [
+ 4834,
+ 437,
+ 698,
+ 595,
+ 548,
+ 611,
+ 701,
+ 535,
+ 776,
+ 586
+ ]
+ },
+ {
+ "type": "stco-box",
+ "boxSize": 52,
+ "offset": 808,
+ "version": 0,
+ "flags": [
+ 0,
+ 0,
+ 0
+ ],
+ "entries": [
+ 1637,
+ 7868,
+ 10486,
+ 12041,
+ 14509,
+ 16080,
+ 17741,
+ 20196,
+ 21932
+ ],
+ "entryCount": 9
+ }
+ ],
+ "offset": 433
+ }
+ ],
+ "offset": 369
+ }
+ ],
+ "offset": 284
+ }
+ ]
+ },
+ {
+ "offset": 860,
+ "boxSize": 613,
+ "type": "trak-box",
+ "children": [
+ {
+ "offset": 868,
+ "boxSize": 92,
+ "type": "tkhd-box",
+ "creationTime": null,
+ "modificationTime": null,
+ "trackId": 2,
+ "duration": 337,
+ "layer": 0,
+ "alternateGroup": 1,
+ "volume": 256,
+ "matrix": [
+ 65536,
+ 0,
+ 0,
+ 0,
+ 65536,
+ 0,
+ 0,
+ 0,
+ 1073741824
+ ],
+ "width": 0,
+ "height": 0,
+ "version": 0
+ },
+ {
+ "type": "regular-box",
+ "boxType": "edts",
+ "boxSize": 36,
+ "children": [],
+ "offset": 960
+ },
+ {
+ "type": "regular-box",
+ "boxType": "mdia",
+ "boxSize": 477,
+ "children": [
+ {
+ "type": "mdhd-box",
+ "duration": 17280,
+ "timescale": 48000,
+ "version": 0,
+ "language": 21956,
+ "quality": 0
+ },
+ {
+ "type": "regular-box",
+ "boxType": "hdlr",
+ "boxSize": 45,
+ "children": [],
+ "offset": 1036
+ },
+ {
+ "type": "regular-box",
+ "boxType": "minf",
+ "boxSize": 392,
+ "children": [
+ {
+ "type": "regular-box",
+ "boxType": "smhd",
+ "boxSize": 16,
+ "children": [],
+ "offset": 1089
+ },
+ {
+ "type": "regular-box",
+ "boxType": "dinf",
+ "boxSize": 36,
+ "children": [],
+ "offset": 1105
+ },
+ {
+ "type": "regular-box",
+ "boxType": "stbl",
+ "boxSize": 332,
+ "children": [
+ {
+ "type": "stsd-box",
+ "boxSize": 116,
+ "offset": 1149,
+ "numberOfEntries": 1,
+ "samples": [
+ {
+ "format": "mp4a",
+ "offset": 1165,
+ "dataReferenceIndex": 1,
+ "version": 0,
+ "revisionLevel": 0,
+ "vendor": [
+ 0,
+ 0,
+ 0,
+ 0
+ ],
+ "size": 100,
+ "type": "audio",
+ "numberOfChannels": 2,
+ "sampleSize": 16,
+ "compressionId": 0,
+ "packetSize": 0,
+ "sampleRate": 48000,
+ "samplesPerPacket": null,
+ "bytesPerPacket": null,
+ "bytesPerFrame": null,
+ "bitsPerSample": null,
+ "children": [
+ {
+ "type": "esds-box",
+ "version": 0,
+ "tag": 3,
+ "sizeOfInstance": 27,
+ "esId": 2,
+ "descriptors": [
+ {
+ "type": "decoder-config-descriptor",
+ "objectTypeIndication": "mp3"
+ },
+ {
+ "type": "sl-config-descriptor"
+ }
+ ]
+ },
+ {
+ "type": "regular-box",
+ "boxType": "btrt",
+ "boxSize": 20,
+ "children": [],
+ "offset": 1245
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "type": "stts-box",
+ "sampleDistribution": [
+ {
+ "sampleCount": 15,
+ "sampleDelta": 1152
+ }
+ ]
+ },
+ {
+ "type": "stsc-box",
+ "boxSize": 112,
+ "offset": 1289,
+ "version": 0,
+ "flags": [
+ 0,
+ 0,
+ 0
+ ],
+ "entryCount": 8,
+ "entries": [
+ {
+ "firstChunk": 1,
+ "samplesPerChunk": 1
+ },
+ {
+ "firstChunk": 2,
+ "samplesPerChunk": 2
+ },
+ {
+ "firstChunk": 3,
+ "samplesPerChunk": 1
+ },
+ {
+ "firstChunk": 4,
+ "samplesPerChunk": 2
+ },
+ {
+ "firstChunk": 5,
+ "samplesPerChunk": 1
+ },
+ {
+ "firstChunk": 7,
+ "samplesPerChunk": 2
+ },
+ {
+ "firstChunk": 8,
+ "samplesPerChunk": 1
+ },
+ {
+ "firstChunk": 9,
+ "samplesPerChunk": 4
+ }
+ ]
+ },
+ {
+ "type": "stsz-box",
+ "boxSize": 20,
+ "offset": 1401,
+ "version": 0,
+ "flags": [
+ 0,
+ 0,
+ 0
+ ],
+ "sampleCount": 15,
+ "countType": "fixed",
+ "sampleSize": 960
+ },
+ {
+ "type": "stco-box",
+ "boxSize": 52,
+ "offset": 1421,
+ "version": 0,
+ "flags": [
+ 0,
+ 0,
+ 0
+ ],
+ "entries": [
+ 6908,
+ 8566,
+ 11081,
+ 12589,
+ 15120,
+ 16781,
+ 18276,
+ 20972,
+ 22518
+ ],
+ "entryCount": 9
+ }
+ ],
+ "offset": 1141
+ }
+ ],
+ "offset": 1081
+ }
+ ],
+ "offset": 996
+ }
+ ]
+ },
+ {
+ "type": "regular-box",
+ "boxType": "udta",
+ "boxSize": 148,
+ "children": [],
+ "offset": 1473
+ }
+ ]
+ },
+ {
+ "type": "regular-box",
+ "boxType": "free",
+ "boxSize": 8,
+ "children": [],
+ "offset": 1621
+ },
+ {
+ "type": "mdat-box",
+ "boxSize": 24729
+ }
+]
\ No newline at end of file
diff --git a/packages/media-parser/src/boxes/iso-base-media/avcc.ts b/packages/media-parser/src/boxes/iso-base-media/avcc.ts
new file mode 100644
index 0000000000..5677b1ca77
--- /dev/null
+++ b/packages/media-parser/src/boxes/iso-base-media/avcc.ts
@@ -0,0 +1,37 @@
+import type {BufferIterator} from '../../buffer-iterator';
+
+export interface AvccBox {
+ type: 'avcc-box';
+ data: Uint8Array;
+}
+
+export const parseAvcc = ({
+ data,
+ size,
+}: {
+ data: BufferIterator;
+ size: number;
+}): AvccBox => {
+ return {
+ type: 'avcc-box',
+ data: data.getSlice(size - 8),
+ };
+};
+
+export interface HvccBox {
+ type: 'hvcc-box';
+ data: Uint8Array;
+}
+
+export const parseHvcc = ({
+ data,
+ size,
+}: {
+ data: BufferIterator;
+ size: number;
+}): HvccBox => {
+ return {
+ type: 'hvcc-box',
+ data: data.getSlice(size - 8),
+ };
+};
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 bb57c38d10..50f86a7b18 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
@@ -62,9 +62,11 @@ export const parseMdat = ({
throw new Error('Could not find sample');
}
+ const bytes = data.getSlice(sampleWithIndex.samplePosition.size);
+
if (sampleWithIndex.type === 'audio' && options.onAudioSample) {
options.onAudioSample({
- bytes: data.getSlice(sampleWithIndex.samplePosition.size),
+ bytes,
start: sampleWithIndex.samplePosition.offset,
offset: data.counter.getOffset(),
trackId: sampleWithIndex.trackId,
@@ -72,7 +74,6 @@ export const parseMdat = ({
}
if (sampleWithIndex.type === 'video' && options.onVideoSample) {
- const bytes = data.getSlice(sampleWithIndex.samplePosition.size);
options.onVideoSample({
bytes,
start: sampleWithIndex.samplePosition.offset,
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 dc76d17239..b95653c7ba 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
@@ -6,6 +6,7 @@ import type {
} from '../../parse-result';
import type {BoxAndNext} from '../../parse-video';
import type {ParserContext} from '../../parser-context';
+import {parseAvcc, parseHvcc} from './avcc';
import {parseEsds} from './esds/esds';
import {parseFtyp} from './ftyp';
import {parseMdat} from './mdat/mdat';
@@ -280,6 +281,34 @@ const processBox = ({
};
}
+ if (boxType === 'avcC') {
+ const box = parseAvcc({
+ data: iterator,
+ size: boxSize,
+ });
+
+ return {
+ type: 'complete',
+ box,
+ size: boxSize,
+ skipTo: null,
+ };
+ }
+
+ if (boxType === 'hvcC') {
+ const box = parseHvcc({
+ data: iterator,
+ size: boxSize,
+ });
+
+ return {
+ type: 'complete',
+ box,
+ size: boxSize,
+ skipTo: null,
+ };
+ }
+
if (boxType === 'mdhd') {
const box = parseMdhd({
data: iterator,
diff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/samples.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/samples.ts
index 6322b71b61..8a522f7698 100644
--- a/packages/media-parser/src/boxes/iso-base-media/stsd/samples.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/samples.ts
@@ -40,6 +40,7 @@ type VideoSample = SampleBase & {
frameCountPerSample: number;
depth: number;
colorTableId: number;
+ descriptors: AnySegment[];
};
type UnknownSample = SampleBase & {
@@ -277,7 +278,17 @@ export const processSample = ({
const bytesRemainingInBox =
boxSize - (iterator.counter.getOffset() - fileOffset);
- iterator.discard(bytesRemainingInBox);
+ const children = parseBoxes({
+ iterator,
+ allowIncompleteBoxes: false,
+ maxBytes: bytesRemainingInBox,
+ initialBoxes: [],
+ options,
+ });
+
+ if (children.status === 'incomplete') {
+ throw new Error('Incomplete boxes are not allowed');
+ }
return {
sample: {
@@ -300,6 +311,7 @@ export const processSample = ({
compressorName,
depth,
colorTableId,
+ descriptors: children.segments,
},
};
}
diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.ts
index 2b42ddf299..ea74b17c48 100644
--- a/packages/media-parser/src/get-tracks.ts
+++ b/packages/media-parser/src/get-tracks.ts
@@ -10,12 +10,14 @@ import {
getStszBox,
getTkhdBox,
getTraks,
+ getVideoDescriptors,
} from './traversal';
type Track = {
type: 'audio' | 'video';
samplePositions: SamplePosition[];
trackId: number;
+ description: Uint8Array | null;
};
// TODO: Use this to determine if all tracks are present
@@ -47,6 +49,7 @@ export const getTracks = (segments: AnySegment[]): Track[] => {
const stcoBox = getStcoBox(track);
const stscBox = getStscBox(track);
const tkhdBox = getTkhdBox(track);
+ const videoDescriptors = getVideoDescriptors(track);
if (!tkhdBox) {
throw new Error('Expected tkhd box in trak box');
@@ -75,6 +78,7 @@ export const getTracks = (segments: AnySegment[]): Track[] => {
type: 'audio',
samplePositions,
trackId: tkhdBox.trackId,
+ description: null,
});
}
@@ -83,6 +87,7 @@ export const getTracks = (segments: AnySegment[]): Track[] => {
type: 'video',
samplePositions,
trackId: tkhdBox.trackId,
+ description: videoDescriptors,
});
}
}
diff --git a/packages/media-parser/src/parse-result.ts b/packages/media-parser/src/parse-result.ts
index 1f20fc98b6..28bc855ddf 100644
--- a/packages/media-parser/src/parse-result.ts
+++ b/packages/media-parser/src/parse-result.ts
@@ -1,3 +1,4 @@
+import type {AvccBox, HvccBox} from './boxes/iso-base-media/avcc';
import type {BaseBox} from './boxes/iso-base-media/base-type';
import type {EsdsBox} from './boxes/iso-base-media/esds/esds';
import type {FtypBox} from './boxes/iso-base-media/ftyp';
@@ -40,7 +41,9 @@ export type IsoBaseMediaBox =
| MdatBox
| StszBox
| StcoBox
- | StscBox;
+ | StscBox
+ | AvccBox
+ | HvccBox;
export type AnySegment = MatroskaSegment | IsoBaseMediaBox;
diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.ts
index 5aeb7a707e..1e3c7c9a22 100644
--- a/packages/media-parser/src/test/stream-samples.test.ts
+++ b/packages/media-parser/src/test/stream-samples.test.ts
@@ -14,6 +14,12 @@ test('Stream samples', async () => {
readerInterface: nodeReader,
});
+ const description = new Uint8Array([
+ 1, 100, 0, 32, 255, 225, 0, 32, 103, 100, 0, 32, 172, 217, 64, 68, 2, 39,
+ 150, 92, 5, 168, 16, 16, 45, 40, 0, 0, 3, 0, 8, 0, 0, 3, 1, 224, 120, 193,
+ 140, 176, 1, 0, 6, 104, 235, 224, 140, 178, 44, 253, 248, 248, 0,
+ ]);
+
const tracks = getTracks(boxes);
expect(tracks).toEqual([
{
@@ -61,6 +67,7 @@ test('Stream samples', async () => {
size: 586,
},
],
+ description,
},
{
type: 'audio',
@@ -127,6 +134,7 @@ test('Stream samples', async () => {
size: 960,
},
],
+ description: null,
},
]);
});
diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.ts
index c5e423e7ae..b1db4e4e8f 100644
--- a/packages/media-parser/src/traversal.ts
+++ b/packages/media-parser/src/traversal.ts
@@ -91,6 +91,28 @@ export const getStsdBox = (trakBox: TrakBox): StsdBox | null => {
return stsdBox;
};
+export const getVideoDescriptors = (trakBox: TrakBox): Uint8Array | null => {
+ const stsdBox = getStsdBox(trakBox);
+
+ if (!stsdBox) {
+ return null;
+ }
+
+ const descriptors = stsdBox.samples.map((s) => {
+ return s.type === 'video'
+ ? s.descriptors.map((d) => {
+ return d.type === 'avcc-box'
+ ? d.data
+ : d.type === 'hvcc-box'
+ ? d.data
+ : null;
+ })
+ : [];
+ });
+
+ return descriptors.flat(1).filter(Boolean)[0] ?? null;
+};
+
export const getStcoBox = (trakBox: TrakBox): StcoBox | null => {
const stblBox = getStblBox(trakBox);