🎥 Make videos programmatically with React
cli command
Jonny Burger47f084417b74
diff --git a/packages/renderer/src/compositor/compose.ts b/packages/renderer/src/compositor/compose.tsindex 45e15b054f..0fe12780fe 100644--- a/packages/renderer/src/compositor/compose.ts+++ b/packages/renderer/src/compositor/compose.ts@@ -6,7 +6,7 @@ import {dynamicLibraryPathOptions} from '../call-ffmpeg';import {getExecutablePath} from './get-executable-path';import {makeNonce} from './make-nonce';import type {- CliInputCommand,+ CompositorCommandSerialized,CompositorImageFormat,ErrorPayload,Layer,@@ -42,7 +42,7 @@ export const compose = async ({return;}- const payload: CliInputCommand = {+ const payload: CompositorCommandSerialized<'Compose'> = {type: 'Compose',params: {height,diff --git a/packages/renderer/src/compositor/compositor.ts b/packages/renderer/src/compositor/compositor.tsindex 1f31459249..fe4ef31560 100644--- a/packages/renderer/src/compositor/compositor.ts+++ b/packages/renderer/src/compositor/compositor.ts@@ -1,21 +1,25 @@import {spawn} from 'child_process';import {dynamicLibraryPathOptions} from '../call-ffmpeg';import {getExecutablePath} from './get-executable-path';-import type {CliInputCommand, CompositorCommand} from './payloads';+import {makeNonce} from './make-nonce';+import type {CompositorCommand, CompositorCommandSerialized} from './payloads';export type Compositor = {finishCommands: () => void;- executeCommand: (payload: CompositorCommand) => Promise<Buffer>;+ executeCommand: <T extends keyof CompositorCommand>(+ type: T,+ payload: CompositorCommand[T]+ ) => Promise<Buffer>;waitForDone: () => Promise<void>;};const compositorMap: Record<string, Compositor> = {};-export const spawnCompositorOrReuse = ({+export const spawnCompositorOrReuse = <T extends keyof CompositorCommand>({initiatePayload,renderId,}: {- initiatePayload: CliInputCommand;+ initiatePayload: CompositorCommand[T];renderId: string;}) => {if (!compositorMap[renderId]) {@@ -43,7 +47,9 @@ type Waiter = {resolve: (data: Buffer) => void;};-export const startCompositor = (payload: CliInputCommand): Compositor => {+export const startCompositor = <T extends keyof CompositorCommand>(+ payload: CompositorCommand[T]+): Compositor => {const bin = getExecutablePath('compositor');const child = spawn(bin,@@ -147,11 +153,22 @@ export const startCompositor = (payload: CliInputCommand): Compositor => {child.stdin.write('EOF\n');},- executeCommand: (command: CompositorCommand) => {+ executeCommand: <Type extends keyof CompositorCommand>(+ command: Type,+ params: CompositorCommand[Type]+ ) => {return new Promise<Buffer>((resolve) => {+ const nonce = makeNonce();+ const composed: CompositorCommandSerialized<Type> = {+ type: command,+ params: {+ ...params,+ nonce,+ },+ };// TODO: Should have a way to error out a single task- child.stdin.write(JSON.stringify(command) + '\n');- waiters.set(command.params.nonce, {+ child.stdin.write(JSON.stringify({command: composed}) + '\n');+ waiters.set(nonce, {resolve: (data: Buffer) => {resolve(data);},diff --git a/packages/renderer/src/compositor/extract-frame.ts b/packages/renderer/src/compositor/extract-frame.tsindex 0d7ea8b704..b7a09ace57 100644--- a/packages/renderer/src/compositor/extract-frame.ts+++ b/packages/renderer/src/compositor/extract-frame.ts@@ -2,7 +2,7 @@ import {spawn} from 'child_process';import {dynamicLibraryPathOptions} from '../call-ffmpeg';import {getExecutablePath} from './get-executable-path';import {makeNonce} from './make-nonce';-import type {CliInputCommand} from './payloads';+import type {CompositorCommandSerialized} from './payloads';export const extractFrameFromVideoCompositor = ({input,@@ -15,7 +15,7 @@ export const extractFrameFromVideoCompositor = ({}) => {return new Promise<void>((resolve, reject) => {const bin = getExecutablePath('compositor');- const payload: CliInputCommand = {+ const payload: CompositorCommandSerialized<'ExtractFrame'> = {type: 'ExtractFrame',params: {input,diff --git a/packages/renderer/src/compositor/payloads.ts b/packages/renderer/src/compositor/payloads.tsindex 4ef534abc1..197a422108 100644--- a/packages/renderer/src/compositor/payloads.ts+++ b/packages/renderer/src/compositor/payloads.ts@@ -33,43 +33,31 @@ export type Layer =export type CompositorImageFormat = 'Png' | 'Jpeg';-export type CompositorCommand =- | {- type: 'Compose';- params: {- output: string;- width: number;- height: number;- layers: Layer[];- output_format: CompositorImageFormat;- nonce: string;- };- }- | {- type: 'ExtractFrame';- params: {- input: string;- output: string;- time: number;- nonce: string;- };- }- | {- type: 'Echo';- params: {- message: string;- nonce: string;- };- };+export type CompositorCommand = {+ Compose: {+ output: string;+ width: number;+ height: number;+ layers: Layer[];+ output_format: CompositorImageFormat;+ };+ ExtractFrame: {+ input: string;+ output: string;+ time: number;+ };+ Echo: {+ message: string;+ };+ StartLongRunningProcess: {};+};-export type CliInputCommand =- | CompositorCommand- | {- type: 'StartLongRunningProcess';- params: {- nonce: string;- };- };+export type CompositorCommandSerialized<T extends keyof CompositorCommand> = {+ type: T;+ params: CompositorCommand[T] & {+ nonce: string;+ };+};export type ErrorPayload = {error: string;diff --git a/packages/renderer/src/test/compositor-rust.test.ts b/packages/renderer/src/test/compositor-rust.test.tsindex 121c23cc2e..b19f62997d 100644--- a/packages/renderer/src/test/compositor-rust.test.ts+++ b/packages/renderer/src/test/compositor-rust.test.ts@@ -17,11 +17,9 @@ test(await new Promise<void>((resolve) => {setTimeout(() => resolve(), Math.random() * 100);});- const nonce = makeNonce();const expectedString = 'mystring-abc-' + String(i);- const output = await compositor.executeCommand({- type: 'Echo',- params: {message: expectedString, nonce},+ const output = await compositor.executeCommand('Echo', {+ message: expectedString,});const isSame = output.toString('utf8') === 'Echo ' + expectedString;return isSame;