Skip to content

🎥 Make videos programmatically with React

stream samples

Jonny Burger100567a51f61
diff --git a/packages/media-parser/src/boxes/iso-base-media/avcc.ts b/packages/media-parser/src/boxes/iso-base-media/avcc.ts
index 5677b1ca77..a9a9b9d0ae 100644
--- a/packages/media-parser/src/boxes/iso-base-media/avcc.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/avcc.ts
@@ -3,6 +3,7 @@ import type {BufferIterator} from '../../buffer-iterator';
export interface AvccBox {
type: 'avcc-box';
data: Uint8Array;
+ configurationString: string;
}
export const parseAvcc = ({
@@ -12,9 +13,23 @@ export const parseAvcc = ({
data: BufferIterator;
size: number;
}): AvccBox => {
+ const confVersion = data.getUint8();
+ if (confVersion !== 1) {
+ throw new Error(`Unsupported AVCC version ${confVersion}`);
+ }
+
+ const profile = data.getUint8();
+ const profileCompatibility = data.getUint8();
+ const level = data.getUint8();
+
+ const str = `${profile.toString(16).padStart(2, '0')}${profileCompatibility.toString(16).padStart(2, '0')}${level.toString(16).padStart(2, '0')}`;
+
+ data.counter.decrement(4);
+
return {
type: 'avcc-box',
data: data.getSlice(size - 8),
+ configurationString: str,
};
};
diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.ts
index 7e41abafb4..939e3a8cfc 100644
--- a/packages/media-parser/src/get-tracks.ts
+++ b/packages/media-parser/src/get-tracks.ts
@@ -6,6 +6,7 @@ import {
} from './get-fps';
import type {SamplePosition} from './get-sample-positions';
import {getSamplePositions} from './get-sample-positions';
+import {getVideoCodecString} from './get-video-codec';
import type {AnySegment} from './parse-result';
import {
getCttsBox,
@@ -28,6 +29,7 @@ export type VideoTrack = {
trackId: number;
description: Uint8Array | null;
timescale: number;
+ codecString: string | null;
};
export type AudioTrack = {
@@ -35,6 +37,7 @@ export type AudioTrack = {
samplePositions: SamplePosition[];
trackId: number;
timescale: number;
+ codecString: string | null;
};
export type Track = VideoTrack | AudioTrack;
@@ -136,6 +139,7 @@ export const getTracks = (
samplePositions,
trackId: tkhdBox.trackId,
timescale: timescaleAndDuration.timescale,
+ codecString: null,
});
}
@@ -146,6 +150,7 @@ export const getTracks = (
trackId: tkhdBox.trackId,
description: videoDescriptors,
timescale: timescaleAndDuration.timescale,
+ codecString: getVideoCodecString(track),
});
}
}
diff --git a/packages/media-parser/src/get-video-codec.ts b/packages/media-parser/src/get-video-codec.ts
index e8038ca392..b477e45815 100644
--- a/packages/media-parser/src/get-video-codec.ts
+++ b/packages/media-parser/src/get-video-codec.ts
@@ -1,4 +1,5 @@
/* eslint-disable max-depth */
+import type {TrakBox} from './boxes/iso-base-media/trak/trak';
import {trakBoxContainsVideo} from './get-fps';
import type {KnownVideoCodecs} from './options';
import type {AnySegment} from './parse-result';
@@ -12,6 +13,25 @@ export const hasVideoCodec = (boxes: AnySegment[]): boolean => {
}
};
+export const getVideoCodecString = (trakBox: TrakBox): string | null => {
+ const stsdBox = getStsdBox(trakBox);
+ if (stsdBox && stsdBox.type === 'stsd-box') {
+ const videoSample = stsdBox.samples.find((s) => s.type === 'video');
+ if (videoSample && videoSample.type === 'video') {
+ const avccBox = videoSample.descriptors.find(
+ (c) => c.type === 'avcc-box',
+ );
+ if (!avccBox || avccBox.type !== 'avcc-box') {
+ return null;
+ }
+
+ return `${videoSample.format}.${avccBox.configurationString}`;
+ }
+ }
+
+ return null;
+};
+
export const getVideoCodec = (boxes: AnySegment[]): KnownVideoCodecs | null => {
const moovBox = getMoovBox(boxes);
if (moovBox) {
diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.ts
index 88420fd167..8ac06efc13 100644
--- a/packages/media-parser/src/test/stream-samples.test.ts
+++ b/packages/media-parser/src/test/stream-samples.test.ts
@@ -8,6 +8,7 @@ test('Stream samples', async () => {
src: RenderInternals.exampleVideos.mp4withmp3,
fields: {
tracks: true,
+ videoCodec: true,
},
readerInterface: nodeReader,
});
@@ -106,6 +107,7 @@ test('Stream samples', async () => {
],
description,
timescale: 15360,
+ codecString: 'avc1.640020',
},
]);
@@ -236,6 +238,7 @@ test('Stream samples', async () => {
},
],
timescale: 48000,
+ codecString: null,
},
]);
});