Skip to content

🎥 Make videos programmatically with React

buffer

Jonny Burgere16b055d32c8
diff --git a/packages/video-parser/src/boxes/iso-base-media/ftype.ts b/packages/video-parser/src/boxes/iso-base-media/ftype.ts
index c1ef7572ac..61db04a039 100644
--- a/packages/video-parser/src/boxes/iso-base-media/ftype.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/ftype.ts
@@ -7,26 +7,28 @@ export interface FtypBox extends BaseBox {
compatibleBrands: string[];
}
-export const fourByteToNumber = (data: Buffer, from: number) => {
+export const fourByteToNumber = (data: ArrayBuffer, from: number) => {
+ const view = new DataView(data);
+
return (
- (data[from + 0] << 24) |
- (data[from + 1] << 16) |
- (data[from + 2] << 8) |
- data[from + 3]
+ (view.getUint8(from + 0) << 24) |
+ (view.getUint8(from + 1) << 16) |
+ (view.getUint8(from + 2) << 8) |
+ view.getUint8(from + 3)
);
};
-export const parseFtyp = (data: Buffer, offset: number): FtypBox => {
- const majorBrand = data.subarray(8, 12).toString('utf-8').trim();
+export const parseFtyp = (data: ArrayBuffer, offset: number): FtypBox => {
+ const majorBrand = new TextDecoder().decode(data.slice(8, 12)).trim();
const minorVersion = fourByteToNumber(data, 12);
- const rest = data.subarray(16);
- const types = rest.length / 4;
+ const rest = data.slice(16);
+ const types = rest.byteLength / 4;
const compatibleBrands: string[] = [];
for (let i = 0; i < types; i++) {
- const fourBytes = rest.subarray(i * 4, i * 4 + 4);
- compatibleBrands.push(fourBytes.toString('utf-8').trim());
+ const fourBytes = rest.slice(i * 4, i * 4 + 4);
+ compatibleBrands.push(new TextDecoder().decode(fourBytes).trim());
}
return {
@@ -35,6 +37,6 @@ export const parseFtyp = (data: Buffer, offset: number): FtypBox => {
minorVersion,
compatibleBrands,
offset,
- boxSize: data.length,
+ boxSize: data.byteLength,
};
};
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 7bd767f7c2..7843d3820d 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
@@ -7,25 +7,28 @@ export interface MoovBox extends BaseBox {
children: Box[];
}
-export const parseMoov = (data: Buffer, offset: number): MoovBox => {
+export const parseMoov = (data: ArrayBuffer, offset: number): MoovBox => {
let chunkOffset = 0;
- const size = data.readUInt32BE(0);
+ const view = new DataView(data);
+ const size = view.getUint32(0);
chunkOffset += 4;
- if (size !== data.length) {
- throw new Error(`Expected moov size of ${data.length}, got ${size}`);
+ if (size !== data.byteLength) {
+ throw new Error(`Expected moov size of ${data.byteLength}, got ${size}`);
}
- const type = data.subarray(chunkOffset, chunkOffset + 4).toString('utf-8');
+ 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 children = parseBoxes(data.subarray(chunkOffset), offset + chunkOffset);
+ const children = parseBoxes(data.slice(chunkOffset), offset + chunkOffset);
return {
offset,
- boxSize: data.length,
+ boxSize: data.byteLength,
type: 'moov-box',
children,
};
diff --git a/packages/video-parser/src/boxes/iso-base-media/mvhd.ts b/packages/video-parser/src/boxes/iso-base-media/mvhd.ts
index 7730daa9aa..344b0400a2 100644
--- a/packages/video-parser/src/boxes/iso-base-media/mvhd.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/mvhd.ts
@@ -26,18 +26,20 @@ export interface MvhdBox extends BaseBox {
type: 'mvhd-box';
}
-export const parseMvhd = (data: Buffer, offset: number): MvhdBox => {
+export const parseMvhd = (data: ArrayBuffer, offset: number): MvhdBox => {
let chunkOffset = 0;
- const size = data.readUInt32BE(chunkOffset);
+ const view = new DataView(data);
+ const size = view.getUint32(chunkOffset);
chunkOffset += 4;
- const atom = data.subarray(chunkOffset, chunkOffset + 4);
- if (atom.toString() !== 'mvhd') {
- throw new Error(`Expected mvhd atom, got ${atom.toString('utf-8')}`);
+ const atom = data.slice(chunkOffset, chunkOffset + 4);
+ const atomString = new TextDecoder().decode(atom);
+ if (atomString !== 'mvhd') {
+ throw new Error(`Expected mvhd atom, got ${atomString}`);
}
chunkOffset += 4;
- const version = data.readUInt8(chunkOffset);
+ const version = view.getUint8(chunkOffset);
if (version !== 0) {
throw new Error(`Unsupported MVHD version ${version}`);
}
@@ -49,30 +51,35 @@ export const parseMvhd = (data: Buffer, offset: number): MvhdBox => {
}
// Flags, we discard them
- data.subarray(chunkOffset, chunkOffset + 3);
+ data.slice(chunkOffset, chunkOffset + 3);
chunkOffset += 3;
- const creationTime = data.readUInt32BE(chunkOffset);
+ const creationTime = view.getUint32(chunkOffset);
chunkOffset += 4;
- const modificationTime = data.readUInt32BE(chunkOffset);
+ const modificationTime = view.getUint32(chunkOffset);
chunkOffset += 4;
- const timeScale = data.readUInt32BE(chunkOffset);
+ const timeScale = view.getUint32(chunkOffset);
chunkOffset += 4;
- const durationInUnits = data.readUInt32BE(chunkOffset);
+ const durationInUnits = view.getUint32(chunkOffset);
const durationInSeconds = durationInUnits / timeScale;
chunkOffset += 4;
- const rateArray = data.subarray(chunkOffset, chunkOffset + 4);
+ const rateArray = data.slice(chunkOffset, chunkOffset + 4);
+ const rateView = new DataView(rateArray);
const rate =
- rateArray[0] * 10 + rateArray[1] + rateArray[2] * 0.1 + rateArray[3] * 0.01;
+ rateView.getInt8(0) * 10 +
+ rateView.getInt8(1) +
+ rateView.getInt8(2) * 0.1 +
+ rateView.getInt8(3) * 0.01;
chunkOffset += 4;
- const volumeArray = data.subarray(chunkOffset, chunkOffset + 2);
+ const volumeArray = data.slice(chunkOffset, chunkOffset + 2);
+ const volumeView = new DataView(volumeArray);
chunkOffset += 2;
- const volume = volumeArray[0] + volumeArray[1] * 0.1;
+ const volume = volumeView.getInt8(0) + volumeView.getInt8(1) * 0.1;
// reserved 16bit
chunkOffset += 2;
@@ -83,7 +90,7 @@ export const parseMvhd = (data: Buffer, offset: number): MvhdBox => {
// matrix
const matrix: number[] = [];
for (let i = 0; i < 9; i++) {
- matrix.push(data.readUInt32BE(chunkOffset));
+ matrix.push(view.getUint32(chunkOffset));
chunkOffset += 4;
}
@@ -91,7 +98,7 @@ export const parseMvhd = (data: Buffer, offset: number): MvhdBox => {
chunkOffset += 4 * 6;
// next track id
- const nextTrackId = data.readUInt32BE(chunkOffset);
+ const nextTrackId = view.getUint32(chunkOffset);
chunkOffset += 4;
return {
@@ -105,7 +112,7 @@ export const parseMvhd = (data: Buffer, offset: number): MvhdBox => {
matrix: matrix as ThreeDMatrix,
nextTrackId,
type: 'mvhd-box',
- boxSize: data.length,
+ boxSize: data.byteLength,
offset,
};
};
diff --git a/packages/video-parser/src/boxes/iso-base-media/process-box.ts b/packages/video-parser/src/boxes/iso-base-media/process-box.ts
index e210267414..b74020f08d 100644
--- a/packages/video-parser/src/boxes/iso-base-media/process-box.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/process-box.ts
@@ -11,20 +11,20 @@ const processBoxAndSubtract = ({
data,
fileOffset,
}: {
- data: Buffer;
+ data: ArrayBuffer;
fileOffset: number;
}): BoxAndNext => {
const boxSize = fourByteToNumber(data, 0);
if (boxSize === 0) {
- throw new Error(`Expected box size of ${data.length}, got ${boxSize}`);
+ throw new Error(`Expected box size of ${data.byteLength}, got ${boxSize}`);
}
- const boxTypeBuffer = data.subarray(4, 8);
+ const boxTypeBuffer = data.slice(4, 8);
- const boxType = boxTypeBuffer.toString('utf-8');
+ const boxType = new TextDecoder().decode(boxTypeBuffer);
- const sub = data.subarray(0, boxSize);
- const next = data.subarray(boxSize);
+ const sub = data.slice(0, boxSize);
+ const next = data.slice(boxSize);
if (boxType === 'ftyp') {
return {
@@ -82,7 +82,7 @@ const processBoxAndSubtract = ({
};
}
- const childArray = sub.subarray(8, boxSize);
+ const childArray = sub.slice(8, boxSize);
const children =
boxType === 'mdia' ||
@@ -106,12 +106,12 @@ const processBoxAndSubtract = ({
};
};
-export const parseBoxes = (data: Buffer, fileOffset: number): Box[] => {
+export const parseBoxes = (data: ArrayBuffer, fileOffset: number): Box[] => {
const boxes: Box[] = [];
let remaining = data;
let bytesConsumed = fileOffset;
- while (remaining.length > 0) {
+ while (remaining.byteLength > 0) {
const {next, box, size} = processBoxAndSubtract({
data: remaining,
fileOffset: bytesConsumed,
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 9ae6d27c57..48790d2207 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
@@ -4,16 +4,19 @@ export interface KeysBox extends BaseBox {
type: 'keys-box';
}
-export const parseKeys = (data: Buffer, offset: number): KeysBox => {
+export const parseKeys = (data: ArrayBuffer, offset: number): KeysBox => {
let chunkOffset = 0;
- const size = data.readUInt32BE(chunkOffset);
+ const view = new DataView(data);
+ const size = view.getUint32(chunkOffset);
chunkOffset += 4;
- if (size !== data.length) {
- throw new Error(`Expected keys size of ${data.length}, got ${size}`);
+ if (size !== data.byteLength) {
+ throw new Error(`Expected keys size of ${data.byteLength}, got ${size}`);
}
- const type = data.subarray(chunkOffset, chunkOffset + 4).toString('utf-8');
+ 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}`);
@@ -21,7 +24,7 @@ export const parseKeys = (data: Buffer, offset: number): KeysBox => {
return {
type: 'keys-box',
- boxSize: data.length,
+ boxSize: data.byteLength,
offset,
};
};
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 16731e11a9..8f849ff632 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
@@ -9,16 +9,19 @@ export interface MebxBox extends BaseBox {
children: Box[];
}
-export const parseMebx = (data: Buffer, offset: number): MebxBox => {
+export const parseMebx = (data: ArrayBuffer, offset: number): MebxBox => {
let chunkOffset = 0;
- const size = data.readUInt32BE(chunkOffset);
- if (size !== data.length) {
- throw new Error(`Expected mebx size of ${data.length}, got ${size}`);
+ const view = new DataView(data);
+ const size = view.getUint32(chunkOffset);
+ if (size !== data.byteLength) {
+ throw new Error(`Expected mebx size of ${data.byteLength}, got ${size}`);
}
chunkOffset += 4;
- const type = data.subarray(chunkOffset, chunkOffset + 4).toString('utf-8');
+ const type = new TextDecoder().decode(
+ data.slice(chunkOffset, chunkOffset + 4),
+ );
if (type !== 'mebx') {
throw new Error(`Expected mebx type of mebx, got ${type}`);
}
@@ -28,16 +31,16 @@ export const parseMebx = (data: Buffer, offset: number): MebxBox => {
// reserved, 6 bit
chunkOffset += 6;
- const dataReferenceIndex = data.readUInt16BE(chunkOffset);
+ const dataReferenceIndex = view.getUint16(chunkOffset);
chunkOffset += 2;
- const rest = data.subarray(chunkOffset);
+ const rest = data.slice(chunkOffset);
const children = parseBoxes(rest, offset + chunkOffset);
return {
type: 'mebx-box',
- boxSize: data.length,
+ boxSize: data.byteLength,
offset,
dataReferenceIndex,
format: 'mebx',
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 7fd053bb56..c42df62722 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
@@ -44,7 +44,7 @@ export type Sample = AudioSample | VideoSample | UnknownSample;
type SampleAndNext = {
sample: Sample | null;
- next: Buffer;
+ next: ArrayBuffer;
size: number;
};
@@ -111,39 +111,41 @@ export const processSampleAndSubtract = ({
data,
fileOffset,
}: {
- data: Buffer;
+ data: ArrayBuffer;
fileOffset: number;
}): SampleAndNext => {
let chunkOffset = 0;
- const boxSize = data.readUint32BE(chunkOffset);
+ const view = new DataView(data);
+
+ const boxSize = view.getUint32(chunkOffset);
chunkOffset += 4;
if (boxSize === 0) {
- throw new Error(`Expected box size of ${data.length}, got ${boxSize}`);
+ throw new Error(`Expected box size of ${data.byteLength}, got ${boxSize}`);
}
- const boxTypeBuffer = data.subarray(chunkOffset, chunkOffset + 4);
+ const boxTypeBuffer = data.slice(chunkOffset, chunkOffset + 4);
chunkOffset += 4;
- const boxType = boxTypeBuffer.toString('utf-8');
+ const boxType = new TextDecoder().decode(boxTypeBuffer);
const isVideo = videoTags.includes(boxType);
const isAudio =
audioTags.includes(boxType) || audioTags.includes(Number(boxTypeBuffer));
- const next = data.subarray(boxSize);
+ const next = data.slice(boxSize);
// 6 reserved bytes
chunkOffset += 6;
- const dataReferenceIndex = data.readUint16BE(chunkOffset);
+ const dataReferenceIndex = view.getUint16(chunkOffset);
chunkOffset += 2;
- const version = data.readInt16BE(chunkOffset);
+ const version = view.getUint16(chunkOffset);
chunkOffset += 2;
- const revisionLevel = data.readUint16BE(chunkOffset);
+ const revisionLevel = view.getUint16(chunkOffset);
chunkOffset += 2;
- const vendor = data.subarray(chunkOffset, chunkOffset + 4);
+ const vendor = data.slice(chunkOffset, chunkOffset + 4);
chunkOffset += 4;
if (!isVideo && !isAudio) {
@@ -154,7 +156,7 @@ export const processSampleAndSubtract = ({
dataReferenceIndex,
version,
revisionLevel,
- vendor: [...vendor],
+ vendor: [...Array.from(new Uint8Array(vendor))],
size: boxSize,
format: boxType,
},
@@ -168,31 +170,31 @@ export const processSampleAndSubtract = ({
throw new Error(`Unsupported version ${version}`);
}
- const numberOfChannels = data.readUint16BE(chunkOffset);
+ const numberOfChannels = view.getUint16(chunkOffset);
chunkOffset += 2;
- const sampleSize = data.readUint16BE(chunkOffset);
+ const sampleSize = view.getUint16(chunkOffset);
chunkOffset += 2;
- const compressionId = data.readUint16BE(chunkOffset);
+ const compressionId = view.getUint16(chunkOffset);
chunkOffset += 2;
- const packetSize = data.readUint16BE(chunkOffset);
+ const packetSize = view.getUint16(chunkOffset);
chunkOffset += 2;
- const sampleRate = data.readUint32BE(chunkOffset) / 2 ** 16;
+ const sampleRate = view.getUint32(chunkOffset) / 2 ** 16;
chunkOffset += 4;
- const samplesPerPacket = data.readUint16BE(chunkOffset);
+ const samplesPerPacket = view.getUint16(chunkOffset);
chunkOffset += 2;
- const bytesPerPacket = data.readUint16BE(chunkOffset);
+ const bytesPerPacket = view.getUint16(chunkOffset);
chunkOffset += 2;
- const bytesPerFrame = data.readUint16BE(chunkOffset);
+ const bytesPerFrame = view.getUint16(chunkOffset);
chunkOffset += 2;
- const bitsPerSample = data.readUint16BE(chunkOffset);
+ const bitsPerSample = view.getUint16(chunkOffset);
chunkOffset += 2;
return {
@@ -202,7 +204,7 @@ export const processSampleAndSubtract = ({
dataReferenceIndex,
version,
revisionLevel,
- vendor: [...vendor],
+ vendor: [...Array.from(new Uint8Array(vendor))],
size: boxSize,
type: 'audio',
numberOfChannels,
@@ -221,37 +223,37 @@ export const processSampleAndSubtract = ({
}
if (isVideo) {
- const temporalQuality = data.readUint32BE(chunkOffset);
+ const temporalQuality = view.getUint32(chunkOffset);
chunkOffset += 4;
- const spacialQuality = data.readUint32BE(chunkOffset);
+ const spacialQuality = view.getUint32(chunkOffset);
chunkOffset += 4;
- const width = data.readUint16BE(chunkOffset);
+ const width = view.getUint16(chunkOffset);
chunkOffset += 2;
- const height = data.readUint16BE(chunkOffset);
+ const height = view.getUint16(chunkOffset);
chunkOffset += 2;
- const horizontalResolution = data.readUint32BE(chunkOffset) / 2 ** 16;
+ const horizontalResolution = view.getUint32(chunkOffset) / 2 ** 16;
chunkOffset += 4;
- const verticalResolution = data.readUint32BE(chunkOffset) / 2 ** 16;
+ const verticalResolution = view.getUint32(chunkOffset) / 2 ** 16;
chunkOffset += 4;
- const dataSize = data.readUint32BE(chunkOffset);
+ const dataSize = view.getUint32(chunkOffset);
chunkOffset += 4;
- const frameCountPerSample = data.readUint16BE(chunkOffset);
+ const frameCountPerSample = view.getUint16(chunkOffset);
chunkOffset += 2;
- const pascalString = data.subarray(chunkOffset, chunkOffset + 32);
+ const pascalString = data.slice(chunkOffset, chunkOffset + 32);
chunkOffset += 32;
- const depth = data.readUint16BE(chunkOffset);
+ const depth = view.getUint16(chunkOffset);
chunkOffset += 2;
- const colorTableId = data.readInt16BE(chunkOffset);
+ const colorTableId = view.getInt16(chunkOffset);
chunkOffset += 2;
chunkOffset += 4;
@@ -263,7 +265,7 @@ export const processSampleAndSubtract = ({
dataReferenceIndex,
version,
revisionLevel,
- vendor: [...vendor],
+ vendor: [...Array.from(new Uint8Array(vendor))],
size: boxSize,
type: 'video',
width,
@@ -274,7 +276,7 @@ export const processSampleAndSubtract = ({
temporalQuality,
dataSize,
frameCountPerSample,
- compressorName: [...pascalString],
+ compressorName: [...Array.from(new Uint8Array(pascalString))],
depth,
colorTableId,
},
@@ -286,12 +288,15 @@ export const processSampleAndSubtract = ({
throw new Error(`Unknown sample format ${boxType}`);
};
-export const parseSamples = (data: Buffer, fileOffset: number): Sample[] => {
+export const parseSamples = (
+ data: ArrayBuffer,
+ fileOffset: number,
+): Sample[] => {
const samples: Sample[] = [];
let remaining = data;
let bytesConsumed = fileOffset;
- while (remaining.length > 0) {
+ while (remaining.byteLength > 0) {
const {next, sample, size} = processSampleAndSubtract({
data: remaining,
fileOffset: bytesConsumed,
diff --git a/packages/video-parser/src/boxes/iso-base-media/stsd/stsd.ts b/packages/video-parser/src/boxes/iso-base-media/stsd/stsd.ts
index e98d1492f3..057a9ff772 100644
--- a/packages/video-parser/src/boxes/iso-base-media/stsd/stsd.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/stsd.ts
@@ -8,37 +8,41 @@ export interface StsdBox extends BaseBox {
samples: Sample[];
}
-export const parseStsd = (data: Buffer, offset: number): StsdBox => {
+export const parseStsd = (data: ArrayBuffer, offset: number): StsdBox => {
let chunkOffset = 0;
- const size = data.readUInt32BE(chunkOffset);
- if (size !== data.length) {
- throw new Error(`Expected stsd size of ${data.length}, got ${size}`);
+ const view = new DataView(data);
+
+ const size = view.getUint32(chunkOffset);
+ if (size !== data.byteLength) {
+ throw new Error(`Expected stsd size of ${data.byteLength}, got ${size}`);
}
chunkOffset += 4;
- const type = data.subarray(chunkOffset, chunkOffset + 4).toString('utf-8');
+ const type = new TextDecoder().decode(
+ data.slice(chunkOffset, chunkOffset + 4),
+ );
if (type !== 'stsd') {
throw new Error(`Expected stsd type of stsd, got ${type}`);
}
chunkOffset += 4;
- const version = data.readUInt8(chunkOffset);
+ const version = view.getUint8(chunkOffset);
chunkOffset += 1;
if (version !== 0) {
throw new Error(`Unsupported STSD version ${version}`);
}
// flags, we discard them
- data.subarray(chunkOffset, chunkOffset + 3);
+ data.slice(chunkOffset, chunkOffset + 3);
chunkOffset += 3;
- const numberOfEntries = data.readUInt32BE(chunkOffset);
+ const numberOfEntries = view.getUint32(chunkOffset);
chunkOffset += 4;
- const boxes = parseSamples(data.subarray(chunkOffset), offset + chunkOffset);
+ const boxes = parseSamples(data.slice(chunkOffset), offset + chunkOffset);
if (boxes.length !== numberOfEntries) {
throw new Error(
@@ -48,7 +52,7 @@ export const parseStsd = (data: Buffer, offset: number): StsdBox => {
return {
type: 'stsd-box',
- boxSize: data.length,
+ boxSize: data.byteLength,
offset,
numberOfEntries,
samples: boxes,
diff --git a/packages/video-parser/src/boxes/iso-base-media/tkhd.ts b/packages/video-parser/src/boxes/iso-base-media/tkhd.ts
index de8ecadbb1..d9e08a2b54 100644
--- a/packages/video-parser/src/boxes/iso-base-media/tkhd.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/tkhd.ts
@@ -17,18 +17,21 @@ export interface TkhdBox extends BaseBox {
height: number;
}
-export const parseTkhd = (data: Buffer, fileOffset: number): TkhdBox => {
+export const parseTkhd = (data: ArrayBuffer, fileOffset: number): TkhdBox => {
let chunkOffset = 0;
- const size = data.readUInt32BE(chunkOffset);
+ const view = new DataView(data);
+
+ const size = view.getUint32(chunkOffset);
chunkOffset += 4;
- const atom = data.subarray(chunkOffset, chunkOffset + 4);
- if (atom.toString() !== 'tkhd') {
- throw new Error(`Expected tkhd atom, got ${atom.toString('utf-8')}`);
+ const atom = data.slice(chunkOffset, chunkOffset + 4);
+ const atomAsString = new TextDecoder().decode(atom);
+ if (atomAsString !== 'tkhd') {
+ throw new Error(`Expected tkhd atom, got ${atomAsString}`);
}
chunkOffset += 4;
- const version = data.readUInt8(chunkOffset);
+ const version = view.getUint8(chunkOffset);
if (version !== 0) {
throw new Error(`Unsupported TKHD version ${version}`);
}
@@ -40,22 +43,22 @@ export const parseTkhd = (data: Buffer, fileOffset: number): TkhdBox => {
}
// Flags, we discard them
- data.subarray(chunkOffset, chunkOffset + 3);
+ data.slice(chunkOffset, chunkOffset + 3);
chunkOffset += 3;
- const creationTime = data.readUInt32BE(chunkOffset);
+ const creationTime = view.getUint32(chunkOffset);
chunkOffset += 4;
- const modificationTime = data.readUInt32BE(chunkOffset);
+ const modificationTime = view.getUint32(chunkOffset);
chunkOffset += 4;
- const trackId = data.readUInt32BE(chunkOffset);
+ const trackId = view.getUint32(chunkOffset);
chunkOffset += 4;
// reserved
chunkOffset += 4;
- const duration = data.readUInt32BE(chunkOffset);
+ const duration = view.getUint32(chunkOffset);
chunkOffset += 4;
// reserved 2
@@ -64,41 +67,41 @@ export const parseTkhd = (data: Buffer, fileOffset: number): TkhdBox => {
// reserved 3
chunkOffset += 4;
- const layer = data.readUInt16BE(chunkOffset);
+ const layer = view.getUint16(chunkOffset);
chunkOffset += 2;
- const alternateGroup = data.readUInt16BE(chunkOffset);
+ const alternateGroup = view.getUint16(chunkOffset);
chunkOffset += 2;
- const volume = data.readUInt16BE(chunkOffset);
+ const volume = view.getUint16(chunkOffset);
chunkOffset += 2;
// reserved 4
chunkOffset += 2;
const matrix = [
- data.readUInt32BE(chunkOffset),
- data.readUInt32BE(chunkOffset + 4),
- data.readUInt32BE(chunkOffset + 8),
- data.readUInt32BE(chunkOffset + 12),
- data.readUInt32BE(chunkOffset + 16),
- data.readUInt32BE(chunkOffset + 20),
- data.readUInt32BE(chunkOffset + 24),
- data.readUInt32BE(chunkOffset + 28),
- data.readUInt32BE(chunkOffset + 32),
+ view.getUint32(chunkOffset),
+ view.getUint32(chunkOffset + 4),
+ view.getUint32(chunkOffset + 8),
+ view.getUint32(chunkOffset + 12),
+ view.getUint32(chunkOffset + 16),
+ view.getUint32(chunkOffset + 20),
+ view.getUint32(chunkOffset + 24),
+ view.getUint32(chunkOffset + 28),
+ view.getUint32(chunkOffset + 32),
];
chunkOffset += 36;
- const width = data.readUInt32BE(chunkOffset);
+ const width = view.getUint32(chunkOffset);
chunkOffset += 4;
- const height = data.readUInt32BE(chunkOffset);
+ const height = view.getUint32(chunkOffset);
chunkOffset += 4;
return {
offset: fileOffset,
- boxSize: data.length,
+ boxSize: data.byteLength,
type: 'tkhd-box',
creationTime: toUnixTimestamp(creationTime),
modificationTime: toUnixTimestamp(modificationTime),
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 5f2d8fc07c..0461c984ab 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
@@ -7,28 +7,32 @@ export interface TrakBox extends BaseBox {
children: Box[];
}
-export const parseTrak = (data: Buffer, offset: number): TrakBox => {
+export const parseTrak = (data: ArrayBuffer, offset: number): TrakBox => {
let chunkOffset = 0;
- const size = data.readUInt32BE(chunkOffset);
+ const view = new DataView(data);
+ const size = view.getUint32(chunkOffset);
chunkOffset += 4;
- if (size !== data.length) {
- throw new Error(`Data size of version 0 is ${size}, got ${data.length}`);
+ if (size !== data.byteLength) {
+ throw new Error(
+ `Data size of version 0 is ${size}, got ${data.byteLength}`,
+ );
}
- const atom = data.subarray(chunkOffset, chunkOffset + 4);
- if (atom.toString() !== 'trak') {
- throw new Error(`Expected trak atom, got ${atom.toString('utf-8')}`);
+ const atom = data.slice(chunkOffset, chunkOffset + 4);
+ const atomString = new TextDecoder().decode(atom);
+ if (atomString !== 'trak') {
+ throw new Error(`Expected trak atom, got ${atomString}`);
}
chunkOffset += 4;
- const children = parseBoxes(data.subarray(chunkOffset), chunkOffset);
+ const children = parseBoxes(data.slice(chunkOffset), chunkOffset);
return {
offset,
- boxSize: data.length,
+ boxSize: data.byteLength,
type: 'trak-box',
children,
};
diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.ts
index d5558b74ab..6f14243d1a 100644
--- a/packages/video-parser/src/parse-video.ts
+++ b/packages/video-parser/src/parse-video.ts
@@ -1,6 +1,5 @@
import {createReadStream} from 'fs';
import type {BaseBox} from './boxes/iso-base-media/base-type';
-import type {DimensionsBox} from './boxes/iso-base-media/dims';
import type {FtypBox} from './boxes/iso-base-media/ftype';
import type {MoovBox} from './boxes/iso-base-media/moov/moov';
import type {MvhdBox} from './boxes/iso-base-media/mvhd';
@@ -24,7 +23,6 @@ export type Box =
| FtypBox
| MvhdBox
| TkhdBox
- | DimensionsBox
| StsdBox
| MebxBox
| KeysBox
@@ -33,7 +31,7 @@ export type Box =
export type BoxAndNext = {
box: Box;
- next: Buffer;
+ next: ArrayBuffer;
size: number;
};
@@ -70,7 +68,7 @@ export const parseVideo = async (
});
if (matchesPattern(isoBaseMediaMp4Pattern)(data.subarray(4, 8))) {
- return parseBoxes(data, 0);
+ return parseBoxes(new Uint8Array(data).buffer as unknown as ArrayBuffer, 0);
}
return [];
diff --git a/packages/video-parser/src/test/keys.test.ts b/packages/video-parser/src/test/keys.test.ts
index cebcc6fd29..150ab621cb 100644
--- a/packages/video-parser/src/test/keys.test.ts
+++ b/packages/video-parser/src/test/keys.test.ts
@@ -2,7 +2,7 @@ import {expect, test} from 'bun:test';
import {parseKeys} from '../boxes/iso-base-media/stsd/keys';
test('Should parse keyd box', () => {
- const buf = Buffer.from([
+ const {buffer} = Uint8Array.from([
0, 0, 2, 60, 107, 101, 121, 115, 0, 0, 2, 52, 0, 0, 0, 1, 0, 0, 0, 47, 107,
101, 121, 100, 109, 100, 116, 97, 99, 111, 109, 46, 97, 112, 112, 108, 101,
46, 113, 117, 105, 99, 107, 116, 105, 109, 101, 46, 108, 105, 118, 101, 45,
@@ -37,7 +37,7 @@ test('Should parse keyd box', () => {
116, 112, 115, 0, 0, 0, 16, 100, 116, 121, 112, 0, 0, 0, 0, 0, 0, 0, 0,
]);
- const data = parseKeys(buf, 0);
+ const data = parseKeys(buffer as unknown as ArrayBuffer, 0);
expect(data).toEqual({
type: 'keys-box',
diff --git a/packages/video-parser/src/test/mvhd.test.ts b/packages/video-parser/src/test/mvhd.test.ts
index 7c12ffab2f..0bac95469d 100644
--- a/packages/video-parser/src/test/mvhd.test.ts
+++ b/packages/video-parser/src/test/mvhd.test.ts
@@ -2,7 +2,7 @@ import {expect, test} from 'bun:test';
import {parseMvhd} from '../boxes/iso-base-media/mvhd';
test('Should be able to parse a MVHD box correctly', () => {
- const buffer = Buffer.from([
+ const {buffer} = Uint8Array.from([
// size, 32 bit
0, 0, 0, 108,
// mvhd atom, 32 bit
@@ -63,7 +63,7 @@ test('Should be able to parse a MVHD box correctly', () => {
0, 0, 0, 2,
]);
- const mvhd = parseMvhd(buffer, 0);
+ const mvhd = parseMvhd(buffer as unknown as ArrayBuffer, 0);
expect(mvhd).toEqual({
creationTime: null,
modificationTime: null,
diff --git a/packages/video-parser/src/test/stsd.test.ts b/packages/video-parser/src/test/stsd.test.ts
index 393c3ba69e..ae08909487 100644
--- a/packages/video-parser/src/test/stsd.test.ts
+++ b/packages/video-parser/src/test/stsd.test.ts
@@ -3,7 +3,7 @@ import {processSampleAndSubtract} from '../boxes/iso-base-media/stsd/samples';
import {parseStsd} from '../boxes/iso-base-media/stsd/stsd';
test('Should be able to parse a STSD audio box correctly', () => {
- const buffer = Buffer.from([
+ const {buffer} = Uint8Array.from([
// box size
0, 0, 0, 159,
// data format "stsd"
@@ -33,7 +33,7 @@ test('Should be able to parse a STSD audio box correctly', () => {
2, 18, 16, 6, 128, 128, 128, 1, 2, 0, 0, 0, 8, 0, 0, 0, 0,
]);
- const parsed = parseStsd(buffer, 0);
+ const parsed = parseStsd(buffer as unknown as ArrayBuffer, 0);
expect(parsed).toEqual({
offset: 0,
@@ -65,7 +65,7 @@ test('Should be able to parse a STSD audio box correctly', () => {
});
test('Should be able to parse a STSD video box correctly', () => {
- const buffer = Buffer.from([
+ const {buffer} = Uint8Array.from([
// box size
0, 0, 0, 158,
// data format "avc1"
@@ -124,7 +124,7 @@ test('Should be able to parse a STSD video box correctly', () => {
0, 0, 0, 16, 112, 97, 115, 112, 0, 0, 0, 1, 0, 0, 0, 1,
]);
- const parsed = processSampleAndSubtract({data: buffer, fileOffset: 0});
+ const parsed = processSampleAndSubtract({data: buffer as unknown as ArrayBuffer, fileOffset: 0});
expect(parsed.sample).toEqual({
size: 158,
format: 'avc1',
diff --git a/packages/video-parser/src/test/tkhd.test.ts b/packages/video-parser/src/test/tkhd.test.ts
index cd7d8bc029..d4453a0284 100644
--- a/packages/video-parser/src/test/tkhd.test.ts
+++ b/packages/video-parser/src/test/tkhd.test.ts
@@ -2,7 +2,7 @@ import {expect, test} from 'bun:test';
import {parseTkhd} from '../boxes/iso-base-media/tkhd';
test('Should be able to parse a TKHD box', () => {
- const buffer = Buffer.from([
+ const {buffer} = Uint8Array.from([
// size, 32 bit
0, 0, 0, 92,
// mvhd atom, 32 bit
@@ -57,7 +57,7 @@ test('Should be able to parse a TKHD box', () => {
0, 0, 0, 0,
]);
- const mvhd = parseTkhd(buffer, 0);
+ const mvhd = parseTkhd(buffer as unknown as ArrayBuffer, 0);
expect(mvhd).toEqual({
type: 'tkhd-box',
boxSize: 92,