Skip to content

🎥 Make videos programmatically with React

volume can be an array

Jonny Burgerae60458653e4
diff --git a/packages/renderer/src/assets/calculate-asset-positions.ts b/packages/renderer/src/assets/calculate-asset-positions.ts
index 2b58db9458..2aca2d2a4d 100644
--- a/packages/renderer/src/assets/calculate-asset-positions.ts
+++ b/packages/renderer/src/assets/calculate-asset-positions.ts
@@ -32,7 +32,7 @@ export const calculateAssetPositions = (frames: TAsset[][]): Assets => {
duration: null,
startInVideo: frame,
trimLeft: asset.sequenceFrame,
- volume: asset.volume,
+ volume: [asset.volume],
});
}
@@ -45,6 +45,7 @@ export const calculateAssetPositions = (frames: TAsset[][]): Assets => {
// start 0, range 0-59:
// 59 - 0 + 1 ==> 60 frames duration
found.duration = frame - found.startInVideo + 1;
+ found.volume = [...found.volume, asset.volume];
}
}
}
diff --git a/packages/renderer/src/assets/flatten-volume-array.ts b/packages/renderer/src/assets/flatten-volume-array.ts
new file mode 100644
index 0000000000..3716442898
--- /dev/null
+++ b/packages/renderer/src/assets/flatten-volume-array.ts
@@ -0,0 +1,24 @@
+// An input is an array with volume levels for each frame, like
+// [1, 1, 1, 1, 1, ...].
+// If all elements are the same, we flatten it to `1`, otherwise we leave it as an array.
+
+import {AssetVolume, MediaAsset} from './types';
+
+const flattenVolumeArray = (volume: AssetVolume): AssetVolume => {
+ if (typeof volume === 'number') {
+ return volume;
+ }
+ if (new Set(volume).size === 1) {
+ return volume[0];
+ }
+ return volume;
+};
+
+export const convertAssetToFlattenedVolume = (
+ asset: MediaAsset
+): MediaAsset => {
+ return {
+ ...asset,
+ volume: flattenVolumeArray(asset.volume),
+ };
+};
diff --git a/packages/renderer/src/assets/split-assets-into-segments.ts b/packages/renderer/src/assets/split-assets-into-segments.ts
index 2f641e130a..6afee432f6 100644
--- a/packages/renderer/src/assets/split-assets-into-segments.ts
+++ b/packages/renderer/src/assets/split-assets-into-segments.ts
@@ -3,6 +3,7 @@
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
@@ -50,7 +51,7 @@ export const splitAssetsIntoSegments = ({
return {
...a,
duration: cutEnd - a.startInVideo,
- };
+ } as MediaAsset;
}
// Video starts before cut
if (assetStart < cutStart && assetEnd > cutStart) {
@@ -60,7 +61,7 @@ export const splitAssetsIntoSegments = ({
startInVideo: cutStart,
duration: a.duration - amountOfFramesThatGetCut,
trimLeft: a.trimLeft + amountOfFramesThatGetCut,
- };
+ } as MediaAsset;
}
// Video is inside cut, leave
@@ -78,6 +79,7 @@ export const splitAssetsIntoSegments = ({
throw new Error('Unhandled');
});
})
- .flat(Infinity)
- .filter(Internals.truthy) as MediaAsset[];
+ .flat(2)
+ .filter(Internals.truthy)
+ .map((a) => convertAssetToFlattenedVolume(a));
};
diff --git a/packages/renderer/src/assets/types.ts b/packages/renderer/src/assets/types.ts
index 8697880cdf..b66d461f52 100644
--- a/packages/renderer/src/assets/types.ts
+++ b/packages/renderer/src/assets/types.ts
@@ -1,13 +1,22 @@
import {TAsset} from 'remotion';
-export type UnsafeAsset = Omit<TAsset, 'sequenceFrame' | 'id'> & {
+// An unsafe asset is an asset with looser types, which occurs
+// during construction of the asset list. Prefer the MediaAsset
+// type instead.
+export type UnsafeAsset = Omit<TAsset, 'sequenceFrame' | 'id' | 'volume'> & {
startInVideo: number;
duration: number | null;
trimLeft: number;
+ volume: number[];
};
-export type MediaAsset = Omit<UnsafeAsset, 'duration'> & {
+// Volume can either be static, for all frames the same,
+// or an array with volume for each frame.
+export type AssetVolume = number | number[];
+
+export type MediaAsset = Omit<UnsafeAsset, 'duration' | 'volume'> & {
duration: number;
+ volume: AssetVolume;
};
export type AssetAudioDetails = {