Skip to content

🎥 Make videos programmatically with React

fix #673 `Volume array must have at least 1 number` error

Jonny Burgerd3b50f4a0627
diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsx
index 70de368b95..899cbb8fca 100644
--- a/packages/core/src/video/VideoForRendering.tsx
+++ b/packages/core/src/video/VideoForRendering.tsx
@@ -42,7 +42,7 @@ const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<
// but at the same time the same on all threads
const id = useMemo(
() =>
- `audio-${random(props.src ?? '')}-${sequenceContext?.cumulatedFrom}-${
+ `video-${random(props.src ?? '')}-${sequenceContext?.cumulatedFrom}-${
sequenceContext?.relativeFrom
}-${sequenceContext?.durationInFrames}-muted:${props.muted}`,
[
diff --git a/packages/renderer/src/assets/calculate-asset-positions.ts b/packages/renderer/src/assets/calculate-asset-positions.ts
index 53d5e6142e..6060eec61c 100644
--- a/packages/renderer/src/assets/calculate-asset-positions.ts
+++ b/packages/renderer/src/assets/calculate-asset-positions.ts
@@ -16,13 +16,24 @@ const findFrom = (target: TAsset[], asset: TAsset) => {
return true;
};
+const copyAndDeduplicateAssets = (assets: TAsset[]) => {
+ const deduplicated: TAsset[] = [];
+ for (const asset of assets) {
+ if (!deduplicated.find((d) => d.id === asset.id)) {
+ deduplicated.push(asset);
+ }
+ }
+
+ return deduplicated;
+};
+
export const calculateAssetPositions = (frames: TAsset[][]): Assets => {
const assets: UnsafeAsset[] = [];
for (let frame = 0; frame < frames.length; frame++) {
- const prev = (frames[frame - 1] ?? []).slice();
- const current = frames[frame];
- const next = (frames[frame + 1] ?? []).slice();
+ const prev = copyAndDeduplicateAssets(frames[frame - 1] ?? []);
+ const current = copyAndDeduplicateAssets(frames[frame]);
+ const next = copyAndDeduplicateAssets(frames[frame + 1] ?? []);
for (const asset of current) {
if (!findFrom(prev, asset)) {
diff --git a/packages/renderer/src/test/avoid-asset-zero-length.test.ts b/packages/renderer/src/test/avoid-asset-zero-length.test.ts
new file mode 100644
index 0000000000..d137860448
--- /dev/null
+++ b/packages/renderer/src/test/avoid-asset-zero-length.test.ts
@@ -0,0 +1,75 @@
+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',
+ isRemote: false,
+ mediaFrame: 0,
+ playbackRate: 1,
+ src: 'audio.mp3',
+ type: 'audio',
+ volume: 0.5,
+ },
+ {
+ frame: 0,
+ id: 'notunique',
+ isRemote: false,
+ mediaFrame: 0,
+ playbackRate: 1,
+ src: 'audio.mp3',
+ type: 'audio',
+ volume: 0.5,
+ },
+ ],
+ [
+ {
+ frame: 1,
+ id: 'notunique',
+ isRemote: false,
+ mediaFrame: 0,
+ playbackRate: 1,
+ src: 'audio.mp3',
+ type: 'audio',
+ volume: 0.5,
+ },
+ {
+ frame: 1,
+ id: 'notunique',
+ isRemote: false,
+ mediaFrame: 0,
+ playbackRate: 1,
+ src: 'audio.mp3',
+ type: 'audio',
+ volume: 0.5,
+ },
+ {
+ frame: 1,
+ id: 'another',
+ isRemote: false,
+ mediaFrame: 0,
+ playbackRate: 1,
+ src: 'audio.mp3',
+ type: 'audio',
+ volume: 0.5,
+ },
+ ],
+ [
+ {
+ frame: 2,
+ id: 'another',
+ isRemote: false,
+ mediaFrame: 0,
+ playbackRate: 1,
+ src: 'audio.mp3',
+ type: 'audio',
+ volume: 0.5,
+ },
+ ],
+ ]).length
+ ).toBe(4);
+});