Skip to content

πŸŽ₯ Make videos programmatically with React

`@remotion/studio`: Fix sequence props watcher warning on scrub

Jonny Burger1a79a390e9f1
diff --git a/packages/example/src/Premount/CascadingPremount.tsx b/packages/example/src/Premount/CascadingPremount.tsx
new file mode 100644
index 0000000000..926e2bc779
--- /dev/null
+++ b/packages/example/src/Premount/CascadingPremount.tsx
@@ -0,0 +1,38 @@
+import {Video} from '@remotion/media';
+import React from 'react';
+import {Sequence, useVideoConfig} from 'remotion';
+
+const SLICE_DURATION_FRAMES = 3; // 0.1sec at 30fps
+const NUM_SLICES = 10;
+const PREMOUNT_SEC = 0.5;
+
+/**
+ * Reproduces https://github.com/remotion-dev/remotion/issues/6727
+ *
+ * The outer Sequence starts at frame 15 with premountFor={15},
+ * so it premounts at frame 0. Each inner Sequence also has premountFor={15}.
+ * The issue is that 6 videos are premounted on frame 0 because
+ * premounting cascades through nested Sequences unexpectedly.
+ */
+export const CascadingPremount: React.FC = () => {
+ const src = 'https://remotion.media/video.mp4';
+ const {fps} = useVideoConfig();
+
+ return (
+ <Sequence from={15} premountFor={15}>
+ {new Array(NUM_SLICES).fill(0).map((_, i) => {
+ const from = i * SLICE_DURATION_FRAMES;
+ return (
+ <Sequence
+ key={i}
+ from={from}
+ durationInFrames={SLICE_DURATION_FRAMES}
+ premountFor={PREMOUNT_SEC * fps}
+ >
+ <Video src={src} trimBefore={from} />
+ </Sequence>
+ );
+ })}
+ </Sequence>
+ );
+};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 70fd2147c9..cd167ee550 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -51,6 +51,7 @@ import {
PostmountWithStyles,
} from './Postmount/PostmountExample';
import {PremountedExample} from './Premount';
+import {CascadingPremount} from './Premount/CascadingPremount';
import {PremountedRemoteVideos} from './Premount/RemoteVideos';
import ReactSvg from './ReactSvg';
import InfinityVideo from './ReallyLongVideo';
@@ -1440,6 +1441,14 @@ export const Index: React.FC = () => {
durationInFrames={300}
width={1920}
/>
+ <Composition
+ id="cascading-premount"
+ component={CascadingPremount}
+ fps={30}
+ height={1080}
+ durationInFrames={60}
+ width={1920}
+ />
</Folder>
<Folder name="Postmount">
<Composition
diff --git a/packages/studio-server/src/preview-server/sequence-props-watchers.ts b/packages/studio-server/src/preview-server/sequence-props-watchers.ts
index 20cea526ef..5a58ab1989 100644
--- a/packages/studio-server/src/preview-server/sequence-props-watchers.ts
+++ b/packages/studio-server/src/preview-server/sequence-props-watchers.ts
@@ -14,6 +14,7 @@ import {
type WatcherInfo = {
unwatch: () => void;
+ refCount: number;
};
const sequencePropsWatchers: Record<string, Record<string, WatcherInfo>> = {};
@@ -86,9 +87,10 @@ export const subscribeToSequencePropsWatchers = ({
const {nodePath} = initialResult;
const watcherKey = makeWatcherKey({absolutePath, nodePath});
- // Unwatch any existing watcher for the same key
+ // If a watcher already exists for this key, just bump the ref count
if (sequencePropsWatchers[clientId]?.[watcherKey]) {
- sequencePropsWatchers[clientId][watcherKey].unwatch();
+ sequencePropsWatchers[clientId][watcherKey].refCount++;
+ return initialResult;
}
const {unwatch} = installFileWatcher({
@@ -119,7 +121,7 @@ export const subscribeToSequencePropsWatchers = ({
sequencePropsWatchers[clientId] = {};
}
- sequencePropsWatchers[clientId][watcherKey] = {unwatch};
+ sequencePropsWatchers[clientId][watcherKey] = {unwatch, refCount: 1};
return initialResult;
};
@@ -142,15 +144,14 @@ export const unsubscribeFromSequencePropsWatchers = ({
!sequencePropsWatchers[clientId] ||
!sequencePropsWatchers[clientId][watcherKey]
) {
- // eslint-disable-next-line no-console
- console.warn(
- `Unexpected: unsubscribe for sequence props watcher that does not exist (clientId=${clientId}, key=${watcherKey})`,
- );
return;
}
- sequencePropsWatchers[clientId][watcherKey].unwatch();
- delete sequencePropsWatchers[clientId][watcherKey];
+ sequencePropsWatchers[clientId][watcherKey].refCount--;
+ if (sequencePropsWatchers[clientId][watcherKey].refCount <= 0) {
+ sequencePropsWatchers[clientId][watcherKey].unwatch();
+ delete sequencePropsWatchers[clientId][watcherKey];
+ }
};
export const unsubscribeClientSequencePropsWatchers = (clientId: string) => {
diff --git a/packages/studio/src/components/Timeline/use-sequence-props-subscription.ts b/packages/studio/src/components/Timeline/use-sequence-props-subscription.ts
index 92064b3fae..8271fe36b8 100644
--- a/packages/studio/src/components/Timeline/use-sequence-props-subscription.ts
+++ b/packages/studio/src/components/Timeline/use-sequence-props-subscription.ts
@@ -89,6 +89,7 @@ export const useSequencePropsSubscription = (
return;
}
+ let cancelled = false;
const keys = schemaKeysString.split(',');
callApi('/api/subscribe-to-sequence-props', {
@@ -99,6 +100,10 @@ export const useSequencePropsSubscription = (
clientId,
})
.then((result) => {
+ if (cancelled) {
+ return;
+ }
+
if (
currentLocationSource.current !== locationSource ||
currentLocationLine.current !== locationLine ||
@@ -116,12 +121,17 @@ export const useSequencePropsSubscription = (
}
})
.catch((err) => {
+ if (cancelled) {
+ return;
+ }
+
nodePathRef.current = null;
Internals.Log.error(err);
setPropStatusesForSequence(null);
});
return () => {
+ cancelled = true;
const currentNodePath = nodePathRef.current;
// Only clear props on true unmount, not on re-subscribe due to
// line number changes β€” avoids flicker while re-subscribing.