Skip to content

🎥 Make videos programmatically with React

support more codecs

Jonny Burgerec26de9205cf
diff --git a/packages/media-parser/src/get-video-codec.ts b/packages/media-parser/src/get-video-codec.ts
index c0b23d705a..52bcca30a6 100644
--- a/packages/media-parser/src/get-video-codec.ts
+++ b/packages/media-parser/src/get-video-codec.ts
@@ -1,3 +1,4 @@
+/* eslint-disable max-depth */
import type {KnownVideoCodecs} from './options';
import type {AnySegment} from './parse-result';
@@ -10,10 +11,52 @@ export const hasVideoCodec = (boxes: AnySegment[]): boolean => {
};
export const getVideoCodec = (boxes: AnySegment[]): KnownVideoCodecs | null => {
- const ftypBox = boxes.find((b) => b.type === 'ftyp-box');
- if (ftypBox && ftypBox.type === 'ftyp-box') {
- if (ftypBox.compatibleBrands.find((b) => b === 'avc1')) {
- return 'h264';
+ const moovBox = boxes.find((b) => b.type === 'moov-box');
+ if (moovBox && moovBox.type === 'moov-box') {
+ const trakBox = moovBox.children.find((b) => b.type === 'trak-box');
+ if (trakBox && trakBox.type === 'trak-box') {
+ const mdiaBox = trakBox.children.find(
+ (b) => b.type === 'regular-box' && b.boxType === 'mdia',
+ );
+ if (
+ mdiaBox &&
+ mdiaBox.type === 'regular-box' &&
+ mdiaBox.boxType === 'mdia'
+ ) {
+ const minfBox = mdiaBox?.children.find(
+ (b) => b.type === 'regular-box' && b.boxType === 'minf',
+ );
+ if (
+ minfBox &&
+ minfBox.type === 'regular-box' &&
+ minfBox.boxType === 'minf'
+ ) {
+ const stblBox = minfBox?.children.find(
+ (b) => b.type === 'regular-box' && b.boxType === 'stbl',
+ );
+ if (stblBox && stblBox.type === 'regular-box') {
+ const stsdBox = stblBox?.children.find(
+ (b) => b.type === 'stsd-box',
+ );
+ if (stsdBox && stsdBox.type === 'stsd-box') {
+ const videoSample = stsdBox.samples.find(
+ (s) => s.type === 'video',
+ );
+ if (videoSample && videoSample.type === 'video') {
+ if (videoSample.format === 'hvc1') {
+ return 'h265';
+ }
+
+ if (videoSample.format === 'avc1') {
+ return 'h264';
+ }
+
+ console.log({videoSample});
+ }
+ }
+ }
+ }
+ }
}
}
@@ -36,6 +79,10 @@ export const getVideoCodec = (boxes: AnySegment[]): KnownVideoCodecs | null => {
if (trackType.codec === 'V_VP8') {
return 'vp8';
}
+
+ if (trackType.codec === 'V_AV1') {
+ return 'av1';
+ }
}
}
}
diff --git a/packages/media-parser/src/options.ts b/packages/media-parser/src/options.ts
index 6fda8fc58a..7403d20a3d 100644
--- a/packages/media-parser/src/options.ts
+++ b/packages/media-parser/src/options.ts
@@ -2,7 +2,7 @@ import type {Dimensions} from './get-dimensions';
import type {AnySegment} from './parse-result';
import type {ReaderInterface} from './reader';
-export type KnownVideoCodecs = 'h264' | 'vp8';
+export type KnownVideoCodecs = 'h264' | 'h265' | 'vp8' | 'av1' | 'prores';
export type Options<
EnableDimensions extends boolean,
diff --git a/packages/media-parser/src/test/stream-local.test.ts b/packages/media-parser/src/test/stream-local.test.ts
index abc829deac..6aa73b154c 100644
--- a/packages/media-parser/src/test/stream-local.test.ts
+++ b/packages/media-parser/src/test/stream-local.test.ts
@@ -9,17 +9,19 @@ test('Should stream ISO base media', async () => {
{
durationInSeconds: true,
fps: true,
+ videoCodec: true,
},
nodeReader,
);
expect(result.durationInSeconds).toBe(12.568333333333333);
expect(result.fps).toBe(29.99602174777881);
+ expect(result.videoCodec).toBe('h265');
});
test('Should stream WebM with no duration', async () => {
const result = await parseMedia(
RenderInternals.exampleVideos.nofps,
- {fps: true, durationInSeconds: true, dimensions: true},
+ {fps: true, durationInSeconds: true, dimensions: true, videoCodec: true},
nodeReader,
);
expect(result.durationInSeconds).toBe(6.57);
@@ -28,6 +30,7 @@ test('Should stream WebM with no duration', async () => {
height: 690,
});
expect(result.fps).toBeDefined();
+ expect(result.videoCodec).toBe('vp8');
});
test('Should stream AV1 with no duration', async () => {
@@ -37,6 +40,7 @@ test('Should stream AV1 with no duration', async () => {
durationInSeconds: true,
dimensions: true,
fps: true,
+ videoCodec: true,
},
nodeReader,
);
@@ -47,6 +51,7 @@ test('Should stream AV1 with no duration', async () => {
width: 1920,
height: 1080,
});
+ expect(parsed.videoCodec).toBe('av1');
});
test('Should stream corrupted video', async () => {
@@ -56,6 +61,7 @@ test('Should stream corrupted video', async () => {
durationInSeconds: true,
dimensions: true,
fps: true,
+ videoCodec: true,
},
nodeReader,
);
@@ -66,6 +72,7 @@ test('Should stream corrupted video', async () => {
width: 1920,
height: 1080,
});
+ expect(parsed.videoCodec).toBe('h264');
});
test('Should stream screen recording video', async () => {
@@ -75,6 +82,7 @@ test('Should stream screen recording video', async () => {
durationInSeconds: true,
dimensions: true,
fps: true,
+ videoCodec: true,
},
nodeReader,
);
@@ -85,6 +93,7 @@ test('Should stream screen recording video', async () => {
height: 1766,
width: 2874,
});
+ expect(parsed.videoCodec).toBe('h264');
});
test('Should stream ProRes video', async () => {
const parsed = await parseMedia(
@@ -93,13 +102,17 @@ test('Should stream ProRes video', async () => {
fps: true,
dimensions: true,
durationInSeconds: true,
+ videoCodec: true,
+ boxes: true,
},
nodeReader,
);
+ await Bun.write('prores.json', JSON.stringify(parsed, null, 2));
// TODO: FPS should be 60
expect(parsed.fps).toBe(3.90625);
expect(parsed.dimensions.width).toBe(1920);
expect(parsed.dimensions.height).toBe(1080);
expect(parsed.durationInSeconds).toBe(0.034);
+ expect(parsed.videoCodec).toBe('prores');
});
diff --git a/packages/media-parser/src/test/stream-remote.test.ts b/packages/media-parser/src/test/stream-remote.test.ts
index 7aa012ce82..c0ec6eda5f 100644
--- a/packages/media-parser/src/test/stream-remote.test.ts
+++ b/packages/media-parser/src/test/stream-remote.test.ts
@@ -8,7 +8,6 @@ test('Should stream', async () => {
durationInSeconds: true,
fps: true,
dimensions: true,
- boxes: true,
videoCodec: true,
},
);