🎥 Make videos programmatically with React
asset splitting
Jonny Burgerb57dae6610c5
diff --git a/packages/renderer/src/assets/asset-is-used-at-time.ts b/packages/renderer/src/assets/asset-is-used-at-time.tsnew file mode 100644index 0000000000..672135e14f--- /dev/null+++ b/packages/renderer/src/assets/asset-is-used-at-time.ts@@ -0,0 +1,7 @@+import {MediaAsset} from './types';++export const assetIsUsedAtTime = (asset: MediaAsset, frame: number) => {+ return (+ frame >= asset.startInVideo && frame < asset.startInVideo + asset.duration+ );+};diff --git a/packages/renderer/src/assets/calculate-asset-positions.ts b/packages/renderer/src/assets/calculate-asset-positions.tsindex 3c9f5c4858..5598d5caf5 100644--- a/packages/renderer/src/assets/calculate-asset-positions.ts+++ b/packages/renderer/src/assets/calculate-asset-positions.ts@@ -1,4 +1,5 @@import {TAsset} from 'remotion';+import {splitAssetsIntoSegments} from './split-assets-into-segments';import {Assets, MediaAsset, UnsafeAsset} from './types';const areEqual = (a: TAsset | UnsafeAsset, b: TAsset | UnsafeAsset) => {@@ -53,5 +54,8 @@ export const calculateAssetPositions = (frames: TAsset[][]): Assets => {throw new Error('duration is unexpectedly null');}}- return assets as MediaAsset[];+ return splitAssetsIntoSegments({+ assets: assets as MediaAsset[],+ duration: frames.length,+ });};diff --git a/packages/renderer/src/assets/split-assets-into-segments.ts b/packages/renderer/src/assets/split-assets-into-segments.tsnew file mode 100644index 0000000000..9352207e22--- /dev/null+++ b/packages/renderer/src/assets/split-assets-into-segments.ts@@ -0,0 +1,83 @@+// Because of how FFMPEG filters work, we might have to split 1 audio+// track into multiple filters to avoid unexpected volume changes.++import {Internals} from 'remotion';+import {assetIsUsedAtTime} from './asset-is-used-at-time';+import {MediaAsset} from './types';++// If there is a track from 0-10 seconds and one from 5-15 seconds+// we need to create the following segments:+// 1. Segment from 0-5 seconds with 1 audio source and volume=1+// 2. Segment from 5-10 seconds with 2 audio sources and volume=2 for each+// 3. Segment from 10-15 seconds with 1 audio source and volume=1++export const splitAssetsIntoSegments = ({+ assets,+ duration,+}: {+ assets: MediaAsset[];+ duration: number;+}): MediaAsset[] => {+ const timeIds = new Array(duration).fill(true).map((_, frame) => {+ const sourcesAtThisTime = assets.filter((a) => {+ return assetIsUsedAtTime(a, frame);+ });+ return sourcesAtThisTime.map((s) => s.id).join(',');+ });+ const framesAtWhichToCut = [+ 0,+ ...timeIds+ .map((t, i) => {+ return {+ timeId: t,+ index: i,+ };+ })+ .filter(({index, timeId}) => {+ return index > 0 && timeId !== timeIds[index - 1];+ })+ .map(({index}) => index),+ duration,+ ];+ return assets+ .map((a) => {+ return framesAtWhichToCut.map((cutEnd, i): MediaAsset | null => {+ const cutStart = i === 0 ? 0 : framesAtWhichToCut[i - 1];+ const assetStart = a.startInVideo;+ const assetEnd = a.startInVideo + a.duration;+ // Video goes beyond cut+ if (assetStart < cutEnd && assetEnd > cutEnd) {+ return {+ ...a,+ duration: cutEnd - a.startInVideo,+ };+ }+ // Video starts before cut+ if (assetStart < cutStart && assetEnd > cutStart) {+ const amountOfFramesThatGetCut = cutStart - assetStart;+ return {+ ...a,+ startInVideo: cutStart,+ duration: a.duration - amountOfFramesThatGetCut,+ trimLeft: a.trimLeft + amountOfFramesThatGetCut,+ };+ }++ // Video is inside cut, leave+ if (assetStart >= cutStart && assetEnd <= cutEnd) {+ return a;+ }+ // Video is left from cut+ if (assetStart <= cutStart && assetEnd <= cutStart) {+ return null;+ }+ // Video is right from cut+ if (assetStart >= cutStart && assetEnd >= cutStart) {+ return null;+ }+ throw new Error('Unhandled');+ });+ })+ .flat(Infinity)+ .filter(Internals.truthy) as MediaAsset[];+};diff --git a/packages/renderer/src/test/asset-is-used-at-time.test.ts b/packages/renderer/src/test/asset-is-used-at-time.test.tsnew file mode 100644index 0000000000..5faffee0b6--- /dev/null+++ b/packages/renderer/src/test/asset-is-used-at-time.test.ts@@ -0,0 +1,59 @@+import {assetIsUsedAtTime} from '../assets/asset-is-used-at-time';++test('Should give true is asset is used at time', () => {+ expect(+ assetIsUsedAtTime(+ {+ duration: 2,+ id: 'hi',+ src: 'hi.mp4',+ startInVideo: 0,+ trimLeft: 2,+ type: 'audio',+ },+ 1+ )+ ).toBe(true);+ expect(+ assetIsUsedAtTime(+ {+ duration: 2,+ id: 'hi',+ src: 'hi.mp4',+ startInVideo: 1,+ trimLeft: 0,+ type: 'audio',+ },+ 1+ )+ ).toBe(true);+});++test('Should give false if asset is not used at time', () => {+ expect(+ assetIsUsedAtTime(+ {+ duration: 2,+ id: 'hi',+ src: 'hi.mp4',+ startInVideo: 1,+ trimLeft: 0,+ type: 'audio',+ },+ 0+ )+ ).toBe(false);+ expect(+ assetIsUsedAtTime(+ {+ duration: 2,+ id: 'hi',+ src: 'hi.mp4',+ startInVideo: 1,+ trimLeft: 0,+ type: 'audio',+ },+ 3+ )+ ).toBe(false);+});diff --git a/packages/renderer/src/test/split-assets-into-segments.test.tsx b/packages/renderer/src/test/split-assets-into-segments.test.tsxnew file mode 100644index 0000000000..1c7f9074a4--- /dev/null+++ b/packages/renderer/src/test/split-assets-into-segments.test.tsx@@ -0,0 +1,203 @@+import {splitAssetsIntoSegments} from '../assets/split-assets-into-segments';+import {MediaAsset} from '../assets/types';++test('Should create segments of audio clips correctly', () => {+ const assets: MediaAsset[] = [+ // Clip from 0-10 seconds+ {+ duration: 300,+ id: '0.1',+ src: 'asset1.mp4',+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ },+ // Clip from 5-15 seconds+ {+ duration: 300,+ id: '0.2',+ src: 'asset2.mp4',+ startInVideo: 150,+ trimLeft: 0,+ type: 'video',+ },+ ];+ expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([+ {+ duration: 150,+ id: '0.1',+ src: 'asset1.mp4',+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ },+ {+ duration: 150,+ id: '0.1',+ src: 'asset1.mp4',+ startInVideo: 150,+ trimLeft: 150,+ type: 'video',+ },+ {+ duration: 150,+ id: '0.2',+ src: 'asset2.mp4',+ startInVideo: 150,+ trimLeft: 0,+ type: 'video',+ },+ {+ duration: 150,+ id: '0.2',+ src: 'asset2.mp4',+ startInVideo: 300,+ trimLeft: 150,+ type: 'video',+ },+ ]);+});++test('Should leave single clip untouched', () => {+ const assets: MediaAsset[] = [+ // Clip from 0-10 seconds+ {+ duration: 300,+ id: '0.1',+ src: 'asset1.mp4',+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ },+ ];+ expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([+ {+ duration: 300,+ id: '0.1',+ src: 'asset1.mp4',+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ },+ ]);+});++test('Should leave two identical clips the same', () => {+ const assets: MediaAsset[] = [+ // Clip from 0-10 seconds+ {+ duration: 300,+ id: '0.1',+ src: 'asset1.mp4',+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ },+ {+ duration: 300,+ id: '0.2',+ src: 'asset2.mp4',+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ },+ ];+ expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([+ {+ duration: 300,+ id: '0.1',+ src: 'asset1.mp4',+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ },+ {+ duration: 300,+ id: '0.2',+ src: 'asset2.mp4',+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ },+ ]);+});++test('Should not have one-off errors', () => {+ const assets: MediaAsset[] = [+ // Clip from 0-10 seconds+ {+ duration: 2,+ id: '0.1',+ src: 'asset1.mp4',+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ },+ {+ duration: 2,+ id: '0.2',+ src: 'asset2.mp4',+ startInVideo: 1,+ trimLeft: 0,+ type: 'video',+ },+ {+ duration: 2,+ id: '0.3',+ src: 'asset3.mp4',+ startInVideo: 2,+ trimLeft: 0,+ type: 'video',+ },+ ];+ expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([+ {+ duration: 1,+ id: '0.1',+ src: 'asset1.mp4',+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ },+ {+ duration: 1,+ id: '0.1',+ src: 'asset1.mp4',+ startInVideo: 1,+ trimLeft: 1,+ type: 'video',+ },+ {+ duration: 1,+ id: '0.2',+ src: 'asset2.mp4',+ startInVideo: 1,+ trimLeft: 0,+ type: 'video',+ },+ {+ duration: 1,+ id: '0.2',+ src: 'asset2.mp4',+ startInVideo: 2,+ trimLeft: 1,+ type: 'video',+ },+ {+ duration: 1,+ id: '0.3',+ src: 'asset3.mp4',+ startInVideo: 2,+ trimLeft: 0,+ type: 'video',+ },+ {+ duration: 1,+ id: '0.3',+ src: 'asset3.mp4',+ startInVideo: 3,+ trimLeft: 1,+ type: 'video',+ },+ ]);+});diff --git a/packages/tsconfig.settings.json b/packages/tsconfig.settings.jsonindex 9f4bb1696e..a658d32af4 100644--- a/packages/tsconfig.settings.json+++ b/packages/tsconfig.settings.json@@ -5,7 +5,7 @@"declaration": true,"declarationMap": true,"sourceMap": true,- "lib": ["es2018", "dom"],+ "lib": ["es2019", "dom"],"jsx": "react-jsx","noEmit": false,"strict": true,