Skip to content

🎥 Make videos programmatically with React

improve rendering with small scale

Jonny Burger83aa99a42855
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index c1f901f625..70dea2072b 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -114,6 +114,7 @@ export const renderMedia = async ({
const renderStart = Date.now();
const tmpdir = tmpDir('pre-encode');
const parallelEncoding = canUseParallelEncoding(codec);
+ const actualImageFormat = imageFormat ?? 'jpeg';
const preEncodedFileLocation = parallelEncoding
? path.join(
@@ -167,6 +168,7 @@ export const renderMedia = async ({
internalOptions: {
parallelEncoding,
preEncodedFileLocation: null,
+ imageFormat: actualImageFormat,
},
assetsInfo: null,
});
@@ -196,7 +198,7 @@ export const renderMedia = async ({
},
inputProps,
envVariables,
- imageFormat: imageFormat ?? 'jpeg',
+ imageFormat: actualImageFormat,
quality,
frameRange: frameRange ?? null,
puppeteerInstance,
@@ -219,8 +221,13 @@ export const renderMedia = async ({
if (stitcherFfmpeg) {
await waitForFinish();
stitcherFfmpeg?.stdin?.end();
- await stitcherFfmpeg;
- preStitcher?.cleanup?.();
+ try {
+ await stitcherFfmpeg;
+ } catch (err) {
+ throw new Error(preStitcher?.getLogs());
+ } finally {
+ preStitcher?.cleanup?.();
+ }
}
renderedDoneIn = Date.now() - renderStart;
@@ -238,6 +245,7 @@ export const renderMedia = async ({
internalOptions: {
parallelEncoding: false,
preEncodedFileLocation,
+ imageFormat: actualImageFormat,
},
force: overwrite ?? Internals.DEFAULT_OVERWRITE,
pixelFormat,
diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.ts
index cc4c9d2828..f76ba0f6ca 100644
--- a/packages/renderer/src/stitcher.ts
+++ b/packages/renderer/src/stitcher.ts
@@ -2,6 +2,7 @@ import execa from 'execa';
import {
Codec,
FfmpegExecutable,
+ ImageFormat,
Internals,
PixelFormat,
ProResProfile,
@@ -45,6 +46,7 @@ export type StitcherOptions = {
internalOptions?: {
preEncodedFileLocation: string | null;
parallelEncoding: boolean;
+ imageFormat: ImageFormat;
};
};
@@ -160,7 +162,9 @@ export const spawnFfmpeg = async (options: StitcherOptions) => {
: [
[
'-f',
- options.internalOptions?.parallelEncoding ? 'image2pipe' : 'image2',
+ options.internalOptions?.parallelEncoding
+ ? ['image2pipe']
+ : 'image2',
],
['-s', `${options.width}x${options.height}`],
options.assetsInfo
@@ -169,6 +173,16 @@ export const spawnFfmpeg = async (options: StitcherOptions) => {
options.assetsInfo
? ['-i', options.assetsInfo.imageSequenceName]
: null,
+ // If scale is very small (like 0.1), FFMPEG cannot figure out the image
+ // format on it's own and we need to hint the format
+ options.internalOptions?.parallelEncoding
+ ? [
+ '-vcodec',
+ options.internalOptions.imageFormat === 'jpeg'
+ ? 'mjpeg'
+ : 'png',
+ ]
+ : null,
options.internalOptions?.parallelEncoding ? ['-i', '-'] : null,
]),
...(options.assetsInfo && assetPositions
@@ -214,28 +228,34 @@ export const spawnFfmpeg = async (options: StitcherOptions) => {
console.log(ffmpegArgs);
}
- const ffmpegString = ffmpegArgs
- .reduce<(string | null | undefined)[]>((acc, val) => acc.concat(val), [])
- .filter(Boolean) as string[];
+ const ffmpegString = ffmpegArgs.flat(2).filter(Boolean) as string[];
const task = execa(options.ffmpegExecutable ?? 'ffmpeg', ffmpegString, {
cwd: options.internalOptions?.parallelEncoding ? undefined : options.dir,
});
+ let ffmpegOutput = '';
task.stderr?.on('data', (data: Buffer) => {
+ const str = data.toString();
+ ffmpegOutput += str;
if (options.onProgress) {
- const parsed = parseFfmpegProgress(data.toString());
+ const parsed = parseFfmpegProgress(str);
if (parsed !== undefined) {
options.onProgress(parsed);
}
}
});
- return {task, cleanup};
+ return {task, cleanup, getLogs: () => ffmpegOutput};
};
export const stitchFramesToVideo = async (
options: StitcherOptions
): Promise<void> => {
- const {task, cleanup} = await spawnFfmpeg(options);
- await task;
- cleanup?.();
+ const {task, cleanup, getLogs} = await spawnFfmpeg(options);
+ try {
+ await task;
+ } catch (err) {
+ throw new Error(getLogs());
+ } finally {
+ cleanup?.();
+ }
};