Skip to content

🎥 Make videos programmatically with React

@remotion/studio: Show keyframe markers in timeline

JonnyBurgera54dafd13ea9
diff --git a/packages/studio/src/components/Timeline/TimelineExpandedTrackKeyframes.tsx b/packages/studio/src/components/Timeline/TimelineExpandedTrackKeyframes.tsx
new file mode 100644
index 0000000000..c91d2b19a4
--- /dev/null
+++ b/packages/studio/src/components/Timeline/TimelineExpandedTrackKeyframes.tsx
@@ -0,0 +1,168 @@
+import React, {useContext, useMemo} from 'react';
+import {Internals, type TSequence, useVideoConfig} from 'remotion';
+import {TIMELINE_TRACK_SEPARATOR, WARNING_COLOR} from '../../helpers/colors';
+import {getXPositionOfItemInTimelineImperatively} from '../../helpers/get-left-of-timeline-slider';
+import type {SequenceNodePathInfo} from '../../helpers/get-timeline-sequence-sort-key';
+import {
+ buildTimelineTree,
+ flattenVisibleTreeNodes,
+ getExpandedTrackHeight,
+ getTreeRowHeight,
+ TIMELINE_ITEM_BORDER_BOTTOM,
+} from '../../helpers/timeline-layout';
+import {ExpandedTracksGetterContext} from '../ExpandedTracksProvider';
+import {getTimelineKeyframes} from './TimelineSchemaField';
+import {TimelineWidthContext} from './TimelineWidthProvider';
+
+const row: React.CSSProperties = {
+ position: 'relative',
+};
+
+const separator: React.CSSProperties = {
+ height: 1,
+ backgroundColor: TIMELINE_TRACK_SEPARATOR,
+};
+
+const section: React.CSSProperties = {
+ borderBottom: `1px solid ${TIMELINE_TRACK_SEPARATOR}`,
+};
+
+const diamondBase: React.CSSProperties = {
+ position: 'absolute',
+ width: 8,
+ height: 8,
+ backgroundColor: WARNING_COLOR,
+ borderRadius: 1,
+ boxShadow: '0 0 0 1px rgba(0, 0, 0, 0.4)',
+ pointerEvents: 'none',
+};
+
+const getNodeKeyframes = ({
+ node,
+ nodePath,
+ codeValues,
+}: {
+ node: ReturnType<typeof flattenVisibleTreeNodes>[number]['node'];
+ nodePath: SequenceNodePathInfo['sequenceSubscriptionKey'];
+ codeValues: React.ContextType<
+ typeof Internals.VisualModeCodeValuesContext
+ >['codeValues'];
+}): ReturnType<typeof getTimelineKeyframes> => {
+ if (node.kind !== 'field' || node.field === null) {
+ return [];
+ }
+
+ if (node.field.kind === 'sequence-field') {
+ return getTimelineKeyframes(
+ Internals.getCodeValuesCtx(codeValues, nodePath)?.[node.field.key],
+ );
+ }
+
+ const effectStatus = Internals.getEffectCodeValuesCtx({
+ codeValues,
+ nodePath,
+ effectIndex: node.field.effectIndex,
+ });
+
+ return getTimelineKeyframes(
+ effectStatus.type === 'can-update-effect'
+ ? effectStatus.props?.[node.field.key]
+ : null,
+ );
+};
+
+const TimelineExpandedTrackKeyframesInner: React.FC<{
+ readonly sequence: TSequence;
+ readonly nodePathInfo: SequenceNodePathInfo;
+}> = ({nodePathInfo, sequence}) => {
+ const videoConfig = useVideoConfig();
+ const timelineWidth = useContext(TimelineWidthContext);
+ const {getIsExpanded} = useContext(ExpandedTracksGetterContext);
+ const {codeValues} = useContext(Internals.VisualModeCodeValuesContext);
+ const {getDragOverrides} = useContext(
+ Internals.VisualModeDragOverridesContext,
+ );
+
+ const tree = useMemo(
+ () =>
+ buildTimelineTree({
+ sequence,
+ nodePathInfo,
+ getDragOverrides,
+ codeValues,
+ }),
+ [codeValues, getDragOverrides, nodePathInfo, sequence],
+ );
+
+ const flat = useMemo(
+ () => flattenVisibleTreeNodes({nodes: tree, getIsExpanded}),
+ [tree, getIsExpanded],
+ );
+
+ const expandedHeight = useMemo(
+ () =>
+ getExpandedTrackHeight({
+ sequence,
+ nodePathInfo,
+ getIsExpanded,
+ codeValues,
+ }),
+ [codeValues, getIsExpanded, nodePathInfo, sequence],
+ );
+
+ const rows = useMemo(
+ () =>
+ flat.map(({node}) => ({
+ height: getTreeRowHeight(node),
+ keyframes: getNodeKeyframes({
+ node,
+ nodePath: nodePathInfo.sequenceSubscriptionKey,
+ codeValues,
+ }),
+ key: JSON.stringify(node.nodePathInfo),
+ })),
+ [codeValues, flat, nodePathInfo.sequenceSubscriptionKey],
+ );
+
+ return (
+ <div
+ style={{
+ height: expandedHeight + TIMELINE_ITEM_BORDER_BOTTOM,
+ }}
+ >
+ <div style={{...section, height: expandedHeight}}>
+ {rows.map(({height, keyframes, key}, i) => {
+ return (
+ <React.Fragment key={key}>
+ {i > 0 ? <div style={separator} /> : null}
+ <div style={{...row, height}}>
+ {timelineWidth === null
+ ? null
+ : keyframes.map((keyframe) => (
+ <div
+ key={`${keyframe.frame}-${JSON.stringify(keyframe.value)}`}
+ style={{
+ ...diamondBase,
+ left: getXPositionOfItemInTimelineImperatively(
+ keyframe.frame,
+ videoConfig.durationInFrames,
+ timelineWidth,
+ ),
+ top: height / 2,
+ transform: 'translate(-50%, -50%) rotate(45deg)',
+ }}
+ title={`Keyframe at frame ${keyframe.frame}`}
+ />
+ ))}
+ </div>
+ </React.Fragment>
+ );
+ })}
+ </div>
+ </div>
+ );
+};
+
+export const TimelineExpandedTrackKeyframes = React.memo(
+ TimelineExpandedTrackKeyframesInner,
+);
diff --git a/packages/studio/src/components/Timeline/TimelineSchemaField.tsx b/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
index 794df49ac7..bdaffafa5e 100644
--- a/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
@@ -1,5 +1,6 @@
import React from 'react';
import type {
+ CanUpdateSequencePropStatus,
CanUpdateSequencePropStatusFalse,
CanUpdateSequencePropStatusTrue,
} from 'remotion';
@@ -49,6 +50,22 @@ export const getComputedStatusLabel = (
return propStatus.keyframes?.length ? 'keyframed' : 'computed';
};
+export const getTimelineKeyframes = (
+ propStatus: CanUpdateSequencePropStatus | null | undefined,
+): {frame: number; value: unknown}[] => {
+ if (!propStatus || propStatus.canUpdate) {
+ return [];
+ }
+
+ if (propStatus.reason !== 'computed') {
+ throw new Error(
+ `Unsupported prop status: ${propStatus.reason satisfies never}`,
+ );
+ }
+
+ return propStatus.keyframes ?? [];
+};
+
export const TimelineNonEditableStatus: React.FC<{
readonly propStatus: CanUpdateSequencePropStatusFalse;
}> = ({propStatus}) => {
diff --git a/packages/studio/src/components/Timeline/TimelineTracks.tsx b/packages/studio/src/components/Timeline/TimelineTracks.tsx
index cecf4648cd..a0f4633a3d 100644
--- a/packages/studio/src/components/Timeline/TimelineTracks.tsx
+++ b/packages/studio/src/components/Timeline/TimelineTracks.tsx
@@ -1,22 +1,14 @@
import React, {useContext, useMemo} from 'react';
-import type {CodeValues} from 'remotion';
-import {Internals, type TSequence} from 'remotion';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
-import type {
- SequenceNodePathInfo,
- TrackWithHash,
-} from '../../helpers/get-timeline-sequence-sort-key';
+import type {TrackWithHash} from '../../helpers/get-timeline-sequence-sort-key';
import {
- getExpandedTrackHeight,
getTimelineLayerHeight,
TIMELINE_ITEM_BORDER_BOTTOM,
TIMELINE_PADDING,
} from '../../helpers/timeline-layout';
-import {
- ExpandedTracksGetterContext,
- type GetIsExpanded,
-} from '../ExpandedTracksProvider';
+import {ExpandedTracksGetterContext} from '../ExpandedTracksProvider';
import {MaxTimelineTracksReached} from './MaxTimelineTracks';
+import {TimelineExpandedTrackKeyframes} from './TimelineExpandedTrackKeyframes';
import {TimelineSequence} from './TimelineSequence';
import {TimelineTimePadding} from './TimelineTimeIndicators';
@@ -30,34 +22,12 @@ const timelineContent: React.CSSProperties = {
minHeight: '100%',
};
-const getExpandedPlaceholderStyle = ({
- sequence,
- nodePathInfo,
- getIsExpanded,
- codeValues,
-}: {
- sequence: TSequence;
- nodePathInfo: SequenceNodePathInfo;
- getIsExpanded: GetIsExpanded;
- codeValues: CodeValues;
-}): React.CSSProperties => ({
- height:
- getExpandedTrackHeight({
- sequence,
- nodePathInfo,
- getIsExpanded,
- codeValues,
- }) + TIMELINE_ITEM_BORDER_BOTTOM,
-});
-
const TimelineTracksInner: React.FC<{
readonly timeline: TrackWithHash[];
readonly hasBeenCut: boolean;
}> = ({timeline, hasBeenCut}) => {
const {getIsExpanded} = useContext(ExpandedTracksGetterContext);
const {previewServerState} = useContext(StudioServerConnectionCtx);
- const {codeValues} = useContext(Internals.VisualModeCodeValuesContext);
-
const previewServerConnected = previewServerState.type === 'connected';
const timelineStyle: React.CSSProperties = useMemo(() => {
@@ -86,13 +56,9 @@ const TimelineTracksInner: React.FC<{
<TimelineSequence s={track.sequence} />
</div>
{isExpanded && track.nodePathInfo && previewServerConnected ? (
- <div
- style={getExpandedPlaceholderStyle({
- sequence: track.sequence,
- nodePathInfo: track.nodePathInfo,
- getIsExpanded,
- codeValues,
- })}
+ <TimelineExpandedTrackKeyframes
+ sequence={track.sequence}
+ nodePathInfo={track.nodePathInfo}
/>
) : null}
</div>