Skip to content

🎥 Make videos programmatically with React

figure out chunkStart

Jonny Burgerabd2b96f2873
diff --git a/packages/cli/src/benchmark.ts b/packages/cli/src/benchmark.ts
index 98697c79bd..7d90de8a79 100644
--- a/packages/cli/src/benchmark.ts
+++ b/packages/cli/src/benchmark.ts
@@ -466,6 +466,7 @@ export const benchmarkCommand = async (
forSeamlessAacConcatenationOption.getValue({
commandLine: parsedCli,
}).value,
+ compositionStart: 0,
},
(run, progress) => {
benchmarkProgress.update(
diff --git a/packages/cli/src/render-flows/render.ts b/packages/cli/src/render-flows/render.ts
index df43d0c5c4..5139172564 100644
--- a/packages/cli/src/render-flows/render.ts
+++ b/packages/cli/src/render-flows/render.ts
@@ -443,6 +443,7 @@ export const renderVideoFlow = async ({
offthreadVideoCacheSizeInBytes,
parallelEncodingEnabled: isUsingParallelEncoding,
binariesDirectory,
+ compositionStart: 0,
});
updateRenderProgress({newline: true, printToConsole: true});
@@ -531,6 +532,7 @@ export const renderVideoFlow = async ({
binariesDirectory,
separateAudioTo: absoluteSeparateAudioTo,
forSeamlessAacConcatenation,
+ compositionStart: 0,
});
Log.info(
diff --git a/packages/cloudrun/src/functions/render-media-single-thread.ts b/packages/cloudrun/src/functions/render-media-single-thread.ts
index b743e03426..db2d0d5d55 100644
--- a/packages/cloudrun/src/functions/render-media-single-thread.ts
+++ b/packages/cloudrun/src/functions/render-media-single-thread.ts
@@ -123,6 +123,7 @@ export const renderMediaSingleThread = async (
binariesDirectory: null,
separateAudioTo: null,
forSeamlessAacConcatenation: false,
+ compositionStart: 0,
});
const storage = new Storage();
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 54eae611a4..f25e907e60 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -286,6 +286,7 @@ const innerLaunchHandler = async ({
deleteAfter: params.deleteAfter,
colorSpace: params.colorSpace,
preferLossless: params.preferLossless,
+ compositionStart: realFrameRange[0],
};
return payload;
});
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index e08a957358..211c12ac18 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -258,6 +258,7 @@ const renderHandler = async (
binariesDirectory: null,
separateAudioTo: audioOutputLocation,
forSeamlessAacConcatenation: seamlessAudio,
+ compositionStart: params.compositionStart,
})
.then(({slowestFrames}) => {
RenderInternals.Log.verbose(
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 3106bab178..39c13d3b4b 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -388,6 +388,7 @@ export type LambdaPayloads = {
offthreadVideoCacheSizeInBytes: number | null;
deleteAfter: DeleteAfter | null;
colorSpace: ColorSpace;
+ compositionStart: number;
};
still: {
type: LambdaRoutines.still;
diff --git a/packages/lambda/src/test/integration/renders/seamless-audio.test.ts b/packages/lambda/src/test/integration/renders/seamless-audio.test.ts
index 31ced0394f..87991b3746 100644
--- a/packages/lambda/src/test/integration/renders/seamless-audio.test.ts
+++ b/packages/lambda/src/test/integration/renders/seamless-audio.test.ts
@@ -17,7 +17,7 @@ test('Should make seamless audio', async () => {
composition: 'framer',
frameRange: [100, 200],
imageFormat: 'png',
- logLevel: 'error',
+ logLevel: 'verbose',
region: 'eu-central-1',
});
diff --git a/packages/lambda/src/test/integration/simulate-lambda-render.ts b/packages/lambda/src/test/integration/simulate-lambda-render.ts
index 56388ad64f..43138fef79 100644
--- a/packages/lambda/src/test/integration/simulate-lambda-render.ts
+++ b/packages/lambda/src/test/integration/simulate-lambda-render.ts
@@ -10,6 +10,37 @@ import {callLambda} from '../../shared/call-lambda';
const functionName = 'remotion-dev-render';
+export const waitUntilDone = async (bucketName: string, renderId: string) => {
+ // eslint-disable-next-line no-constant-condition
+ while (true) {
+ const progress = await callLambda({
+ type: LambdaRoutines.status,
+ payload: {
+ bucketName,
+ renderId,
+ version: VERSION,
+ logLevel: 'error',
+ },
+ functionName: 'remotion-dev-lambda',
+ receivedStreamingPayload: () => undefined,
+ region: 'eu-central-1',
+ timeoutInTest: 120000,
+ retriesRemaining: 0,
+ });
+ if (progress.done) {
+ return progress;
+ }
+
+ if (progress.fatalErrorEncountered) {
+ throw new Error(progress.errors.join('\n'));
+ }
+
+ await new Promise((resolve) => {
+ setTimeout(resolve, 1000);
+ });
+ }
+};
+
export const simulateLambdaRender = async (
input: Omit<RenderMediaOnLambdaInput, 'serveUrl' | 'functionName'>,
) => {
@@ -45,20 +76,7 @@ export const simulateLambdaRender = async (
retriesRemaining: 0,
});
- const progress = await callLambda({
- type: LambdaRoutines.status,
- payload: {
- bucketName: res.bucketName,
- renderId: res.renderId,
- version: VERSION,
- logLevel: 'error',
- },
- functionName: 'remotion-dev-lambda',
- receivedStreamingPayload: () => undefined,
- region: 'eu-central-1',
- timeoutInTest: 120000,
- retriesRemaining: 0,
- });
+ const progress = await waitUntilDone(res.bucketName, res.renderId);
const file = await lambdaReadFile({
bucketName: progress.outBucket as string,
diff --git a/packages/renderer/src/assets/download-map.ts b/packages/renderer/src/assets/download-map.ts
index 789f59bf94..9f396084ec 100644
--- a/packages/renderer/src/assets/download-map.ts
+++ b/packages/renderer/src/assets/download-map.ts
@@ -51,6 +51,7 @@ export type RenderAssetInfo = {
firstFrameIndex: number;
downloadMap: DownloadMap;
compositionStart: number;
+ chunkStart: number;
};
const makeAndReturn = (dir: string, name: string) => {
diff --git a/packages/renderer/src/calculate-ffmpeg-filters.ts b/packages/renderer/src/calculate-ffmpeg-filters.ts
index 502f844768..0a12c40146 100644
--- a/packages/renderer/src/calculate-ffmpeg-filters.ts
+++ b/packages/renderer/src/calculate-ffmpeg-filters.ts
@@ -28,37 +28,30 @@ export const calculateFfmpegFilter = ({
return null;
}
+ const realLeftEnd = chunkStart - compositionStart;
+ const realRightEnd = realLeftEnd + durationInFrames / fps;
+
+ const actualLeftEnd =
+ getClosestAlignedTime(realLeftEnd) - 2 * (1024 / DEFAULT_SAMPLE_RATE);
+ const actualRightEnd =
+ getClosestAlignedTime(realRightEnd) + 2 * (1024 / DEFAULT_SAMPLE_RATE);
+
+ console.log({actualLeftEnd, actualRightEnd});
+
const assetTrimLeft = forSeamlessAacConcatenation
? Math.max(
0,
- getClosestAlignedTime(
- (asset.trimLeft * asset.playbackRate) / fps,
- compositionStart,
- true,
- ) /
- 1_000_000 -
+ getClosestAlignedTime((asset.trimLeft * asset.playbackRate) / fps) -
2 * (1024 / DEFAULT_SAMPLE_RATE),
)
: (asset.trimLeft * asset.playbackRate) / fps;
const assetTrimRight = forSeamlessAacConcatenation
? getClosestAlignedTime(
((asset.trimLeft + asset.duration) * asset.playbackRate) / fps,
- compositionStart,
- true,
- ) /
- 1_000_000 +
+ ) +
2 * (1024 / DEFAULT_SAMPLE_RATE)
: assetTrimLeft + (asset.duration * asset.playbackRate) / fps;
- console.log({
- assetTrimLeft,
- assetTrimRight,
- asset,
- targetRight:
- ((asset.trimLeft + asset.duration * asset.playbackRate) / fps) *
- 1_000_000,
- });
-
return stringifyFfmpegFilter({
channels,
startInVideo: asset.startInVideo,
diff --git a/packages/renderer/src/combine-audio.ts b/packages/renderer/src/combine-audio.ts
index 41ee0735ae..7400990830 100644
--- a/packages/renderer/src/combine-audio.ts
+++ b/packages/renderer/src/combine-audio.ts
@@ -13,21 +13,10 @@ import {truthy} from './truthy';
export const durationOf1Frame = (1024 / DEFAULT_SAMPLE_RATE) * 1_000_000;
-export const getClosestAlignedTime = (
- targetTime: number,
- compositionStart: number,
- _override: boolean,
-) => {
- if (_override) {
- compositionStart = 3.333333;
- }
-
+export const getClosestAlignedTime = (targetTime: number) => {
const decimalFramesToTargetTime = (targetTime * 1_000_000) / durationOf1Frame;
const nearestFrameIndexForTargetTime = Math.round(decimalFramesToTargetTime);
- return (
- nearestFrameIndexForTargetTime * durationOf1Frame +
- compositionStart * 1_000_000
- );
+ return (nearestFrameIndexForTargetTime * durationOf1Frame) / 1_000_000;
};
const encodeAudio = async ({
@@ -150,11 +139,10 @@ const combineAudioSeamlessly = async ({
const targetStart = i * chunkDurationInSeconds;
const endStart = (i + 1) * chunkDurationInSeconds;
- const startTime = getClosestAlignedTime(targetStart, 0, false);
- const endTime = getClosestAlignedTime(endStart, 0, false);
+ const startTime = getClosestAlignedTime(targetStart) * 1_000_000;
+ const endTime = getClosestAlignedTime(endStart) * 1_000_000;
const realDuration = endTime - startTime;
- console.log({realDuration, chunkDurationInSeconds});
let inpoint = 0;
if (i > 0) {
diff --git a/packages/renderer/src/create-audio.ts b/packages/renderer/src/create-audio.ts
index 0bb35213f7..d630a01664 100644
--- a/packages/renderer/src/create-audio.ts
+++ b/packages/renderer/src/create-audio.ts
@@ -33,6 +33,7 @@ export const createAudio = async ({
cancelSignal,
forSeamlessAacConcatenation,
compositionStart,
+ chunkStart,
}: {
assets: TRenderAsset[][];
onDownload: RenderMediaOnDownload | undefined;
@@ -49,6 +50,7 @@ export const createAudio = async ({
cancelSignal: CancelSignal | undefined;
forSeamlessAacConcatenation: boolean;
compositionStart: number;
+ chunkStart: number;
}): Promise<string> => {
const fileUrlAssets = await convertAssetsToFileUrls({
assets,
@@ -102,6 +104,7 @@ export const createAudio = async ({
updateProgress();
},
compositionStart,
+ chunkStart,
});
preprocessProgress[index] = 1;
updateProgress();
diff --git a/packages/renderer/src/preprocess-audio-track.ts b/packages/renderer/src/preprocess-audio-track.ts
index d2eb0c83dd..0f6335bd70 100644
--- a/packages/renderer/src/preprocess-audio-track.ts
+++ b/packages/renderer/src/preprocess-audio-track.ts
@@ -25,6 +25,7 @@ type Options = {
cancelSignal: CancelSignal | undefined;
forSeamlessAacConcatenation: boolean;
compositionStart: number;
+ chunkStart: number;
onProgress: (progress: number) => void;
};
@@ -46,6 +47,7 @@ const preprocessAudioTrackUnlimited = async ({
forSeamlessAacConcatenation,
onProgress,
compositionStart,
+ chunkStart,
}: Options): Promise<PreprocessedAudioTrack | null> => {
const {channels, duration} = await getAudioChannelsAndDuration({
downloadMap,
@@ -64,6 +66,7 @@ const preprocessAudioTrackUnlimited = async ({
assetDuration: duration,
forSeamlessAacConcatenation,
compositionStart,
+ chunkStart,
});
if (filter === null) {
diff --git a/packages/renderer/src/render-frames.ts b/packages/renderer/src/render-frames.ts
index d72f9064e8..2ee092e4db 100644
--- a/packages/renderer/src/render-frames.ts
+++ b/packages/renderer/src/render-frames.ts
@@ -94,6 +94,7 @@ export type InternalRenderFramesOptions = {
serializedInputPropsWithCustomSchema: string;
serializedResolvedPropsWithCustomSchema: string;
parallelEncodingEnabled: boolean;
+ compositionStart: number;
} & ToOptions<typeof optionsMap.renderFrames>;
type InnerRenderFramesOptions = {
@@ -132,6 +133,7 @@ type InnerRenderFramesOptions = {
serializedInputPropsWithCustomSchema: string;
serializedResolvedPropsWithCustomSchema: string;
parallelEncodingEnabled: boolean;
+ compositionStart: number;
} & ToOptions<typeof optionsMap.renderFrames>;
export type RenderFramesOptions = {
@@ -207,6 +209,7 @@ const innerRenderFrames = async ({
logLevel,
indent,
parallelEncodingEnabled,
+ compositionStart,
}: Omit<
InnerRenderFramesOptions,
'offthreadVideoCacheSizeInBytes'
@@ -577,8 +580,9 @@ const innerRenderFrames = async ({
),
firstFrameIndex,
downloadMap,
- compositionStart:
+ chunkStart:
frameRange === null ? 0 : realFrameRange[0] / composition.fps,
+ compositionStart,
},
frameCount: framesToRender.length,
};
@@ -623,6 +627,7 @@ const internalRenderFramesRaw = ({
offthreadVideoCacheSizeInBytes,
parallelEncodingEnabled,
binariesDirectory,
+ compositionStart,
}: InternalRenderFramesOptions): Promise<RenderFramesOutput> => {
validateDimension(
composition.height,
@@ -748,6 +753,7 @@ const internalRenderFramesRaw = ({
serializedResolvedPropsWithCustomSchema,
parallelEncodingEnabled,
binariesDirectory,
+ compositionStart,
});
}),
])
@@ -892,5 +898,7 @@ export const renderFrames = (
offthreadVideoCacheSizeInBytes: offthreadVideoCacheSizeInBytes ?? null,
parallelEncodingEnabled: false,
binariesDirectory: binariesDirectory ?? null,
+ // TODO: In the future, introduce this as a public API when launching the distributed rendering API
+ compositionStart: 0,
});
};
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index e04c5d0337..1a706cb712 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -122,6 +122,7 @@ export type InternalRenderMediaOptions = {
concurrency: number | string | null;
finishRenderProgress: () => void;
binariesDirectory: string | null;
+ compositionStart: number;
} & MoreRenderMediaOptions;
type Prettify<T> = {
@@ -237,6 +238,7 @@ const internalRenderMediaRaw = ({
binariesDirectory,
separateAudioTo,
forSeamlessAacConcatenation,
+ compositionStart,
}: InternalRenderMediaOptions): Promise<RenderMediaResult> => {
if (repro) {
enableRepro({
@@ -625,6 +627,7 @@ const internalRenderMediaRaw = ({
offthreadVideoCacheSizeInBytes,
parallelEncodingEnabled: parallelEncoding,
binariesDirectory,
+ compositionStart,
});
return renderFramesProc;
@@ -910,5 +913,7 @@ export const renderMedia = ({
binariesDirectory: binariesDirectory ?? null,
separateAudioTo: separateAudioTo ?? null,
forSeamlessAacConcatenation: forSeamlessAacConcatenation ?? false,
+ // TODO: In the future, introduce this as a public API when launching the distributed rendering API
+ compositionStart: 0,
});
};
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index 1e652d2701..cf86fbec6d 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -270,6 +270,7 @@ const innerStitchFramesToVideo = async (
cancelSignal: cancelSignal ?? undefined,
forSeamlessAacConcatenation,
compositionStart: assetsInfo.compositionStart,
+ chunkStart: assetsInfo.chunkStart,
})
: null;
diff --git a/packages/renderer/src/test/ffmpeg-filters.test.ts b/packages/renderer/src/test/ffmpeg-filters.test.ts
index 6db78387b2..26f2290bb8 100644
--- a/packages/renderer/src/test/ffmpeg-filters.test.ts
+++ b/packages/renderer/src/test/ffmpeg-filters.test.ts
@@ -31,6 +31,7 @@ test('Should create a basic filter correctly', () => {
assetDuration: 10,
forSeamlessAacConcatenation: true,
compositionStart: 0,
+ chunkStart: 0,
}),
).toEqual({
filter:
@@ -49,6 +50,7 @@ test('Trim the end', () => {
assetDuration: 10,
forSeamlessAacConcatenation: true,
compositionStart: 0,
+ chunkStart: 0,
}),
).toEqual({
filter:
@@ -71,6 +73,7 @@ test('Should handle trim correctly', () => {
assetDuration: 10,
forSeamlessAacConcatenation: true,
compositionStart: 0,
+ chunkStart: 0,
}),
).toEqual({
filter:
@@ -93,6 +96,7 @@ test('Should add padding if audio is too short', () => {
assetDuration: 1,
forSeamlessAacConcatenation: false,
compositionStart: 0,
+ chunkStart: 0,
}),
).toEqual({
filter:
@@ -117,6 +121,7 @@ test('Should handle delay correctly', () => {
assetDuration: 1,
forSeamlessAacConcatenation: true,
compositionStart: 0,
+ chunkStart: 0,
}),
).toEqual({
filter:
@@ -140,6 +145,7 @@ test('Should offset multiple channels', () => {
assetDuration: 1,
forSeamlessAacConcatenation: true,
compositionStart: 0,
+ chunkStart: 0,
}),
).toEqual({
filter:
@@ -173,6 +179,7 @@ test('Should calculate pad correctly with a lot of playbackRate', () => {
assetDuration: 33.333333,
forSeamlessAacConcatenation: true,
compositionStart: 0,
+ chunkStart: 0,
}),
).toEqual({
filter: