🎥 Make videos programmatically with React
progress
Jonny Burger06333779314e
diff --git a/packages/video-parser/package.json b/packages/video-parser/package.jsonindex 56dfb03f88..42a6f710e1 100644--- a/packages/video-parser/package.json+++ b/packages/video-parser/package.json@@ -36,7 +36,7 @@"@jonny/eslint-config": "3.0.281","@types/bun": "1.0.12","@types/node": "18.14.6",- "@remotion/renderer": "workspace:",+ "@remotion/renderer": "workspace:*","eslint": "8.56.0","eslint-plugin-10x": "1.5.2","eslint-plugin-react": "7.32.2",diff --git a/packages/video-parser/src/boxes/iso-base-media/base-type.ts b/packages/video-parser/src/boxes/iso-base-media/base-type.tsnew file mode 100644index 0000000000..7600321dbc--- /dev/null+++ b/packages/video-parser/src/boxes/iso-base-media/base-type.ts@@ -0,0 +1,5 @@+export interface BaseBox {+ boxType: string;+ boxSize: number;+ offset: number;+}diff --git a/packages/video-parser/src/boxes/iso-base-media/ftype.ts b/packages/video-parser/src/boxes/iso-base-media/ftype.tsnew file mode 100644index 0000000000..59f3134a94--- /dev/null+++ b/packages/video-parser/src/boxes/iso-base-media/ftype.ts@@ -0,0 +1,41 @@+import type {BaseBox} from './base-type';++export interface FtypBox extends BaseBox {+ type: 'ftyp-box';+ majorBrand: string;+ minorVersion: number;+ compatibleBrands: string[];+}++export const fourByteToNumber = (data: Buffer, from: number) => {+ return (+ (data[from + 0] << 24) |+ (data[from + 1] << 16) |+ (data[from + 2] << 8) |+ data[from + 3]+ );+};++export const parseFtyp = (data: Buffer, offset: number): FtypBox => {+ const majorBrand = data.subarray(8, 12).toString('utf-8').trim();++ const minorVersion = fourByteToNumber(data, 12);++ const rest = data.subarray(16);+ const types = rest.length / 4;+ const compatibleBrands: string[] = [];+ for (let i = 0; i < types; i++) {+ const fourBytes = rest.subarray(i * 4, i * 4 + 4);+ compatibleBrands.push(fourBytes.toString('utf-8').trim());+ }++ return {+ type: 'ftyp-box',+ majorBrand,+ minorVersion,+ compatibleBrands,+ offset,+ boxSize: data.length,+ boxType: 'ftyp',+ };+};diff --git a/packages/video-parser/src/boxes/iso-base-media/mvhd.ts b/packages/video-parser/src/boxes/iso-base-media/mvhd.tsindex a3409f5618..23b3588410 100644--- a/packages/video-parser/src/boxes/iso-base-media/mvhd.ts+++ b/packages/video-parser/src/boxes/iso-base-media/mvhd.ts@@ -1,3 +1,5 @@+import type {BaseBox} from './base-type';+type ThreeDMatrix = [number,number,@@ -10,7 +12,7 @@ type ThreeDMatrix = [number,];-export type MvhdBox = {+export interface MvhdBox extends BaseBox {durationInUnits: number;durationInSeconds: number;creationTime: number | null;@@ -20,75 +22,76 @@ export type MvhdBox = {volume: number;matrix: ThreeDMatrix;nextTrackId: number;-};+ type: 'mvhd-box';+}-export const parseMvhd = (data: Buffer): MvhdBox => {- let offset = 0;+export const parseMvhd = (data: Buffer, offset: number): MvhdBox => {+ let chunkOffset = 0;- const size = data.readUInt32BE(offset);- offset += 4;- const atom = data.subarray(offset, offset + 4);+ const size = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;+ const atom = data.subarray(chunkOffset, chunkOffset + 4);if (atom.toString() !== 'mvhd') {throw new Error(`Expected mvhd atom, got ${atom.toString('utf-8')}`);}- offset += 4;- const version = data.readUInt8(offset);+ chunkOffset += 4;+ const version = data.readUInt8(chunkOffset);if (version !== 0) {throw new Error(`Unsupported MVHD version ${version}`);}- offset += 1;+ chunkOffset += 1;if (size !== 108) {throw new Error(`Expected mvhd size of version 0 to be 108, got ${size}`);}// Flags, we discard them- data.subarray(offset, offset + 3);- offset += 3;+ data.subarray(chunkOffset, chunkOffset + 3);+ chunkOffset += 3;- const creationTime = data.readUInt32BE(offset);- offset += 4;+ const creationTime = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;- const modificationTime = data.readUInt32BE(offset);- offset += 4;+ const modificationTime = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;- const timeScale = data.readUInt32BE(offset);- offset += 4;+ const timeScale = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;- const durationInUnits = data.readUInt32BE(offset);+ const durationInUnits = data.readUInt32BE(chunkOffset);const durationInSeconds = durationInUnits / timeScale;- offset += 4;+ chunkOffset += 4;- const rateArray = data.subarray(offset, offset + 4);+ const rateArray = data.subarray(chunkOffset, chunkOffset + 4);const rate =rateArray[0] * 10 + rateArray[1] + rateArray[2] * 0.1 + rateArray[3] * 0.01;- offset += 4;+ chunkOffset += 4;- const volumeArray = data.subarray(offset, offset + 2);- offset += 2;+ const volumeArray = data.subarray(chunkOffset, chunkOffset + 2);+ chunkOffset += 2;const volume = volumeArray[0] + volumeArray[1] * 0.1;// reserved 16bit- offset += 2;+ chunkOffset += 2;// reserved 32bit x2- offset += 4 * 2;+ chunkOffset += 4 * 2;// matrixconst matrix: number[] = [];for (let i = 0; i < 9; i++) {- matrix.push(data.readUInt32BE(offset));- offset += 4;+ matrix.push(data.readUInt32BE(chunkOffset));+ chunkOffset += 4;}// pre-defined- offset += 4 * 6;+ chunkOffset += 4 * 6;// next track id- const nextTrackId = data.readUInt32BE(offset);- offset += 4;+ const nextTrackId = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;return {creationTime: creationTime === 0 ? null : 0,@@ -100,5 +103,9 @@ export const parseMvhd = (data: Buffer): MvhdBox => {volume,matrix: matrix as ThreeDMatrix,nextTrackId,+ type: 'mvhd-box',+ boxSize: data.length,+ offset,+ boxType: 'mvhd',};};diff --git a/packages/video-parser/src/get-duration.ts b/packages/video-parser/src/get-duration.tsindex d4a2507104..af283f547e 100644--- a/packages/video-parser/src/get-duration.ts+++ b/packages/video-parser/src/get-duration.ts@@ -1,28 +1,27 @@import type {Box} from './parse-video';export const getDuration = (boxes: Box[]): number | null => {- const moovBox = boxes.find((b) => b.boxType === 'moov');- if (!moovBox) {+ const moovBox = boxes.find(+ (b) => b.boxType === 'moov' && b.type === 'regular-box',+ );+ if (moovBox === undefined || moovBox.type !== 'regular-box') {return null;}- const {extraData} = moovBox;- if (!extraData) {+ const {children} = moovBox;+ if (!children) {return null;}- if (extraData.type !== 'boxes') {- return null;- }+ const mvhdBox = children.find((b) => b.boxType === 'mvhd');- const mvhdBox = extraData.boxes.find((b) => b.boxType === 'mvhd');- if (!mvhdBox || !mvhdBox.extraData) {+ if (!mvhdBox) {return null;}- if (mvhdBox.extraData.type !== 'mvhd-box') {+ if (mvhdBox.type !== 'mvhd-box') {throw new Error('Expected mvhd-box');}- return mvhdBox.extraData.box.durationInSeconds;+ return mvhdBox.durationInSeconds;};diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.tsindex 7f360e1202..7813a23008 100644--- a/packages/video-parser/src/parse-video.ts+++ b/packages/video-parser/src/parse-video.ts@@ -1,49 +1,35 @@import {createReadStream} from 'fs';+import type {BaseBox} from './boxes/iso-base-media/base-type';+import type {FtypBox} from './boxes/iso-base-media/ftype';+import {fourByteToNumber, parseFtyp} from './boxes/iso-base-media/ftype';import type {MvhdBox} from './boxes/iso-base-media/mvhd';import {parseMvhd} from './boxes/iso-base-media/mvhd';-const fourByteToNumber = (data: Buffer, from: number) => {- return (- (data[from + 0] << 24) |- (data[from + 1] << 16) |- (data[from + 2] << 8) |- data[from + 3]- );-};--type ExtraData =- | {- type: 'ftyp-box';- majorBrand: string;- minorVersion: number;- compatibleBrands: string[];- }- | {- type: 'boxes';- boxes: Box[];- }- | {- type: 'mvhd-box';- box: MvhdBox;- };--export type Box = {+interface RegularBox extends BaseBox {boxType: string;boxSize: number;- extraData: ExtraData | undefined;+ children: Box[];offset: number;-};--const getExtraDataFromBox = (- box: Buffer,- type: string,-): ExtraData | undefined => {+ type: 'regular-box';+}++export type Box = RegularBox | FtypBox | MvhdBox;++const getExtraDataFromBox = ({+ box,+ type,+ offset,+}: {+ box: Buffer;+ type: string;+ offset: number;+}): Box[] => {if (type === 'ftyp') {- return parseFtyp(box);+ return [parseFtyp(box, offset)];}if (type === 'mvhd') {- return {type: 'mvhd-box', box: parseMvhd(box)};+ return [parseMvhd(box, offset)];}if (@@ -56,6 +42,8 @@ const getExtraDataFromBox = () {return parseBoxes(box.subarray(8));}++ return [];};const processBoxAndSubtract = (data: Buffer, offset: number): BoxAndNext => {@@ -64,58 +52,45 @@ const processBoxAndSubtract = (data: Buffer, offset: number): BoxAndNext => {const next = data.subarray(boxSize);+ const children = getExtraDataFromBox({+ box: data.subarray(0, boxSize),+ type: boxType,+ offset,+ });+return {+ box: {+ type: 'regular-box',+ boxType,+ boxSize,+ children,+ offset: offset + boxSize,+ },next,- boxType,- boxSize,- extraData: getExtraDataFromBox(data.subarray(0, boxSize), boxType),- offset: offset + boxSize,};};-type BoxAndNext = Box & {+type BoxAndNext = {+ box: Box;next: Buffer;};const isoBaseMediaMp4Pattern = Buffer.from('ftyp');-const parseBoxes = (data: Buffer): ExtraData => {+const parseBoxes = (data: Buffer): Box[] => {const boxes: Box[] = [];let remaining = data;let bytesConsumed = 0;while (remaining.length > 0) {- const {next, boxType, boxSize, extraData, offset} = processBoxAndSubtract(- remaining,- bytesConsumed,- );- remaining = next;- boxes.push({boxType, boxSize, extraData, offset: bytesConsumed});- bytesConsumed = offset;- }-- return {boxes, type: 'boxes'};-};--const parseFtyp = (data: Buffer): ExtraData => {- const majorBrand = data.subarray(8, 12).toString('utf-8').trim();-- const minorVersion = fourByteToNumber(data, 12);+ const {next, box} = processBoxAndSubtract(remaining, bytesConsumed);- const rest = data.subarray(16);- const types = rest.length / 4;- const compatibleBrands: string[] = [];- for (let i = 0; i < types; i++) {- const fourBytes = rest.subarray(i * 4, i * 4 + 4);- compatibleBrands.push(fourBytes.toString('utf-8').trim());+ remaining = next;+ boxes.push(box);+ bytesConsumed = box.offset;}- return {- type: 'ftyp-box',- majorBrand,- minorVersion,- compatibleBrands,- };+ return boxes;};const matchesPattern = (pattern: Buffer) => {@@ -124,7 +99,10 @@ const matchesPattern = (pattern: Buffer) => {};};-export const parseVideo = async (src: string, bytes: number) => {+export const parseVideo = async (+ src: string,+ bytes: number,+): Promise<Box[]> => {const stream = createReadStream(src,Number.isFinite(bytes) ? {end: bytes - 1} : {},@@ -146,9 +124,8 @@ export const parseVideo = async (src: string, bytes: number) => {});if (matchesPattern(isoBaseMediaMp4Pattern)(data.subarray(4, 8))) {- const boxes = parseBoxes(data);- if (boxes.type === 'boxes') {- return boxes.boxes;- }+ return parseBoxes(data);}++ return [];};diff --git a/packages/video-parser/src/test/mvhd.test.ts b/packages/video-parser/src/test/mvhd.test.tsindex d147408a72..e9ca9b2709 100644--- a/packages/video-parser/src/test/mvhd.test.ts+++ b/packages/video-parser/src/test/mvhd.test.ts@@ -63,7 +63,7 @@ test('Should be able to parse a MVHD box correctly', () => {0, 0, 0, 2,]);- const mvhd = parseMvhd(buffer);+ const mvhd = parseMvhd(buffer, 0);expect(mvhd).toEqual({creationTime: null,modificationTime: null,@@ -74,5 +74,9 @@ test('Should be able to parse a MVHD box correctly', () => {volume: 1,matrix: [65536, 0, 0, 0, 65536, 0, 0, 0, 1073741824],nextTrackId: 2,+ type: 'mvhd-box',+ boxSize: 108,+ offset: 0,+ boxType: 'mvhd',});});diff --git a/packages/video-parser/src/test/parse-video.test.ts b/packages/video-parser/src/test/parse-video.test.tsindex 0f83035534..a539cd0145 100644--- a/packages/video-parser/src/test/parse-video.test.ts+++ b/packages/video-parser/src/test/parse-video.test.ts@@ -12,24 +12,24 @@ test('Parse Big Buck bunny', async () => {offset: 0,boxSize: 32,boxType: 'ftyp',- extraData: {- type: 'ftyp-box',- majorBrand: 'isom',- minorVersion: 512,- compatibleBrands: ['isom', 'iso2', 'avc1', 'mp41'],- },+ type: 'ftyp-box',+ majorBrand: 'isom',+ minorVersion: 512,+ compatibleBrands: ['isom', 'iso2', 'avc1', 'mp41'],},{offset: 32,boxSize: 8,boxType: 'free',- extraData: undefined,+ type: 'regular-box',+ children: [],},{boxSize: 14282275,boxType: 'mdat',- extraData: undefined,offset: 40,+ type: 'regular-box',+ children: [],},]);});@@ -40,25 +40,25 @@ test('Parse an iPhone video', async () => {{boxSize: 20,boxType: 'ftyp',- extraData: {- type: 'ftyp-box',- majorBrand: 'qt',- minorVersion: 0,- compatibleBrands: ['qt'],- },+ type: 'ftyp-box',+ majorBrand: 'qt',+ minorVersion: 0,+ compatibleBrands: ['qt'],offset: 0,},{+ type: 'regular-box',boxType: 'wide',boxSize: 8,- extraData: undefined,offset: 20,+ children: [],},{boxSize: 39048800,boxType: 'mdat',- extraData: undefined,offset: 28,+ type: 'regular-box',+ children: [],},]);});@@ -73,24 +73,24 @@ test('Parse framer', async () => {offset: 0,boxSize: 32,boxType: 'ftyp',- extraData: {- compatibleBrands: ['isom', 'iso2', 'avc1', 'mp41'],- majorBrand: 'isom',- minorVersion: 512,- type: 'ftyp-box',- },+ compatibleBrands: ['isom', 'iso2', 'avc1', 'mp41'],+ majorBrand: 'isom',+ minorVersion: 512,+ type: 'ftyp-box',},{offset: 32,boxSize: 8,boxType: 'free',- extraData: undefined,+ type: 'regular-box',+ children: [],},{offset: 40,boxSize: 73010,boxType: 'mdat',- extraData: undefined,+ children: [],+ type: 'regular-box',},]);});@@ -103,27 +103,27 @@ test('Parse a full video', async () => {offset: 0,boxSize: 32,boxType: 'ftyp',- extraData: {- type: 'ftyp-box',- majorBrand: 'isom',- minorVersion: 512,- compatibleBrands: ['isom', 'iso2', 'avc1', 'mp41'],- },+ type: 'ftyp-box',+ majorBrand: 'isom',+ minorVersion: 512,+ compatibleBrands: ['isom', 'iso2', 'avc1', 'mp41'],});expect(data[1]).toEqual({offset: 32,boxType: 'free',boxSize: 8,- extraData: undefined,+ type: 'regular-box',+ children: [],});expect(data[2]).toEqual({offset: 40,boxSize: 57014,boxType: 'mdat',- extraData: undefined,+ children: [],+ type: 'regular-box',});- const moov = data[3].extraData;+ const moov = data[3];if (!moov) {throw new Error('No extra data');}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 6f295131ed..6af4e4d5b1 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -2198,7 +2198,7 @@ importers:specifier: 3.0.281version: 3.0.281([email protected])([email protected])'@remotion/renderer':- specifier: 'workspace:'+ specifier: workspace:*version: link:../renderer'@types/bun':specifier: 1.0.12diff --git a/tsconfig.json b/tsconfig.jsonindex e2468ad926..eca5a18572 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -129,6 +129,9 @@{"path": "./packages/transitions"},+ {+ "path": "./packages/video-parser"+ },{"path": "./packages/zod-types"}