🎥 Make videos programmatically with React
seamless
Jonny Burgere465aa757a39
diff --git a/packages/lambda/src/functions/helpers/can-concat-seamlessly.ts b/packages/lambda/src/functions/helpers/can-concat-seamlessly.tsnew file mode 100644index 0000000000..9c69d4fee3--- /dev/null+++ b/packages/lambda/src/functions/helpers/can-concat-seamlessly.ts@@ -0,0 +1,8 @@+import type {AudioCodec, Codec} from '@remotion/renderer';++export const canConcatSeamlessly = (+ audioCodec: AudioCodec | null,+ codec: Codec,+) => {+ return audioCodec === 'aac' && codec === 'h264';+};diff --git a/packages/lambda/src/functions/helpers/concat-videos.ts b/packages/lambda/src/functions/helpers/concat-videos.tsindex fbf0e13a96..2b0cba219e 100644--- a/packages/lambda/src/functions/helpers/concat-videos.ts+++ b/packages/lambda/src/functions/helpers/concat-videos.ts@@ -11,6 +11,7 @@ import {rendersPrefix,} from '../../shared/constants';import type {LambdaCodec} from '../../shared/validate-lambda-codec';+import {canConcatSeamlessly} from './can-concat-seamlessly';import {inspectErrors} from './inspect-errors';import {lambdaLs, lambdaReadFile} from './io';import {timer} from './timer';@@ -217,6 +218,8 @@ export const concatVideosS3 = async ({const chunkDurationInSeconds = framesPerLambda / fps;+ const seamless = canConcatSeamlessly(audioCodec, codec);+await RenderInternals.combineVideos({files,filelistDir,@@ -233,6 +236,7 @@ export const concatVideosS3 = async ({chunkDurationInSeconds,binariesDirectory,cancelSignal,+ seamless,});combine.end();diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.tsindex 36d68475bb..8d2a180967 100644--- a/packages/lambda/src/functions/renderer.ts+++ b/packages/lambda/src/functions/renderer.ts@@ -20,6 +20,7 @@ import type {ChunkTimingData,ObjectChunkTimingData,} from './chunk-optimization/types';+import {canConcatSeamlessly} from './helpers/can-concat-seamlessly';import {forgetBrowserEventLoop,getBrowserInstance,@@ -102,18 +103,19 @@ const renderHandler = async (const outdir = RenderInternals.tmpDir(RENDERER_PATH_TOKEN);- const chunkCodec: Codec =- params.codec === 'gif'- ? 'h264-mkv'- : params.codec === 'h264'- ? 'h264-ts'- : params.codec;const chunk = `localchunk-${String(params.chunk).padStart(8, '0')}`;const defaultAudioCodec = RenderInternals.getDefaultAudioCodec({codec: params.codec,preferLossless: params.preferLossless,});+ const chunkCodec: Codec =+ params.codec === 'gif'+ ? 'h264-mkv'+ : canConcatSeamlessly(defaultAudioCodec, params.codec)+ ? 'h264-ts'+ : params.codec;+const videoExtension = RenderInternals.getFileExtensionFromCodec(chunkCodec,defaultAudioCodec,@@ -256,38 +258,38 @@ const renderHandler = async ('Writing chunk to S3',);const writeStart = Date.now();- await lambdaWriteFile({- bucketName: params.bucketName,- key: chunkKeyForIndex({- renderId: params.renderId,- index: params.chunk,- type: 'video',- }),- body: fs.createReadStream(videoOutputLocation),- region: getCurrentRegionInFunction(),- privacy: params.privacy,- expectedBucketOwner: options.expectedBucketOwner,- downloadBehavior: null,- customCredentials: null,- });-- // TODO: Make it parallel- if (audioOutputLocation) {- await lambdaWriteFile({+ await Promise.all([+ lambdaWriteFile({bucketName: params.bucketName,key: chunkKeyForIndex({renderId: params.renderId,index: params.chunk,- type: 'audio',+ type: 'video',}),- body: fs.createReadStream(audioOutputLocation),+ body: fs.createReadStream(videoOutputLocation),region: getCurrentRegionInFunction(),privacy: params.privacy,expectedBucketOwner: options.expectedBucketOwner,downloadBehavior: null,customCredentials: null,- });- }+ }),+ audioOutputLocation+ ? lambdaWriteFile({+ bucketName: params.bucketName,+ key: chunkKeyForIndex({+ renderId: params.renderId,+ index: params.chunk,+ type: 'audio',+ }),+ body: fs.createReadStream(audioOutputLocation),+ region: getCurrentRegionInFunction(),+ privacy: params.privacy,+ expectedBucketOwner: options.expectedBucketOwner,+ downloadBehavior: null,+ customCredentials: null,+ })+ : null,+ ]);RenderInternals.Log.verbose({indent: false, logLevel: params.logLevel},diff --git a/packages/renderer/src/combine-videos.ts b/packages/renderer/src/combine-videos.tsindex 2107ab4f49..2dd52999a4 100644--- a/packages/renderer/src/combine-videos.ts+++ b/packages/renderer/src/combine-videos.ts@@ -33,6 +33,7 @@ type Options = {chunkDurationInSeconds: number;binariesDirectory: string | null;cancelSignal: CancelSignal | undefined;+ seamless: boolean;};export const combineVideos = async ({@@ -51,6 +52,7 @@ export const combineVideos = async ({chunkDurationInSeconds,binariesDirectory,cancelSignal,+ seamless,}: Options) => {const resolvedAudioCodec =audioCodec ?? getDefaultAudioCodec({codec, preferLossless: false});@@ -87,11 +89,9 @@ export const combineVideos = async ({onProgress((actualProgress / totalFrames) * numberOfFrames);};- // TODO: Better way of determining if the concatenation is seamless- const seamless = resolvedAudioCodec === 'aac' && codec === 'h264';Log.verbose({indent, logLevel},- 'Combining chunks ' + seamless ? 'seamlessly' : 'normally',+ `Combining chunks ${seamless ? 'seamlessly' : 'normally'}`,);await Promise.all([