Skip to content

🎥 Make videos programmatically with React

@remotion/studio: Add freeze frame sequence action

Jonny Burgerf4ee094c5e06
diff --git a/packages/studio-server/src/codemods/update-sequence-props/update-sequence-props.ts b/packages/studio-server/src/codemods/update-sequence-props/update-sequence-props.ts
index 1353dcf17a..b927055a8b 100644
--- a/packages/studio-server/src/codemods/update-sequence-props/update-sequence-props.ts
+++ b/packages/studio-server/src/codemods/update-sequence-props/update-sequence-props.ts
@@ -20,6 +20,7 @@ export type SequencePropUpdate = {
key: string;
value: unknown;
defaultValue: unknown | null;
+ remove?: boolean;
};
export type RemovedProp = {
@@ -143,12 +144,13 @@ const updateSequencePropsNode = ({
}),
);
- for (const {key, value, defaultValue} of updates) {
+ for (const {key, value, defaultValue, remove} of updates) {
let oldValueString = '';
const isDefault =
- defaultValue !== null &&
- JSON.stringify(value) === JSON.stringify(defaultValue);
+ remove === true ||
+ (defaultValue !== null &&
+ JSON.stringify(value) === JSON.stringify(defaultValue));
const dotIndex = key.indexOf('.');
const isNested = dotIndex !== -1;
@@ -200,7 +202,7 @@ const updateSequencePropsNode = ({
if (attr && attr.type !== 'JSXSpreadAttribute' && node.attributes) {
node.attributes.splice(attrIndex!, 1);
}
- } else {
+ } else if (!remove) {
const parsed = parseValueExpression(value);
const newValue =
diff --git a/packages/studio-server/src/preview-server/routes/save-sequence-props.ts b/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
index 8e7905464b..085217f5c8 100644
--- a/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
+++ b/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
@@ -35,6 +35,7 @@ type ResolvedSequencePropEdit = {
defaultValue: unknown | null;
defaultValueString: string | null;
schema: SaveSequencePropEdit['schema'];
+ remove: boolean;
};
type SequencePropEditGroup = {
@@ -60,7 +61,8 @@ export const convertSequencePropEditToCodemodChange = (
edit: Pick<
ResolvedSequencePropEdit,
'nodePath' | 'key' | 'value' | 'defaultValue' | 'schema'
- >,
+ > &
+ Pick<Partial<ResolvedSequencePropEdit>, 'remove'>,
): SequencePropsNodeUpdate => {
return {
nodePath: edit.nodePath.nodePath,
@@ -69,6 +71,7 @@ export const convertSequencePropEditToCodemodChange = (
key: edit.key,
value: edit.value,
defaultValue: edit.defaultValue,
+ ...(edit.remove === true ? {remove: edit.remove} : null),
},
],
schema: edit.schema,
@@ -102,7 +105,7 @@ export const saveSequencePropsHandler: ApiHandler<
const editGroups = new Map<string, SequencePropEditGroup>();
for (const [index, edit] of edits.entries()) {
- const parsedValue = JSON.parse(edit.value);
+ const parsedValue = edit.remove ? undefined : JSON.parse(edit.value);
const parsedDefaultValue =
edit.defaultValue !== null ? JSON.parse(edit.defaultValue) : null;
const {absolutePath, fileRelativeToRoot} = resolveFileInsideProject({
@@ -121,13 +124,14 @@ export const saveSequencePropsHandler: ApiHandler<
nodePath: edit.nodePath,
key: edit.key,
value: parsedValue,
- valueString: JSON.stringify(parsedValue),
+ valueString: edit.remove ? 'undefined' : JSON.stringify(parsedValue),
defaultValue: parsedDefaultValue,
defaultValueString:
parsedDefaultValue !== null
? JSON.stringify(parsedDefaultValue)
: null,
schema: edit.schema,
+ remove: edit.remove ?? false,
});
editGroups.set(absolutePath, group);
}
diff --git a/packages/studio-server/src/test/update-sequence-props.test.ts b/packages/studio-server/src/test/update-sequence-props.test.ts
index 4e070a99f7..8b679d6f96 100644
--- a/packages/studio-server/src/test/update-sequence-props.test.ts
+++ b/packages/studio-server/src/test/update-sequence-props.test.ts
@@ -82,6 +82,32 @@ test('updateSequenceProps should remove attribute when value equals default', as
expect(output.split('\n')[7]).toContain('hueShift={30}');
});
+test('updateSequenceProps should remove optional attribute', async () => {
+ const input = `import {Sequence} from 'remotion';
+
+export const Example: React.FC = () => {
+ return (
+ <Sequence from={0} freeze={12}>
+ <div />
+ </Sequence>
+ );
+};
+`;
+
+ const {output, oldValueStrings} = await updateSequenceProps({
+ input,
+ nodePath: lineColumnToNodePath(input, 5),
+ updates: [
+ {key: 'freeze', value: undefined, defaultValue: null, remove: true},
+ ],
+ schema: NoReactInternals.sequenceSchema,
+ prettierConfigOverride: null,
+ });
+
+ expect(oldValueStrings[0]).toBe('12');
+ expect(output).not.toContain('freeze=');
+});
+
test('updateSequenceProps should set boolean true as shorthand', async () => {
const {output} = await updateSequenceProps({
input: lightLeakInput,
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index d8bb0514ce..98070b47e9 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -299,10 +299,18 @@ export type SaveSequencePropEdit = {
fileName: string;
nodePath: SequencePropsSubscriptionKey;
key: string;
- value: string;
defaultValue: string | null;
schema: SequenceSchema;
-};
+} & (
+ | {
+ value: string;
+ remove?: false;
+ }
+ | {
+ remove: true;
+ value?: undefined;
+ }
+);
export type SaveSequencePropsRequest = {
edits: SaveSequencePropEdit[];
diff --git a/packages/studio/src/components/Timeline/TimelineList.tsx b/packages/studio/src/components/Timeline/TimelineList.tsx
index 0b02ea461a..66da99ea0d 100644
--- a/packages/studio/src/components/Timeline/TimelineList.tsx
+++ b/packages/studio/src/components/Timeline/TimelineList.tsx
@@ -25,6 +25,7 @@ export const TimelineList: React.FC<{
sequence={track.sequence}
nodePathInfo={track.nodePathInfo}
keyframeDisplayOffset={track.keyframeDisplayOffset}
+ sequenceFrameOffset={track.sequenceFrameOffset}
/>
</div>
);
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index 88a5005dbb..b2a9938b4f 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -65,6 +65,7 @@ const TimelineSequenceCurrentFrame: React.FC<{
readonly nodePathInfo: SequenceNodePathInfo | null;
readonly sequenceFrameOffset: number;
readonly fromCanUpdate: boolean;
+ readonly frozenFrame: number | null;
readonly onMoveDragPointerDown: (
e: React.PointerEvent<HTMLDivElement>,
) => void;
@@ -78,6 +79,7 @@ const TimelineSequenceCurrentFrame: React.FC<{
nodePathInfo,
sequenceFrameOffset,
fromCanUpdate,
+ frozenFrame,
onMoveDragPointerDown,
}) => {
const ref = useRef<HTMLDivElement>(null);
@@ -198,6 +200,7 @@ const TimelineSequenceCurrentFrame: React.FC<{
premounted={isPremounting}
postmounted={isPostmounting ? s.duration - 1 : null}
roundedFrame={roundedFrame}
+ frozenFrame={frozenFrame}
/>
</div>
) : null}
@@ -250,6 +253,17 @@ const TimelineSequenceInner: React.FC<{
const fromCanUpdate = Boolean(
propStatusesForOverride?.from?.status === 'static',
);
+ const freezeStatus = propStatusesForOverride?.freeze;
+ const runtimeFreezeFrame =
+ typeof s.controls?.currentRuntimeValueDotNotation.freeze === 'number'
+ ? s.controls.currentRuntimeValueDotNotation.freeze
+ : null;
+ const frozenFrame =
+ freezeStatus?.status === 'static'
+ ? typeof freezeStatus.codeValue === 'number'
+ ? freezeStatus.codeValue
+ : null
+ : runtimeFreezeFrame;
const {onPointerDown: onMoveDragPointerDown} = useTimelineSequenceFromDrag({
nodePathInfo,
@@ -329,6 +343,7 @@ const TimelineSequenceInner: React.FC<{
nodePathInfo={nodePathInfo}
sequenceFrameOffset={sequenceFrameOffset}
fromCanUpdate={fromCanUpdate}
+ frozenFrame={frozenFrame}
onMoveDragPointerDown={onMoveDragPointerDown}
>
{s.type === 'audio' ? (
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceFrame.tsx b/packages/studio/src/components/Timeline/TimelineSequenceFrame.tsx
index d4077de0e6..7224ff2433 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceFrame.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceFrame.tsx
@@ -1,8 +1,11 @@
import React from 'react';
const relativeFrameStyle: React.CSSProperties = {
+ alignItems: 'center',
+ display: 'flex',
fontSize: 11,
fontFamily: 'Arial, Helvetica, sans-serif',
+ gap: 4,
color: 'white',
opacity: 0.5,
whiteSpace: 'nowrap',
@@ -10,18 +13,40 @@ const relativeFrameStyle: React.CSSProperties = {
userSelect: 'none',
};
+const snowflakeStyle: React.CSSProperties = {
+ flexShrink: 0,
+ height: 12,
+ width: 12,
+};
+
+const FreezeFrameIcon: React.FC = () => {
+ return (
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 512 512"
+ style={snowflakeStyle}
+ fill="currentColor"
+ >
+ <path d="M288 32l0-32-64 0 0 88.4-64-64-39.6 39.6 103.6 103.6 0 56.4-56.4 0-103.6-103.6-39.6 39.6 64 64-88.4 0 0 64 88.4 0c-33.5 33.5-54.9 54.9-64 64L64 391.6c2.5-2.5 37.1-37.1 103.6-103.6l56.4 0 0 56.4C157.5 410.9 122.9 445.5 120.4 448L160 487.6c9.1-9.1 30.5-30.5 64-64l0 88.4 64 0 0-88.4 64 64 39.6-39.6-103.6-103.6 0-56.4 56.4 0 103.6 103.6 39.6-39.6-64-64 88.4 0 0-64-88.4 0c33.5-33.5 54.9-54.9 64-64L448 120.4c-2.5 2.5-37.1 37.1-103.6 103.6l-56.4 0 0-56.4C354.5 101.1 389.1 66.5 391.6 64L352 24.4c-9.1 9.1-30.5 30.5-64 64L288 32z" />
+ </svg>
+ );
+};
+
export const TimelineSequenceFrame: React.FC<{
readonly roundedFrame: number;
+ readonly frozenFrame: number | null;
readonly premounted: boolean;
readonly postmounted: number | null;
-}> = ({roundedFrame, premounted, postmounted}) => {
+}> = ({roundedFrame, frozenFrame, premounted, postmounted}) => {
return (
<div style={relativeFrameStyle}>
- {premounted
- ? '0 (Premounted)'
- : postmounted !== null
- ? `${postmounted} (Postmounted)`
- : roundedFrame}
+ {frozenFrame === null ? null : <FreezeFrameIcon />}
+ {frozenFrame ??
+ (premounted
+ ? '0 (Premounted)'
+ : postmounted !== null
+ ? `${postmounted} (Postmounted)`
+ : roundedFrame)}
</div>
);
};
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
index 53cadcbcdd..9f72898998 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
@@ -175,12 +175,14 @@ export const TimelineSequenceItem: React.FC<{
readonly nestedDepth: number;
readonly nodePathInfo: SequenceNodePathInfo | null;
readonly keyframeDisplayOffset: number;
+ readonly sequenceFrameOffset: number;
readonly trackIndex: number;
}> = ({
nestedDepth,
sequence,
nodePathInfo,
keyframeDisplayOffset,
+ sequenceFrameOffset,
trackIndex,
}) => {
const nodePath = nodePathInfo?.sequenceSubscriptionKey ?? null;
@@ -200,6 +202,7 @@ export const TimelineSequenceItem: React.FC<{
const [sequenceDropRejection, setSequenceDropRejection] = useState<
string | null
>(null);
+ const timelinePosition = Internals.Timeline.useTimelinePosition();
const {canOpenInEditor, openInEditor, originalLocation} =
useOpenSequenceInEditor(sequence);
@@ -544,6 +547,76 @@ export const TimelineSequenceItem: React.FC<{
[mediaSrc],
);
+ const propStatusesForOverride = useMemo(() => {
+ return nodePath
+ ? Internals.getPropStatusesCtx(propStatuses, nodePath)
+ : undefined;
+ }, [propStatuses, nodePath]);
+
+ const freezeStatus = propStatusesForOverride?.freeze;
+ const isFrozen =
+ freezeStatus?.status === 'static' && freezeStatus.codeValue !== undefined;
+
+ const canToggleFreeze =
+ previewConnected &&
+ Boolean(sequence.controls) &&
+ nodePath !== null &&
+ validatedLocation !== null &&
+ freezeStatus !== undefined &&
+ freezeStatus !== null &&
+ freezeStatus.status === 'static';
+
+ const onToggleFreezeFrame = useCallback(() => {
+ if (
+ !canToggleFreeze ||
+ !sequence.controls ||
+ !nodePath ||
+ !validatedLocation ||
+ previewServerState.type !== 'connected'
+ ) {
+ return;
+ }
+
+ const rawFreezeFrame = Math.round(
+ timelinePosition - sequence.from + sequenceFrameOffset,
+ );
+ const maxFrame = Number.isFinite(sequence.duration)
+ ? Math.max(0, sequence.duration - 1)
+ : rawFreezeFrame;
+ const freezeFrame = Math.min(Math.max(0, rawFreezeFrame), maxFrame);
+ const remove = isFrozen;
+
+ saveSequenceProps({
+ changes: [
+ {
+ fileName: validatedLocation.source,
+ nodePath,
+ fieldKey: 'freeze',
+ value: remove ? undefined : freezeFrame,
+ defaultValue: null,
+ schema: sequence.controls.schema,
+ remove,
+ },
+ ],
+ setPropStatuses,
+ clientId: previewServerState.clientId,
+ undoLabel: remove ? 'Unfreeze sequence' : 'Freeze sequence',
+ redoLabel: remove ? 'Freeze sequence again' : 'Unfreeze sequence again',
+ });
+ }, [
+ canToggleFreeze,
+ isFrozen,
+ nodePath,
+ previewServerState,
+ sequence.controls,
+ sequence.duration,
+ sequence.from,
+ sequenceFrameOffset,
+ setPropStatuses,
+ timelinePosition,
+ validatedLocation,
+ ]);
+
const contextMenuValues = useMemo((): ComboboxValue[] => {
if (!previewConnected) {
return [];
@@ -629,6 +702,20 @@ export const TimelineSequenceItem: React.FC<{
value: 'show-asset',
}
: null,
+ {
+ type: 'item' as const,
+ id: 'toggle-freeze-frame',
+ keyHint: null,
+ label: isFrozen ? 'Unfreeze frame' : 'Freeze frame',
+ leftItem: null,
+ disabled: !canToggleFreeze,
+ onClick: () => {
+ onToggleFreezeFrame();
+ },
+ quickSwitcherLabel: null,
+ subMenu: null,
+ value: 'toggle-freeze-frame',
+ },
documentationLink
? {
type: 'divider' as const,
@@ -688,13 +775,16 @@ export const TimelineSequenceItem: React.FC<{
].filter(NoReactInternals.truthy);
}, [
assetLinkInfo,
+ canToggleFreeze,
deleteDisabled,
disableInteractivityDisabled,
duplicateDisabled,
fileLocation,
+ isFrozen,
onDeleteSequenceFromSource,
onDisableSequenceInteractivity,
onDuplicateSequenceFromSource,
+ onToggleFreezeFrame,
canOpenInEditor,
openInEditor,
previewConnected,
@@ -730,12 +820,6 @@ export const TimelineSequenceItem: React.FC<{
[canOpenInEditor, openInEditor],
);
- const propStatusesForOverride = useMemo(() => {
- return nodePath
- ? Internals.getPropStatusesCtx(propStatuses, nodePath)
- : undefined;
- }, [propStatuses, nodePath]);
-
const codeHiddenStatus = propStatusesForOverride?.hidden;
const isItemHidden = useMemo(() => {
diff --git a/packages/studio/src/components/Timeline/save-sequence-prop.ts b/packages/studio/src/components/Timeline/save-sequence-prop.ts
index 19da94d55b..5bc40d2fb8 100644
--- a/packages/studio/src/components/Timeline/save-sequence-prop.ts
+++ b/packages/studio/src/components/Timeline/save-sequence-prop.ts
@@ -21,6 +21,7 @@ export type SaveSequencePropChange = {
value: unknown;
defaultValue: string | null;
schema: SequenceSchema;
+ remove?: boolean;
};
type SaveSequencePropsOptions = {
@@ -65,9 +66,11 @@ export const saveSequenceProps = ({
fileName: change.fileName,
nodePath: change.nodePath,
key: change.fieldKey,
- value: JSON.stringify(change.value),
defaultValue: change.defaultValue,
schema: change.schema,
+ ...(change.remove
+ ? {remove: true}
+ : {value: JSON.stringify(change.value)}),
},
],
clientId,
@@ -95,9 +98,11 @@ export const saveSequenceProps = ({
fileName: change.fileName,
nodePath: change.nodePath,
key: change.fieldKey,
- value: JSON.stringify(change.value),
defaultValue: change.defaultValue,
schema: change.schema,
+ ...(change.remove
+ ? {remove: true}
+ : {value: JSON.stringify(change.value)}),
};
}),
clientId,