🎥 Make videos programmatically with React
lol it kinda works
Jonny Burgerc5a0693c3275
diff --git a/packages/media-parser/src/boxes/webm/make-header.ts b/packages/media-parser/src/boxes/webm/make-header.tsindex 018d6cce13..df153efc08 100644--- a/packages/media-parser/src/boxes/webm/make-header.ts+++ b/packages/media-parser/src/boxes/webm/make-header.ts@@ -1,5 +1,6 @@import {getVariableInt} from './ebml';import type {+ FloatWithSize,PossibleEbml,PossibleEbmlOrUint8Array,matroskaElements,@@ -51,6 +52,10 @@ const makeFromHeaderStructure = (const struct = ebmlMap[getIdForName(fields.type)];+ if (struct.type === 'uint8array') {+ return fields.value as Uint8Array;+ }+if (struct.type === 'children') {for (const item of fields.value as PossibleEbml[]) {arrays.push(makeMatroskaBytes(item));@@ -78,8 +83,17 @@ const makeFromHeaderStructure = (return arr;}- if (struct.type === 'void') {- throw new Error('Serializing Void is not implemented');+ if (struct.type === 'float') {+ const value = fields.value as FloatWithSize;+ if (value.size === '32') {+ const dataView = new DataView(new ArrayBuffer(4));+ dataView.setFloat32(0, value.value);+ return new Uint8Array(dataView.buffer);+ }++ const dataView2 = new DataView(new ArrayBuffer(8));+ dataView2.setFloat64(0, fields.value as number);+ return new Uint8Array(dataView2.buffer);}throw new Error('Unexpected type');@@ -99,7 +113,15 @@ export const makeMatroskaBytes = (fields: PossibleEbmlOrUint8Array) => {]);};-const combineUint8Arrays = (arrays: Uint8Array[]) => {+export const combineUint8Arrays = (arrays: Uint8Array[]) => {+ if (arrays.length === 0) {+ return new Uint8Array([]);+ }++ if (arrays.length === 1) {+ return arrays[0];+ }+let totalLength = 0;for (const array of arrays) {totalLength += array.length;diff --git a/packages/media-parser/src/boxes/webm/parse-ebml.ts b/packages/media-parser/src/boxes/webm/parse-ebml.tsindex 06662a6789..68542b88fc 100644--- a/packages/media-parser/src/boxes/webm/parse-ebml.ts+++ b/packages/media-parser/src/boxes/webm/parse-ebml.ts@@ -56,16 +56,10 @@ export const parseEbml = async (return {type: hasInMap.name,- value,- };- }-- if (hasInMap.type === 'void') {- iterator.discard(size);-- return {- type: hasInMap.name,- value: undefined,+ value: {+ value,+ size: size === 4 ? '32' : '64',+ },};}diff --git a/packages/media-parser/src/boxes/webm/segments/all-segments.ts b/packages/media-parser/src/boxes/webm/segments/all-segments.tsindex 706900aeb9..659b0f0aa7 100644--- a/packages/media-parser/src/boxes/webm/segments/all-segments.ts+++ b/packages/media-parser/src/boxes/webm/segments/all-segments.ts@@ -329,11 +329,6 @@ export type EbmlWithString = {type: EbmlType;};-export type EbmlWithVoid = {- name: MatroskaKey;- type: 'void';-};-export type EbmlWithFloat = {name: MatroskaKey;type: 'float';@@ -348,7 +343,6 @@ export type Ebml =| EbmlWithString| EbmlWithUint8| EbmlWithChildren- | EbmlWithVoid| EbmlWithFloat| EbmlWithHexString| EbmlWithUint8Array;@@ -388,9 +382,9 @@ export const docTypeReadVersion = {type: 'uint',} satisfies Ebml;-export const voidEbml = {+const voidEbml = {name: 'Void',- type: 'void',+ type: 'uint8array',} satisfies Ebml;export type EmblTypes = {@@ -403,16 +397,6 @@ export type EmblTypes = {uint8array: Uint8Array;};-export const matroskaHeaderStructure = [- ebmlVersion,- ebmlReadVersion,- ebmlMaxIdLength,- ebmlMaxSizeLength,- docType,- docTypeVersion,- docTypeReadVersion,-] as const satisfies Ebml[];-export const matroskaHeader = {name: 'Header',type: 'children',@@ -440,7 +424,7 @@ export const seekHead = {export const voidHeader = {name: 'Void',- type: 'void',+ type: 'uint8array',} as const satisfies Ebml;export const codecID = {@@ -535,7 +519,7 @@ export const flagLacing = {export const tagSegment = {name: 'Tag',- type: 'void',+ type: 'uint8array',} as const satisfies Ebml;export const tags = {@@ -555,7 +539,7 @@ export const trackUID = {export const color = {name: 'Colour',- type: 'void',+ type: 'uint8array',} as const satisfies Ebml;export const language = {@@ -595,7 +579,7 @@ export const videoSegment = {export const flagDefault = {name: 'FlagDefault',- type: 'void',+ type: 'uint8array',} as const satisfies Ebml;export const referenceBlock = {@@ -605,7 +589,7 @@ export const referenceBlock = {export const blockElement = {name: 'Block',- type: 'void',+ type: 'uint8array',} as const satisfies Ebml;export const codecName = {@@ -675,24 +659,24 @@ export type SimpleBlockSegment = EbmlParsed<typeof simpleBlock>;export type MainSegment = EbmlParsed<typeof segment>;export type ClusterSegment = EbmlParsed<typeof cluster>;+export type FloatWithSize = {value: number; size: '32' | '64'};+export type EbmlValue<T extends Ebml,Child = PossibleEbml,> = T extends EbmlWithUint8? number- : T extends EbmlWithVoid- ? undefined- : T extends EbmlWithString- ? string- : T extends EbmlWithFloat- ? number- : T extends EbmlWithHexString- ? string- : T extends EbmlWithUint8Array- ? Uint8Array- : T extends EbmlWithChildren- ? Child[]- : never;+ : T extends EbmlWithString+ ? string+ : T extends EbmlWithFloat+ ? FloatWithSize+ : T extends EbmlWithHexString+ ? string+ : T extends EbmlWithUint8Array+ ? Uint8Array+ : T extends EbmlWithChildren+ ? Child[]+ : never;export type EbmlValueOrUint8Array<T extends Ebml> =| Uint8Array@@ -720,37 +704,37 @@ export const ebmlMap = {[matroskaElements.Void]: voidEbml,[matroskaElements.Cues]: {name: 'Cues',- type: 'void',+ type: 'uint8array',},[matroskaElements.DateUTC]: {name: 'DateUTC',- type: 'void',+ type: 'uint8array',},[matroskaElements.TrackTimestampScale]: trackTimestampScale,[matroskaElements.CodecDelay]: {name: 'CodecDelay',- type: 'void',+ type: 'uint8array',},[matroskaElements.SeekPreRoll]: {name: 'SeekPreRoll',- type: 'void',+ type: 'uint8array',},[matroskaElements.DiscardPadding]: {name: 'DiscardPadding',- type: 'void',+ type: 'uint8array',},[matroskaElements.OutputSamplingFrequency]: {name: 'OutputSamplingFrequency',- type: 'void',+ type: 'uint8array',},[matroskaElements.CodecName]: codecName,[matroskaElements.Position]: {name: 'Position',- type: 'void',+ type: 'uint8array',},[matroskaElements.SliceDuration]: {name: 'SliceDuration',- type: 'void',+ type: 'uint8array',},[matroskaElements.SeekHead]: seekHead,[matroskaElements.Seek]: seek,@@ -758,7 +742,7 @@ export const ebmlMap = {[matroskaElements.SeekPosition]: seekPosition,[matroskaElements.Crc32]: {name: 'Crc32',- type: 'void',+ type: 'uint8array',},[matroskaElements.MuxingApp]: muxingApp,[matroskaElements.WritingApp]: {diff --git a/packages/media-parser/src/combine-uint8array.ts b/packages/media-parser/src/combine-uint8array.tsdeleted file mode 100644index f89947f982..0000000000--- a/packages/media-parser/src/combine-uint8array.ts+++ /dev/null@@ -1,13 +0,0 @@-export const combineUint8Arrays = (- array1: Uint8Array | null,- array2: Uint8Array,-) => {- if (!array1) {- return array2;- }-- const combined = new Uint8Array(array1.length + array2.length);- combined.set(array1);- combined.set(array2, array1.length);- return combined;-};diff --git a/packages/media-parser/src/get-duration.ts b/packages/media-parser/src/get-duration.tsindex b797ef6554..97b6f9fcb8 100644--- a/packages/media-parser/src/get-duration.ts+++ b/packages/media-parser/src/get-duration.ts@@ -32,7 +32,7 @@ const getDurationFromMatroska = (segments: AnySegment[]): number | null => {return null;}- return (duration.value / timestampScale.value) * 1000;+ return (duration.value.value / timestampScale.value) * 1000;};export const getDuration = (boxes: AnySegment[]): number | null => {diff --git a/packages/media-parser/src/parser-state.ts b/packages/media-parser/src/parser-state.tsindex ce381f9a95..c4cb127b7e 100644--- a/packages/media-parser/src/parser-state.ts+++ b/packages/media-parser/src/parser-state.ts@@ -41,7 +41,7 @@ export const makeParserState = ({trackEntries[trackId] = {codec: codec.value,- trackTimescale,+ trackTimescale: trackTimescale?.value ?? null,};};diff --git a/packages/media-parser/src/test/create-matroska.test.ts b/packages/media-parser/src/test/create-matroska.test.tsindex a141d53376..c9eef064d3 100644--- a/packages/media-parser/src/test/create-matroska.test.ts+++ b/packages/media-parser/src/test/create-matroska.test.ts@@ -1,7 +1,12 @@+import {RenderInternals} from '@remotion/renderer';import {expect, test} from 'bun:test';-import {makeMatroskaBytes} from '../boxes/webm/make-header';+import {statSync} from 'fs';+import {combineUint8Arrays, makeMatroskaBytes} from '../boxes/webm/make-header';import {parseEbml} from '../boxes/webm/parse-ebml';+import type {MatroskaSegment} from '../boxes/webm/segments';import {getArrayBufferIterator} from '../buffer-iterator';+import {nodeReader} from '../from-node';+import {parseMedia} from '../parse-media';import type {ParserContext} from '../parser-context';import {makeParserState} from '../parser-state';@@ -177,4 +182,28 @@ test('Should parse seekHead', async () => {});});-test('Can we disassemble a Matroska file and assembled it again', () => {});+test('Can we disassemble a Matroska file and assembled it again', async () => {+ process.env.KEEP_SAMPLES = 'true';+ const originalFile = statSync(RenderInternals.exampleVideos.opusWebm);+ const {boxes} = await parseMedia({+ src: RenderInternals.exampleVideos.opusWebm,+ fields: {+ boxes: true,+ },+ reader: nodeReader,+ });+ let bytesTotal = 0;+ const buffers: Uint8Array[] = [];+ for (const box of boxes) {+ const bytes = makeMatroskaBytes(box as MatroskaSegment);+ bytesTotal += bytes.length;+ buffers.push(bytes);+ }++ const combined = combineUint8Arrays(buffers);++ // TODO: Remove this+ await Bun.write('combined.webm', combined);++ expect(bytesTotal).toBe(originalFile.size);+});diff --git a/packages/media-parser/src/test/matroska.test.ts b/packages/media-parser/src/test/matroska.test.tsindex 337a41219b..ecc55b0157 100644--- a/packages/media-parser/src/test/matroska.test.ts+++ b/packages/media-parser/src/test/matroska.test.ts@@ -118,7 +118,7 @@ test('Should get duration of AV1 video', async () => {},{type: 'Void',- value: undefined,+ value: new Uint8Array([]),},{type: 'Info',@@ -137,7 +137,10 @@ test('Should get duration of AV1 video', async () => {},{type: 'Duration',- value: 1000,+ value: {+ value: 1000,+ size: '32',+ },},],},@@ -188,7 +191,7 @@ test('Should get duration of AV1 video', async () => {},{type: 'Colour',- value: undefined,+ value: new Uint8Array([]),},],},@@ -208,11 +211,11 @@ test('Should get duration of AV1 video', async () => {value: [{type: 'Tag',- value: undefined,+ value: new Uint8Array([]),},{type: 'Tag',- value: undefined,+ value: new Uint8Array([]),},],},@@ -327,7 +330,7 @@ test('Should get duration of AV1 video', async () => {},{type: 'Cues',- value: undefined,+ value: new Uint8Array([]),},],},diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.tsindex 53bd80e573..78d7f942d9 100644--- a/packages/media-parser/src/traversal.ts+++ b/packages/media-parser/src/traversal.ts@@ -312,7 +312,7 @@ export const getSampleRate = (track: TrackEntrySegment): number | null => {return null;}- return samplingFrequency.value;+ return samplingFrequency.value.value;};export const getNumberOfChannels = (track: TrackEntrySegment): number => {