Skip to content

🎥 Make videos programmatically with React

Merge remote-tracking branch 'upstream/main' into audio

yuta080164493e71f8db
diff --cc packages/cli/src/render.tsx
index 1d2edf05b6,8790164bd8..78f203816b
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@@ -91,9 -146,9 +146,9 @@@ export const render = async () =>
},
cliProgress.Presets.shades_grey
);
- await renderFrames({
+ const assets = await renderFrames({
config,
- onFrameUpdate: (f) => renderProgress.update(f),
+ onFrameUpdate: (frame) => renderProgress.update(frame),
parallelism,
compositionId,
outputDir,
@@@ -123,17 -190,28 +190,29 @@@
fps: config.fps,
outputLocation: absoluteOutputFile,
force: overwrite,
- imageFormat: getFrameFormat(renderMode),
- pixelFormat: Internals.getPixelFormat(),
+ imageFormat,
+ pixelFormat,
+ codec,
+ crf,
+ assets,
+ onProgress: (frame) => {
+ stitchingProgress.update(frame);
+ },
});
+ stitchingProgress.stop();
+
console.log('Cleaning up...');
- await fs.promises.rmdir(outputDir, {
- recursive: true,
- });
+ await Promise.all([
+ fs.promises.rmdir(outputDir, {
+ recursive: true,
+ }),
+ fs.promises.rmdir(bundled, {
+ recursive: true,
+ }),
+ ]);
console.log('\n▶️ Your video is ready - hit play!');
} else {
- console.log('\n▶️ Your PNG sequence is ready!');
+ console.log('\n▶️ Your image sequence is ready!');
}
console.log(absoluteOutputFile);
};
diff --cc packages/example/src/Video.tsx
index 7b296d3a12,6d3b43e4fe..780719fd2e
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@@ -125,14 -129,14 +129,22 @@@ export const Index: React.FC = () =>
fps={30}
durationInFrames={10}
/>
+ <Composition
+ id="missing-img"
+ component={MissingImg}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={10}
+ />
+ <Composition
+ id="sound"
+ lazyComponent={() => import('./Sound')}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={300}
+ />
</>
);
};
diff --cc packages/renderer/src/render.ts
index 14913a36f7,97f08e51c2..1917803155
--- a/packages/renderer/src/render.ts
+++ b/packages/renderer/src/render.ts
@@@ -57,10 -56,12 +57,12 @@@ export const renderFrames = async (
const pool = new Pool(await Promise.all(pages));
const {durationInFrames: frames} = config;
- const filePadLength = String(frames).length;
+ // Substract one because 100 frames will be 00-99
+ // --> 2 digits
+ const filePadLength = String(frames - 1).length;
let framesRendered = 0;
onStart();
- await Promise.all(
+ const assetsFrames = await Promise.all(
new Array(frames)
.fill(Boolean)
.map((x, i) => i)
diff --cc packages/renderer/src/stitcher.ts
index f678c4705e,7a04158086..7abd1ace28
--- a/packages/renderer/src/stitcher.ts
+++ b/packages/renderer/src/stitcher.ts
@@@ -1,11 -1,26 +1,28 @@@
import execa from 'execa';
import fs from 'fs';
- import {PixelFormat} from 'remotion';
+ import {Codec, Internals, PixelFormat} from 'remotion';
+import url from 'url';
+import {Assets} from './assets';
import {DEFAULT_IMAGE_FORMAT, ImageFormat} from './image-format';
+ import {parseFfmpegProgress} from './parse-ffmpeg-progress';
import {validateFfmpeg} from './validate-ffmpeg';
+ const getCodecName = (codec: Codec): string => {
+ if (codec === 'h264') {
+ return 'libx264';
+ }
+ if (codec === 'h265') {
+ return 'libx265';
+ }
+ if (codec === 'vp8') {
+ return 'libvpx';
+ }
+ if (codec === 'vp9') {
+ return 'libvpx-vp9';
+ }
+ throw new TypeError(`Cannot find FFMPEG codec for ${codec}`);
+ };
+
export const stitchFramesToVideo = async (options: {
dir: string;
fps: number;
@@@ -15,9 -30,14 +32,15 @@@
force: boolean;
imageFormat?: ImageFormat;
pixelFormat?: PixelFormat;
+ codec?: Codec;
+ crf?: number;
+ assets: Assets;
+ onProgress?: (num: number) => void;
}): Promise<void> => {
- const format = options.imageFormat ?? DEFAULT_IMAGE_FORMAT;
+ const codec = options.codec ?? Internals.DEFAULT_CODEC;
+ const crf = options.crf ?? Internals.getDefaultCrfForCodec(codec);
+ const imageFormat = options.imageFormat ?? DEFAULT_IMAGE_FORMAT;
+ const pixelFormat = options.pixelFormat ?? Internals.DEFAULT_PIXEL_FORMAT;
await validateFfmpeg();
const files = await fs.promises.readdir(options.dir);
const biggestNumber = Math.max(
@@@ -30,51 -50,48 +53,72 @@@
);
const numberLength = String(biggestNumber).length;
- await execa(
- 'ffmpeg',
- [
- '-r',
- String(options.fps),
- '-f',
- 'image2',
- '-s',
- `${options.width}x${options.height}`,
- '-i',
- `element-%0${numberLength}d.${format}`,
- ...options.assets.reduce<string[]>(
- (acc, asset) => [...acc, '-i', url.fileURLToPath(asset.src)],
- []
- ),
- '-vcodec',
- 'libx264',
- '-crf',
- '16',
- '-pix_fmt',
- options.pixelFormat ?? 'yuv420p',
- '-acodec',
- 'aac',
- '-filter_complex',
- [
- ...options.assets.map((asset, i) => {
- const duration = asset.duration
- ? (asset.duration / options.fps).toFixed(3)
- : undefined; // in secounds with millisecounds level precision
- const from = ((asset.from / options.fps) * 1000).toFixed(); // in milliseconds
- return [
- `[${i + 1}:a]`,
- duration ? `atrim=0:${duration},` : '',
- `adelay=${from}`,
- `[a${i + 1}]`,
- ].join('');
- }),
- `${options.assets.map((asset, i) => `[a${i + 1}]`).join('')}amix=${
- options.assets.length
- },dynaudnorm`,
- ].join(';'),
- options.force ? '-y' : null,
- '-shortest',
- options.outputLocation,
- ].filter(Boolean) as string[],
- {cwd: options.dir}
+ const encoderName = getCodecName(codec);
+ Internals.validateSelectedCrfAndCodecCombination(crf, codec);
+ Internals.validateSelectedPixelFormatAndImageFormatCombination(
+ pixelFormat,
+ imageFormat
);
+ Internals.validateSelectedPixelFormatAndCodecCombination(pixelFormat, codec);
+
+ const ffmpegArgs = [
+ '-r',
+ String(options.fps),
+ '-f',
+ 'image2',
+ '-s',
+ `${options.width}x${options.height}`,
+ '-i',
+ `element-%0${numberLength}d.${imageFormat}`,
++ ...options.assets.reduce<string[]>(
++ (acc, asset) => [...acc, '-i', url.fileURLToPath(asset.src)],
++ []
++ ),
+ '-c:v',
+ encoderName,
+ '-crf',
+ crf,
+ '-b:v',
+ '1M',
++ '-c:a',
++ 'aac',
+ options.force ? '-y' : null,
+ '-pix_fmt',
++ '-filter_complex',
++ [
++ ...options.assets.map((asset, i) => {
++ const duration = asset.duration
++ ? (asset.duration / options.fps).toFixed(3)
++ : undefined; // in secounds with millisecounds level precision
++ const from = ((asset.from / options.fps) * 1000).toFixed(); // in milliseconds
++ return [
++ `[${i + 1}:a]`,
++ duration ? `atrim=0:${duration},` : '',
++ `adelay=${from}`,
++ `[a${i + 1}]`,
++ ].join('');
++ }),
++ `${options.assets.map((asset, i) => `[a${i + 1}]`).join('')}amix=${
++ options.assets.length
++ },dynaudnorm`,
++ ].join(';'),
+ pixelFormat,
+ // Without explicitly disabling auto-alt-ref,
+ // transparent WebM generation doesn't work
+ pixelFormat === 'yuva420p' ? '-auto-alt-ref' : null,
+ pixelFormat === 'yuva420p' ? '0' : null,
+ options.outputLocation,
+ ].filter(Boolean) as string[];
+
+ const task = execa('ffmpeg', ffmpegArgs, {cwd: options.dir});
+
+ task.stderr?.on('data', (data: Buffer) => {
+ if (options.onProgress) {
+ const parsed = parseFfmpegProgress(data.toString());
+ if (parsed !== undefined) {
+ options.onProgress(parsed);
+ }
+ }
+ });
+ await task;
};