🎥 Make videos programmatically with React
tweak ffmpeg filters
Jonny Burgerb66ac16811d8
diff --git a/packages/renderer/src/assets/calculate-atempo.ts b/packages/renderer/src/assets/calculate-atempo.tsindex 38199b3e53..fff42bf6e2 100644--- a/packages/renderer/src/assets/calculate-atempo.ts+++ b/packages/renderer/src/assets/calculate-atempo.ts@@ -2,7 +2,10 @@// 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 => {+export const calculateATempo = (playbackRate: number): string | null => {+ if (playbackRate === 1) {+ return null;+ }if (playbackRate >= 0.5 && playbackRate <= 2) {return `atempo=${playbackRate.toFixed(5)}`;}diff --git a/packages/renderer/src/create-silent-audio.ts b/packages/renderer/src/create-silent-audio.tsindex d25b80f611..a67b7c1805 100644--- a/packages/renderer/src/create-silent-audio.ts+++ b/packages/renderer/src/create-silent-audio.ts@@ -15,7 +15,7 @@ export const createSilentAudio = async ({'-f','lavfi','-i',- 'anullsrc',+ `anullsrc=r=${DEFAULT_SAMPLE_RATE}`,'-c:a','pcm_s16le','-t',diff --git a/packages/renderer/src/stringify-ffmpeg-filter.ts b/packages/renderer/src/stringify-ffmpeg-filter.tsindex 11584abfd9..48575c5c4e 100644--- a/packages/renderer/src/stringify-ffmpeg-filter.ts+++ b/packages/renderer/src/stringify-ffmpeg-filter.ts@@ -13,8 +13,8 @@ export const stringifyFfmpegFilter = ({playbackRate,durationInFrames,}: {- trimLeft: string;- trimRight: string;+ trimLeft: number;+ trimRight: number;channels: number;startInVideo: number;volume: AssetVolume;@@ -22,18 +22,24 @@ export const stringifyFfmpegFilter = ({durationInFrames: number;playbackRate: number;}) => {- const startInVideoSeconds = ((startInVideo / fps) * 1000).toFixed(); // in milliseconds+ const startInVideoSeconds = startInVideo / fps;const volumeFilter = ffmpegVolumeExpression({volume,startInVideo,fps,});+ const durationInSeconds = durationInFrames / fps;+ const trimDuration = trimRight - trimLeft;++ const needsExtendingInEnd =+ durationInSeconds - trimDuration - startInVideoSeconds > 0.00000001;+return (`[0:a]` +[// Order matters! First trim the audio- `atrim=${trimLeft}:${trimRight}`,+ `atrim=${trimLeft.toFixed(6)}:${trimRight.toFixed(6)}`,// then set the tempocalculateATempo(playbackRate),// For n channels, we delay n + 1 channels.@@ -42,11 +48,20 @@ export const stringifyFfmpegFilter = ({// This should be fine because FFMPEG documentation states:// "Unused delays will be silently ignored."// https://ffmpeg.org/ffmpeg-filters.html#adelay- `adelay=${new Array(channels + 1).fill(startInVideoSeconds).join('|')}`,+ startInVideoSeconds === 0+ ? null+ : `adelay=${new Array(channels + 1)+ .fill((startInVideoSeconds * 1000).toFixed(0))+ .join('|')}`,// set the volume- `volume=${volumeFilter.value}:eval=${volumeFilter.eval}`,+ volumeFilter.value === '1'+ ? null+ : `volume=${volumeFilter.value}:eval=${volumeFilter.eval}`,+// Only in the end, we pad to the full length.- 'apad=whole_dur=' + (durationInFrames / fps).toFixed(3),+ needsExtendingInEnd+ ? 'apad=whole_dur=' + (durationInFrames / fps).toFixed(6)+ : null,].filter(Internals.truthy).join(',') +