Skip to content

🎥 Make videos programmatically with React

renderer: trim then set speed

Jonny Burgera1f93dc7d3f1
diff --git a/packages/lambda/src/test/integration/renders/regular-audio.bmp b/packages/lambda/src/test/integration/renders/regular-audio.bmp
index 31d8c4c46e..67c79798ea 100644
Binary files a/packages/lambda/src/test/integration/renders/regular-audio.bmp and b/packages/lambda/src/test/integration/renders/regular-audio.bmp differ
diff --git a/packages/lambda/src/test/integration/renders/seamless-audio.bmp b/packages/lambda/src/test/integration/renders/seamless-audio.bmp
index 54edbedc0b..d903368a31 100644
Binary files a/packages/lambda/src/test/integration/renders/seamless-audio.bmp and b/packages/lambda/src/test/integration/renders/seamless-audio.bmp differ
diff --git a/packages/renderer/src/assets/ffmpeg-volume-expression.ts b/packages/renderer/src/assets/ffmpeg-volume-expression.ts
index 4167bb7df9..3b0a13a1f9 100644
--- a/packages/renderer/src/assets/ffmpeg-volume-expression.ts
+++ b/packages/renderer/src/assets/ffmpeg-volume-expression.ts
@@ -22,10 +22,12 @@ const ffmpegIsOneOfFrames = ({
frames,
trimLeft,
fps,
+ timestampMultiplier,
}: {
frames: number[];
trimLeft: number;
fps: number;
+ timestampMultiplier: number;
}) => {
const consecutiveArrays: number[][] = [];
for (let i = 0; i < frames.length; i++) {
@@ -42,8 +44,8 @@ const ffmpegIsOneOfFrames = ({
.map((f) => {
const firstFrame = f[0];
const lastFrame = f[f.length - 1];
- const before = (firstFrame - 0.5) / fps;
- const after = (lastFrame + 0.5) / fps;
+ const before = ((firstFrame - 0.5) / fps) * timestampMultiplier;
+ const after = ((lastFrame + 0.5) / fps) * timestampMultiplier;
return `between(${FFMPEG_TIME_VARIABLE},${(before + trimLeft).toFixed(
4,
)},${(after + trimLeft).toFixed(4)})`;
@@ -55,10 +57,12 @@ const ffmpegBuildVolumeExpression = ({
arr,
delay,
fps,
+ timestampMultiplier,
}: {
arr: VolumeArray;
delay: number;
fps: number;
+ timestampMultiplier: number;
}): string => {
if (arr.length === 0) {
throw new Error('Volume array expression should never have length 0');
@@ -72,9 +76,9 @@ const ffmpegBuildVolumeExpression = ({
const [volume, frames] = first;
return ffmpegIfOrElse(
- ffmpegIsOneOfFrames({frames, trimLeft: delay, fps}),
+ ffmpegIsOneOfFrames({frames, trimLeft: delay, fps, timestampMultiplier}),
String(volume),
- ffmpegBuildVolumeExpression({arr: rest, delay, fps}),
+ ffmpegBuildVolumeExpression({arr: rest, delay, fps, timestampMultiplier}),
);
};
@@ -88,11 +92,13 @@ export const ffmpegVolumeExpression = ({
fps,
trimLeft,
allowAmplificationDuringRender,
+ timestampMultiplier,
}: {
volume: AssetVolume;
trimLeft: number;
fps: number;
allowAmplificationDuringRender: boolean;
+ timestampMultiplier: number;
}): FfmpegVolumeExpression => {
const maxVolume = allowAmplificationDuringRender ? Infinity : 1;
// If it's a static volume, we return it and tell
@@ -110,6 +116,7 @@ export const ffmpegVolumeExpression = ({
fps,
trimLeft,
allowAmplificationDuringRender,
+ timestampMultiplier,
});
}
@@ -147,6 +154,7 @@ export const ffmpegVolumeExpression = ({
arr: volumeArray,
delay: trimLeft,
fps,
+ timestampMultiplier,
});
return {
diff --git a/packages/renderer/src/stringify-ffmpeg-filter.ts b/packages/renderer/src/stringify-ffmpeg-filter.ts
index b9e0894fb8..2bead6cd49 100644
--- a/packages/renderer/src/stringify-ffmpeg-filter.ts
+++ b/packages/renderer/src/stringify-ffmpeg-filter.ts
@@ -29,11 +29,14 @@ export const getActualTrimLeft = ({
asset,
fps,
trimLeftOffset,
+ seamless,
}: {
asset: MediaAsset;
fps: number;
trimLeftOffset: number;
-}) => asset.trimLeft / fps + trimLeftOffset;
+ seamless: boolean;
+}) =>
+ asset.trimLeft / fps / (seamless ? asset.playbackRate : 1) + trimLeftOffset;
const trimAndSetTempo = ({
forSeamlessAacConcatenation,
@@ -51,6 +54,7 @@ const trimAndSetTempo = ({
fps: number;
}): {
actualTrimLeft: number;
+ timestampMultiplier: number;
filter: (string | null)[];
audibleDuration: number;
} => {
@@ -59,8 +63,14 @@ const trimAndSetTempo = ({
// and the offset needs to be the same for all audio tracks, before processing it further.
// This also affects the trimLeft and trimRight values, as they need to be adjusted.
if (forSeamlessAacConcatenation) {
- const trimLeft = getActualTrimLeft({asset, fps, trimLeftOffset});
- const trimRight = trimLeft + (asset.duration / fps + trimRightOffset);
+ const trimLeft = getActualTrimLeft({
+ asset,
+ fps,
+ trimLeftOffset,
+ seamless: true,
+ });
+ const trimRight =
+ trimLeft + asset.duration / fps / asset.playbackRate + trimRightOffset;
const trimRightOrAssetDuration = assetDuration
? Math.min(trimRight, assetDuration / asset.playbackRate)
@@ -84,6 +94,7 @@ const trimAndSetTempo = ({
`atrim=${stringifyTrim(trimLeft)}:${stringifyTrim(trimRight)}`,
],
actualTrimLeft: trimLeft,
+ timestampMultiplier: 1,
audibleDuration: trimRight - trimLeft,
};
}
@@ -91,25 +102,29 @@ const trimAndSetTempo = ({
// Otherwise, we first trim and then apply playback rate, as then the atempo
// filter needs to do less work.
if (!forSeamlessAacConcatenation) {
- const trimLeft = getActualTrimLeft({asset, fps, trimLeftOffset});
- const trimRight = (trimLeft + asset.duration / fps) * asset.playbackRate;
+ const trimLeft = getActualTrimLeft({
+ asset,
+ fps,
+ trimLeftOffset,
+ seamless: false,
+ });
+ const trimRight = trimLeft + (asset.duration / fps) * asset.playbackRate;
const trimRightOrAssetDuration = assetDuration
? Math.min(trimRight, assetDuration)
: trimRight;
- const actualTrimLeft = trimLeft * asset.playbackRate;
-
return {
filter: [
- `atrim=${stringifyTrim(actualTrimLeft)}:${stringifyTrim(
+ `atrim=${stringifyTrim(trimLeft)}:${stringifyTrim(
trimRightOrAssetDuration,
)}`,
calculateATempo(asset.playbackRate),
],
- actualTrimLeft,
+ actualTrimLeft: trimLeft,
+ timestampMultiplier: 1,
audibleDuration:
- (trimRightOrAssetDuration - actualTrimLeft) / asset.playbackRate,
+ (trimRightOrAssetDuration - trimLeft) / asset.playbackRate,
};
}
@@ -147,7 +162,12 @@ export const stringifyFfmpegFilter = ({
if (
assetDuration &&
- getActualTrimLeft({asset, fps, trimLeftOffset}) >=
+ getActualTrimLeft({
+ asset,
+ fps,
+ trimLeftOffset,
+ seamless: forSeamlessAacConcatenation,
+ }) >=
assetDuration / playbackRate
) {
return null;
@@ -163,6 +183,7 @@ export const stringifyFfmpegFilter = ({
actualTrimLeft,
audibleDuration,
filter: trimAndTempoFilter,
+ timestampMultiplier,
} = trimAndSetTempo({
forSeamlessAacConcatenation,
assetDuration,
@@ -177,6 +198,7 @@ export const stringifyFfmpegFilter = ({
fps,
trimLeft: actualTrimLeft,
allowAmplificationDuringRender: asset.allowAmplificationDuringRender,
+ timestampMultiplier,
});
const padAtEnd = chunkLengthInSeconds - audibleDuration - startInVideoSeconds;
diff --git a/packages/renderer/src/test/ffmpeg-volume-expression.test.ts b/packages/renderer/src/test/ffmpeg-volume-expression.test.ts
index 45976a5fe5..62dfd553c2 100644
--- a/packages/renderer/src/test/ffmpeg-volume-expression.test.ts
+++ b/packages/renderer/src/test/ffmpeg-volume-expression.test.ts
@@ -9,6 +9,7 @@ test('Simple expression', () => {
fps: 30,
trimLeft: 0,
allowAmplificationDuringRender: false,
+ timestampMultiplier: 1,
}),
).toEqual({
eval: 'once',
@@ -23,6 +24,7 @@ test('Simple expression with volume multiplier', () => {
fps: 30,
trimLeft: 0,
allowAmplificationDuringRender: false,
+ timestampMultiplier: 1,
}),
).toEqual({
eval: 'once',
@@ -37,6 +39,7 @@ test('Complex expression with volume multiplier', () => {
fps: 30,
trimLeft: 0,
allowAmplificationDuringRender: false,
+ timestampMultiplier: 1,
}),
).toEqual({
eval: 'frame',
@@ -51,6 +54,7 @@ test('Should respect trimLeft multiplier', () => {
fps: 30,
trimLeft: 0.5,
allowAmplificationDuringRender: false,
+ timestampMultiplier: 1,
}),
).toEqual({
eval: 'frame',
@@ -68,6 +72,7 @@ test('Really complex volume expression', () => {
fps: 30,
trimLeft: 0,
allowAmplificationDuringRender: false,
+ timestampMultiplier: 1,
}),
).toEqual({
eval: 'frame',
@@ -82,6 +87,7 @@ test('Should use 0 as else statement', () => {
fps: 30,
allowAmplificationDuringRender: false,
trimLeft: 0,
+ timestampMultiplier: 1,
}),
).toEqual({
eval: 'frame',
@@ -96,6 +102,7 @@ test('Simple expression - should not be higher than 1', () => {
fps: 30,
trimLeft: 0,
allowAmplificationDuringRender: false,
+ timestampMultiplier: 1,
}),
).toEqual({
eval: 'once',
@@ -110,6 +117,7 @@ test('Complex expression - should be higher than 1 if allowed', () => {
fps: 30,
trimLeft: 0,
allowAmplificationDuringRender: true,
+ timestampMultiplier: 1,
}),
).toEqual({
eval: 'frame',
@@ -124,6 +132,7 @@ test('Should simplify an expression', () => {
fps: 30,
trimLeft: 0,
allowAmplificationDuringRender: false,
+ timestampMultiplier: 1,
}),
).toEqual({
eval: 'frame',
@@ -143,6 +152,7 @@ test('Should stay under half 8000 windows character limit', () => {
fps: 30,
trimLeft: 0,
allowAmplificationDuringRender: false,
+ timestampMultiplier: 1,
});
expect(expression.value.length).toBeLessThan(4000);
@@ -158,6 +168,7 @@ test('Last volume should be default case', () => {
fps: 30,
trimLeft: 0,
allowAmplificationDuringRender: false,
+ timestampMultiplier: 1,
});
expect(expression).toEqual({
eval: 'frame',