Skip to content

🎥 Make videos programmatically with React

fix a video

Jonny Burger8aade4a552c6
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 4f3a5a9fbf..ff92d1f167 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
@@ -26,6 +26,7 @@ const getChildren = ({
boxType === 'minf' ||
boxType === 'stbl' ||
boxType === 'dims' ||
+ boxType === 'wave' ||
boxType === 'stsb';
if (parseChildren) {
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 d1ebaa1249..7107faecdf 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
@@ -205,18 +205,30 @@ export const processSample = ({
if (version === 1) {
const numberOfChannels = iterator.getUint16();
const sampleSize = iterator.getUint16();
- const compressionId = iterator.getUint16();
+ const compressionId = iterator.getInt16();
const packetSize = iterator.getUint16();
const sampleRate = iterator.getFixedPoint1616Number();
- const samplesPerPacket = iterator.getUint16();
- const bytesPerPacket = iterator.getUint16();
- const bytesPerFrame = iterator.getUint16();
- const bitsPerSample = iterator.getUint16();
+
+ const samplesPerPacket = iterator.getUint32();
+
+ const bytesPerPacket = iterator.getUint32();
+ const bytesPerFrame = iterator.getUint32();
+ const bytesPerSample = iterator.getUint32();
const bytesRemainingInBox =
boxSize - (iterator.counter.getOffset() - fileOffset);
- iterator.discard(bytesRemainingInBox);
+ const children = parseBoxes({
+ iterator,
+ allowIncompleteBoxes: false,
+ maxBytes: bytesRemainingInBox,
+ initialBoxes: [],
+ });
+
+ if (children.status === 'incomplete') {
+ throw new Error('Incomplete boxes are not allowed');
+ }
+
return {
sample: {
format: boxFormat,
@@ -235,8 +247,8 @@ export const processSample = ({
samplesPerPacket,
bytesPerPacket,
bytesPerFrame,
- bitsPerSample,
- children: [],
+ bitsPerSample: bytesPerSample,
+ children: children.segments,
},
};
}
diff --git a/packages/media-parser/src/buffer-iterator.ts b/packages/media-parser/src/buffer-iterator.ts
index fed9e20e96..3a320c5bb0 100644
--- a/packages/media-parser/src/buffer-iterator.ts
+++ b/packages/media-parser/src/buffer-iterator.ts
@@ -287,6 +287,7 @@ export const getArrayBufferIterator = (initialData: Uint8Array) => {
counter.increment(2);
return val;
},
+
getInt16: () => {
const val = view.getInt16(counter.getDiscardedOffset());
counter.increment(2);
diff --git a/packages/media-parser/src/get-audio-codec.ts b/packages/media-parser/src/get-audio-codec.ts
index 8d406c41ff..0c0d7bf33d 100644
--- a/packages/media-parser/src/get-audio-codec.ts
+++ b/packages/media-parser/src/get-audio-codec.ts
@@ -1,4 +1,5 @@
/* eslint-disable max-depth */
+import type {Sample} from './boxes/iso-base-media/stsd/samples';
import {trakBoxContainsAudio} from './get-fps';
import type {KnownAudioCodecs} from './options';
import type {AnySegment} from './parse-result';
@@ -11,6 +12,40 @@ export const hasAudioCodec = (boxes: AnySegment[]): boolean => {
}
};
+const onEsdsBox = (child: AnySegment): KnownAudioCodecs | null => {
+ if (child && child.type === 'esds-box') {
+ const descriptor = child.descriptors.find(
+ (d) => d.type === 'decoder-config-descriptor',
+ );
+ if (descriptor && descriptor.type === 'decoder-config-descriptor') {
+ return descriptor.objectTypeIndication;
+ }
+ }
+};
+
+const onSample = (sample: Sample): KnownAudioCodecs | null | undefined => {
+ if (!sample) {
+ return null;
+ }
+
+ if (sample.type !== 'audio') {
+ return null;
+ }
+
+ if (sample.format === 'sowt') {
+ return 'aiff';
+ }
+
+ const child = sample.children.find((c) => c.type === 'esds-box');
+
+ if (child && child.type === 'esds-box') {
+ const ret = onEsdsBox(child);
+ if (ret) {
+ return ret;
+ }
+ }
+};
+
export const getAudioCodec = (boxes: AnySegment[]): KnownAudioCodecs | null => {
const moovBox = boxes.find((b) => b.type === 'moov-box');
if (moovBox && moovBox.type === 'moov-box') {
@@ -42,28 +77,31 @@ export const getAudioCodec = (boxes: AnySegment[]): KnownAudioCodecs | null => {
(b) => b.type === 'stsd-box',
);
if (stsdBox && stsdBox.type === 'stsd-box') {
- const videoSample = stsdBox.samples.find(
- (s) => s.type === 'audio',
- );
- if (videoSample && videoSample.type === 'audio') {
- const child = videoSample.children.find(
- (c) => c.type === 'esds-box',
+ const sample = stsdBox.samples.find((s) => s.type === 'audio');
+ if (sample && sample.type === 'audio') {
+ const ret = onSample(sample);
+ if (ret) {
+ return ret;
+ }
+
+ const waveBox = sample.children.find(
+ (b) => b.type === 'regular-box' && b.boxType === 'wave',
);
- if (child && child.type === 'esds-box') {
- const descriptor = child.descriptors.find(
- (d) => d.type === 'decoder-config-descriptor',
+ if (
+ waveBox &&
+ waveBox.type === 'regular-box' &&
+ waveBox.boxType === 'wave'
+ ) {
+ const esdsBox = waveBox.children.find(
+ (b) => b.type === 'esds-box',
);
- if (
- descriptor &&
- descriptor.type === 'decoder-config-descriptor'
- ) {
- return descriptor.objectTypeIndication;
+ if (esdsBox && esdsBox.type === 'esds-box') {
+ const ret2 = onEsdsBox(esdsBox);
+ if (ret2) {
+ return ret2;
+ }
}
}
-
- if (videoSample.format === 'sowt') {
- return 'aiff';
- }
}
}
}
diff --git a/packages/media-parser/src/test/stsd.test.ts b/packages/media-parser/src/test/stsd.test.ts
index 4b75e064c7..a124d0f67d 100644
--- a/packages/media-parser/src/test/stsd.test.ts
+++ b/packages/media-parser/src/test/stsd.test.ts
@@ -60,14 +60,60 @@ test('Should be able to parse a STSD audio box correctly', () => {
numberOfChannels: 2,
type: 'audio',
sampleSize: 16,
- compressionId: 65534,
+ compressionId: -2,
packetSize: 0,
- samplesPerPacket: 0,
- bitsPerSample: 1,
- bytesPerFrame: 0,
- bytesPerPacket: 1024,
+ samplesPerPacket: 1024,
+ bitsPerSample: 2,
+ bytesPerFrame: 2,
+ bytesPerPacket: 1,
sampleRate: 44100,
- children: [],
+ children: [
+ {
+ boxSize: 91,
+ boxType: 'wave',
+ children: [
+ {
+ boxSize: 12,
+ boxType: 'frma',
+ children: [],
+ offset: 76,
+ type: 'regular-box',
+ },
+ {
+ boxSize: 12,
+ boxType: 'mp4a',
+ children: [],
+ offset: 88,
+ type: 'regular-box',
+ },
+ {
+ descriptors: [
+ {
+ objectTypeIndication: 'aac',
+ type: 'decoder-config-descriptor',
+ },
+ {
+ type: 'sl-config-descriptor',
+ },
+ ],
+ esId: 0,
+ sizeOfInstance: 34,
+ tag: 3,
+ type: 'esds-box',
+ version: 0,
+ },
+ {
+ boxSize: 8,
+ boxType: '\u0000\u0000\u0000\u0000',
+ children: [],
+ offset: 151,
+ type: 'regular-box',
+ },
+ ],
+ offset: 68,
+ type: 'regular-box',
+ },
+ ],
},
],
});