Skip to content

🎥 Make videos programmatically with React

cluster samples

Jonny Burger4b6305dfb60f
diff --git a/packages/media-parser/src/boxes/webm/get-sample-from-block.ts b/packages/media-parser/src/boxes/webm/get-sample-from-block.ts
index e0f658db47..b67019f8d3 100644
--- a/packages/media-parser/src/boxes/webm/get-sample-from-block.ts
+++ b/packages/media-parser/src/boxes/webm/get-sample-from-block.ts
@@ -27,6 +27,7 @@ export const getSampleFromBlock = (
parserContext: ParserContext,
offset: number,
): SampleResult => {
+ console.log('get sample');
const iterator = getArrayBufferIterator(ebml.value);
const trackNumber = iterator.getVint();
if (trackNumber === null) {
@@ -35,7 +36,12 @@ export const getSampleFromBlock = (
const timecodeRelativeToCluster = iterator.getUint16();
- const {keyframe} = parseBlockFlags(iterator, matroskaElements.Block);
+ const {keyframe} = parseBlockFlags(
+ iterator,
+ ebml.type === 'SimpleBlock'
+ ? matroskaElements.SimpleBlock
+ : matroskaElements.Block,
+ );
const {codec, trackTimescale} =
parserContext.parserState.getTrackInfoByNumber(trackNumber);
@@ -76,6 +82,8 @@ export const getSampleFromBlock = (
};
if (keyframe === null) {
+ iterator.destroy();
+
return {
type: 'partial-video-sample',
partialVideoSample,
@@ -87,6 +95,8 @@ export const getSampleFromBlock = (
type: keyframe ? 'key' : 'delta',
};
+ iterator.destroy();
+
return {
type: 'video-sample',
videoSample: sample,
@@ -94,17 +104,22 @@ export const getSampleFromBlock = (
}
if (codec.startsWith('A_')) {
+ const audioSample: AudioSample = {
+ data: iterator.getSlice(remainingNow),
+ trackId: trackNumber,
+ timestamp: timecodeInMicroseconds,
+ type: 'key',
+ };
+
+ iterator.destroy();
+
return {
type: 'audio-sample',
- audioSample: {
- data: iterator.getSlice(remainingNow),
- trackId: trackNumber,
- timestamp: timecodeInMicroseconds,
- type: 'key',
- },
+ audioSample,
};
}
+ iterator.destroy();
return {
type: 'no-sample',
};
diff --git a/packages/media-parser/src/boxes/webm/parse-ebml.ts b/packages/media-parser/src/boxes/webm/parse-ebml.ts
index 708e6c1a89..5881a44391 100644
--- a/packages/media-parser/src/boxes/webm/parse-ebml.ts
+++ b/packages/media-parser/src/boxes/webm/parse-ebml.ts
@@ -95,8 +95,12 @@ export const parseEbml = async (
while (true) {
const offset = iterator.counter.getOffset();
const value = await parseEbml(iterator, parserContext);
- await postprocessEbml(offset, value, parserContext);
- children.push(value);
+ const remapped = await postprocessEbml({
+ offset,
+ ebml: value,
+ parserContext,
+ });
+ children.push(remapped);
const offsetNow = iterator.counter.getOffset();
@@ -118,11 +122,15 @@ export const parseEbml = async (
throw new Error(`Unknown segment type ${hasInMap.type}`);
};
-export const postprocessEbml = async (
- offset: number,
- ebml: Prettify<PossibleEbml>,
- parserContext: ParserContext,
-) => {
+export const postprocessEbml = async ({
+ offset,
+ ebml,
+ parserContext,
+}: {
+ offset: number;
+ ebml: Prettify<PossibleEbml>;
+ parserContext: ParserContext;
+}): Promise<Prettify<PossibleEbml>> => {
if (ebml.type === 'TimestampScale') {
parserContext.parserState.setTimescale(ebml.value);
}
@@ -148,7 +156,7 @@ export const postprocessEbml = async (
parserContext.parserState.setTimestampOffset(offset, ebml.value);
}
- if (ebml.type === 'Block') {
+ if (ebml.type === 'Block' || ebml.type === 'SimpleBlock') {
const sample = getSampleFromBlock(ebml, parserContext, offset);
if (sample.type === 'video-sample') {
@@ -156,6 +164,10 @@ export const postprocessEbml = async (
sample.videoSample.trackId,
sample.videoSample,
);
+ return {
+ type: 'Block',
+ value: new Uint8Array([]),
+ };
}
if (sample.type === 'audio-sample') {
@@ -163,6 +175,17 @@ export const postprocessEbml = async (
sample.audioSample.trackId,
sample.audioSample,
);
+ return {
+ type: 'Block',
+ value: new Uint8Array([]),
+ };
+ }
+
+ if (sample.type === 'no-sample') {
+ return {
+ type: 'Block',
+ value: new Uint8Array([]),
+ };
}
}
@@ -195,5 +218,12 @@ export const postprocessEbml = async (
completeFrame,
);
}
+
+ return {
+ type: 'BlockGroup',
+ value: [],
+ };
}
+
+ return ebml;
};
diff --git a/packages/media-parser/src/boxes/webm/segments.ts b/packages/media-parser/src/boxes/webm/segments.ts
index 022927b106..a56abc3ddd 100644
--- a/packages/media-parser/src/boxes/webm/segments.ts
+++ b/packages/media-parser/src/boxes/webm/segments.ts
@@ -32,9 +32,9 @@ const parseSegment = async ({
const offset = iterator.counter.getOffset();
const ebml = await parseEbml(iterator, parserContext);
- await postprocessEbml(offset, ebml, parserContext);
+ const remapped = await postprocessEbml({offset, ebml, parserContext});
- return ebml;
+ return remapped;
};
export const expectSegment = async (
diff --git a/packages/media-parser/src/test/matroska.test.ts b/packages/media-parser/src/test/matroska.test.ts
index 00acbfaaab..5e619b55d9 100644
--- a/packages/media-parser/src/test/matroska.test.ts
+++ b/packages/media-parser/src/test/matroska.test.ts
@@ -216,7 +216,115 @@ test('Should get duration of AV1 video', async () => {
},
],
},
-
+ {
+ children: [
+ {
+ type: 'Timestamp',
+ value: 0,
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ {
+ type: 'Block',
+ value: new Uint8Array(),
+ },
+ ],
+ type: 'cluster-segment',
+ },
{
type: 'Cues',
value: undefined,