Skip to content

🎥 Make videos programmatically with React

alright!

Jonny Burger06d7360e949f
diff --git a/packages/video-parser/src/boxes/iso-base-media/moov/moov.ts b/packages/video-parser/src/boxes/iso-base-media/moov/moov.ts
index 7843d3820d..14676f9046 100644
--- a/packages/video-parser/src/boxes/iso-base-media/moov/moov.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/moov/moov.ts
@@ -1,4 +1,5 @@
import type {Box} from '../../../parse-video';
+import {getArrayBufferIterator} from '../../../read-and-increment-offset';
import type {BaseBox} from '../base-type';
import {parseBoxes} from '../process-box';
@@ -8,23 +9,21 @@ export interface MoovBox extends BaseBox {
}
export const parseMoov = (data: ArrayBuffer, offset: number): MoovBox => {
- let chunkOffset = 0;
- const view = new DataView(data);
- const size = view.getUint32(0);
- chunkOffset += 4;
+ const iterator = getArrayBufferIterator(data, 0);
+ const size = iterator.getUint32();
if (size !== data.byteLength) {
throw new Error(`Expected moov size of ${data.byteLength}, got ${size}`);
}
- const type = new TextDecoder().decode(
- data.slice(chunkOffset, chunkOffset + 4),
- );
- chunkOffset += 4;
- if (type !== 'moov') {
- throw new Error(`Expected moov type of moov, got ${type}`);
+ const atom = iterator.getAtom();
+ if (atom !== 'moov') {
+ throw new Error(`Expected moov type of moov, got ${atom}`);
}
- const children = parseBoxes(data.slice(chunkOffset), offset + chunkOffset);
+ const children = parseBoxes(
+ iterator.data.slice(iterator.counter.getOffset()),
+ offset + iterator.counter.getOffset(),
+ );
return {
offset,
diff --git a/packages/video-parser/src/boxes/iso-base-media/stsd/keys.ts b/packages/video-parser/src/boxes/iso-base-media/stsd/keys.ts
index 48790d2207..ad0f034b53 100644
--- a/packages/video-parser/src/boxes/iso-base-media/stsd/keys.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/keys.ts
@@ -1,3 +1,4 @@
+import {getArrayBufferIterator} from '../../../read-and-increment-offset';
import type {BaseBox} from '../base-type';
export interface KeysBox extends BaseBox {
@@ -5,21 +6,15 @@ export interface KeysBox extends BaseBox {
}
export const parseKeys = (data: ArrayBuffer, offset: number): KeysBox => {
- let chunkOffset = 0;
-
- const view = new DataView(data);
- const size = view.getUint32(chunkOffset);
- chunkOffset += 4;
+ const iterator = getArrayBufferIterator(data, 0);
+ const size = iterator.getUint32();
if (size !== data.byteLength) {
throw new Error(`Expected keys size of ${data.byteLength}, got ${size}`);
}
- const type = new TextDecoder().decode(
- data.slice(chunkOffset, chunkOffset + 4),
- );
- chunkOffset += 4;
- if (type !== 'keys') {
- throw new Error(`Expected keys type of keys, got ${type}`);
+ const atom = iterator.getAtom();
+ if (atom !== 'keys') {
+ throw new Error(`Expected keys type of keys, got ${atom}`);
}
return {
diff --git a/packages/video-parser/src/boxes/iso-base-media/stsd/mebx.ts b/packages/video-parser/src/boxes/iso-base-media/stsd/mebx.ts
index 8f849ff632..55a9ef053e 100644
--- a/packages/video-parser/src/boxes/iso-base-media/stsd/mebx.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/mebx.ts
@@ -1,4 +1,5 @@
import type {Box} from '../../../parse-video';
+import {getArrayBufferIterator} from '../../../read-and-increment-offset';
import type {BaseBox} from '../base-type';
import {parseBoxes} from '../process-box';
@@ -10,33 +11,26 @@ export interface MebxBox extends BaseBox {
}
export const parseMebx = (data: ArrayBuffer, offset: number): MebxBox => {
- let chunkOffset = 0;
-
- const view = new DataView(data);
- const size = view.getUint32(chunkOffset);
+ const iterator = getArrayBufferIterator(data, 0);
+ const size = iterator.getUint32();
if (size !== data.byteLength) {
throw new Error(`Expected mebx size of ${data.byteLength}, got ${size}`);
}
- chunkOffset += 4;
- const type = new TextDecoder().decode(
- data.slice(chunkOffset, chunkOffset + 4),
- );
- if (type !== 'mebx') {
- throw new Error(`Expected mebx type of mebx, got ${type}`);
+ const atom = iterator.getAtom();
+ if (atom !== 'mebx') {
+ throw new Error(`Expected mebx type of mebx, got ${atom}`);
}
- chunkOffset += 4;
-
// reserved, 6 bit
- chunkOffset += 6;
+ iterator.discard(6);
- const dataReferenceIndex = view.getUint16(chunkOffset);
- chunkOffset += 2;
+ const dataReferenceIndex = iterator.getUint16();
- const rest = data.slice(chunkOffset);
-
- const children = parseBoxes(rest, offset + chunkOffset);
+ const children = parseBoxes(
+ iterator.data.slice(iterator.counter.getOffset()),
+ offset + iterator.counter.getOffset(),
+ );
return {
type: 'mebx-box',
diff --git a/packages/video-parser/src/boxes/iso-base-media/stsd/samples.ts b/packages/video-parser/src/boxes/iso-base-media/stsd/samples.ts
index c42df62722..4e8dfd5b5b 100644
--- a/packages/video-parser/src/boxes/iso-base-media/stsd/samples.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/samples.ts
@@ -1,3 +1,5 @@
+import {getArrayBufferIterator} from '../../../read-and-increment-offset';
+
type SampleBase = {
format: string;
offset: number;
@@ -114,39 +116,26 @@ export const processSampleAndSubtract = ({
data: ArrayBuffer;
fileOffset: number;
}): SampleAndNext => {
- let chunkOffset = 0;
-
- const view = new DataView(data);
-
- const boxSize = view.getUint32(chunkOffset);
- chunkOffset += 4;
- if (boxSize === 0) {
+ const iterator = getArrayBufferIterator(data, 0);
+ const boxSize = iterator.getUint32();
+ if (boxSize !== data.byteLength) {
throw new Error(`Expected box size of ${data.byteLength}, got ${boxSize}`);
}
- const boxTypeBuffer = data.slice(chunkOffset, chunkOffset + 4);
- chunkOffset += 4;
- const boxType = new TextDecoder().decode(boxTypeBuffer);
- const isVideo = videoTags.includes(boxType);
+ const boxFormat = iterator.getAtom();
+ const isVideo = videoTags.includes(boxFormat);
const isAudio =
- audioTags.includes(boxType) || audioTags.includes(Number(boxTypeBuffer));
+ audioTags.includes(boxFormat) || audioTags.includes(Number(boxFormat));
const next = data.slice(boxSize);
// 6 reserved bytes
- chunkOffset += 6;
-
- const dataReferenceIndex = view.getUint16(chunkOffset);
- chunkOffset += 2;
+ iterator.discard(6);
- const version = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const revisionLevel = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const vendor = data.slice(chunkOffset, chunkOffset + 4);
- chunkOffset += 4;
+ const dataReferenceIndex = iterator.getUint16();
+ const version = iterator.getUint16();
+ const revisionLevel = iterator.getUint16();
+ const vendor = iterator.getSlice(4);
if (!isVideo && !isAudio) {
return {
@@ -158,7 +147,7 @@ export const processSampleAndSubtract = ({
revisionLevel,
vendor: [...Array.from(new Uint8Array(vendor))],
size: boxSize,
- format: boxType,
+ format: boxFormat,
},
next,
size: boxSize,
@@ -170,36 +159,19 @@ export const processSampleAndSubtract = ({
throw new Error(`Unsupported version ${version}`);
}
- const numberOfChannels = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const sampleSize = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const compressionId = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const packetSize = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const sampleRate = view.getUint32(chunkOffset) / 2 ** 16;
- chunkOffset += 4;
-
- const samplesPerPacket = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const bytesPerPacket = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const bytesPerFrame = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const bitsPerSample = view.getUint16(chunkOffset);
- chunkOffset += 2;
+ const numberOfChannels = iterator.getUint16();
+ const sampleSize = iterator.getUint16();
+ const compressionId = iterator.getUint16();
+ const packetSize = iterator.getUint16();
+ const sampleRate = iterator.getFixedPoint1616Number();
+ const samplesPerPacket = iterator.getUint16();
+ const bytesPerPacket = iterator.getUint16();
+ const bytesPerFrame = iterator.getUint16();
+ const bitsPerSample = iterator.getUint16();
return {
sample: {
- format: boxType,
+ format: boxFormat,
offset: fileOffset,
dataReferenceIndex,
version,
@@ -223,44 +195,23 @@ export const processSampleAndSubtract = ({
}
if (isVideo) {
- const temporalQuality = view.getUint32(chunkOffset);
- chunkOffset += 4;
-
- const spacialQuality = view.getUint32(chunkOffset);
- chunkOffset += 4;
-
- const width = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const height = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const horizontalResolution = view.getUint32(chunkOffset) / 2 ** 16;
- chunkOffset += 4;
-
- const verticalResolution = view.getUint32(chunkOffset) / 2 ** 16;
- chunkOffset += 4;
-
- const dataSize = view.getUint32(chunkOffset);
- chunkOffset += 4;
-
- const frameCountPerSample = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const pascalString = data.slice(chunkOffset, chunkOffset + 32);
- chunkOffset += 32;
-
- const depth = view.getUint16(chunkOffset);
- chunkOffset += 2;
-
- const colorTableId = view.getInt16(chunkOffset);
- chunkOffset += 2;
-
- chunkOffset += 4;
+ const temporalQuality = iterator.getUint32();
+ const spacialQuality = iterator.getUint32();
+ const width = iterator.getUint16();
+ const height = iterator.getUint16();
+ const horizontalResolution = iterator.getFixedPoint1616Number();
+ const verticalResolution = iterator.getFixedPoint1616Number();
+ const dataSize = iterator.getUint32();
+ const frameCountPerSample = iterator.getUint16();
+ const compressorName = iterator.getPascalString();
+ const depth = iterator.getUint16();
+ const colorTableId = iterator.getInt16();
+
+ iterator.discard(4);
return {
sample: {
- format: boxType,
+ format: boxFormat,
offset: fileOffset,
dataReferenceIndex,
version,
@@ -276,7 +227,7 @@ export const processSampleAndSubtract = ({
temporalQuality,
dataSize,
frameCountPerSample,
- compressorName: [...Array.from(new Uint8Array(pascalString))],
+ compressorName,
depth,
colorTableId,
},
@@ -285,7 +236,7 @@ export const processSampleAndSubtract = ({
};
}
- throw new Error(`Unknown sample format ${boxType}`);
+ throw new Error(`Unknown sample format ${boxFormat}`);
};
export const parseSamples = (
diff --git a/packages/video-parser/src/boxes/iso-base-media/trak/trak.ts b/packages/video-parser/src/boxes/iso-base-media/trak/trak.ts
index 0461c984ab..578d6b0043 100644
--- a/packages/video-parser/src/boxes/iso-base-media/trak/trak.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/trak/trak.ts
@@ -1,4 +1,5 @@
import type {Box} from '../../../parse-video';
+import {getArrayBufferIterator} from '../../../read-and-increment-offset';
import type {BaseBox} from '../base-type';
import {parseBoxes} from '../process-box';
@@ -8,11 +9,8 @@ export interface TrakBox extends BaseBox {
}
export const parseTrak = (data: ArrayBuffer, offset: number): TrakBox => {
- let chunkOffset = 0;
-
- const view = new DataView(data);
- const size = view.getUint32(chunkOffset);
- chunkOffset += 4;
+ const iterator = getArrayBufferIterator(data, 0);
+ const size = iterator.getUint32();
if (size !== data.byteLength) {
throw new Error(
@@ -20,15 +18,15 @@ export const parseTrak = (data: ArrayBuffer, offset: number): TrakBox => {
);
}
- const atom = data.slice(chunkOffset, chunkOffset + 4);
- const atomString = new TextDecoder().decode(atom);
- if (atomString !== 'trak') {
- throw new Error(`Expected trak atom, got ${atomString}`);
+ const atom = iterator.getAtom();
+ if (atom !== 'trak') {
+ throw new Error(`Expected trak atom, got ${atom}`);
}
- chunkOffset += 4;
-
- const children = parseBoxes(data.slice(chunkOffset), chunkOffset);
+ const children = parseBoxes(
+ data.slice(iterator.counter.getOffset()),
+ offset + iterator.counter.getOffset(),
+ );
return {
offset,
diff --git a/packages/video-parser/src/read-and-increment-offset.ts b/packages/video-parser/src/read-and-increment-offset.ts
index e0460d4df8..1a779c03b5 100644
--- a/packages/video-parser/src/read-and-increment-offset.ts
+++ b/packages/video-parser/src/read-and-increment-offset.ts
@@ -36,6 +36,12 @@ export const getArrayBufferIterator = (
return val;
};
+ const getUint32 = () => {
+ const val = view.getUint32(counter.getOffset());
+ counter.increment(4);
+ return val;
+ };
+
return {
view,
counter,
@@ -68,11 +74,22 @@ export const getArrayBufferIterator = (
counter.increment(2);
return val;
},
- getUint32: () => {
- const val = view.getUint32(counter.getOffset());
- counter.increment(4);
+ getInt16: () => {
+ const val = view.getInt16(counter.getOffset());
+ counter.increment(2);
return val;
},
+ getUint32,
+ // https://developer.apple.com/documentation/quicktime-file-format/sound_sample_description_version_1
+ // A 32-bit unsigned fixed-point number (16.16) that indicates the rate at which the sound samples were obtained.
+ getFixedPoint1616Number: () => {
+ const val = getUint32();
+ return val / 2 ** 16;
+ },
+ getPascalString: () => {
+ const val = getSlice(32);
+ return [...Array.from(new Uint8Array(val))];
+ },
};
};