Skip to content

🎥 Make videos programmatically with React

`@remotion/media-parser`: Support `largesize` and `co64` conventions

Jonny Burger7bd78287f95e
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 ba8cc6b0f5..0d7eed364b 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
@@ -26,7 +26,7 @@ export const parseMdat = async ({
}): Promise<MdatBox> => {
const alreadyHas = hasTracks(existingBoxes);
if (!alreadyHas) {
- data.discard(size - 8);
+ data.discard(size - data.counter.getOffset() - fileOffset);
return Promise.resolve({
type: 'mdat-box',
boxSize: size,
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 42e5d80973..29d56faf83 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
@@ -135,8 +135,26 @@ export const processBox = async ({
const fileOffset = iterator.counter.getOffset();
const bytesRemaining = iterator.bytesRemaining();
- const boxSize = iterator.getFourByteNumber(littleEndian);
- if (boxSize === 0) {
+ const boxSizeRaw = iterator.getFourByteNumber(littleEndian);
+
+ // If `boxSize === 1`, the 8 bytes after the box type are the size of the box.
+ if (
+ (boxSizeRaw === 1 && iterator.bytesRemaining() < 12) ||
+ iterator.bytesRemaining() < 4
+ ) {
+ iterator.counter.decrement(iterator.counter.getOffset() - fileOffset);
+ if (allowIncompleteBoxes) {
+ return {
+ type: 'incomplete',
+ };
+ }
+
+ throw new Error(
+ `Expected box size of ${bytesRemaining}, got ${boxSizeRaw}. Incomplete boxes are not allowed.`,
+ );
+ }
+
+ if (boxSizeRaw === 0) {
return {
type: 'complete',
box: {
@@ -148,42 +166,41 @@ export const processBox = async ({
};
}
+ const boxType = iterator.getByteString(4);
+
+ const boxSize =
+ boxSizeRaw === 1 ? iterator.getEightByteNumber(littleEndian) : boxSizeRaw;
+
if (bytesRemaining < boxSize) {
- if (bytesRemaining >= 4) {
- const type = iterator.getByteString(4);
- iterator.counter.decrement(4);
-
- if (type === 'mdat') {
- const shouldSkip = options.canSkipVideoData || !hasTracks(parsedBoxes);
-
- if (shouldSkip) {
- const skipTo = fileOffset + boxSize;
- const bytesToSkip = skipTo - iterator.counter.getOffset();
-
- // If there is a huge mdat chunk, we can skip it because we don't need it for the metadata
- if (bytesToSkip > 1_000_000) {
- return {
- type: 'complete',
- box: {
- type: 'mdat-box',
- boxSize,
- fileOffset,
- samplesProcessed: false,
- },
- size: boxSize,
- skipTo: fileOffset + boxSize,
- };
- }
- } else {
- iterator.discard(4);
- return parseMdatPartially({
- iterator,
- boxSize,
- fileOffset,
- parsedBoxes,
- options,
- });
+ if (boxType === 'mdat') {
+ const shouldSkip = options.canSkipVideoData || !hasTracks(parsedBoxes);
+
+ if (shouldSkip) {
+ const skipTo = fileOffset + boxSize;
+ const bytesToSkip = skipTo - iterator.counter.getOffset();
+
+ // If there is a huge mdat chunk, we can skip it because we don't need it for the metadata
+ if (bytesToSkip > 1_000_000) {
+ return {
+ type: 'complete',
+ box: {
+ type: 'mdat-box',
+ boxSize,
+ fileOffset,
+ samplesProcessed: false,
+ },
+ size: boxSize,
+ skipTo: fileOffset + boxSize,
+ };
}
+ } else {
+ return parseMdatPartially({
+ iterator,
+ boxSize,
+ fileOffset,
+ parsedBoxes,
+ options,
+ });
}
}
@@ -199,8 +216,6 @@ export const processBox = async ({
);
}
- const boxType = iterator.getByteString(4);
-
if (boxType === 'ftyp') {
const box = parseFtyp({iterator, size: boxSize, offset: fileOffset});
return {
@@ -276,11 +291,12 @@ export const processBox = async ({
};
}
- if (boxType === 'stco') {
+ if (boxType === 'stco' || boxType === 'co64') {
const box = parseStco({
iterator,
offset: fileOffset,
size: boxSize,
+ mode64Bit: boxType === 'co64',
});
return {
diff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/stco.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/stco.ts
index 729dad1188..294d56bae3 100644
--- a/packages/media-parser/src/boxes/iso-base-media/stsd/stco.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/stco.ts
@@ -6,17 +6,19 @@ export interface StcoBox extends BaseBox {
version: number;
flags: number[];
entryCount: number;
- entries: number[];
+ entries: (number | bigint)[];
}
export const parseStco = ({
iterator,
offset,
size,
+ mode64Bit,
}: {
iterator: BufferIterator;
offset: number;
size: number;
+ mode64Bit: boolean;
}): StcoBox => {
const version = iterator.getUint8();
if (version !== 0) {
@@ -26,14 +28,14 @@ export const parseStco = ({
const flags = iterator.getSlice(3);
const entryCount = iterator.getUint32();
- const entries: number[] = [];
+ const entries: (number | bigint)[] = [];
for (let i = 0; i < entryCount; i++) {
const bytesRemaining = size - (iterator.counter.getOffset() - offset);
if (bytesRemaining < 4) {
break;
}
- entries.push(iterator.getUint32());
+ entries.push(mode64Bit ? iterator.getUint64() : iterator.getUint32());
}
iterator.discard(size - (iterator.counter.getOffset() - offset));
diff --git a/packages/media-parser/src/buffer-iterator.ts b/packages/media-parser/src/buffer-iterator.ts
index a5cb4b3dad..1ab75e6a49 100644
--- a/packages/media-parser/src/buffer-iterator.ts
+++ b/packages/media-parser/src/buffer-iterator.ts
@@ -96,6 +96,41 @@ export const getArrayBufferIterator = (
return val;
};
+ const getEightByteNumber = (littleEndian = false) => {
+ if (littleEndian) {
+ const one = getUint8();
+ const two = getUint8();
+ const three = getUint8();
+ const four = getUint8();
+ const five = getUint8();
+ const six = getUint8();
+ const seven = getUint8();
+ const eight = getUint8();
+
+ return (
+ (eight << 56) |
+ (seven << 48) |
+ (six << 40) |
+ (five << 32) |
+ (four << 24) |
+ (three << 16) |
+ (two << 8) |
+ one
+ );
+ }
+
+ return (
+ (getUint8() << 56) |
+ (getUint8() << 48) |
+ (getUint8() << 40) |
+ (getUint8() << 32) |
+ (getUint8() << 24) |
+ (getUint8() << 16) |
+ (getUint8() << 8) |
+ getUint8()
+ );
+ };
+
const getFourByteNumber = (littleEndian = false) => {
if (littleEndian) {
const one = getUint8();
@@ -125,6 +160,12 @@ export const getArrayBufferIterator = (
return val;
};
+ const getUint64 = (littleEndian = false) => {
+ const val = view.getBigUint64(counter.getDiscardedOffset(), littleEndian);
+ counter.increment(8);
+ return val;
+ };
+
const getUint32Le = () => {
const val = view.getUint32(counter.getDiscardedOffset(), true);
counter.increment(4);
@@ -294,6 +335,7 @@ export const getArrayBufferIterator = (
discard: (length: number) => {
counter.increment(length);
},
+ getEightByteNumber,
getFourByteNumber,
getSlice,
getAtom: () => {
@@ -416,6 +458,7 @@ export const getArrayBufferIterator = (
return val;
},
getUint32,
+ getUint64,
// 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.
getFixedPointUnsigned1616Number: () => {
diff --git a/packages/media-parser/src/get-sample-positions.ts b/packages/media-parser/src/get-sample-positions.ts
index 69c526a935..cd11753a1b 100644
--- a/packages/media-parser/src/get-sample-positions.ts
+++ b/packages/media-parser/src/get-sample-positions.ts
@@ -77,7 +77,7 @@ export const getSamplePositions = ({
const cts = dts + ctsOffset;
samples.push({
- offset: chunks[i] + offsetInThisChunk,
+ offset: Number(chunks[i]) + offsetInThisChunk,
size,
isKeyframe,
dts,
diff --git a/packages/media-parser/src/test/parse-stco.test.ts b/packages/media-parser/src/test/parse-stco.test.ts
index 5fb0d024bd..7d16d659a4 100644
--- a/packages/media-parser/src/test/parse-stco.test.ts
+++ b/packages/media-parser/src/test/parse-stco.test.ts
@@ -18,6 +18,7 @@ test('Parse stco box', () => {
iterator,
size: buf.length - 8,
offset: 0,
+ mode64Bit: false,
});
expect(result).toEqual({
type: 'stco-box',
@@ -46,6 +47,7 @@ test('Parse stco box with empty chunk', () => {
iterator,
size: buf.length - 8,
offset: 0,
+ mode64Bit: false,
});
expect(result).toEqual({
type: 'stco-box',