Skip to content

🎥 Make videos programmatically with React

a bunch of progress

Jonny Burger6491a411390c
diff --git a/packages/lambda/src/functions/helpers/concat-videos.ts b/packages/lambda/src/functions/helpers/concat-videos.ts
index b3d1a4e5ea..6bed487f21 100644
--- a/packages/lambda/src/functions/helpers/concat-videos.ts
+++ b/packages/lambda/src/functions/helpers/concat-videos.ts
@@ -184,6 +184,7 @@ export const concatVideosS3 = async ({
audioCodec,
audioBitrate,
logLevel,
+ framesPerLambda,
}: {
onProgress: (frames: number) => void;
numberOfFrames: number;
@@ -195,6 +196,7 @@ export const concatVideosS3 = async ({
audioCodec: AudioCodec | null;
audioBitrate: string | null;
logLevel: LogLevel;
+ framesPerLambda: number;
}) => {
const outfile = join(
RenderInternals.tmpDir(REMOTION_CONCATED_TOKEN),
@@ -203,6 +205,8 @@ export const concatVideosS3 = async ({
const combine = timer('Combine videos');
const filelistDir = RenderInternals.tmpDir(REMOTION_FILELIST_TOKEN);
+ const chunkDurationInSeconds = framesPerLambda / fps;
+
await RenderInternals.combineVideos({
files,
filelistDir,
@@ -216,6 +220,7 @@ export const concatVideosS3 = async ({
audioBitrate,
indent: false,
logLevel,
+ chunkDurationInSeconds,
});
combine.end();
diff --git a/packages/lambda/src/functions/helpers/merge-chunks.ts b/packages/lambda/src/functions/helpers/merge-chunks.ts
index eacb9fd2fd..2a0945008d 100644
--- a/packages/lambda/src/functions/helpers/merge-chunks.ts
+++ b/packages/lambda/src/functions/helpers/merge-chunks.ts
@@ -38,6 +38,7 @@ import {writePostRenderData} from './write-post-render-data';
export type OnAllChunksAvailable = (options: {
inputProps: SerializedInputProps;
serializedResolvedProps: SerializedInputProps;
+ framesPerLambda: number;
}) => void;
export const mergeChunksAndFinishRender = async (options: {
@@ -61,6 +62,7 @@ export const mergeChunksAndFinishRender = async (options: {
onAllChunks: OnAllChunksAvailable;
audioBitrate: string | null;
logLevel: LogLevel;
+ framesPerLambda: number;
}): Promise<PostRenderData> => {
let lastProgressUploaded = 0;
@@ -147,6 +149,7 @@ export const mergeChunksAndFinishRender = async (options: {
options.onAllChunks({
inputProps: options.inputProps,
serializedResolvedProps: options.serializedResolvedProps,
+ framesPerLambda: options.framesPerLambda,
});
const encodingStart = Date.now();
const {outfile, cleanupChunksProm} = await concatVideosS3({
@@ -160,6 +163,7 @@ export const mergeChunksAndFinishRender = async (options: {
audioCodec: options.audioCodec,
audioBitrate: options.audioBitrate,
logLevel: options.logLevel,
+ framesPerLambda: options.framesPerLambda,
});
const encodingStop = Date.now();
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 2b085bb3ed..c84a7f2b96 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -400,6 +400,7 @@ const innerLaunchHandler = async ({
onAllChunks: onAllChunksAvailable,
audioBitrate: params.audioBitrate,
logLevel: params.logLevel,
+ framesPerLambda,
});
return postRenderData;
@@ -408,6 +409,7 @@ const innerLaunchHandler = async ({
type AllChunksAvailable = {
inputProps: SerializedInputProps;
serializedResolvedProps: SerializedInputProps;
+ framesPerLambda: number;
};
export const launchHandler = async (
@@ -453,6 +455,7 @@ export const launchHandler = async (
serializedResolvedProps: allChunksAvailable.serializedResolvedProps,
inputProps: allChunksAvailable.inputProps,
logLevel: params.logLevel,
+ framesPerLambda: allChunksAvailable.framesPerLambda,
},
retries: 2,
});
@@ -574,8 +577,16 @@ export const launchHandler = async (
functionName,
params,
options,
- onAllChunksAvailable: ({inputProps, serializedResolvedProps}) => {
- allChunksAvailable = {inputProps, serializedResolvedProps};
+ onAllChunksAvailable: ({
+ inputProps,
+ serializedResolvedProps,
+ framesPerLambda,
+ }) => {
+ allChunksAvailable = {
+ inputProps,
+ serializedResolvedProps,
+ framesPerLambda,
+ };
},
});
clearTimeout(webhookDueToTimeout);
diff --git a/packages/lambda/src/functions/merge.ts b/packages/lambda/src/functions/merge.ts
index 1daf0093f1..f9c87656e6 100644
--- a/packages/lambda/src/functions/merge.ts
+++ b/packages/lambda/src/functions/merge.ts
@@ -72,6 +72,7 @@ export const mergeHandler = async (
},
audioBitrate: renderMetadata.audioBitrate,
logLevel: params.logLevel,
+ framesPerLambda: params.framesPerLambda,
});
return {type: 'success' as const, postRenderData};
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index a448f83b7d..a2adc1b9f6 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -101,10 +101,7 @@ const renderHandler = async (
const outdir = RenderInternals.tmpDir(RENDERER_PATH_TOKEN);
- const chunkCodec: Codec =
- params.codec === 'gif' || params.codec === 'h264'
- ? 'h264-mkv'
- : params.codec;
+ const chunkCodec: Codec = params.codec === 'gif' ? 'h264-mkv' : params.codec;
const outputLocation = path.join(
outdir,
@@ -115,7 +112,7 @@ const renderHandler = async (
chunkCodec,
RenderInternals.getDefaultAudioCodec({
codec: params.codec,
- preferLossless: true,
+ preferLossless: params.preferLossless,
}),
)}`,
);
@@ -210,11 +207,8 @@ const renderHandler = async (
videoBitrate: params.videoBitrate,
encodingBufferSize: params.encodingBufferSize,
encodingMaxRate: params.encodingMaxRate,
- // Lossless flag takes priority over audio codec
- // https://github.com/remotion-dev/remotion/issues/1647
- // Special flag only in Lambda renderer which improves the audio quality
audioCodec: null,
- preferLossless: true,
+ preferLossless: params.preferLossless,
browserExecutable: executablePath(),
cancelSignal: undefined,
disallowParallelEncoding: false,
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 62cf573c7d..0cf4b26fdb 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -430,6 +430,7 @@ export type LambdaPayloads = {
inputProps: SerializedInputProps;
serializedResolvedProps: SerializedInputProps;
logLevel: LogLevel;
+ framesPerLambda: number;
};
};
diff --git a/packages/renderer/src/combine-audio.ts b/packages/renderer/src/combine-audio.ts
index eff5c7748c..58a1c11b40 100644
--- a/packages/renderer/src/combine-audio.ts
+++ b/packages/renderer/src/combine-audio.ts
@@ -1,5 +1,6 @@
import {rmSync, writeFileSync} from 'fs';
import {join} from 'path';
+import {VERSION} from 'remotion/version';
import type {AudioCodec} from './audio-codec';
import {mapAudioCodecToFfmpegAudioCodecName} from './audio-codec';
import {callFf} from './call-ffmpeg';
@@ -25,6 +26,7 @@ const encodeAudio = async ({
output,
indent,
logLevel,
+ addRemotionMetadata,
}: {
files: string[];
resolvedAudioCodec: AudioCodec;
@@ -33,17 +35,18 @@ const encodeAudio = async ({
output: string;
indent: boolean;
logLevel: LogLevel;
+ addRemotionMetadata: boolean;
}) => {
const fileList = files.map((p) => `file '${p}'`).join('\n');
const fileListTxt = join(filelistDir, 'audio-files.txt');
writeFileSync(fileListTxt, fileList);
const command = [
- '-i',
'-f',
'concat',
'-safe',
'0',
+ '-i',
fileListTxt,
'-c:a',
mapAudioCodecToFfmpegAudioCodecName(resolvedAudioCodec),
@@ -52,6 +55,8 @@ const encodeAudio = async ({
'-b:a',
audioBitrate ? audioBitrate : '320k',
'-vn',
+ addRemotionMetadata ? `-metadata` : null,
+ addRemotionMetadata ? `comment=Made with Remotion ${VERSION}` : null,
'-y',
output,
];
@@ -86,6 +91,7 @@ const combineAudioSeamlessly = async ({
logLevel,
output,
chunkDurationInSeconds,
+ addRemotionMetadata,
}: {
files: string[];
filelistDir: string;
@@ -93,6 +99,7 @@ const combineAudioSeamlessly = async ({
logLevel: LogLevel;
output: string;
chunkDurationInSeconds: number;
+ addRemotionMetadata: boolean;
}) => {
const fileList = files
.map((p, i) => {
@@ -124,7 +131,9 @@ const combineAudioSeamlessly = async ({
`file '${p}'`,
`inpoint ${inpoint}us`,
isLast ? null : `outpoint ${outpoint}us`,
- ].filter(truthy);
+ ]
+ .filter(truthy)
+ .join('\n');
})
.join('\n');
@@ -138,9 +147,11 @@ const combineAudioSeamlessly = async ({
'0',
'-i',
fileListTxt,
- '-c',
+ '-c:a',
'copy',
'-vn',
+ addRemotionMetadata ? `-metadata` : null,
+ addRemotionMetadata ? `comment=Made with Remotion ${VERSION}` : null,
'-y',
output,
];
@@ -178,6 +189,7 @@ export const createCombinedAudio = ({
resolvedAudioCodec,
output,
chunkDurationInSeconds,
+ addRemotionMetadata,
}: {
seamless: boolean;
filelistDir: string;
@@ -188,6 +200,7 @@ export const createCombinedAudio = ({
resolvedAudioCodec: AudioCodec;
output: string;
chunkDurationInSeconds: number;
+ addRemotionMetadata: boolean;
}): Promise<string> => {
if (seamless) {
return combineAudioSeamlessly({
@@ -197,6 +210,7 @@ export const createCombinedAudio = ({
logLevel,
output,
chunkDurationInSeconds,
+ addRemotionMetadata,
});
}
@@ -208,5 +222,6 @@ export const createCombinedAudio = ({
output,
indent,
logLevel,
+ addRemotionMetadata,
});
};
diff --git a/packages/renderer/src/combine-video-streams.ts b/packages/renderer/src/combine-video-streams.ts
index 1db0df7576..5e64a401a0 100644
--- a/packages/renderer/src/combine-video-streams.ts
+++ b/packages/renderer/src/combine-video-streams.ts
@@ -19,6 +19,7 @@ export const combineVideoStreams = async ({
logLevel,
onProgress,
files,
+ addRemotionMetadata,
}: {
fps: number;
codec: Codec;
@@ -29,6 +30,7 @@ export const combineVideoStreams = async ({
logLevel: LogLevel;
onProgress: (p: number) => void;
files: string[];
+ addRemotionMetadata: boolean;
}) => {
const fileList = files.map((p) => `file '${p}'`).join('\n');
@@ -53,8 +55,8 @@ export const combineVideoStreams = async ({
codec === 'gif' ? 'gif' : 'copy',
codec === 'h264' ? '-movflags' : null,
codec === 'h264' ? 'faststart' : null,
- `-metadata`,
- `comment=Made with Remotion ${VERSION}`,
+ addRemotionMetadata ? `-metadata` : null,
+ addRemotionMetadata ? `comment=Made with Remotion ${VERSION}` : null,
'-y',
output,
].filter(truthy);
diff --git a/packages/renderer/src/combine-videos.ts b/packages/renderer/src/combine-videos.ts
index fc5d920f9a..49087abe0a 100644
--- a/packages/renderer/src/combine-videos.ts
+++ b/packages/renderer/src/combine-videos.ts
@@ -2,18 +2,15 @@
import {rmSync} from 'node:fs';
import {join} from 'node:path';
-import {VERSION} from 'remotion/version';
+import {RenderInternals} from '.';
import type {AudioCodec} from './audio-codec';
import {getDefaultAudioCodec} from './audio-codec';
-import {callFf} from './call-ffmpeg';
import type {Codec} from './codec';
import {createCombinedAudio} from './combine-audio';
import {combineVideoStreams} from './combine-video-streams';
import {isAudioCodec} from './is-audio-codec';
import type {LogLevel} from './log-level';
-import {Log} from './logger';
-import {parseFfmpegProgress} from './parse-ffmpeg-progress';
-import {truthy} from './truthy';
+import {muxVideoAndAudio} from './mux-video-and-audio';
type Options = {
files: string[];
@@ -48,8 +45,7 @@ export const combineVideos = async (options: Options) => {
chunkDurationInSeconds,
} = options;
- const audioOutput = join(filelistDir, 'audio');
- const videoOutput = join(filelistDir, 'video');
+ // TODO: onProgress is now reused across 3 functions
const resolvedAudioCodec =
audioCodec ?? getDefaultAudioCodec({codec, preferLossless: false});
@@ -57,6 +53,21 @@ export const combineVideos = async (options: Options) => {
const shouldCreateAudio = resolvedAudioCodec !== null;
const shouldCreateVideo = !isAudioCodec(codec);
+ const newLocal = shouldCreateAudio
+ ? `audio.${
+ resolvedAudioCodec === 'pcm-16'
+ ? 'wav'
+ : resolvedAudioCodec === 'opus'
+ ? 'opus'
+ : RenderInternals.getFileExtensionFromCodec(
+ resolvedAudioCodec,
+ null,
+ )
+ }`
+ : null;
+ const audioOutput = newLocal ? join(filelistDir, newLocal) : null;
+ const videoOutput = join(filelistDir, 'video');
+
if (shouldCreateAudio) {
await createCombinedAudio({
audioBitrate,
@@ -64,10 +75,11 @@ export const combineVideos = async (options: Options) => {
files,
indent,
logLevel,
- output: audioOutput,
+ output: shouldCreateVideo && audioOutput ? audioOutput : output,
resolvedAudioCodec,
seamless: resolvedAudioCodec === 'aac',
chunkDurationInSeconds,
+ addRemotionMetadata: !shouldCreateVideo,
});
}
@@ -80,46 +92,26 @@ export const combineVideos = async (options: Options) => {
logLevel,
numberOfGifLoops,
onProgress,
- output: videoOutput,
+ output: shouldCreateAudio ? videoOutput : output,
files,
+ addRemotionMetadata: !shouldCreateAudio,
});
}
- const command = [
- '-i',
- videoOutput,
- '-i',
- audioOutput,
- '-c:v',
- 'copy',
- '-c:a',
- 'copy',
- `-metadata`,
- `comment=Made with Remotion ${VERSION}`,
- '-y',
- output,
- ].filter(truthy);
-
- Log.verbose({indent, logLevel}, 'Combining command: ', command);
+ if (!(shouldCreateAudio && shouldCreateVideo)) {
+ rmSync(filelistDir, {recursive: true});
+ return;
+ }
try {
- const task = callFf({
- bin: 'ffmpeg',
- args: command,
- indent: options.indent,
- logLevel: options.logLevel,
- });
- task.stderr?.on('data', (data: Buffer) => {
- const parsed = parseFfmpegProgress(data.toString('utf8'));
- if (parsed === undefined) {
- Log.verbose({indent, logLevel}, data.toString('utf8'));
- } else {
- Log.verbose({indent, logLevel}, `Combined ${parsed} frames`);
- onProgress(parsed);
- }
+ await muxVideoAndAudio({
+ audioOutput: audioOutput as string,
+ indent,
+ logLevel,
+ onProgress,
+ output,
+ videoOutput,
});
-
- await task;
onProgress(numberOfFrames);
rmSync(filelistDir, {recursive: true});
} catch (err) {
diff --git a/packages/renderer/src/mux-video-and-audio.ts b/packages/renderer/src/mux-video-and-audio.ts
new file mode 100644
index 0000000000..2b2a032bee
--- /dev/null
+++ b/packages/renderer/src/mux-video-and-audio.ts
@@ -0,0 +1,57 @@
+import {VERSION} from 'remotion/version';
+import {callFf} from './call-ffmpeg';
+import type {LogLevel} from './log-level';
+import {Log} from './logger';
+import {parseFfmpegProgress} from './parse-ffmpeg-progress';
+import {truthy} from './truthy';
+
+export const muxVideoAndAudio = async ({
+ videoOutput,
+ audioOutput,
+ output,
+ indent,
+ logLevel,
+ onProgress,
+}: {
+ videoOutput: string;
+ audioOutput: string;
+ output: string;
+ indent: boolean;
+ logLevel: LogLevel;
+ onProgress: (p: number) => void;
+}) => {
+ const command = [
+ '-i',
+ videoOutput,
+ '-i',
+ audioOutput,
+ '-c:v',
+ 'copy',
+ '-c:a',
+ 'copy',
+ `-metadata`,
+ `comment=Made with Remotion ${VERSION}`,
+ '-y',
+ output,
+ ].filter(truthy);
+
+ Log.verbose({indent, logLevel}, 'Combining command: ', command);
+
+ const task = callFf({
+ bin: 'ffmpeg',
+ args: command,
+ indent,
+ logLevel,
+ });
+ task.stderr?.on('data', (data: Buffer) => {
+ const parsed = parseFfmpegProgress(data.toString('utf8'));
+ if (parsed === undefined) {
+ Log.verbose({indent, logLevel}, data.toString('utf8'));
+ } else {
+ Log.verbose({indent, logLevel}, `Combined ${parsed} frames`);
+ onProgress(parsed);
+ }
+ });
+
+ await task;
+};