🎥 Make videos programmatically with React
fix audio assets exception
Jonny Burgerbacfdf3e9fa2
diff --git a/packages/renderer/src/assets/calculate-asset-positions.ts b/packages/renderer/src/assets/calculate-asset-positions.tsindex 6060eec61c..e03ae6a7f0 100644--- a/packages/renderer/src/assets/calculate-asset-positions.ts+++ b/packages/renderer/src/assets/calculate-asset-positions.ts@@ -1,6 +1,7 @@import {TAsset} from 'remotion';+import {resolveAssetSrc} from '../resolve-asset-src';import {splitAssetsIntoSegments} from './split-assets-into-segments';-import {Assets, MediaAsset, UnsafeAsset} from './types';+import {Assets, MediaAsset, uncompressMediaAsset, UnsafeAsset} from './types';const areEqual = (a: TAsset | UnsafeAsset, b: TAsset) => {return a.id === b.id;@@ -38,7 +39,7 @@ export const calculateAssetPositions = (frames: TAsset[][]): Assets => {for (const asset of current) {if (!findFrom(prev, asset)) {assets.push({- src: asset.src,+ src: resolveAssetSrc(uncompressMediaAsset(frames.flat(1), asset).src),type: asset.type,duration: null,id: asset.id,diff --git a/packages/renderer/src/assets/types.ts b/packages/renderer/src/assets/types.tsindex d93b4b6867..4c728ab0c2 100644--- a/packages/renderer/src/assets/types.ts+++ b/packages/renderer/src/assets/types.ts@@ -26,8 +26,8 @@ export type MediaAsset = Omit<UnsafeAsset, 'duration' | 'volume'> & {export const uncompressMediaAsset = (allAssets: TAsset[],- assetToUncompress: MediaAsset-): MediaAsset => {+ assetToUncompress: TAsset+): TAsset => {const isCompressed = assetToUncompress.src.match(/same-as-(.*)-([0-9]+)$/);if (!isCompressed) {return assetToUncompress;diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.tsindex f8f15434d8..462cabddd8 100644--- a/packages/renderer/src/stitcher.ts+++ b/packages/renderer/src/stitcher.ts@@ -116,14 +116,8 @@ export const stitchFramesToVideo = async (options: {markAllAssetsAsDownloaded();const assetPositions = calculateAssetPositions(fileUrlAssets);- const assetPaths = assetPositions.map((asset) =>- resolveAssetSrc(- uncompressMediaAsset(options.assetsInfo.assets.flat(1), asset).src- )- );-const assetAudioDetails = await getAssetAudioDetails({- assetPaths,+ assetPaths: assetPositions.map((a) => a.src),parallelism: options.parallelism,});@@ -151,7 +145,7 @@ export const stitchFramesToVideo = async (options: {? ['-i', `element-%0${frameInfo.numberLength}d.${imageFormat}`]: null,...assetsToFfmpegInputs({- assets: assetPaths,+ assets: assetPositions.map((a) => a.src),isAudioOnly,fps: options.fps,frameCount: options.assetsInfo.assets.length,diff --git a/packages/renderer/src/test/asset-calculation.test.tsx b/packages/renderer/src/test/asset-calculation.test.tsxindex 91a643b7df..192936fb15 100644--- a/packages/renderer/src/test/asset-calculation.test.tsx+++ b/packages/renderer/src/test/asset-calculation.test.tsx@@ -28,8 +28,7 @@ test('Should be able to collect assets', async () => {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',+ src: 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',duration: 60,startInVideo: 0,trimLeft: 0,@@ -49,8 +48,7 @@ test('Should get multiple assets', async () => {expect(assetPositions.length).toBe(2);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',+ src: 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',duration: 60,startInVideo: 0,trimLeft: 0,@@ -60,8 +58,7 @@ test('Should get multiple assets', async () => {});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',+ src: 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp3',duration: 60,startInVideo: 0,trimLeft: 0,@@ -85,8 +82,7 @@ test('Should handle jumps inbetween', async () => {expect(assetPositions.length).toBe(2);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',+ src: 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',duration: 20,startInVideo: 0,trimLeft: 0,@@ -96,8 +92,7 @@ test('Should handle jumps inbetween', async () => {});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',+ src: 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',duration: 39,startInVideo: 21,trimLeft: 21,@@ -118,8 +113,7 @@ test('Should support sequencing', async () => {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',+ src: 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',duration: 10,startInVideo: 0,trimLeft: 20,@@ -145,8 +139,7 @@ test('Should calculate volumes correctly', async () => {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',+ src: 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',duration: 60,startInVideo: 0,trimLeft: 0,@@ -183,8 +176,7 @@ test('Should calculate startFrom correctly', async () => {expect(assetPositions.length).toBe(1);expect(withoutId(assetPositions[0])).toEqual({type: 'audio',- src:- 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',+ src: 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',duration: 59,startInVideo: 1,trimLeft: 100,diff --git a/packages/renderer/src/test/asset-compression.test.ts b/packages/renderer/src/test/asset-compression.test.tsnew file mode 100644index 0000000000..8b8c973c21--- /dev/null+++ b/packages/renderer/src/test/asset-compression.test.ts@@ -0,0 +1,54 @@+import {TAsset} from 'remotion';+import {compressAsset} from 'remotion/src/compress-assets';+import {calculateAssetPositions} from '../assets/calculate-asset-positions';++test('Should compress and uncompress assets', () => {+ const uncompressed: TAsset[] = [+ [+ {+ frame: 0,+ id: 'my-id',+ src: String('x').repeat(1000),+ isRemote: true,+ mediaFrame: 0,+ playbackRate: 1,+ type: 'video' as const,+ volume: 1,+ },+ ],+ [+ {+ frame: 1,+ id: 'my-id',+ src: String('x').repeat(1000),+ isRemote: true,+ mediaFrame: 0,+ playbackRate: 1,+ type: 'video' as const,+ volume: 1,+ },+ ],+ ].flat(1);++ const compressedAssets = uncompressed.map((asset, i) => {+ return compressAsset(uncompressed.slice(0, i), asset);+ });++ expect(compressedAssets[0].src).toBe(String('x').repeat(1000));+ expect(compressedAssets[1].src).toBe('same-as-my-id-0');++ const assPos = calculateAssetPositions([compressedAssets]);+ expect(assPos).toEqual([+ {+ duration: 1,+ id: 'my-id',+ isRemote: true,+ playbackRate: 1,+ src: String('x').repeat(1000),+ startInVideo: 0,+ trimLeft: 0,+ type: 'video',+ volume: 1,+ },+ ]);+});diff --git a/packages/renderer/src/test/dont-skip-assets.test.ts b/packages/renderer/src/test/dont-skip-assets.test.tsindex 814b8c39fe..2dc14f8674 100644--- a/packages/renderer/src/test/dont-skip-assets.test.ts+++ b/packages/renderer/src/test/dont-skip-assets.test.ts@@ -14,8 +14,7 @@ test('Dont skip assets', () => {src: 'http://localhost:3000/4793bac32f610ffba8197b8a3422456f.mp3',type: 'audio',duration: 180,- id:- 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',+ id: 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',startInVideo: 0,trimLeft: 0,volume: 1,@@ -26,8 +25,7 @@ test('Dont skip assets', () => {src: 'http://localhost:3000/4793bac32f610ffba8197b8a3422456f.mp3',type: 'audio',duration: 40,- id:- 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',+ id: 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',startInVideo: 180,trimLeft: 180,volume: 1,@@ -38,8 +36,7 @@ test('Dont skip assets', () => {src: 'http://localhost:3000/4793bac32f610ffba8197b8a3422456f.mp3',type: 'audio',duration: 2714,- id:- 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',+ id: 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',startInVideo: 220,trimLeft: 220,volume: 1,@@ -80,8 +77,7 @@ const mock: TAsset[][] = new Array(2934){type: 'audio' as const,src: 'http://localhost:3000/4793bac32f610ffba8197b8a3422456f.mp3',- id:- 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',+ id: 'audio-0.24816237785853446-undefined-undefined-undefined-muted:undefined',frame: k,volume: 1,isRemote: false,