Skip to content

🎥 Make videos programmatically with React

Add shortcut for matching composition duration to timeline

JonnyBurger131fa33742ee
diff --git a/packages/studio/src/components/InspectorPanel/CompositionMetadata.tsx b/packages/studio/src/components/InspectorPanel/CompositionMetadata.tsx
index 5331d1e973..d4510a9c48 100644
--- a/packages/studio/src/components/InspectorPanel/CompositionMetadata.tsx
+++ b/packages/studio/src/components/InspectorPanel/CompositionMetadata.tsx
@@ -25,6 +25,7 @@ import {updateCompositionMetadata} from '../RenderQueue/actions';
import {TimelineTickFormatContext} from '../Timeline/TimelineTickFormatProvider';
import {useResolvedStack} from '../Timeline/use-resolved-stack';
import {InspectorDetailRow} from './common';
+import {getTimelineDurationFromDom} from './get-timeline-duration-from-dom';
import {
acceptPendingCompositionMetadataValue,
type CompositionMetadataField,
@@ -170,7 +171,7 @@ const frameRatePresets = [23.976, 24, 25, 29.97, 30, 50, 60] as const;
const PresetDropdown: React.FC<{
readonly disabled: boolean;
readonly 'aria-label': string;
- readonly values: ComboboxValue[];
+ readonly values: ComboboxValue[] | (() => ComboboxValue[]);
readonly visible: boolean;
}> = ({disabled, 'aria-label': ariaLabel, values, visible}) => {
const renderAction = useCallback((color: string) => {
@@ -192,7 +193,8 @@ const PresetDropdown: React.FC<{
disabled={disabled}
renderAction={renderAction}
aria-label={ariaLabel}
- values={values}
+ values={typeof values === 'function' ? undefined : values}
+ getItems={typeof values === 'function' ? values : undefined}
variant="compact"
/>
</div>
@@ -332,6 +334,7 @@ export const CompositionMetadata: React.FC<{
readonly stack: string | null;
}> = ({compositionId, disabled, stack}) => {
const video = Internals.useVideo();
+ const {showFrames} = useContext(TimelineTickFormatContext);
const resolvedVideoConfig = Internals.useResolvedVideoConfig(compositionId);
const resolvedConfig =
resolvedVideoConfig?.type === 'success' ||
@@ -614,7 +617,47 @@ export const CompositionMetadata: React.FC<{
/>
</div>
</InspectorDetailRow>
- <InspectorDetailRow label="Duration">
+ <InspectorDetailRow
+ label={(hovered) => (
+ <div style={metadataLabelControls}>
+ <span style={metadataLabelText}>Duration</span>
+ {disabled || durationIsComputed ? null : (
+ <PresetDropdown
+ disabled={pendingValues.durationInFrames !== undefined}
+ aria-label="Choose duration preset"
+ visible={hovered}
+ values={() => {
+ const timelineEnd = getTimelineDurationFromDom({
+ durationInFrames: video.durationInFrames,
+ });
+
+ return [
+ {
+ type: 'item',
+ id: 'match-timeline',
+ label:
+ timelineEnd === null
+ ? 'Match timeline'
+ : `Match timeline (${showFrames ? `${timelineEnd} ${timelineEnd === 1 ? 'frame' : 'frames'}` : renderFrame(timelineEnd, video.fps)})`,
+ value: 'match-timeline',
+ disabled: timelineEnd === null,
+ onClick: () => {
+ if (timelineEnd !== null) {
+ saveMetadata({durationInFrames: timelineEnd});
+ }
+ },
+ keyHint: null,
+ leftItem: null,
+ subMenu: null,
+ quickSwitcherLabel: null,
+ },
+ ];
+ }}
+ />
+ )}
+ </div>
+ )}
+ >
<CompositionMetadataValue
computed={durationIsComputed}
disabled={disabled}
diff --git a/packages/studio/src/components/InspectorPanel/get-timeline-duration-from-dom.ts b/packages/studio/src/components/InspectorPanel/get-timeline-duration-from-dom.ts
new file mode 100644
index 0000000000..88941431fb
--- /dev/null
+++ b/packages/studio/src/components/InspectorPanel/get-timeline-duration-from-dom.ts
@@ -0,0 +1,64 @@
+import {TIMELINE_PADDING} from '../../helpers/timeline-layout';
+import {scrollableRef} from '../Timeline/timeline-refs';
+import {getTimelineRenderWindow} from '../Timeline/TimelineViewport';
+
+// Called only when opening the duration menu. Read the geometry of rendered
+// layer bodies, excluding premount/postmount stripes and available-content outlines.
+export const getTimelineDurationFromDom = ({
+ durationInFrames,
+}: {
+ readonly durationInFrames: number;
+}): number | null => {
+ const scrollable = scrollableRef.current;
+ const tracks = scrollable?.querySelector<HTMLElement>(
+ '[data-remotion-timeline-tracks]',
+ );
+ if (
+ !scrollable ||
+ !tracks ||
+ tracks.dataset.remotionTimelineComplete !== 'true'
+ ) {
+ return null;
+ }
+
+ // Virtualized rows or a cropped horizontal render window may hide a later end.
+ const renderWindow = getTimelineRenderWindow(scrollable);
+ const tracksWidth = parseFloat(getComputedStyle(tracks).width);
+ const width = tracksWidth - TIMELINE_PADDING * 2;
+ if (
+ width <= 0 ||
+ renderWindow.left !== 0 ||
+ renderWindow.width < tracksWidth
+ ) {
+ return null;
+ }
+
+ let end = 0;
+ for (const layer of tracks.querySelectorAll<HTMLElement>(
+ '[data-remotion-annotation-surface="layer"]',
+ )) {
+ const bodyWidth = Number(layer.dataset.remotionTimelineBodyWidth);
+ if (!(bodyWidth > 0)) {
+ continue;
+ }
+
+ end = Math.max(
+ end,
+ parseFloat(layer.style.marginLeft) +
+ Number(layer.dataset.remotionTimelineBodyLeft) +
+ bodyWidth,
+ );
+ }
+
+ const frames = (end / width) * durationInFrames;
+ // Ignore CSS serialization rounding without dropping a fractional last frame.
+ const rounded = Math.round(frames);
+ const duration =
+ Math.abs(frames - rounded) < 0.001 ? rounded : Math.ceil(frames);
+ // A bar at the composition boundary may be clipped or have unlimited duration.
+ return Number.isFinite(duration) &&
+ duration > 0 &&
+ duration < durationInFrames
+ ? duration
+ : null;
+};
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index 19cfb0b873..306c3d47aa 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -301,6 +301,7 @@ const TimelineSequenceCurrentFrame: React.FC<{
readonly annotationLocation: ResolvedStackLocation | null;
readonly activeTrimEdge: 'left' | 'right' | null;
readonly displayDurationInFrames: number;
+ readonly media: {readonly left: number; readonly width: number} | null;
readonly premount: {readonly left: number; readonly width: number} | null;
readonly postmount: {readonly left: number; readonly width: number} | null;
readonly negativeStart: {
@@ -330,6 +331,7 @@ const TimelineSequenceCurrentFrame: React.FC<{
activeTrimEdge,
annotationLocation,
displayDurationInFrames,
+ media,
premount,
postmount,
negativeStart,
@@ -511,6 +513,8 @@ const TimelineSequenceCurrentFrame: React.FC<{
<div
ref={ref}
role="group"
+ data-remotion-timeline-body-left={media?.left}
+ data-remotion-timeline-body-width={media?.width}
{...getSequenceAnnotationAttributes({
sequence: s,
location: annotationLocation,
@@ -1591,6 +1595,7 @@ const TimelineSequenceInner: React.FC<{
const sequence = (
<TimelineSequenceCurrentFrame
s={s}
+ media={visibleLayout.media}
annotationLocation={originalLocation}
activeTrimEdge={activeTrimEdge}
displayDurationInFrames={displayDurationInFrames}
diff --git a/packages/studio/src/components/Timeline/TimelineTracks.tsx b/packages/studio/src/components/Timeline/TimelineTracks.tsx
index d1346d43c1..235f6c4d74 100644
--- a/packages/studio/src/components/Timeline/TimelineTracks.tsx
+++ b/packages/studio/src/components/Timeline/TimelineTracks.tsx
@@ -26,7 +26,14 @@ const TimelineTracksInner: React.FC<{
}, []);
return (
- <div style={timelineStyle} {...{'oai-annotation-container': ''}}>
+ <div
+ style={timelineStyle}
+ data-remotion-timeline-tracks
+ data-remotion-timeline-complete={
+ !hasBeenCut && virtualItems.length === rows.length
+ }
+ {...{'oai-annotation-container': ''}}
+ >
<div style={{...content, height: tracksEnd}}>
{virtualItems.map((virtualItem) => (
<div