Skip to content

🎥 Make videos programmatically with React

feat: audio muxing and parallelEncoding progress bar

辉宇42b9ea8a787c
diff --git a/packages/cli/src/progress-bar.ts b/packages/cli/src/progress-bar.ts
index eb8f41ae37..19d983b0df 100644
--- a/packages/cli/src/progress-bar.ts
+++ b/packages/cli/src/progress-bar.ts
@@ -72,18 +72,20 @@ export const makeStitchingProgress = ({
totalFrames,
steps,
doneIn,
+ parallelEncoding,
}: {
frames: number;
totalFrames: number;
steps: number;
doneIn: number | null;
+ parallelEncoding?: boolean;
}) => {
const progress = frames / totalFrames;
return [
'🎞 ',
`(3/${steps})`,
makeProgressBar(progress),
- `${doneIn ? 'Encoded' : 'Encoding'} video`,
+ parallelEncoding?`${doneIn ? 'Muxed' : 'Muxing'} audio`:`${doneIn ? 'Encoded' : 'Encoding'} video`,
doneIn === null ? `${frames}/${totalFrames}` : chalk.gray(`${doneIn}ms`),
].join(' ');
};
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index 5896c15a8d..7c6b3a2a86 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -25,9 +25,9 @@ import {
import {bundleOnCli} from './setup-cache';
import {checkAndValidateFfmpegVersion} from './validate-ffmpeg-version';
import {getActualConcurrency} from "@remotion/renderer/dist/get-concurrency";
-import {ChildProcess} from "child_process";
import {spawnFfmpeg} from "@remotion/renderer/dist/stitcher";
import { ExecaChildProcess } from 'execa';
+import {getUserPassedFileExtension} from "./user-passed-output-location";
const onError = async (info: OnErrorInfo) => {
Log.error();
@@ -87,7 +87,7 @@ export const render = async () => {
});
}
- const steps = (shouldOutputImageSequence||parallelEncoding) ? 2 : 3;
+ const steps = shouldOutputImageSequence ? 2 : 3;
const bundled = await bundleOnCli(fullPath, steps);
@@ -121,10 +121,11 @@ export const render = async () => {
let totalFrames = 0;
const renderStart = Date.now();
- let stitcherFfmpeg: ExecaChildProcess<string>;
+ let stitcherFfmpeg: ExecaChildProcess<string>|undefined;
let preStitcher;
let encodedFrames:number|undefined;
let renderedFrames: number;
+ let preEncodedFileLocation:string|undefined;
const updateRenderProgress=()=>renderProgress.update(
makeRenderingProgress({
frames: renderedFrames||0,
@@ -140,14 +141,15 @@ export const render = async () => {
throw new TypeError('CRF is unexpectedly not a number');
}
+ preEncodedFileLocation=path.join(outputDir,'pre-encode.'+getUserPassedFileExtension());
preStitcher = await spawnFfmpeg({
dir: outputDir,
width: config.width,
height: config.height,
fps: config.fps,
- outputLocation: absoluteOutputFile,
- force: overwrite,
+ outputLocation: preEncodedFileLocation,
+ force: true,
imageFormat,
pixelFormat,
codec,
@@ -198,8 +200,6 @@ export const render = async () => {
});
const {assetsInfo}=await renderer;
if(stitcherFfmpeg){
- // (await stitcherFfmpeg)?.stderr?.pipe(process.stderr);
- // (await stitcherFfmpeg)?.stdout?.pipe(process.stdout);
stitcherFfmpeg?.stdin?.end();
await stitcherFfmpeg;
preStitcher?.cleanup?.();
@@ -227,56 +227,60 @@ export const render = async () => {
throw new TypeError('CRF is unexpectedly not a number');
}
- if(!parallelEncoding){
- const stitchingProgress = createProgressBar();
- stitchingProgress.update(
- makeStitchingProgress({
- doneIn: null,
- frames: 0,
- steps,
- totalFrames,
- })
- );
- const stitchStart = Date.now();
- await stitchFramesToVideo({
- dir: outputDir,
- width: config.width,
- height: config.height,
- fps: config.fps,
- outputLocation: absoluteOutputFile,
- force: overwrite,
- imageFormat,
- pixelFormat,
- codec,
- proResProfile,
- crf,
- assetsInfo,
- parallelism,
- onProgress: (frame: number) => {
- stitchingProgress.update(
- makeStitchingProgress({
- doneIn: null,
- frames: frame,
- steps,
- totalFrames,
- })
- );
- },
- onDownload: (src: string) => {
- Log.info('Downloading asset... ', src);
- },
- verbose: Internals.Logging.isEqualOrBelowLogLevel('verbose'),
- });
- stitchingProgress.update(
- makeStitchingProgress({
- doneIn: Date.now() - stitchStart,
- frames: totalFrames,
- steps,
- totalFrames,
- }) + '\n'
- );
- }
+ const stitchingProgress = createProgressBar();
+
+ stitchingProgress.update(
+ makeStitchingProgress({
+ doneIn: null,
+ frames: 0,
+ steps,
+ totalFrames,
+ parallelEncoding
+ })
+ );
+ const stitchStart = Date.now();
+ await stitchFramesToVideo({
+ dir: outputDir,
+ width: config.width,
+ height: config.height,
+ fps: config.fps,
+ outputLocation: absoluteOutputFile,
+ preEncodedFileLocation,
+ force: overwrite,
+ imageFormat,
+ pixelFormat,
+ codec,
+ proResProfile,
+ crf,
+ assetsInfo,
+ parallelism,
+ onProgress: (frame: number) => {
+ stitchingProgress.update(
+ makeStitchingProgress({
+ doneIn: null,
+ frames: frame,
+ steps,
+ totalFrames,
+ parallelEncoding
+ })
+ );
+ },
+ onDownload: (src: string) => {
+ Log.info('Downloading asset... ', src);
+ },
+ verbose: Internals.Logging.isEqualOrBelowLogLevel('verbose'),
+ });
+ stitchingProgress.update(
+ makeStitchingProgress({
+ doneIn: Date.now() - stitchStart,
+ frames: totalFrames,
+ steps,
+ totalFrames,
+ parallelEncoding
+ }) + '\n'
+ );
+
Log.verbose('Cleaning up...');
try {
diff --git a/packages/renderer/src/render.ts b/packages/renderer/src/render.ts
index 7aa93a2d6f..4e02db992f 100644
--- a/packages/renderer/src/render.ts
+++ b/packages/renderer/src/render.ts
@@ -210,7 +210,7 @@ export const renderFrames = async ({
quality,
options: {
frame,
- output: path.join(
+ output: parallelEncoding?undefined:path.join(
outputDir,
`element-${paddedIndex}.${imageFormat}`
),
diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.ts
index 4dac34d35b..4690261227 100644
--- a/packages/renderer/src/stitcher.ts
+++ b/packages/renderer/src/stitcher.ts
@@ -23,7 +23,6 @@ import {parseFfmpegProgress} from './parse-ffmpeg-progress';
import {resolveAssetSrc} from './resolve-asset-src';
import {validateEvenDimensionsWithCodec} from './validate-even-dimensions-with-codec';
import {validateFfmpeg} from './validate-ffmpeg';
-import {ChildProcess} from "child_process";
export type StitcherOptions= {
dir: string;
@@ -45,6 +44,7 @@ export type StitcherOptions= {
proResProfile?: ProResProfile;
verbose?: boolean;
parallelEncoding?: boolean;
+ preEncodedFileLocation?: string;
}
const getAssetsData=async (options:StitcherOptions)=>{
@@ -53,7 +53,7 @@ const getAssetsData=async (options:StitcherOptions)=>{
const isAudioOnly = encoderName === null;
// eslint-disable-next-line @typescript-eslint/no-shadow
const [frameInfo, fileUrlAssets] = await Promise.all([
- getFrameInfo({
+ options.preEncodedFileLocation?undefined:getFrameInfo({
dir: options.dir,
isAudioOnly,
}),
@@ -161,25 +161,30 @@ export const spawnFfmpeg = async (options: StitcherOptions) => {
const ffmpegArgs = [
['-r', String(options.fps)],
- isAudioOnly ? null : ['-f', options.parallelEncoding?'image2pipe':'image2'],
- isAudioOnly ? null : ['-s', `${options.width}x${options.height}`],
- frameInfo ? ['-start_number', String(frameInfo.startNumber)] : null,
- frameInfo
- ? ['-i', `element-%0${frameInfo.numberLength}d.${imageFormat}`]
- : null,
- options.parallelEncoding?['-i','-']:null,
+ ...options.preEncodedFileLocation?[
+ ['-i',options.preEncodedFileLocation]
+ ]:[
+ isAudioOnly ? null : ['-f', options.parallelEncoding?'image2pipe':'image2'],
+ isAudioOnly ? null : ['-s', `${options.width}x${options.height}`],
+ frameInfo ? ['-start_number', String(frameInfo.startNumber)] : null,
+ frameInfo
+ ? ['-i', `element-%0${frameInfo.numberLength}d.${imageFormat}`]
+ : null,
+ options.parallelEncoding?['-i','-']:null,
+ ],
...assetPaths?assetsToFfmpegInputs({
assets: assetPaths,
isAudioOnly,
fps: options.fps,
frameCount: options.assetsInfo.assets.length,
}): [],
- encoderName
+ options.preEncodedFileLocation?['-c:v','copy']:encoderName
? // -c:v is the same as -vcodec as -codec:video
// and specified the video codec.
['-c:v', encoderName]
: // If only exporting audio, we drop the video explicitly
['-vn'],
+ ...options.preEncodedFileLocation?[]:[
proResProfileName ? ['-profile:v', proResProfileName] : null,
supportsCrf ? ['-crf', String(crf)] : null,
isAudioOnly ? null : ['-pix_fmt', pixelFormat],
@@ -188,6 +193,7 @@ export const spawnFfmpeg = async (options: StitcherOptions) => {
// transparent WebM generation doesn't work
pixelFormat === 'yuva420p' ? ['-auto-alt-ref', '0'] : null,
isAudioOnly ? null : ['-b:v', '1M'],
+ ],
audioCodecName ? ['-c:a', audioCodecName] : null,
complexFilterFlag,
// Ignore audio from image sequence