Skip to content

🎥 Make videos programmatically with React

cool

Jonny Burgerbabf4b76b654
diff --git a/packages/media-parser/src/boxes/webm/make-header.ts b/packages/media-parser/src/boxes/webm/make-header.ts
index c7a030b85c..d93b105f1e 100644
--- a/packages/media-parser/src/boxes/webm/make-header.ts
+++ b/packages/media-parser/src/boxes/webm/make-header.ts
@@ -1,18 +1,21 @@
import {getVariableInt} from './ebml';
import type {
Ebml,
+ EbmlWithChildren,
+ EbmlWithString,
+ EbmlWithUint8,
EmblTypes,
HeaderStructure,
matroskaElements,
} from './segments/all-segments';
-import {getIdForName, matroskaHeaderStructure} from './segments/all-segments';
+import {getIdForName} from './segments/all-segments';
export const webmPattern = new Uint8Array([0x1a, 0x45, 0xdf, 0xa3]);
const matroskaToHex = (
matrId: (typeof matroskaElements)[keyof typeof matroskaElements],
) => {
- const numbers: Uint8Array = new Uint8Array(2);
+ const numbers: Uint8Array = new Uint8Array((matrId.length - 2) / 2);
for (let i = 2; i < matrId.length; i += 2) {
const hex = matrId.substring(i, i + 2);
@@ -22,57 +25,66 @@ const matroskaToHex = (
return numbers;
};
-const makeField = <T extends Ebml>(element: T, value: EmblTypes[T['type']]) => {
- let val;
- if (typeof value === 'string') {
- val = new TextEncoder().encode(value);
- } else if (typeof value === 'number') {
- val = new Uint8Array([value]);
- } else {
- throw new Error('element value');
- }
+type Numbers = '0' | '1' | '2' | '3' | '4' | '5' | '6';
- return combineUint8Arrays([
- matroskaToHex(getIdForName(element.name)),
- getVariableInt(val.byteLength),
- val,
- ]);
+type ChildFields<Struct extends HeaderStructure> = {
+ [key in keyof Struct &
+ Numbers as Struct[key]['name']]: EmblTypes[Struct[key]['type']];
};
-type Numbers = '0' | '1' | '2' | '3' | '4' | '5' | '6';
+type SerializeValue<Struct extends Ebml> = Struct extends EbmlWithChildren
+ ? ChildFields<Struct['children']>
+ : Struct extends EbmlWithString
+ ? string
+ : Struct extends EbmlWithUint8
+ ? number
+ : undefined;
-const makeFromHeaderStructure = <Struct extends HeaderStructure>(
+const makeFromHeaderStructure = <Struct extends Ebml>(
struct: Struct,
- fields: {
- [key in keyof Struct &
- Numbers as Struct[key]['name']]: EmblTypes[Struct[key]['type']];
- },
-) => {
+ fields: SerializeValue<Struct>,
+): Uint8Array => {
const arrays: Uint8Array[] = [];
- for (const item of struct) {
- // @ts-expect-error
- arrays.push(makeField(item, fields[item.name]));
+ if (struct.type === 'children') {
+ for (const item of struct.children) {
+ arrays.push(
+ makeMatroskaHeader(
+ item,
+ // @ts-expect-error
+ fields[item.name],
+ ),
+ );
+ }
+
+ return combineUint8Arrays(arrays);
}
- return combineUint8Arrays(arrays);
+ if (struct.type === 'string') {
+ return new TextEncoder().encode(fields as string);
+ }
+
+ if (struct.type === 'uint-8') {
+ return new Uint8Array([fields as number]);
+ }
+
+ if (struct.type === 'void') {
+ throw new Error('Serializing Void is not implemented');
+ }
+
+ throw new Error('Unexpected type');
};
-export const makeMatroskaHeader = () => {
- const fields = makeFromHeaderStructure(matroskaHeaderStructure, {
- DocType: 'matroska',
- DocTypeVersion: 4,
- DocTypeReadVersion: 2,
- EBMLMaxIDLength: 4,
- EBMLMaxSizeLength: 8,
- EBMLReadVersion: 1,
- EBMLVersion: 1,
- });
+export const makeMatroskaHeader = <Struct extends Ebml>(
+ struct: Struct,
+ fields: SerializeValue<Struct>,
+) => {
+ const value = makeFromHeaderStructure(struct, fields);
return combineUint8Arrays([
- webmPattern,
- getVariableInt(fields.length),
- fields,
+ matroskaToHex(getIdForName(struct.name)),
+ getVariableInt(value.length),
+ value,
]);
};
diff --git a/packages/media-parser/src/test/create-matroska.test.ts b/packages/media-parser/src/test/create-matroska.test.ts
index b24138408b..1696b15fa5 100644
--- a/packages/media-parser/src/test/create-matroska.test.ts
+++ b/packages/media-parser/src/test/create-matroska.test.ts
@@ -1,6 +1,7 @@
import {RenderInternals} from '@remotion/renderer';
import {expect, test} from 'bun:test';
import {makeMatroskaHeader} from '../boxes/webm/make-header';
+import {matroskaHeader} from '../boxes/webm/segments/all-segments';
test('Should make Matroska header that is same as input', async () => {
const exampleVideo = RenderInternals.exampleVideos.matroskaMp3;
@@ -8,7 +9,15 @@ test('Should make Matroska header that is same as input', async () => {
const headerInput = new Uint8Array(file.slice(0, 0x23 + 5));
- const headerOutput = makeMatroskaHeader();
+ const headerOutput = makeMatroskaHeader(matroskaHeader, {
+ DocType: 'matroska',
+ DocTypeVersion: 4,
+ DocTypeReadVersion: 2,
+ EBMLMaxIDLength: 4,
+ EBMLMaxSizeLength: 8,
+ EBMLReadVersion: 1,
+ EBMLVersion: 1,
+ });
expect(headerInput).toEqual(headerOutput);
});