Skip to content

🎥 Make videos programmatically with React

calculate atempo filter

Jonny Burgerc8babddf5561
diff --git a/packages/renderer/src/assets/calculate-atempo.ts b/packages/renderer/src/assets/calculate-atempo.ts
new file mode 100644
index 0000000000..38199b3e53
--- /dev/null
+++ b/packages/renderer/src/assets/calculate-atempo.ts
@@ -0,0 +1,13 @@
+// Solving this problem of FFMPEG:
+// https://trac.ffmpeg.org/wiki/How%20to%20speed%20up%20/%20slow%20down%20a%20video
+// "The atempo filter is limited to using values between 0.5 and 2.0 (so it can slow it down to no less than half the original speed, and speed up to no more than double the input). If you need to, you can get around this limitation by stringing multiple atempo filters together. The following with quadruple the audio speed:"
+
+export const calculateATempo = (playbackRate: number): string => {
+ if (playbackRate >= 0.5 && playbackRate <= 2) {
+ return `atempo=${playbackRate.toFixed(5)}`;
+ }
+
+ return `${calculateATempo(Math.sqrt(playbackRate))},${calculateATempo(
+ Math.sqrt(playbackRate)
+ )}`;
+};
diff --git a/packages/renderer/src/stringify-ffmpeg-filter.ts b/packages/renderer/src/stringify-ffmpeg-filter.ts
index 8430b72658..39462e2f8b 100644
--- a/packages/renderer/src/stringify-ffmpeg-filter.ts
+++ b/packages/renderer/src/stringify-ffmpeg-filter.ts
@@ -1,4 +1,5 @@
import {Internals} from 'remotion';
+import {calculateATempo} from './assets/calculate-atempo';
import {ffmpegVolumeExpression} from './assets/ffmpeg-volume-expression';
import {AssetVolume} from './assets/types';
@@ -42,8 +43,7 @@ export const stringifyFfmpegFilter = ({
// "Unused delays will be silently ignored."
// https://ffmpeg.org/ffmpeg-filters.html#adelay
`adelay=${new Array(channels + 1).fill(startInVideoSeconds).join('|')}`,
- // TODO: Can only be between 0.5 and 2
- `atempo=${playbackRate}`,
+ calculateATempo(playbackRate),
`volume=${volumeFilter.value}:eval=${volumeFilter.eval}`,
]
.filter(Internals.truthy)
diff --git a/packages/renderer/src/test/calculate-atempo.test.ts b/packages/renderer/src/test/calculate-atempo.test.ts
new file mode 100644
index 0000000000..ab4be6ce25
--- /dev/null
+++ b/packages/renderer/src/test/calculate-atempo.test.ts
@@ -0,0 +1,20 @@
+import {calculateATempo} from '../assets/calculate-atempo';
+
+describe('Calculate atempo', () => {
+ test('Basic atempo', () => {
+ expect(calculateATempo(0.5)).toBe('atempo=0.50000');
+ });
+ test('Below 0.5', () => {
+ expect(calculateATempo(0.25)).toBe('atempo=0.50000,atempo=0.50000');
+ });
+ test('Above 2', () => {
+ expect(calculateATempo(6)).toBe(
+ 'atempo=1.56508,atempo=1.56508,atempo=1.56508,atempo=1.56508'
+ );
+ });
+ test('Extreme value', () => {
+ expect(calculateATempo(0.0000001)).toBe(
+ 'atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430,atempo=0.60430'
+ );
+ });
+});
diff --git a/packages/renderer/src/test/ffmpeg-filters.test.ts b/packages/renderer/src/test/ffmpeg-filters.test.ts
index a625b39fd3..3197110829 100644
--- a/packages/renderer/src/test/ffmpeg-filters.test.ts
+++ b/packages/renderer/src/test/ffmpeg-filters.test.ts
@@ -29,7 +29,9 @@ test('Should create a basic filter correctly', () => {
assetAudioDetails,
videoTrackCount: 1,
})[0].filter
- ).toBe('[1:a]atrim=0.000:0.667,adelay=0|0,atempo=1,volume=1:eval=once[a1]');
+ ).toBe(
+ '[1:a]atrim=0.000:0.667,adelay=0|0,atempo=1.00000,volume=1:eval=once[a1]'
+ );
});
test('Should handle trim correctly', () => {
@@ -49,7 +51,9 @@ test('Should handle trim correctly', () => {
assetAudioDetails,
videoTrackCount: 1,
})[0].filter
- ).toBe('[1:a]atrim=0.333:1.000,adelay=0|0,atempo=1,volume=1:eval=once[a1]');
+ ).toBe(
+ '[1:a]atrim=0.333:1.000,adelay=0|0,atempo=1.00000,volume=1:eval=once[a1]'
+ );
});
test('Should handle delay correctly', () => {
@@ -71,7 +75,7 @@ test('Should handle delay correctly', () => {
videoTrackCount: 1,
})[0].filter
).toBe(
- '[1:a]atrim=0.333:1.000,adelay=2667|2667,atempo=1,volume=1:eval=once[a1]'
+ '[1:a]atrim=0.333:1.000,adelay=2667|2667,atempo=1.00000,volume=1:eval=once[a1]'
);
});
@@ -94,7 +98,7 @@ test('Should offset multiple channels', () => {
videoTrackCount: 1,
})[0].filter
).toBe(
- '[1:a]atrim=0.333:1.000,adelay=2667|2667|2667|2667,atempo=1,volume=1:eval=once[a1]'
+ '[1:a]atrim=0.333:1.000,adelay=2667|2667|2667|2667,atempo=1.00000,volume=1:eval=once[a1]'
);
});
@@ -133,7 +137,7 @@ test('Should calculate correct indices even if some muted channels are removed b
});
expect(makeFilters()[0].filter).toBe(
// Should be index 2 - make sure that index 1 is not current, because it is muted
- '[2:a]atrim=0.333:1.000,adelay=2667|2667|2667|2667,atempo=1,volume=1:eval=once[a2]'
+ '[2:a]atrim=0.333:1.000,adelay=2667|2667|2667|2667,atempo=1.00000,volume=1:eval=once[a2]'
);
// Also test basic case: if first one is unmuted, both channels are there again
@@ -141,9 +145,9 @@ test('Should calculate correct indices even if some muted channels are removed b
channels: 1,
});
expect(makeFilters()[0].filter).toBe(
- '[1:a]atrim=0.333:3.667,adelay=2667|2667,atempo=1,volume=2:eval=once[a1]'
+ '[1:a]atrim=0.333:3.667,adelay=2667|2667,atempo=1.00000,volume=2:eval=once[a1]'
);
expect(makeFilters()[1].filter).toBe(
- '[2:a]atrim=0.333:1.000,adelay=2667|2667|2667|2667,atempo=1,volume=2:eval=once[a2]'
+ '[2:a]atrim=0.333:1.000,adelay=2667|2667|2667|2667,atempo=1.00000,volume=2:eval=once[a2]'
);
});