Skip to content

🎥 Make videos programmatically with React

works kinda

Jonny Burger3a18a8cbb97a
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index 60a16faa0f..dd3e01dbad 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -1,4 +1,5 @@
import {ExecaChildProcess} from 'execa';
+import os from 'os';
import fs from 'fs';
import path from 'path';
import type {Browser as PuppeteerBrowser} from 'puppeteer-core';
@@ -110,7 +111,7 @@ export const renderMedia = async ({
let encodedDoneIn: number | null = null;
const renderStart = Date.now();
const tmpdir = tmpDir('pre-encode');
- const parallelEncoding = !Internals.isAudioCodec(codec);
+ const parallelEncoding = false;
const preEncodedFileLocation = parallelEncoding
? path.join(
tmpdir,
@@ -118,6 +119,10 @@ export const renderMedia = async ({
)
: null;
+ const outputDir = await fs.promises.mkdtemp(
+ path.join(os.tmpdir(), 'react-motion-render')
+ );
+
try {
const callUpdate = () => {
onProgress?.({
@@ -170,7 +175,7 @@ export const renderMedia = async ({
callUpdate();
},
parallelism,
- outputDir: null,
+ outputDir: parallelEncoding ? null : outputDir,
onStart: (data) => {
renderedFrames = 0;
callUpdate();
@@ -182,12 +187,14 @@ export const renderMedia = async ({
quality,
frameRange: frameRange ?? null,
puppeteerInstance,
- onFrameBuffer: async (buffer, frame) => {
- await waitForRightTimeOfFrameToBeInserted(frame);
- stitcherFfmpeg?.stdin?.write(buffer);
+ onFrameBuffer: parallelEncoding
+ ? async (buffer, frame) => {
+ await waitForRightTimeOfFrameToBeInserted(frame);
+ stitcherFfmpeg?.stdin?.write(buffer);
- setFrameToStitch(frame + 1);
- },
+ setFrameToStitch(frame + 1);
+ }
+ : undefined,
serveUrl,
dumpBrowserLogs,
onBrowserLog,
@@ -209,6 +216,7 @@ export const renderMedia = async ({
ensureOutputDirectory(outputLocation);
const stitchStart = Date.now();
+
await stitchFramesToVideo({
width: composition.width,
height: composition.height,
@@ -232,7 +240,8 @@ export const renderMedia = async ({
Internals.Logging.getLogLevel(),
'verbose'
),
- parallelEncoding: false,
+ parallelEncoding,
+ dir: outputDir,
});
encodedFrames = composition.durationInFrames;
encodedDoneIn = Date.now() - stitchStart;
diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.ts
index bcec4d0bb2..7fb51ffbbb 100644
--- a/packages/renderer/src/stitcher.ts
+++ b/packages/renderer/src/stitcher.ts
@@ -43,6 +43,7 @@ export type StitcherOptions = {
parallelEncoding?: boolean;
preEncodedFileLocation?: string | null;
ffmpegExecutable?: FfmpegExecutable;
+ dir?: string;
};
const getAssetsData = async (options: StitcherOptions) => {
@@ -210,7 +211,9 @@ export const spawnFfmpeg = async (options: StitcherOptions) => {
.reduce<(string | null | undefined)[]>((acc, val) => acc.concat(val), [])
.filter(Boolean) as string[];
- const task = execa(options.ffmpegExecutable ?? 'ffmpeg', ffmpegString);
+ const task = execa(options.ffmpegExecutable ?? 'ffmpeg', ffmpegString, {
+ cwd: options.parallelEncoding ? undefined : options.dir,
+ });
task.stderr?.on('data', (data: Buffer) => {
if (options.onProgress) {
const parsed = parseFfmpegProgress(data.toString());