🎥 Make videos programmatically with React
return a buffer for rendermedia
Jonny Burger69507eac04b5
diff --git a/packages/docs/docs/renderer/render-frames.md b/packages/docs/docs/renderer/render-frames.mdindex e12958ff2b..8cd3aa28d5 100644--- a/packages/docs/docs/renderer/render-frames.md+++ b/packages/docs/docs/renderer/render-frames.md@@ -75,7 +75,7 @@ const onFrameUpdate = (frame: number) => {### `outputDir`-A `string` specifying the directory (absolute path) to which frames should be saved. Pass `null` and a `onFrameBuffer` callback instead to get a `Buffer` of the frame rather than to write it to any location-+A `string` specifying the directory (absolute path) to which frames should be saved. Pass `null` to this option and use the `onFrameBuffer` callback instead to get a `Buffer` of the frame rather than to write it to any location.### `inputProps`diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.tsindex 5fcb5aafb8..5401f9016e 100644--- a/packages/renderer/src/render-media.ts+++ b/packages/renderer/src/render-media.ts@@ -112,7 +112,7 @@ export const renderMedia = ({port,cancelSignal,...options-}: RenderMediaOptions): Promise<void> => {+}: RenderMediaOptions): Promise<Buffer> => {Internals.validateQuality(quality);if (typeof crf !== 'undefined' && crf !== null) {Internals.validateSelectedCrfAndCodecCombination(crf, codec);@@ -316,13 +316,14 @@ export const renderMedia = ({stitchStart,]);})- .then(([, stitchStart]) => {+ .then(([buffer, stitchStart]) => {encodedFrames = getDurationFromFrameRange(frameRange ?? null,composition.durationInFrames);encodedDoneIn = Date.now() - stitchStart;callUpdate();+ return buffer;}).catch((err) => {/**@@ -359,7 +360,7 @@ export const renderMedia = ({return Promise.race([happyPath,- new Promise<void>((_resolve, reject) => {+ new Promise<Buffer>((_resolve, reject) => {cancelSignal?.(() => {reject(new Error('renderMedia() got cancelled'));});diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.tsindex b241c13665..a2517284a9 100644--- a/packages/renderer/src/stitch-frames-to-video.ts+++ b/packages/renderer/src/stitch-frames-to-video.ts@@ -1,5 +1,6 @@import execa from 'execa';import fs from 'fs';+import {readFile} from 'fs/promises';import path from 'path';import type {Codec,@@ -59,7 +60,7 @@ export type StitcherOptions = {};type ReturnType = {- task: Promise<void>;+ task: Promise<Buffer>;getLogs: () => string;};@@ -242,9 +243,10 @@ export const spawnFfmpeg = async (});await ffmpegTask;options.onProgress?.(expectedFrames);+ const file = await readFile(options.outputLocation);return {getLogs: () => '',- task: Promise.resolve(),+ task: Promise.resolve(file),};}@@ -327,12 +329,16 @@ export const spawnFfmpeg = async (}}});- return {task: task.then(() => undefined), getLogs: () => ffmpegOutput};++ return {+ task: task.then(() => readFile(options.outputLocation)),+ getLogs: () => ffmpegOutput,+ };};export const stitchFramesToVideo = async (options: StitcherOptions-): Promise<void> => {+): Promise<Buffer> => {const {task, getLogs} = await spawnFfmpeg(options);const happyPath = task.catch(() => {@@ -341,7 +347,7 @@ export const stitchFramesToVideo = async (return Promise.race([happyPath,- new Promise<void>((_resolve, reject) => {+ new Promise<Buffer>((_resolve, reject) => {options.cancelSignal?.(() => {reject(new Error('stitchFramesToVideo() got cancelled'));});