Skip to content

🎥 Make videos programmatically with React

use native nodejs child process instead of execa

Jonny Burgerbf7be2351cb8
diff --git a/packages/renderer/src/call-ffmpeg.ts b/packages/renderer/src/call-ffmpeg.ts
index e39c678cad..d7dcef3f95 100644
--- a/packages/renderer/src/call-ffmpeg.ts
+++ b/packages/renderer/src/call-ffmpeg.ts
@@ -1,4 +1,6 @@
import execa from 'execa';
+import type {SpawnOptionsWithoutStdio} from 'node:child_process';
+import {spawn} from 'node:child_process';
import {chmodSync} from 'node:fs';
import path from 'path';
import {getExecutablePath} from './compositor/get-executable-path';
@@ -28,3 +30,27 @@ export const callFf = ({
...options,
});
};
+
+export const callFfNative = ({
+ args,
+ bin,
+ indent,
+ logLevel,
+ options,
+}: {
+ bin: 'ffmpeg' | 'ffprobe';
+ args: (string | null)[];
+ indent: boolean;
+ logLevel: LogLevel;
+ options?: SpawnOptionsWithoutStdio;
+}) => {
+ const executablePath = getExecutablePath(bin, indent, logLevel);
+ if (!process.env.READ_ONLY_FS) {
+ chmodSync(executablePath, 0o755);
+ }
+
+ return spawn(executablePath, args.filter(truthy), {
+ cwd: path.dirname(executablePath),
+ ...options,
+ });
+};
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index ad24b3602f..01ad2b589f 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -15,7 +15,7 @@ import {
getDefaultAudioCodec,
mapAudioCodecToFfmpegAudioCodecName,
} from './audio-codec';
-import {callFf} from './call-ffmpeg';
+import {callFf, callFfNative} from './call-ffmpeg';
import type {Codec} from './codec';
import {DEFAULT_CODEC} from './codec';
import {codecSupportsMedia} from './codec-supports-media';
@@ -107,10 +107,7 @@ export type StitchFramesToVideoOptions = {
colorSpace?: ColorSpace;
};
-type ReturnType = {
- task: Promise<Buffer | null>;
- getLogs: () => string;
-};
+type ReturnType = Promise<Buffer | null>;
const getAssetsData = async ({
assets,
@@ -405,10 +402,7 @@ const innerStitchFramesToVideo = async (
});
deleteDirectory(assetsInfo.downloadMap.stitchFrames);
- return {
- getLogs: () => '',
- task: Promise.resolve(file),
- };
+ return Promise.resolve(file);
}
const ffmpegArgs = [
@@ -476,7 +470,7 @@ const innerStitchFramesToVideo = async (
finalFfmpegString.join(' '),
);
- const task = callFf({
+ const task = callFfNative({
bin: 'ffmpeg',
args: finalFfmpegString,
indent,
@@ -485,11 +479,12 @@ const innerStitchFramesToVideo = async (
cancelSignal?.(() => {
task.kill();
});
- let ffmpegOutput = '';
+ let ffmpegStderr = '';
let isFinished = false;
+
task.stderr?.on('data', (data: Buffer) => {
const str = data.toString();
- ffmpegOutput += str;
+ ffmpegStderr += str;
if (onProgress) {
const parsed = parseFfmpegProgress(str);
// FFMPEG bug: In some cases, FFMPEG does hang after it is finished with it's job
@@ -509,35 +504,48 @@ const innerStitchFramesToVideo = async (
}
});
- return {
- task: task.then(() => {
- assetsInfo.downloadMap.allowCleanup();
+ return new Promise<Buffer | null>((resolve, reject) => {
+ task.once('close', (code, signal) => {
+ if (code === 0) {
+ assetsInfo.downloadMap.allowCleanup();
- if (tempFile === null) {
- cleanDownloadMap(assetsInfo.downloadMap);
- return null;
- }
+ if (tempFile === null) {
+ cleanDownloadMap(assetsInfo.downloadMap);
+ return resolve(null);
+ }
- return promises.readFile(tempFile).finally(() => {
- cleanDownloadMap(assetsInfo.downloadMap);
- });
- }),
- getLogs: () => ffmpegOutput,
- };
+ promises
+ .readFile(tempFile)
+ .then((f) => {
+ resolve(f);
+ })
+ .catch((e) => {
+ reject(e);
+ })
+ .finally(() => {
+ cleanDownloadMap(assetsInfo.downloadMap);
+ });
+ } else {
+ reject(
+ new Error(
+ `FFmpeg quit with code ${code} ${
+ signal ? `(${signal})` : ''
+ } The FFmpeg output was ${ffmpegStderr}`,
+ ),
+ );
+ }
+ });
+ });
};
export const internalStitchFramesToVideo = async (
options: InternalStitchFramesToVideoOptions,
): Promise<Buffer | null> => {
const remotionRoot = findRemotionRoot();
- const {task, getLogs} = await innerStitchFramesToVideo(options, remotionRoot);
-
- const happyPath = task.catch(() => {
- throw new Error(getLogs());
- });
+ const task = await innerStitchFramesToVideo(options, remotionRoot);
return Promise.race([
- happyPath,
+ task,
new Promise<Buffer | null>((_resolve, reject) => {
options.cancelSignal?.(() => {
reject(new Error(cancelErrorMessages.stitchFramesToVideo));