🎥 Make videos programmatically with React
more precise audio cutting
Jonny Burger391e7fb3d0f0
diff --git a/packages/renderer/src/assets/ffmpeg-volume-expression.ts b/packages/renderer/src/assets/ffmpeg-volume-expression.tsindex 40524fd59b..57022135a8 100644--- a/packages/renderer/src/assets/ffmpeg-volume-expression.ts+++ b/packages/renderer/src/assets/ffmpeg-volume-expression.ts@@ -61,14 +61,7 @@ const ffmpegBuildVolumeExpression = (}if (arr.length === 1) {- // FFMpeg tends to request volume for frames outside the range- // where the audio actually plays.- // If this is the case, we just return volume 0 to clip it.- return ffmpegIfOrElse(- ffmpegIsOneOfFrames({frames: arr[0][1], trimLeft: delay, fps}),- String(arr[0][0]),- String(0)- );+ return String(arr[0][0]);}const [first, ...rest] = arr;@@ -114,12 +107,18 @@ export const ffmpegVolumeExpression = ({});}+ // A 1 sec video with frames 0-29 would mean that+ // frame 29 corresponds to timestamp 0.966666...+ // but the audio is actually 1 sec long. For that reason we pad the last+ // timestamp.+ const paddedVolume = [...volume, volume[volume.length - 1]];+// Otherwise, we construct an FFMPEG expression. First step:// Make a map of all possible volumes// {possibleVolume1} => [frame1, frame2]// {possibleVolume2} => [frame3, frame4]const volumeMap: {[volume: string]: number[]} = {};- volume.forEach((baseVolume, frame) => {+ paddedVolume.forEach((baseVolume, frame) => {// Adjust volume based on how many other tracks have not yet finishedconst actualVolume = roundVolumeToAvoidStackOverflow(Math.min(1, baseVolume)@@ -139,6 +138,7 @@ export const ffmpegVolumeExpression = ({// Construct and tell FFMPEG it has to evaluate expression on each frameconst expression = ffmpegBuildVolumeExpression(volumeArray, trimLeft, fps);+return {eval: 'frame',value: `'${expression}'`,diff --git a/packages/renderer/src/test/ffmpeg-volume-expression.test.ts b/packages/renderer/src/test/ffmpeg-volume-expression.test.tsindex 6a98c324ad..186b02c488 100644--- a/packages/renderer/src/test/ffmpeg-volume-expression.test.ts+++ b/packages/renderer/src/test/ffmpeg-volume-expression.test.ts@@ -1,3 +1,4 @@+import {interpolate} from 'remotion';import {ffmpegVolumeExpression} from '../assets/ffmpeg-volume-expression';test('Simple expression', () => {@@ -38,7 +39,7 @@ test('Complex expression with volume multiplier', () => {})).toEqual({eval: 'frame',- value: "'if(between(t,-0.0167,0.0167),0,if(between(t,0.0167,0.0500),1,0))'",+ value: "'if(between(t,-0.0167,0.0167),0,1)'",});});@@ -52,13 +53,13 @@ test('Should respect trimLeft multiplier', () => {})).toEqual({eval: 'frame',- value: "'if(between(t,0.4833,0.5167),0,if(between(t,0.5167,0.5500),1,0))'",+ value: "'if(between(t,0.4833,0.5167),0,1)'",});});test('Really complex volume expression', () => {const expectedExpression =- "'if(between(t,-0.0167,0.0167),0,if(between(t,0.0167,0.0500),0.247,if(between(t,0.0500,0.0833),0.505,if(between(t,0.0833,0.2167),0.99,if(between(t,0.2167,0.3833),1,0)))))'";+ "'if(between(t,-0.0167,0.0167),0,if(between(t,0.0167,0.0500),0.247,if(between(t,0.0500,0.0833),0.505,if(between(t,0.0833,0.2167),0.99,1))))'";expect(ffmpegVolumeExpression({@@ -83,8 +84,7 @@ test('Should use 0 as else statement', () => {})).toEqual({eval: 'frame',- value:- "'if(between(t,0.0833,0.1500),1,if(between(t,-0.0167,0.0167)+between(t,0.0167,0.0833),0,0))'",+ value: "'if(between(t,-0.0167,0.0833),0,1)'",});});@@ -107,8 +107,7 @@ test('Complex expression - should not be higher than 1', () => {})).toEqual({eval: 'frame',- value:- "'if(between(t,0.0167,0.0500),1,if(between(t,-0.0167,0.0167),0.505,0))'",+ value: "'if(between(t,-0.0167,0.0167),0.505,1)'",});});@@ -122,8 +121,7 @@ test('Should simplify an expression', () => {})).toEqual({eval: 'frame',- value:- "'if(between(t,-0.0167,0.0167)+between(t,0.1167,0.1500),0,if(between(t,0.0167,0.1167)+between(t,0.1500,0.1833),1,0))'",+ value: "'if(between(t,-0.0167,0.0167)+between(t,0.1167,0.1500),0,1)'",});});@@ -143,3 +141,21 @@ test('Should stay under half 8000 windows character limit', () => {expect(expression.value.length).toBeLessThan(4000);});++test('Last volume should be default case', () => {+ const expression = ffmpegVolumeExpression({+ volume: new Array(20).fill(1).map((_, i) => {+ return interpolate(i, [0, 200, 400, 600], [0, 1, 1, 0], {+ extrapolateLeft: 'clamp',+ });+ }),+ fps: 30,+ startInVideo: 0,+ trimLeft: 0,+ });+ expect(expression).toEqual({+ eval: 'frame',+ value:+ "'if(between(t,-0.0167,0.0500),0,if(between(t,0.0500,0.1167),0.01,if(between(t,0.1167,0.1833),0.021,if(between(t,0.1833,0.2500),0.031,if(between(t,0.2500,0.3167),0.041,if(between(t,0.3167,0.3833),0.052,if(between(t,0.3833,0.4500),0.062,if(between(t,0.4500,0.5167),0.072,if(between(t,0.5167,0.5833),0.082,0.093)))))))))'",+ });+});