Skip to content

🎥 Make videos programmatically with React

support `Block` segments

Jonny Burgerdb66584b4a59
diff --git a/packages/media-parser/src/boxes/webm/segments.ts b/packages/media-parser/src/boxes/webm/segments.ts
index f702ce907d..074fd581b3 100644
--- a/packages/media-parser/src/boxes/webm/segments.ts
+++ b/packages/media-parser/src/boxes/webm/segments.ts
@@ -2,6 +2,7 @@ import {registerTrack} from '../../add-new-matroska-tracks';
import type {BufferIterator} from '../../buffer-iterator';
import type {ParseResult} from '../../parse-result';
import type {ParserContext} from '../../parser-context';
+import type {VideoSample} from '../../webcodec-sample-types';
import {getTrack} from './get-track';
import {matroskaElements} from './segments/all-segments';
import type {DurationSegment} from './segments/duration';
@@ -399,7 +400,42 @@ const parseSegment = async ({
}
if (segmentId === '0xa0') {
- return parseBlockGroupSegment(iterator, length, parserContext);
+ const blockGroup = await parseBlockGroupSegment(
+ iterator,
+ length,
+ parserContext,
+ );
+
+ // Blocks don't have information about keyframes.
+ // https://ffmpeg.org/pipermail/ffmpeg-devel/2015-June/173825.html
+ // "For Blocks, keyframes is
+ // inferred by the absence of ReferenceBlock element (as done by matroskadec).""
+
+ const block = blockGroup.children.find(
+ (c) => c.type === 'simple-block-or-block-segment',
+ );
+ if (!block || block.type !== 'simple-block-or-block-segment') {
+ throw new Error('Expected block segment');
+ }
+
+ const hasReferenceBlock = blockGroup.children.find(
+ (c) => c.type === 'reference-block-segment',
+ );
+
+ const partialVideoSample = block.videoSample;
+
+ if (partialVideoSample) {
+ const completeFrame: VideoSample = {
+ ...partialVideoSample,
+ type: hasReferenceBlock ? 'delta' : 'key',
+ };
+ await parserContext.parserState.onVideoSample(
+ partialVideoSample.trackId,
+ completeFrame,
+ );
+ }
+
+ return blockGroup;
}
if (segmentId === '0xa1') {
diff --git a/packages/media-parser/src/boxes/webm/segments/track-entry.ts b/packages/media-parser/src/boxes/webm/segments/track-entry.ts
index d355a1548d..8f51629cc9 100644
--- a/packages/media-parser/src/boxes/webm/segments/track-entry.ts
+++ b/packages/media-parser/src/boxes/webm/segments/track-entry.ts
@@ -1,5 +1,6 @@
import type {BufferIterator} from '../../../buffer-iterator';
import type {ParserContext} from '../../../parser-context';
+import type {VideoSample} from '../../../webcodec-sample-types';
import type {MatroskaSegment} from '../segments';
import type {matroskaElements} from './all-segments';
import {parseBlockFlags} from './block-simple-block-flags';
@@ -598,6 +599,7 @@ export type SimpleBlockOrBlockSegment = {
keyframe: boolean | null;
lacing: number;
invisible: boolean;
+ videoSample: Omit<VideoSample, 'type'> | null;
};
export type GetTracks = () => TrackEntrySegment[];
@@ -639,22 +641,34 @@ export const parseSimpleBlockOrBlockSegment = async ({
throw new Error('Could not find codec for track ' + trackNumber);
}
- if (codec.codec.startsWith('V_')) {
- const remainingNow = length - (iterator.counter.getOffset() - start);
+ const remainingNow = length - (iterator.counter.getOffset() - start);
+
+ let videoSample: Omit<VideoSample, 'type'> | null = null;
- await parserContext.parserState.onVideoSample(trackNumber, {
+ if (codec.codec.startsWith('V_')) {
+ const partialVideoSample: Omit<VideoSample, 'type'> = {
data: iterator.getSlice(remainingNow),
cts: null,
dts: null,
duration: undefined,
- type: keyframe ? 'key' : 'delta',
trackId: trackNumber,
timestamp: timecode,
- });
+ };
+
+ if (keyframe === null) {
+ // If we don't know if this is a keyframe, we know after we emit the BlockGroup
+ videoSample = partialVideoSample;
+ } else {
+ const sample: VideoSample = {
+ ...partialVideoSample,
+ type: keyframe ? 'key' : 'delta',
+ };
+
+ await parserContext.parserState.onVideoSample(trackNumber, sample);
+ }
}
if (codec.codec.startsWith('A_')) {
- const remainingNow = length - (iterator.counter.getOffset() - start);
await parserContext.parserState.onAudioSample(trackNumber, {
data: iterator.getSlice(remainingNow),
trackId: trackNumber,
@@ -676,6 +690,7 @@ export const parseSimpleBlockOrBlockSegment = async ({
keyframe,
lacing,
invisible,
+ videoSample,
};
};
diff --git a/packages/media-parser/src/test/matroska.test.ts b/packages/media-parser/src/test/matroska.test.ts
index e6f13d30b8..1573a8106c 100644
--- a/packages/media-parser/src/test/matroska.test.ts
+++ b/packages/media-parser/src/test/matroska.test.ts
@@ -200,6 +200,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: true,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -209,6 +210,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -218,6 +220,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -227,6 +230,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -236,6 +240,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -245,6 +250,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -254,6 +260,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -263,6 +270,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -272,6 +280,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -281,6 +290,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -290,6 +300,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -299,6 +310,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -308,6 +320,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -317,6 +330,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -326,6 +340,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -335,6 +350,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -344,6 +360,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -353,6 +370,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -362,6 +380,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -371,6 +390,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -380,6 +400,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -389,6 +410,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -398,6 +420,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -407,6 +430,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
{
type: 'simple-block-or-block-segment',
@@ -416,6 +440,7 @@ test('Should get duration of AV1 video', async () => {
keyframe: false,
lacing: 0,
invisible: false,
+ videoSample: null,
},
],
},
diff --git a/packages/media-parser/src/test/stream-local.test.ts b/packages/media-parser/src/test/stream-local.test.ts
index 4beb8b92f1..0757795cea 100644
--- a/packages/media-parser/src/test/stream-local.test.ts
+++ b/packages/media-parser/src/test/stream-local.test.ts
@@ -705,6 +705,9 @@ test('Should stream transparent video', async () => {
expect(track.codedWidth).toBe(512);
videoTracks++;
return (sample) => {
+ // https://ffmpeg.org/pipermail/ffmpeg-devel/2015-June/173825.html
+ // For Blocks, keyframes is
+ // inferred by the absence of ReferenceBlock element (as done by matroskadec).
if (sample.type === 'key') {
keyFrames++;
}