🎥 Make videos programmatically with React
reintroduce ID and fix tests
Jonny Burger289f65ecfd1c
diff --git a/packages/renderer/src/assets/calculate-asset-positions.ts b/packages/renderer/src/assets/calculate-asset-positions.tsindex 2aca2d2a4d..75fdc7effe 100644--- a/packages/renderer/src/assets/calculate-asset-positions.ts+++ b/packages/renderer/src/assets/calculate-asset-positions.ts@@ -2,8 +2,8 @@ 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) => {- return a.type === b.type && a.src === b.src;+const areEqual = (a: TAsset | UnsafeAsset, b: TAsset) => {+ return a.id === b.id;};const findFrom = (target: TAsset[], asset: TAsset) => {@@ -30,23 +30,24 @@ export const calculateAssetPositions = (frames: TAsset[][]): Assets => {src: asset.src,type: asset.type,duration: null,+ id: asset.id,startInVideo: frame,trimLeft: asset.sequenceFrame,- volume: [asset.volume],+ volume: [],});}+ const found = assets.find(+ (a) => a.duration === null && areEqual(a, asset)+ );+ if (!found) throw new Error('something wrong');if (!findFrom(next, asset)) {- const found = assets.find(- (a) => a.duration === null && areEqual(a, asset)- );- if (!found) throw new Error('something wrong');// Duration calculation:// start 0, range 0-59:// 59 - 0 + 1 ==> 60 frames durationfound.duration = frame - found.startInVideo + 1;- found.volume = [...found.volume, asset.volume];}+ found.volume = [...found.volume, asset.volume];}}diff --git a/packages/renderer/src/assets/types.ts b/packages/renderer/src/assets/types.tsindex b66d461f52..09357b63b8 100644--- a/packages/renderer/src/assets/types.ts+++ b/packages/renderer/src/assets/types.ts@@ -8,6 +8,7 @@ export type UnsafeAsset = Omit<TAsset, 'sequenceFrame' | 'id' | 'volume'> & {duration: number | null;trimLeft: number;volume: number[];+ id: string;};// Volume can either be static, for all frames the same,diff --git a/packages/renderer/src/test/asset-calculation.test.tsx b/packages/renderer/src/test/asset-calculation.test.tsxindex f6ec98c629..717aaae877 100644--- a/packages/renderer/src/test/asset-calculation.test.tsx+++ b/packages/renderer/src/test/asset-calculation.test.tsx@@ -1,6 +1,7 @@import React from 'react';-import {Audio, Sequence, useCurrentFrame, Video} from 'remotion';+import {Audio, interpolate, Sequence, useCurrentFrame, Video} from 'remotion';import {calculateAssetPositions} from '../assets/calculate-asset-positions';+import {MediaAsset} from '../assets/types';import {getAssetsForMarkup} from './get-assets-for-markup';const basicConfig = {@@ -15,12 +16,17 @@ const getPositions = async (Markup: React.FC) => {return calculateAssetPositions(assets);};+const withoutId = (asset: MediaAsset) => {+ const {id, ...others} = asset;+ return others;+};+test('Should be able to collect assets', async () => {const assetPositions = await getPositions(() => (<Video src="https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4" />));expect(assetPositions.length).toBe(1);- expect(assetPositions[0]).toEqual({+ expect(withoutId(assetPositions[0])).toEqual({type: 'video',src:'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',@@ -39,7 +45,7 @@ test('Should get multiple assets', async () => {</div>));expect(assetPositions.length).toBe(2);- expect(assetPositions[0]).toEqual({+ expect(withoutId(assetPositions[0])).toEqual({type: 'video',src:'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',@@ -48,7 +54,7 @@ test('Should get multiple assets', async () => {trimLeft: 0,volume: 1,});- expect(assetPositions[1]).toEqual({+ expect(withoutId(assetPositions[1])).toEqual({type: 'audio',src:'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp3',@@ -71,7 +77,7 @@ test('Should handle jumps inbetween', async () => {);});expect(assetPositions.length).toBe(2);- expect(assetPositions[0]).toEqual({+ expect(withoutId(assetPositions[0])).toEqual({type: 'video',src:'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',@@ -80,7 +86,7 @@ test('Should handle jumps inbetween', async () => {trimLeft: 0,volume: 1,});- expect(assetPositions[1]).toEqual({+ expect(withoutId(assetPositions[1])).toEqual({type: 'video',src:'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',@@ -100,7 +106,7 @@ test('Should support sequencing', async () => {);});expect(assetPositions.length).toBe(1);- expect(assetPositions[0]).toEqual({+ expect(withoutId(assetPositions[0])).toEqual({type: 'video',src:'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',@@ -110,3 +116,31 @@ test('Should support sequencing', async () => {volume: 1,});});++test('Should calculate volumes correctly', async () => {+ const assetPositions = await getPositions(() => {+ const frame = useCurrentFrame();+ return (+ <Video+ volume={interpolate(frame, [0, 4], [0, 1], {+ extrapolateRight: 'clamp',+ })}+ src="https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4"+ />+ );+ });+ expect(assetPositions.length).toBe(1);+ expect(withoutId(assetPositions[0])).toEqual({+ type: 'video',+ src:+ 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',+ duration: 60,+ startInVideo: 0,+ trimLeft: 0,+ volume: new Array(60)+ .fill(true)+ .map((_, i) =>+ interpolate(i, [0, 4], [0, 1], {extrapolateRight: 'clamp'})+ ),+ });+});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.tsindex d5a2889df6..7a1abd6c00 100644--- a/packages/renderer/src/test/asset-is-used-at-time.test.ts+++ b/packages/renderer/src/test/asset-is-used-at-time.test.ts@@ -10,6 +10,7 @@ test('Should give true is asset is used at time', () => {trimLeft: 2,type: 'audio',volume: 1,+ id: '1',},1)@@ -23,6 +24,7 @@ test('Should give true is asset is used at time', () => {trimLeft: 0,type: 'audio',volume: 1,+ id: '1',},1)@@ -39,6 +41,7 @@ test('Should give false if asset is not used at time', () => {trimLeft: 0,type: 'audio',volume: 1,+ id: '1',},0)@@ -52,6 +55,7 @@ test('Should give false if asset is not used at time', () => {trimLeft: 0,type: 'audio',volume: 1,+ id: '1',},3)diff --git a/packages/renderer/src/test/ffmpeg-filters.test.ts b/packages/renderer/src/test/ffmpeg-filters.test.tsindex c0d0f888ae..b163cb5024 100644--- a/packages/renderer/src/test/ffmpeg-filters.test.ts+++ b/packages/renderer/src/test/ffmpeg-filters.test.ts@@ -12,6 +12,7 @@ const asset: MediaAsset = {startInVideo: 0,trimLeft: 0,volume: 1,+ id: '1',};test('Should create a basic filter correctly', () => {diff --git a/packages/renderer/src/test/split-assets-into-segments.test.tsx b/packages/renderer/src/test/split-assets-into-segments.test.tsxindex a0d0bd2d77..d8b46f7d57 100644--- a/packages/renderer/src/test/split-assets-into-segments.test.tsx+++ b/packages/renderer/src/test/split-assets-into-segments.test.tsx@@ -11,6 +11,7 @@ test('Should create segments of audio clips correctly', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '1',},// Clip from 5-15 seconds{@@ -20,6 +21,7 @@ test('Should create segments of audio clips correctly', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '2',},];expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([@@ -30,6 +32,7 @@ test('Should create segments of audio clips correctly', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '1',},{duration: 150,@@ -38,6 +41,7 @@ test('Should create segments of audio clips correctly', () => {trimLeft: 150,type: 'video',volume: 1,+ id: '1',},{duration: 150,@@ -46,6 +50,7 @@ test('Should create segments of audio clips correctly', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '2',},{duration: 150,@@ -54,6 +59,7 @@ test('Should create segments of audio clips correctly', () => {trimLeft: 150,type: 'video',volume: 1,+ id: '2',},]);});@@ -68,6 +74,7 @@ test('Should leave single clip untouched', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '1',},];expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([@@ -78,6 +85,7 @@ test('Should leave single clip untouched', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '1',},]);});@@ -92,6 +100,7 @@ test('Should leave two identical clips the same', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '1',},{duration: 300,@@ -100,6 +109,7 @@ test('Should leave two identical clips the same', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '2',},];expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([@@ -110,6 +120,7 @@ test('Should leave two identical clips the same', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '1',},{duration: 300,@@ -118,6 +129,7 @@ test('Should leave two identical clips the same', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '2',},]);});@@ -132,6 +144,7 @@ test('Should not have one-off errors', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '1',},{duration: 2,@@ -140,6 +153,7 @@ test('Should not have one-off errors', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '2',},{duration: 2,@@ -148,6 +162,7 @@ test('Should not have one-off errors', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '3',},];expect(splitAssetsIntoSegments({assets, duration: 450})).toEqual([@@ -158,6 +173,7 @@ test('Should not have one-off errors', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '1',},{duration: 1,@@ -166,6 +182,7 @@ test('Should not have one-off errors', () => {trimLeft: 1,type: 'video',volume: 1,+ id: '1',},{duration: 1,@@ -174,6 +191,7 @@ test('Should not have one-off errors', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '2',},{duration: 1,@@ -182,6 +200,7 @@ test('Should not have one-off errors', () => {trimLeft: 1,type: 'video',volume: 1,+ id: '2',},{duration: 1,@@ -190,6 +209,7 @@ test('Should not have one-off errors', () => {trimLeft: 0,type: 'video',volume: 1,+ id: '3',},{duration: 1,@@ -198,6 +218,7 @@ test('Should not have one-off errors', () => {trimLeft: 1,type: 'video',volume: 1,+ id: '3',},]);});