Skip to content

🎥 Make videos programmatically with React

codec stuff

Jonny Burger3b3867776a41
diff --git a/packages/lambda/src/functions/helpers/concat-videos.ts b/packages/lambda/src/functions/helpers/concat-videos.ts
index cced68ca9c..b62683793e 100644
--- a/packages/lambda/src/functions/helpers/concat-videos.ts
+++ b/packages/lambda/src/functions/helpers/concat-videos.ts
@@ -8,8 +8,10 @@ import {
rmSync,
} from 'fs';
import path, {join} from 'path';
+import {Codec} from 'remotion';
import {AwsRegion} from '../../pricing/aws-regions';
import {chunkKey} from '../../shared/constants';
+import {getFileExtensionFromCodec} from '../../shared/get-file-extension-from-codec';
import {tmpDir} from '../../shared/tmpdir';
import {lambdaLs, lambdaReadFile} from './io';
import {timer} from './timer';
@@ -139,6 +141,7 @@ export const concatVideosS3 = async ({
numberOfFrames,
renderId,
region,
+ codec,
}: {
bucket: string;
expectedFiles: number;
@@ -146,6 +149,7 @@ export const concatVideosS3 = async ({
numberOfFrames: number;
renderId: string;
region: AwsRegion;
+ codec: Codec;
}) => {
const outdir = join(tmpDir('remotion-concat'), 'bucket');
if (existsSync(outdir)) {
@@ -163,7 +167,10 @@ export const concatVideosS3 = async ({
region,
});
- const outfile = join(tmpDir('remotion-concated'), 'concat.mp4');
+ const outfile = join(
+ tmpDir('remotion-concated'),
+ 'concat.' + getFileExtensionFromCodec(codec, 'final')
+ );
const combine = timer('Combine videos');
const filelistDir = tmpDir('remotion-filelist');
await combineVideos({
@@ -172,6 +179,7 @@ export const concatVideosS3 = async ({
output: outfile,
onProgress,
numberOfFrames,
+ codec,
});
combine.end();
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 675f6f1502..83a842e14c 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -175,6 +175,7 @@ const innerLaunchHandler = async (params: LambdaPayload) => {
numberOfFrames: comp.durationInFrames,
renderId: params.renderId,
region: getCurrentRegion(),
+ codec: params.codec,
});
// TODO: Enable or disable chunk optimization
await lambdaWriteFile({
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index 46108b4cfd..b74376f85d 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -12,6 +12,7 @@ import {
LambdaPayloads,
LambdaRoutines,
} from '../shared/constants';
+import {getFileExtensionFromCodec} from '../shared/get-file-extension-from-codec';
import {randomHash} from '../shared/random-hash';
import {tmpDir} from '../shared/tmpdir';
import {
@@ -100,7 +101,10 @@ const renderHandler = async (params: LambdaPayload) => {
const outputLocation = path.join(
outdir,
- `localchunk-${String(params.chunk).padStart(8, '0')}.mkv`
+ `localchunk-${String(params.chunk).padStart(
+ 8,
+ '0'
+ )}.${getFileExtensionFromCodec(params.codec, 'chunk')}`
);
const stitchLabel = timer('stitcher');
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 510773c5d1..12c507cea6 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -25,7 +25,7 @@ export const optimizationProfile = (siteId: string, compositionId: string) =>
`optimization-profiles/${siteId}/${compositionId}/optimization-profile`;
export const getSitesKey = (siteId: string) => `sites/${siteId}`;
export const outName = (renderId: string, codec: Codec) =>
- `${rendersPrefix(renderId)}/out.${getFileExtensionFromCodec(codec)}`;
+ `${rendersPrefix(renderId)}/out.${getFileExtensionFromCodec(codec, 'final')}`;
export const getBinariesBucketName = (region: AwsRegion) => {
return 'lambda-remotion-binaries-' + region;
};
diff --git a/packages/lambda/src/shared/get-file-extension-from-codec.ts b/packages/lambda/src/shared/get-file-extension-from-codec.ts
index f8bed7ddd2..0f37356313 100644
--- a/packages/lambda/src/shared/get-file-extension-from-codec.ts
+++ b/packages/lambda/src/shared/get-file-extension-from-codec.ts
@@ -1,6 +1,9 @@
import {Codec} from 'remotion';
-export const getFileExtensionFromCodec = (codec: Codec) => {
+export const getFileExtensionFromCodec = (
+ codec: Codec,
+ type: 'chunk' | 'final'
+) => {
switch (codec) {
case 'aac':
return 'aac';
@@ -8,7 +11,12 @@ export const getFileExtensionFromCodec = (codec: Codec) => {
return 'mp4';
// The chunks will be rendered as mkv, but the final output will still be MP4
case 'h264-mkv':
+ if (type === 'chunk') {
+ return 'mkv';
+ }
+
return 'mp4';
+
case 'h265':
return 'mp4';
case 'mp3':
diff --git a/packages/renderer/src/combine-videos.ts b/packages/renderer/src/combine-videos.ts
index 8e79f0986a..40bc4bee1f 100644
--- a/packages/renderer/src/combine-videos.ts
+++ b/packages/renderer/src/combine-videos.ts
@@ -3,6 +3,7 @@
import execa from 'execa';
import {rmdirSync, rmSync, writeFileSync} from 'fs';
import {join} from 'path';
+import {Codec, Internals} from 'remotion';
import {parseFfmpegProgress} from './parse-ffmpeg-progress';
export const combineVideos = async ({
@@ -11,12 +12,14 @@ export const combineVideos = async ({
output,
onProgress,
numberOfFrames,
+ codec,
}: {
files: string[];
filelistDir: string;
output: string;
onProgress: (p: number) => void;
numberOfFrames: number;
+ codec: Codec;
}) => {
const fileList = files.map((p) => `file '${p}'`).join('\n');
@@ -24,20 +27,23 @@ export const combineVideos = async ({
writeFileSync(fileListTxt, fileList);
try {
- const task = execa('ffmpeg', [
- '-f',
- 'concat',
- '-safe',
- '0',
- '-i',
- fileListTxt,
- '-c:v',
- 'copy',
- '-c:a',
- 'aac',
- '-y',
- output,
- ]);
+ const task = execa(
+ 'ffmpeg',
+ [
+ '-f',
+ 'concat',
+ '-safe',
+ '0',
+ '-i',
+ fileListTxt,
+ Internals.isAudioCodec(codec) ? null : '-c:v',
+ Internals.isAudioCodec(codec) ? null : 'copy',
+ '-c:a',
+ Internals.isAudioCodec(codec) ? 'copy' : 'aac',
+ '-y',
+ output,
+ ].filter(Internals.truthy)
+ );
task.stderr?.on('data', (data: Buffer) => {
if (onProgress) {
const parsed = parseFfmpegProgress(data.toString());