🎥 Make videos programmatically with React
remove unnecessary
Jonny Burgerfbbf5fc92635
diff --git a/packages/renderer/src/assets/asset-is-used-at-time.ts b/packages/renderer/src/assets/asset-is-used-at-time.tsdeleted file mode 100644index 672135e14f..0000000000--- a/packages/renderer/src/assets/asset-is-used-at-time.ts+++ /dev/null@@ -1,7 +0,0 @@-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 ed51966f07..04dad31d4c 100644--- a/packages/renderer/src/assets/calculate-asset-positions.ts+++ b/packages/renderer/src/assets/calculate-asset-positions.ts@@ -1,6 +1,6 @@import {TAsset} from 'remotion';import {resolveAssetSrc} from '../resolve-asset-src';-import {splitAssetsIntoSegments} from './split-assets-into-segments';+import {convertAssetToFlattenedVolume} from './flatten-volume-array';import {Assets, MediaAsset, uncompressMediaAsset, UnsafeAsset} from './types';const areEqual = (a: TAsset | UnsafeAsset, b: TAsset) => {@@ -71,8 +71,5 @@ export const calculateAssetPositions = (frames: TAsset[][]): Assets => {}}- return splitAssetsIntoSegments({- assets: assets as MediaAsset[],- duration: frames.length,- });+ return (assets as MediaAsset[]).map((a) => convertAssetToFlattenedVolume(a));};diff --git a/packages/renderer/src/assets/split-assets-into-segments.ts b/packages/renderer/src/assets/split-assets-into-segments.tsdeleted file mode 100644index d4144b881d..0000000000--- a/packages/renderer/src/assets/split-assets-into-segments.ts+++ /dev/null@@ -1,108 +0,0 @@-// 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 {convertAssetToFlattenedVolume} from './flatten-volume-array';-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.src).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;- const amountOfFramesThatGetCut = cutStart - assetStart;- // Video goes beyond cut- if (assetStart < cutEnd && assetEnd > cutEnd) {- const newDuration = cutEnd - cutStart;-- return {- ...a,- startInVideo: cutStart,- volume:- typeof a.volume === 'number'- ? a.volume- : a.volume.slice(- amountOfFramesThatGetCut,- newDuration + amountOfFramesThatGetCut- ),- duration: cutEnd - cutStart,- trimLeft: a.trimLeft + amountOfFramesThatGetCut,- } as MediaAsset;- }-- // Video starts before cut- if (assetStart < cutStart && assetEnd > cutStart) {- const newDuration = a.duration - amountOfFramesThatGetCut;- return {- ...a,- startInVideo: cutStart,- volume:- typeof a.volume === 'number'- ? a.volume- : a.volume.slice(- amountOfFramesThatGetCut,- newDuration + amountOfFramesThatGetCut- ),- duration: newDuration,- trimLeft: a.trimLeft + amountOfFramesThatGetCut,- } as MediaAsset;- }-- // 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(2)- .filter(Internals.truthy)- .map((a) => convertAssetToFlattenedVolume(a));-};diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.tsindex 9ba4173639..3e0ffa82f6 100644--- a/packages/renderer/src/stitch-frames-to-video.ts+++ b/packages/renderer/src/stitch-frames-to-video.ts@@ -62,8 +62,6 @@ const getAssetsData = async (options: StitcherOptions) => {markAllAssetsAsDownloaded();const assetPositions: Assets = calculateAssetPositions(fileUrlAssets);- markAllAssetsAsDownloaded();-const assetAudioDetails = await getAssetAudioDetails({assetPaths: assetPositions.map((a) => a.src),});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.tsdeleted file mode 100644index 2c6b834b27..0000000000--- a/packages/renderer/src/test/asset-is-used-at-time.test.ts+++ /dev/null@@ -1,67 +0,0 @@-import {assetIsUsedAtTime} from '../assets/asset-is-used-at-time';--test('Should give true is asset is used at time', () => {- expect(- assetIsUsedAtTime(- {- duration: 2,- src: 'hi.mp4',- startInVideo: 0,- trimLeft: 2,- type: 'audio',- volume: 1,- id: '1',- playbackRate: 1,- },- 1- )- ).toBe(true);- expect(- assetIsUsedAtTime(- {- duration: 2,- src: 'hi.mp4',- startInVideo: 1,- trimLeft: 0,- type: 'audio',- volume: 1,- id: '1',- playbackRate: 1,- },- 1- )- ).toBe(true);-});--test('Should give false if asset is not used at time', () => {- expect(- assetIsUsedAtTime(- {- duration: 2,- src: 'hi.mp4',- startInVideo: 1,- trimLeft: 0,- type: 'audio',- volume: 1,- id: '1',- playbackRate: 1,- },- 0- )- ).toBe(false);- expect(- assetIsUsedAtTime(- {- duration: 2,- src: 'hi.mp4',- startInVideo: 1,- trimLeft: 0,- type: 'audio',- volume: 1,- id: '1',- playbackRate: 1,- },- 3- )- ).toBe(false);-});diff --git a/packages/renderer/src/test/avoid-asset-zero-length.test.ts b/packages/renderer/src/test/avoid-asset-zero-length.test.tsdeleted file mode 100644index 3ab7d6fa69..0000000000--- a/packages/renderer/src/test/avoid-asset-zero-length.test.ts+++ /dev/null@@ -1,69 +0,0 @@-import {calculateAssetPositions} from '../assets/calculate-asset-positions';--// Previously <v2.5.1 this would error out due to two assets having the exact same-test('Conflicting IDs should be handled', () => {- expect(- calculateAssetPositions([- [- {- frame: 0,- id: 'notunique',- mediaFrame: 0,- playbackRate: 1,- src: 'audio.mp3',- type: 'audio',- volume: 0.5,- },- {- frame: 0,- id: 'notunique',- mediaFrame: 0,- playbackRate: 1,- src: 'audio.mp3',- type: 'audio',- volume: 0.5,- },- ],- [- {- frame: 1,- id: 'notunique',- mediaFrame: 0,- playbackRate: 1,- src: 'audio.mp3',- type: 'audio',- volume: 0.5,- },- {- frame: 1,- id: 'notunique',- mediaFrame: 0,- playbackRate: 1,- src: 'audio.mp3',- type: 'audio',- volume: 0.5,- },- {- frame: 1,- id: 'another',- mediaFrame: 0,- playbackRate: 1,- src: 'audio.mp3',- type: 'audio',- volume: 0.5,- },- ],- [- {- frame: 2,- id: 'another',- mediaFrame: 0,- playbackRate: 1,- src: 'audio.mp3',- type: 'audio',- volume: 0.5,- },- ],- ]).length- ).toBe(4);-});diff --git a/packages/renderer/src/test/dont-skip-assets.test.ts b/packages/renderer/src/test/dont-skip-assets.test.tsindex 17753e5c56..c2332370f7 100644--- a/packages/renderer/src/test/dont-skip-assets.test.ts+++ b/packages/renderer/src/test/dont-skip-assets.test.ts@@ -13,33 +13,13 @@ test('Dont skip assets', () => {{src: 'http://localhost:3000/4793bac32f610ffba8197b8a3422456f.mp3',type: 'audio',- duration: 180,+ duration: 2934,id: 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',startInVideo: 0,trimLeft: 0,volume: 1,playbackRate: 1,},- {- src: 'http://localhost:3000/4793bac32f610ffba8197b8a3422456f.mp3',- type: 'audio',- duration: 40,- id: 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',- startInVideo: 180,- trimLeft: 180,- volume: 1,- playbackRate: 1,- },- {- src: 'http://localhost:3000/4793bac32f610ffba8197b8a3422456f.mp3',- type: 'audio',- duration: 2714,- id: 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',- startInVideo: 220,- trimLeft: 220,- volume: 1,- playbackRate: 1,- },{src: 'http://localhost:3000/e15ac5e3d531199ebb1828ca6a99100d.webm',type: 'video',diff --git a/packages/renderer/src/test/split-assets-into-segments.test.tsx b/packages/renderer/src/test/split-assets-into-segments.test.tsxdeleted file mode 100644index 926621df35..0000000000--- a/packages/renderer/src/test/split-assets-into-segments.test.tsx+++ /dev/null@@ -1,306 +0,0 @@-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,- src: 'asset1.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- // Clip from 5-15 seconds- {- duration: 300,- src: 'asset2.mp4',- startInVideo: 150,- trimLeft: 0,- type: 'video',- volume: 1,- id: '2',- playbackRate: 1,- },- ];- expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([- {- duration: 150,- src: 'asset1.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- {- duration: 150,- src: 'asset1.mp4',- startInVideo: 150,- trimLeft: 150,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- {- duration: 150,- src: 'asset2.mp4',- startInVideo: 150,- trimLeft: 0,- type: 'video',- volume: 1,- id: '2',- playbackRate: 1,- },- {- duration: 150,- src: 'asset2.mp4',- startInVideo: 300,- trimLeft: 150,- type: 'video',- volume: 1,- id: '2',- playbackRate: 1,- },- ]);-});--test('Should leave single clip untouched', () => {- const assets: MediaAsset[] = [- // Clip from 0-10 seconds- {- duration: 300,- src: 'asset1.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- ];- expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([- {- duration: 300,- src: 'asset1.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- ]);-});--test('Should leave two identical clips the same', () => {- const assets: MediaAsset[] = [- // Clip from 0-10 seconds- {- duration: 300,- src: 'asset1.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- {- duration: 300,- src: 'asset2.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: 1,- id: '2',- playbackRate: 1,- },- ];- expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([- {- duration: 300,- src: 'asset1.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- {- duration: 300,- src: 'asset2.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: 1,- id: '2',- playbackRate: 1,- },- ]);-});--test('Should not have one-off errors', () => {- const assets: MediaAsset[] = [- // Clip from 0-10 seconds- {- duration: 2,- src: 'asset1.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- {- duration: 2,- src: 'asset2.mp4',- startInVideo: 1,- trimLeft: 0,- type: 'video',- volume: 1,- id: '2',- playbackRate: 1,- },- {- duration: 2,- src: 'asset3.mp4',- startInVideo: 2,- trimLeft: 0,- type: 'video',- volume: 1,- id: '3',- playbackRate: 1,- },- ];- expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([- {- duration: 1,- src: 'asset1.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- {- duration: 1,- src: 'asset1.mp4',- startInVideo: 1,- trimLeft: 1,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- {- duration: 1,- src: 'asset2.mp4',- startInVideo: 1,- trimLeft: 0,- type: 'video',- volume: 1,- id: '2',- playbackRate: 1,- },- {- duration: 1,- src: 'asset2.mp4',- startInVideo: 2,- trimLeft: 1,- type: 'video',- volume: 1,- id: '2',- playbackRate: 1,- },- {- duration: 1,- src: 'asset3.mp4',- startInVideo: 2,- trimLeft: 0,- type: 'video',- volume: 1,- id: '3',- playbackRate: 1,- },- {- duration: 1,- src: 'asset3.mp4',- startInVideo: 3,- trimLeft: 1,- type: 'video',- volume: 1,- id: '3',- playbackRate: 1,- },- ]);-});--test('Should have correct volume arrays', () => {- expect(- splitAssetsIntoSegments({- assets: [- {- duration: 100,- src: 'asset1.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: new Array(100).fill(1).map((_, i) => (i === 0 ? 0 : 1)),- id: '1',- playbackRate: 1,- },- {- duration: 50,- src: 'asset2.mp4',- startInVideo: 50,- trimLeft: 0,- type: 'video',- volume: new Array(100).fill(1).map((_, i) => (i === 0 ? 0 : 1)),- id: 'breaker',- playbackRate: 1,- },- ],- duration: 100,- })- ).toEqual([- {- duration: 50,- src: 'asset1.mp4',- startInVideo: 0,- trimLeft: 0,- type: 'video',- volume: new Array(50).fill(1).map((_, i) => (i === 0 ? 0 : 1)),- id: '1',- playbackRate: 1,- },- {- duration: 50,- src: 'asset1.mp4',- startInVideo: 50,- trimLeft: 50,- type: 'video',- volume: 1,- id: '1',- playbackRate: 1,- },- {- duration: 50,- src: 'asset2.mp4',- startInVideo: 50,- trimLeft: 0,- type: 'video',- volume: new Array(100).fill(1).map((_, i) => (i === 0 ? 0 : 1)),- id: 'breaker',- playbackRate: 1,- },- ]);-});