Skip to content

🎥 Make videos programmatically with React

Add `trimAfter` and `loop` to `<Sequence>` and all timed components

JonnyBurger98f629f6b464
diff --git a/packages/brand/src/3DContext/Div3D.tsx b/packages/brand/src/3DContext/Div3D.tsx
index 8f3410f211..30de90dbd0 100644
--- a/packages/brand/src/3DContext/Div3D.tsx
+++ b/packages/brand/src/3DContext/Div3D.tsx
@@ -413,6 +413,8 @@ const ExtrudeDivInner = React.forwardRef<
durationInFrames,
from,
trimBefore,
+ trimAfter,
+ loop,
freeze,
hidden,
name,
@@ -486,6 +488,8 @@ const ExtrudeDivInner = React.forwardRef<
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/brand/src/3DContext/transformation-context.tsx b/packages/brand/src/3DContext/transformation-context.tsx
index 9e664fd1b8..6f1172a479 100644
--- a/packages/brand/src/3DContext/transformation-context.tsx
+++ b/packages/brand/src/3DContext/transformation-context.tsx
@@ -135,6 +135,8 @@ const make3DTransform = <ValueKey extends string>({
durationInFrames,
from,
trimBefore,
+ trimAfter,
+ loop,
freeze,
hidden,
name,
@@ -149,6 +151,8 @@ const make3DTransform = <ValueKey extends string>({
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/brand/src/Compose/WhatIsRemotion.tsx b/packages/brand/src/Compose/WhatIsRemotion.tsx
index e685bdd691..780b776819 100644
--- a/packages/brand/src/Compose/WhatIsRemotion.tsx
+++ b/packages/brand/src/Compose/WhatIsRemotion.tsx
@@ -74,6 +74,8 @@ const LabelInner = React.forwardRef<
durationInFrames,
from,
trimBefore,
+ trimAfter,
+ loop,
freeze,
hidden,
name,
@@ -98,6 +100,8 @@ const LabelInner = React.forwardRef<
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/brand/src/video-elements/Studio.tsx b/packages/brand/src/video-elements/Studio.tsx
index a0ffd04172..c1ba1a5a19 100644
--- a/packages/brand/src/video-elements/Studio.tsx
+++ b/packages/brand/src/video-elements/Studio.tsx
@@ -1656,7 +1656,9 @@ const StudioInner = React.forwardRef<
showInTimeline,
showTimelineZoom,
timelineOffset,
+ trimAfter,
trimBefore,
+ loop,
viewportHeight,
viewportWidth,
},
@@ -1714,7 +1716,9 @@ const StudioInner = React.forwardRef<
name={name ?? '<Studio>'}
outlineRef={outlineRef}
showInTimeline={showInTimeline ?? true}
+ trimAfter={trimAfter}
trimBefore={trimBefore}
+ loop={loop}
>
<div
ref={outlineRef}
diff --git a/packages/core/src/AbsoluteFill.tsx b/packages/core/src/AbsoluteFill.tsx
index 9ebc95a417..b74fa6c80c 100644
--- a/packages/core/src/AbsoluteFill.tsx
+++ b/packages/core/src/AbsoluteFill.tsx
@@ -21,6 +21,7 @@ import {
transformSchema,
type InteractivitySchema,
} from './interactivity-schema.js';
+import {resolveSequenceDuration} from './resolve-sequence-duration.js';
import {Sequence} from './Sequence.js';
import {usePremounting} from './use-premounting.js';
import {useUnsafeVideoConfig} from './use-unsafe-video-config.js';
@@ -74,7 +75,9 @@ const AbsoluteFillWithTiming: React.FC<
styleWhilePremounted,
styleWhilePostmounted,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
durationInFrames,
hidden,
@@ -95,7 +98,13 @@ const AbsoluteFillWithTiming: React.FC<
premountingStyle,
} = usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: divProps.style ?? null,
@@ -109,7 +118,9 @@ const AbsoluteFillWithTiming: React.FC<
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
freeze={freeze}
durationInFrames={durationInFrames ?? Infinity}
hidden={hidden}
@@ -146,7 +157,9 @@ const AbsoluteFillInner: React.FC<
styleWhilePremounted,
styleWhilePostmounted,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
durationInFrames,
hidden,
@@ -186,7 +199,9 @@ const AbsoluteFillInner: React.FC<
styleWhilePremounted={styleWhilePremounted}
styleWhilePostmounted={styleWhilePostmounted}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
freeze={freeze}
durationInFrames={durationInFrames}
hidden={hidden}
diff --git a/packages/core/src/HtmlInCanvas.tsx b/packages/core/src/HtmlInCanvas.tsx
index 6b1546735c..098af95c24 100644
--- a/packages/core/src/HtmlInCanvas.tsx
+++ b/packages/core/src/HtmlInCanvas.tsx
@@ -31,6 +31,7 @@ import {
transformSchema,
type InteractivitySchema,
} from './interactivity-schema.js';
+import {resolveSequenceDuration} from './resolve-sequence-duration.js';
import {Sequence} from './Sequence.js';
import type {AbsoluteFillLayout} from './Sequence.js';
import {useCropStyle} from './use-crop-style.js';
@@ -781,7 +782,13 @@ const HtmlInCanvasInner = forwardRef<
premountingStyle,
} = usePremounting({
from: sequenceProps.from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: resolveSequenceDuration({
+ durationInFrames,
+ trimBefore: sequenceProps.trimBefore,
+ trimAfter: sequenceProps.trimAfter,
+ playbackRate: sequenceProps.playbackRate,
+ loop: sequenceProps.loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
diff --git a/packages/core/src/Img.tsx b/packages/core/src/Img.tsx
index b8382363ec..e862236763 100644
--- a/packages/core/src/Img.tsx
+++ b/packages/core/src/Img.tsx
@@ -34,6 +34,7 @@ import {
type InteractivitySchema,
} from './interactivity-schema.js';
import {usePreload} from './prefetch.js';
+import {resolveSequenceDuration} from './resolve-sequence-duration.js';
import {Sequence} from './Sequence.js';
import {SequenceContext} from './SequenceContext.js';
import {truncateSrcForLabel} from './truncate-src-for-label.js';
@@ -68,7 +69,7 @@ export type ImgProps = NativeImgProps & {
readonly effects?: EffectsProp;
readonly showInTimeline?: boolean;
readonly name?: string;
-} & Omit<InteractiveBaseProps, 'playbackRate'> &
+} & Omit<InteractiveBaseProps, 'playbackRate' | 'loop'> &
InteractiveCropProps &
InteractivePremountProps;
@@ -84,6 +85,7 @@ type ImgContentProps = Omit<
| 'showInTimeline'
| 'from'
| 'trimBefore'
+ | 'trimAfter'
| 'durationInFrames'
| 'freeze'
| 'effects'
@@ -366,6 +368,7 @@ const NativeImgInner: React.FC<NativeImgInnerProps> = ({
src,
from,
trimBefore,
+ trimAfter,
durationInFrames,
freeze,
premountFor,
@@ -403,7 +406,13 @@ const NativeImgInner: React.FC<NativeImgInnerProps> = ({
premountingStyle,
} = usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate: undefined,
+ loop: undefined,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
@@ -426,6 +435,7 @@ const NativeImgInner: React.FC<NativeImgInnerProps> = ({
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
_remotionInternalDocumentationLink="https://www.remotion.dev/docs/img"
@@ -562,6 +572,7 @@ const ImgInner: React.FC<
src,
from,
trimBefore,
+ trimAfter,
durationInFrames,
freeze,
premountFor,
@@ -599,6 +610,7 @@ const ImgInner: React.FC<
src={src}
from={from}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
durationInFrames={durationInFrames}
freeze={freeze}
premountFor={premountFor}
@@ -662,6 +674,7 @@ const ImgInner: React.FC<
delayRenderTimeoutInMilliseconds={delayRenderTimeoutInMilliseconds}
from={from}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
durationInFrames={durationInFrames}
freeze={freeze}
premountFor={premountFor}
diff --git a/packages/core/src/Interactive.tsx b/packages/core/src/Interactive.tsx
index 3095ad8b86..42801372a6 100644
--- a/packages/core/src/Interactive.tsx
+++ b/packages/core/src/Interactive.tsx
@@ -8,6 +8,7 @@ import {Freeze} from './freeze.js';
import {
backgroundSchema,
baseSchema,
+ baseSchemaWithoutLoop,
borderRadiusSchema,
captionsSchema,
borderSchema,
@@ -21,6 +22,7 @@ import {
transformSchema,
type InteractivitySchema,
} from './interactivity-schema.js';
+import {resolveSequenceDuration} from './resolve-sequence-duration.js';
import {Sequence} from './Sequence.js';
import type {AbsoluteFillLayout, SequenceProps} from './Sequence.js';
import {useCropStyle} from './use-crop-style.js';
@@ -83,7 +85,9 @@ export type InteractiveBaseProps = Pick<
| 'durationInFrames'
| 'from'
| 'trimBefore'
+ | 'trimAfter'
| 'playbackRate'
+ | 'loop'
| 'freeze'
| 'hidden'
| 'name'
@@ -247,7 +251,9 @@ const makeInteractiveElement = <Tag extends InteractiveTag>(
styleWhilePremounted,
styleWhilePostmounted,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
hidden,
name,
@@ -273,7 +279,13 @@ const makeInteractiveElement = <Tag extends InteractiveTag>(
premountingStyle,
} = usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
@@ -304,7 +316,9 @@ const makeInteractiveElement = <Tag extends InteractiveTag>(
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
@@ -376,6 +390,7 @@ const makeInteractiveSvgStrokeElement = <Tag extends InteractiveSvgTag>(
*/
export const Interactive = {
baseSchema,
+ baseSchemaWithoutLoop,
captionsSchema,
transformSchema,
textSchema,
diff --git a/packages/core/src/Sequence.tsx b/packages/core/src/Sequence.tsx
index a57e18ba96..c48745dc87 100644
--- a/packages/core/src/Sequence.tsx
+++ b/packages/core/src/Sequence.tsx
@@ -22,6 +22,8 @@ import {
sequenceSchema,
sequenceSchemaWithoutFrom,
} from './interactivity-schema.js';
+import {LoopContext, type LoopContextType} from './loop/loop-context.js';
+import {resolveSequenceDuration} from './resolve-sequence-duration.js';
import type {RuntimeValueStore} from './runtime-value-store.js';
import {
getSequenceCropClipPath,
@@ -78,7 +80,9 @@ export type SequencePropsWithoutDuration = {
readonly cropBottom?: number;
readonly from?: number;
readonly trimBefore?: number;
+ readonly trimAfter?: number;
readonly playbackRate?: number;
+ readonly loop?: boolean;
readonly freeze?: number | null;
readonly name?: string;
readonly showInTimeline?: boolean;
@@ -147,7 +151,9 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
{
from = 0,
trimBefore = 0,
+ trimAfter,
playbackRate = 1,
+ loop = false,
freeze,
durationInFrames = Infinity,
children,
@@ -264,6 +270,41 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
);
}
+ if (typeof trimAfter !== 'undefined') {
+ if (typeof trimAfter !== 'number') {
+ throw new TypeError(
+ `The "trimAfter" prop of <Sequence /> must be a number, but is of type ${typeof trimAfter}.`,
+ );
+ }
+
+ if (Number.isNaN(trimAfter)) {
+ throw new TypeError(
+ 'The "trimAfter" prop of <Sequence /> must be a real number, but it is NaN.',
+ );
+ }
+
+ if (trimAfter <= trimBefore) {
+ throw new TypeError(
+ `The "trimAfter" prop of <Sequence /> must be greater than "trimBefore" (${trimBefore}), but got ${trimAfter}.`,
+ );
+ }
+ }
+
+ if (typeof loop !== 'boolean') {
+ throw new TypeError(
+ `The "loop" prop of <Sequence /> must be a boolean, but is of type ${typeof loop}.`,
+ );
+ }
+
+ if (
+ loop &&
+ (typeof trimAfter === 'undefined' || !Number.isFinite(trimAfter))
+ ) {
+ throw new Error(
+ 'The "loop" prop of <Sequence /> requires a finite "trimAfter" prop, because a <Sequence /> has no intrinsic duration. Set "trimAfter" to the frame at which the children should start over, or use <Loop /> to repeat a duration measured in the parent timeline.',
+ );
+ }
+
if (typeof freeze !== 'undefined' && freeze !== null) {
if (typeof freeze !== 'number') {
throw new TypeError(
@@ -307,20 +348,66 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
lastPlaybackRate.current = {playbackRate, frame: absoluteFrame};
const videoConfig = useVideoConfig();
- const effectiveRelativeFrom = from - trimBefore / playbackRate;
- const relativeFrom = effectiveRelativeFrom / parentPlaybackRate;
- const absoluteFrom = (parentSequence?.absoluteFrom ?? 0) + relativeFrom;
+ const effectiveDurationInFrames = resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ });
+ const windowRelativeFrom = from - trimBefore / playbackRate;
const parentSequenceDuration = parentSequence
? Math.min(
- parentSequence.durationInFrames - effectiveRelativeFrom,
- durationInFrames,
+ parentSequence.durationInFrames - windowRelativeFrom,
+ effectiveDurationInFrames,
)
- : durationInFrames;
+ : effectiveDurationInFrames;
const actualDurationInFrames = Math.max(
0,
Math.min(videoConfig.durationInFrames - from, parentSequenceDuration),
);
+
+ // A looped sequence repeats the child range [trimBefore, trimAfter). Each
+ // iteration lasts `loopPeriod` parent frames and moves the clock origin,
+ // while the visible window stays at [from, from + effectiveDurationInFrames).
+ const frameInParent = (absoluteFrame - cumulatedFrom) * parentPlaybackRate;
+ const loopPeriod =
+ loop && typeof trimAfter === 'number'
+ ? (trimAfter - trimBefore) / playbackRate
+ : null;
+ let loopIteration = 0;
+ if (loopPeriod !== null) {
+ const loopsElapsed = (frameInParent - from) / loopPeriod;
+ const nearestIteration = Math.round(loopsElapsed);
+ // Fractional periods and nested playback rates can put an exact loop
+ // boundary a few floating-point units before the next iteration.
+ const isAtBoundary =
+ Math.abs(loopsElapsed - nearestIteration) <=
+ Number.EPSILON * Math.max(1, Math.abs(loopsElapsed)) * 4;
+ const lastIteration = Math.ceil(actualDurationInFrames / loopPeriod) - 1;
+ loopIteration = Math.max(
+ 0,
+ Math.min(
+ lastIteration,
+ isAtBoundary ? nearestIteration : Math.floor(loopsElapsed),
+ ),
+ );
+ }
+
+ const loopOffset = loopPeriod === null ? 0 : loopIteration * loopPeriod;
+ const clockFrom = from + loopOffset;
+ const effectiveRelativeFrom = clockFrom - trimBefore / playbackRate;
+ const relativeFrom = effectiveRelativeFrom / parentPlaybackRate;
+ const absoluteFrom = (parentSequence?.absoluteFrom ?? 0) + relativeFrom;
+ // Inside a loop, the local clock ends at `trimAfter`, except in a final
+ // iteration that `durationInFrames` or the parent cuts short.
+ const localClockEnd =
+ loopPeriod === null
+ ? actualDurationInFrames * playbackRate + trimBefore
+ : Math.min(loopPeriod, actualDurationInFrames - loopOffset) *
+ playbackRate +
+ trimBefore;
const sequenceRegistrationEnabled = useContext(SequenceRegistrationContext);
const canvasOutlinesEnabled = useContext(SequenceOutlineContext);
const env = useRemotionEnvironment();
@@ -378,7 +465,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
const firstFrame = Math.max(
0,
parentFirstFrame,
- cumulatedFrom + from / parentPlaybackRate,
+ cumulatedFrom + clockFrom / parentPlaybackRate,
);
const cumulatedNegativeFrom =
(currentSequenceStart - firstFrame) * cumulativePlaybackRate;
@@ -390,7 +477,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
cumulatedFrom,
relativeFrom,
cumulatedNegativeFrom,
- durationInFrames: actualDurationInFrames * playbackRate + trimBefore,
+ durationInFrames: localClockEnd,
parentFrom: parentSequence?.relativeFrom ?? 0,
id,
height: height ?? parentSequence?.height ?? null,
@@ -405,9 +492,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
absoluteFrom,
relativeFrom,
cumulativePlaybackRate,
- playbackRate,
- trimBefore,
- actualDurationInFrames,
+ localClockEnd,
parentSequence,
id,
height,
@@ -419,6 +504,26 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
cumulatedNegativeFrom,
]);
+ const loopContextValue = useMemo((): LoopContextType | null => {
+ if (loopPeriod === null) {
+ return null;
+ }
+
+ return {iteration: loopIteration, durationInFrames: loopPeriod};
+ }, [loopIteration, loopPeriod]);
+
+ const resolvedLoopDisplay = useMemo((): LoopDisplay | undefined => {
+ if (loopDisplay || loopPeriod === null) {
+ return loopDisplay;
+ }
+
+ return {
+ numberOfTimes: actualDurationInFrames / loopPeriod,
+ startOffset: 0,
+ durationInFrames: loopPeriod,
+ };
+ }, [actualDurationInFrames, loopDisplay, loopPeriod]);
+
const timelineClipName = useMemo(() => {
return name ?? '';
}, [name]);
@@ -453,8 +558,8 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
? registeredFrozenFrame === null
? null
: mediaFrameAtSequenceZero +
- (loopDisplay
- ? registeredFrozenFrame % loopDisplay.durationInFrames
+ (resolvedLoopDisplay
+ ? registeredFrozenFrame % resolvedLoopDisplay.durationInFrames
: registeredFrozenFrame) *
isMedia.data.playbackRate
: null;
@@ -522,7 +627,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
from,
trimBefore: registeredTrimBefore,
id,
- loopDisplay,
+ loopDisplay: resolvedLoopDisplay,
parent: parentSequence?.id ?? null,
postmountDisplay: postmountDisplay ?? null,
premountDisplay: premountDisplay ?? null,
@@ -550,7 +655,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
from,
trimBefore: registeredTrimBefore,
id,
- loopDisplay,
+ loopDisplay: resolvedLoopDisplay,
parent: parentSequence?.id ?? null,
playbackRate: isMedia.data.playbackRate,
postmountDisplay: postmountDisplay ?? null,
@@ -583,7 +688,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
type: 'sequence',
showInTimeline,
timelineOrder: null,
- loopDisplay,
+ loopDisplay: resolvedLoopDisplay,
getStack: () => stackRef.current,
premountDisplay: premountDisplay ?? null,
postmountDisplay: postmountDisplay ?? null,
@@ -604,7 +709,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
from,
registeredTrimBefore,
showInTimeline,
- loopDisplay,
+ resolvedLoopDisplay,
premountDisplay,
postmountDisplay,
registrationControls,
@@ -629,14 +734,13 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
});
// Use an exclusive end so fractional clocks and frozen subframes remain visible.
- const frameInParent = (absoluteFrame - cumulatedFrom) * parentPlaybackRate;
- const endThreshold = from + durationInFrames;
+ const endThreshold = from + effectiveDurationInFrames;
const boundaryTolerance = getSequenceBoundaryTolerance({
absoluteFrame,
cumulatedFrom,
from,
parentPlaybackRate,
- durationInFrames,
+ durationInFrames: effectiveDurationInFrames,
});
const content =
frameInParent - from < -boundaryTolerance
@@ -650,6 +754,14 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
) : (
<Freeze frame={freeze}>{content}</Freeze>
);
+ const loopedContent =
+ frozenContent === null || loopContextValue === null ? (
+ frozenContent
+ ) : (
+ <LoopContext.Provider value={loopContextValue}>
+ {frozenContent}
+ </LoopContext.Provider>
+ );
const styleIfThere = other.layout === 'none' ? undefined : other.style;
const cropClipPath = getSequenceCropClipPath({
@@ -706,15 +818,15 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
const sequence = (
<SequenceContext.Provider value={contextValue}>
- {frozenContent === null ? null : other.layout === 'none' ? (
- frozenContent
+ {loopedContent === null ? null : other.layout === 'none' ? (
+ loopedContent
) : (
<AbsoluteFillElement
ref={sequenceRef}
style={defaultStyle}
className={other.className}
>
- {frozenContent}
+ {loopedContent}
</AbsoluteFillElement>
)}
</SequenceContext.Provider>
@@ -763,7 +875,13 @@ const PremountedPostmountedSequenceRefForwardingFunction: React.ForwardRefRender
premountingStyle,
} = usePremounting({
from,
- durationInFrames,
+ durationInFrames: resolveSequenceDuration({
+ durationInFrames,
+ trimBefore: otherProps.trimBefore,
+ trimAfter: otherProps.trimAfter,
+ playbackRate: otherProps.playbackRate,
+ loop: otherProps.loop,
+ }),
premountFor,
postmountFor,
style: passedStyle ?? null,
diff --git a/packages/core/src/animated-image/AnimatedImage.tsx b/packages/core/src/animated-image/AnimatedImage.tsx
index 4cad3d2356..bbd0777848 100644
--- a/packages/core/src/animated-image/AnimatedImage.tsx
+++ b/packages/core/src/animated-image/AnimatedImage.tsx
@@ -18,7 +18,7 @@ import {addSequenceStackTraces} from '../enable-sequence-stack-traces.js';
import {Freeze} from '../freeze.js';
import {
backgroundSchema,
- baseSchema,
+ baseSchemaWithoutLoop,
borderRadiusSchema,
borderSchema,
cropSchema,
@@ -26,6 +26,7 @@ import {
transformSchema,
type InteractivitySchema,
} from '../interactivity-schema.js';
+import {resolveSequenceDuration} from '../resolve-sequence-duration.js';
import {Sequence} from '../Sequence.js';
import {useCropStyle} from '../use-crop-style.js';
import {useCurrentFrame} from '../use-current-frame.js';
@@ -54,7 +55,8 @@ export const animatedImageSchema = {
description: 'Source',
keyframable: false,
},
- ...baseSchema,
+ // Looping is controlled by `loopBehavior`, which repeats the intrinsic duration.
+ ...baseSchemaWithoutLoop,
...cropSchema,
...premountSchema,
...transformSchema,
@@ -333,7 +335,13 @@ const AnimatedImageInner = ({
premountingStyle,
} = usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: resolveSequenceDuration({
+ durationInFrames,
+ trimBefore: sequenceProps.trimBefore,
+ trimAfter: sequenceProps.trimAfter,
+ playbackRate,
+ loop: undefined,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
diff --git a/packages/core/src/animated-image/props.ts b/packages/core/src/animated-image/props.ts
index b118d579c0..907d712cb4 100644
--- a/packages/core/src/animated-image/props.ts
+++ b/packages/core/src/animated-image/props.ts
@@ -28,7 +28,7 @@ export type RemotionAnimatedImageProps = {
requestInit?: RequestInit;
} & AnimatedImageCanvasProps;
-export type AnimatedImageProps = InteractiveBaseProps &
+export type AnimatedImageProps = Omit<InteractiveBaseProps, 'loop'> &
InteractiveCropProps &
InteractivePremountProps &
RemotionAnimatedImageProps & {
diff --git a/packages/core/src/canvas-image/CanvasImage.tsx b/packages/core/src/canvas-image/CanvasImage.tsx
index 34ec50c8e6..a043d1a0e4 100644
--- a/packages/core/src/canvas-image/CanvasImage.tsx
+++ b/packages/core/src/canvas-image/CanvasImage.tsx
@@ -31,6 +31,7 @@ import {
type InteractivitySchema,
} from '../interactivity-schema.js';
import {usePreload} from '../prefetch.js';
+import {resolveSequenceDuration} from '../resolve-sequence-duration.js';
import {Sequence} from '../Sequence.js';
import {SequenceContext} from '../SequenceContext.js';
import {truncateSrcForLabel} from '../truncate-src-for-label.js';
@@ -543,6 +544,7 @@ const CanvasImageInner = forwardRef<
durationInFrames,
from,
trimBefore,
+ trimAfter,
freeze,
premountFor,
postmountFor,
@@ -590,7 +592,13 @@ const CanvasImageInner = forwardRef<
premountingStyle,
} = usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate: undefined,
+ loop: undefined,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
@@ -613,6 +621,7 @@ const CanvasImageInner = forwardRef<
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/core/src/canvas-image/props.ts b/packages/core/src/canvas-image/props.ts
index 8fbcc7f8b4..20c04ff21a 100644
--- a/packages/core/src/canvas-image/props.ts
+++ b/packages/core/src/canvas-image/props.ts
@@ -7,7 +7,10 @@ import type {
InteractivePremountProps,
} from '../Interactive.js';
-type CanvasImageSequenceProps = Omit<InteractiveBaseProps, 'playbackRate'> &
+type CanvasImageSequenceProps = Omit<
+ InteractiveBaseProps,
+ 'playbackRate' | 'loop'
+> &
InteractiveCropProps &
InteractivePremountProps;
diff --git a/packages/core/src/effects/Solid.tsx b/packages/core/src/effects/Solid.tsx
index e0d8abda6a..16fee11107 100644
--- a/packages/core/src/effects/Solid.tsx
+++ b/packages/core/src/effects/Solid.tsx
@@ -17,7 +17,7 @@ import type {
} from '../Interactive.js';
import {
backgroundSchema,
- baseSchema,
+ baseSchemaWithoutLoop,
premountSchema,
borderRadiusSchema,
borderSchema,
@@ -25,6 +25,7 @@ import {
transformSchema,
type InteractivitySchema,
} from '../interactivity-schema.js';
+import {resolveSequenceDuration} from '../resolve-sequence-duration.js';
import {Sequence} from '../Sequence.js';
import {useCropStyle} from '../use-crop-style.js';
import {useDelayRender} from '../use-delay-render.js';
@@ -77,8 +78,9 @@ export type SolidProps = MandatoryProps &
Partial<OptionalProps> &
InteractiveCropProps;
+// A solid color does not change with the frame, so looping it would be inert.
export const solidSchema = {
- ...baseSchema,
+ ...baseSchemaWithoutLoop,
...premountSchema,
color: {
type: 'color',
@@ -259,7 +261,7 @@ const SolidOuter = forwardRef<
HTMLCanvasElement,
SolidProps & {
readonly controls: SequenceControls | undefined;
- } & InteractiveBaseProps &
+ } & Omit<InteractiveBaseProps, 'loop'> &
InteractivePremountProps
>(
(
@@ -279,6 +281,7 @@ const SolidOuter = forwardRef<
styleWhilePremounted,
styleWhilePostmounted,
trimBefore,
+ trimAfter,
playbackRate,
freeze,
hidden,
@@ -310,7 +313,13 @@ const SolidOuter = forwardRef<
premountingStyle,
} = usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop: undefined,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
@@ -333,6 +342,7 @@ const SolidOuter = forwardRef<
layout="none"
from={from}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
freeze={freeze}
hidden={hidden}
diff --git a/packages/core/src/interactivity-schema.ts b/packages/core/src/interactivity-schema.ts
index 3a89b32934..7a156819c9 100644
--- a/packages/core/src/interactivity-schema.ts
+++ b/packages/core/src/interactivity-schema.ts
@@ -611,6 +611,21 @@ export const trimBeforeField = {
hiddenFromList: true,
} as const satisfies InteractivitySchemaField;
+export const trimAfterField = {
+ type: 'number',
+ default: undefined,
+ min: 1,
+ step: 1,
+ hiddenFromList: true,
+} as const satisfies InteractivitySchemaField;
+
+export const loopField = {
+ type: 'boolean',
+ default: false,
+ description: 'Loop',
+ keyframable: false,
+} as const satisfies InteractivitySchemaField;
+
export const freezeField = {
type: 'number',
default: null,
@@ -632,6 +647,22 @@ export const baseSchema = {
durationInFrames: durationInFramesField,
from: fromField,
trimBefore: trimBeforeField,
+ trimAfter: trimAfterField,
+ playbackRate: playbackRateField,
+ loop: loopField,
+ freeze: freezeField,
+ hidden: hiddenField,
+ name: sequenceNameField,
+ showInTimeline: showInTimelineField,
+} as const satisfies InteractivitySchema;
+
+// For components whose content does not advance with the frame, or which
+// implement their own looping, so that a `loop` control is never inert.
+export const baseSchemaWithoutLoop = {
+ durationInFrames: durationInFramesField,
+ from: fromField,
+ trimBefore: trimBeforeField,
+ trimAfter: trimAfterField,
playbackRate: playbackRateField,
freeze: freezeField,
hidden: hiddenField,
@@ -639,10 +670,12 @@ export const baseSchema = {
showInTimeline: showInTimelineField,
} as const satisfies InteractivitySchema;
+// For static images: neither speed nor looping changes what they show.
export const baseSchemaWithoutPlaybackRate = {
durationInFrames: durationInFramesField,
from: fromField,
trimBefore: trimBeforeField,
+ trimAfter: trimAfterField,
freeze: freezeField,
hidden: hiddenField,
name: sequenceNameField,
@@ -665,7 +698,9 @@ export const sequenceSchema = {
export const baseSchemaWithoutFrom = {
durationInFrames: durationInFramesField,
trimBefore: trimBeforeField,
+ trimAfter: trimAfterField,
playbackRate: playbackRateField,
+ loop: loopField,
freeze: freezeField,
hidden: hiddenField,
name: sequenceNameField,
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index eedfcff74d..84087620d9 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -97,6 +97,7 @@ import {
import type {SerializedJSONWithCustomFields} from './input-props-serialization.js';
import {
baseSchema,
+ baseSchemaWithoutLoop,
durationInFramesField,
freezeField,
fromField,
@@ -148,6 +149,7 @@ import {
RenderAssetManager,
RenderAssetManagerProvider,
} from './RenderAssetManager.js';
+import {resolveSequenceDuration} from './resolve-sequence-duration.js';
import {
resolveVideoConfig,
resolveVideoConfigOrCatch,
@@ -352,6 +354,7 @@ export const Internals = {
SequenceOutlineContext,
SequenceStackTracesUpdateContext,
baseSchema,
+ baseSchemaWithoutLoop,
sequenceSchema,
SequenceWithoutSchema,
sequenceStyleSchema,
@@ -375,6 +378,7 @@ export const Internals = {
SequenceContext,
PremountContext,
usePremounting,
+ resolveSequenceDuration,
useRemotionContexts,
RemotionContextProvider,
CSSUtils,
diff --git a/packages/core/src/loop/index.tsx b/packages/core/src/loop/index.tsx
index 07af9d82d9..af976cc9d7 100644
--- a/packages/core/src/loop/index.tsx
+++ b/packages/core/src/loop/index.tsx
@@ -6,6 +6,7 @@ import {SequenceContext} from '../SequenceContext.js';
import {useCurrentFrame} from '../use-current-frame.js';
import {useVideoConfig} from '../use-video-config.js';
import {validateDurationInFrames} from '../validation/validate-duration-in-frames.js';
+import {LoopContext, type LoopContextType} from './loop-context.js';
export type LoopProps = {
// The duration of the content to be looped
@@ -17,13 +18,6 @@ export type LoopProps = {
} & LayoutAndStyle &
Pick<SequenceProps, 'showInTimeline' | 'playbackRate'>;
-type LoopContextType = {
- iteration: number;
- durationInFrames: number;
-};
-
-const LoopContext = createContext<LoopContextType | null>(null);
-
export const LoopTimelineContext = createContext<{
startFrame: number;
firstVisibleFrame: number;
diff --git a/packages/core/src/loop/loop-context.ts b/packages/core/src/loop/loop-context.ts
new file mode 100644
index 0000000000..afa01af66d
--- /dev/null
+++ b/packages/core/src/loop/loop-context.ts
@@ -0,0 +1,8 @@
+import {createContext} from 'react';
+
+export type LoopContextType = {
+ iteration: number;
+ durationInFrames: number;
+};
+
+export const LoopContext = createContext<LoopContextType | null>(null);
diff --git a/packages/core/src/resolve-sequence-duration.ts b/packages/core/src/resolve-sequence-duration.ts
new file mode 100644
index 0000000000..9f7b22d3c1
--- /dev/null
+++ b/packages/core/src/resolve-sequence-duration.ts
@@ -0,0 +1,26 @@
+// Length of an item in its parent timeline. `trimAfter` is measured in the
+// child clock, so it only bounds the duration once converted by `playbackRate`.
+// A looped item repeats the trimmed range, so only `durationInFrames` caps it.
+export const resolveSequenceDuration = ({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+}: {
+ durationInFrames: number | undefined;
+ trimBefore: number | undefined;
+ trimAfter: number | undefined;
+ playbackRate: number | undefined;
+ loop: boolean | undefined;
+}): number => {
+ const duration = durationInFrames ?? Infinity;
+ if (loop || trimAfter === undefined) {
+ return duration;
+ }
+
+ return Math.min(
+ duration,
+ (trimAfter - (trimBefore ?? 0)) / (playbackRate ?? 1),
+ );
+};
diff --git a/packages/core/src/series/index.tsx b/packages/core/src/series/index.tsx
index 7e1290c8fb..672c8d8610 100644
--- a/packages/core/src/series/index.tsx
+++ b/packages/core/src/series/index.tsx
@@ -39,7 +39,9 @@ type SeriesSequenceProps = PropsWithChildren<
| 'showInTimeline'
| 'freeze'
| 'trimBefore'
+ | 'trimAfter'
| 'playbackRate'
+ | 'loop'
> &
LayoutAndStyle
>;
@@ -65,7 +67,9 @@ const seriesSequenceSchema = {
showInTimeline: Interactive.sequenceSchema.showInTimeline,
freeze: Interactive.baseSchema.freeze,
trimBefore: Interactive.sequenceSchema.trimBefore,
+ trimAfter: Interactive.sequenceSchema.trimAfter,
playbackRate: Interactive.sequenceSchema.playbackRate,
+ loop: Interactive.sequenceSchema.loop,
layout: Interactive.sequenceSchema.layout,
} as const satisfies InteractivitySchema;
diff --git a/packages/core/src/test/with-interactivity-schema-helpers.test.ts b/packages/core/src/test/with-interactivity-schema-helpers.test.ts
index 0da4c95837..bf6c9206f7 100644
--- a/packages/core/src/test/with-interactivity-schema-helpers.test.ts
+++ b/packages/core/src/test/with-interactivity-schema-helpers.test.ts
@@ -149,7 +149,9 @@ test('baseSchema exposes common timeline fields', () => {
'name',
'showInTimeline',
'trimBefore',
+ 'trimAfter',
'playbackRate',
+ 'loop',
].sort(),
);
});
@@ -241,7 +243,9 @@ test('getFlatSchema(sequenceSchema) exposes every variant key', () => {
'from',
'freeze',
'trimBefore',
+ 'trimAfter',
'playbackRate',
+ 'loop',
].sort(),
);
});
@@ -562,7 +566,9 @@ test('selectActiveKeys returns only the hidden + layout keys when layout=none',
'durationInFrames',
'from',
'trimBefore',
+ 'trimAfter',
'playbackRate',
+ 'loop',
'freeze',
].sort(),
);
@@ -584,7 +590,9 @@ test('selectActiveKeys exposes style.* keys when layout=absolute-fill', () => {
'durationInFrames',
'from',
'trimBefore',
+ 'trimAfter',
'playbackRate',
+ 'loop',
'freeze',
'style.translate',
'style.scale',
@@ -616,7 +624,9 @@ test('selectActiveKeys exposes style.* keys when layout=absolute-fill', () => {
'durationInFrames',
'from',
'trimBefore',
+ 'trimAfter',
'playbackRate',
+ 'loop',
'freeze',
].sort(),
);
@@ -668,7 +678,9 @@ test('end-to-end: layout=none drops style.scale from active props', () => {
'durationInFrames',
'from',
'trimBefore',
+ 'trimAfter',
'playbackRate',
+ 'loop',
'freeze',
].sort(),
);
diff --git a/packages/docs/docs/interactive.mdx b/packages/docs/docs/interactive.mdx
index 99c1c80d89..fa0815bd91 100644
--- a/packages/docs/docs/interactive.mdx
+++ b/packages/docs/docs/interactive.mdx
@@ -72,9 +72,11 @@ Use them with [`Interactive.withSchema()`](/docs/interactive-with-schema).
Controls inherited from [`<Sequence>`](/docs/sequence):
-`durationInFrames`, `from`, `trimBefore`, `playbackRate`, `freeze`, `hidden`, `name` and `showInTimeline`.
+`durationInFrames`, `from`, `trimBefore`, `trimAfter`, `playbackRate`, `loop`, `freeze`, `hidden`, `name` and `showInTimeline`.
-Use it for components that render a `<Sequence>` internally.
+`trimAfter` and `loop` are included since <AvailableFrom v="4.0.530" inline />.
+
+Use it for components that render a `<Sequence>` internally and forward these props to it.
```tsx twoslash title="schema.ts"
import {Interactive, type InteractivitySchema} from 'remotion';
@@ -90,6 +92,12 @@ export const shapeSchema = {
} as const satisfies InteractivitySchema;
```
+### `Interactive.baseSchemaWithoutLoop`<AvailableFrom v="4.0.530" />
+
+The same controls as [`Interactive.baseSchema`](#interactivebaseschema) without `loop`.
+
+Use it for components whose output does not change with the frame, such as a static image or shape, so that the Studio does not show a `Loop` toggle that has no effect.
+
### `Interactive.transformSchema`<AvailableFrom v="4.0.479" />
Controls for transform-related style props:
diff --git a/packages/docs/docs/interactivity-schema.mdx b/packages/docs/docs/interactivity-schema.mdx
index 3062eaece4..aeeee799b5 100644
--- a/packages/docs/docs/interactivity-schema.mdx
+++ b/packages/docs/docs/interactivity-schema.mdx
@@ -37,7 +37,9 @@ Keys may use dot notation. For example, a key named `style.opacity` reads from a
### `Interactive.baseSchema`
-Timeline props inherited from [`<Sequence>`](/docs/sequence): [`durationInFrames`](/docs/sequence#durationinframes), [`from`](/docs/sequence#from), [`trimBefore`](/docs/sequence#trimbefore), [`playbackRate`](/docs/sequence#playbackrate), [`freeze`](/docs/sequence#freeze), [`hidden`](/docs/sequence#hidden), [`name`](/docs/sequence#name) and [`showInTimeline`](/docs/sequence#showintimeline).
+Timeline props inherited from [`<Sequence>`](/docs/sequence): [`durationInFrames`](/docs/sequence#durationinframes), [`from`](/docs/sequence#from), [`trimBefore`](/docs/sequence#trimbefore), [`trimAfter`](/docs/sequence#trimafter), [`playbackRate`](/docs/sequence#playbackrate), [`loop`](/docs/sequence#loop), [`freeze`](/docs/sequence#freeze), [`hidden`](/docs/sequence#hidden), [`name`](/docs/sequence#name) and [`showInTimeline`](/docs/sequence#showintimeline).
+
+Use [`Interactive.baseSchemaWithoutLoop`](/docs/interactive#interactivebaseschemawithoutloop) for components whose output does not change with the frame.
### `Interactive.captionsSchema`<AvailableFrom v="4.0.500" />
diff --git a/packages/docs/docs/loop.mdx b/packages/docs/docs/loop.mdx
index c3e8d7cda4..15b5eb0656 100644
--- a/packages/docs/docs/loop.mdx
+++ b/packages/docs/docs/loop.mdx
@@ -63,6 +63,31 @@ CSS styles to be applied to the container. If `layout` is set to `none`, there i
`<Loop>` inherits [`premountFor`](/docs/sequence#premountfor), [`postmountFor`](/docs/sequence#postmountfor), [`styleWhilePremounted`](/docs/sequence#stylewhilepremounted) and [`styleWhilePostmounted`](/docs/sequence#stylewhilepostmounted) from [`<Sequence>`](/docs/sequence). These props require `layout="absolute-fill"` (the default). Postmounting the end of the loop requires a finite `times`.
+## Relation to `<Sequence loop>`<AvailableFrom v="4.0.530" />
+
+[`<Sequence>`](/docs/sequence#loop) and other timed components accept a [`loop`](/docs/timing#loop-repeat-an-item) prop that repeats the range selected by `trimBefore` and `trimAfter`.
+
+`<Loop>` measures its period in the parent timeline and counts iterations with `times`:
+
+```tsx twoslash
+// @include: example-BlueSquare
+import {Loop, Sequence} from 'remotion';
+// ---cut---
+const WithLoop = () => (
+ <Loop durationInFrames={50} times={3}>
+ <BlueSquare />
+ </Loop>
+);
+
+const WithSequence = () => (
+ <Sequence trimAfter={50} durationInFrames={150} loop>
+ <BlueSquare />
+ </Sequence>
+);
+```
+
+Both render the same frames. Use `<Sequence loop>` if the content should start at a `trimBefore` offset or play at a `playbackRate`.
+
## Examples
All the examples below are based on the following animation of a blue square:
diff --git a/packages/docs/docs/sequence.mdx b/packages/docs/docs/sequence.mdx
index 19f1af2463..8174d3eeda 100644
--- a/packages/docs/docs/sequence.mdx
+++ b/packages/docs/docs/sequence.mdx
@@ -216,13 +216,68 @@ export const MyVideo = () => {
In the example above, `Child` mounts at composition frame `30` and receives frame `15`.
+### `trimAfter?`<AvailableFrom v="4.0.530" />
+
+Ends the child timeline at the specified frame, measured in the same clock as [`trimBefore`](#trimbefore).
+Children receive frames from `trimBefore` up to, but not including, `trimAfter`.
+
+The sequence unmounts after `(trimAfter - trimBefore) / playbackRate` frames in the parent timeline, unless [`durationInFrames`](#durationinframes) or the parent ends it earlier.
+Must be greater than `trimBefore`.
+
+```tsx twoslash title="MyVideo.tsx"
+import {Sequence, useCurrentFrame} from 'remotion';
+
+const Child = () => <div>{useCurrentFrame()}</div>;
+
+// ---cut---
+export const MyVideo = () => {
+ return (
+ <Sequence from={30} trimBefore={15} trimAfter={45}>
+ <Child />
+ </Sequence>
+ );
+};
+```
+
+In the example above, `Child` receives frames `15` through `44` and the sequence is visible from composition frame `30` through `59`.
+
+See [Timing and trimming](/docs/timing#why-both-trimafter-and-durationinframes) for how `trimAfter` and `durationInFrames` differ.
+
+### `loop?`<AvailableFrom v="4.0.530" />
+
+Repeats the child range selected by [`trimBefore`](#trimbefore) and [`trimAfter`](#trimafter). Default: `false`.
+
+Requires a finite `trimAfter`, because a sequence has no intrinsic duration.
+Each cycle lasts `(trimAfter - trimBefore) / playbackRate` frames in the parent timeline.
+[`durationInFrames`](#durationinframes) limits the total length; without it, the sequence loops until its parent ends.
+
+```tsx twoslash title="MyVideo.tsx"
+import {Sequence, useCurrentFrame} from 'remotion';
+
+const Child = () => <div>{useCurrentFrame()}</div>;
+
+// ---cut---
+export const MyVideo = () => {
+ return (
+ <Sequence trimBefore={30} trimAfter={60} playbackRate={2} durationInFrames={90} loop>
+ <Child />
+ </Sequence>
+ );
+};
+```
+
+In the example above, `Child` receives frames `30` through `59` in a cycle of `15` composition frames, repeated six times.
+
+[`Loop.useLoop()`](/docs/loop#useloop) returns the current iteration inside a looping sequence.
+To repeat a duration measured in the parent timeline, use [`<Loop>`](/docs/loop).
+
### `playbackRate?`<AvailableFrom v="4.0.528" />
Changes the speed of the children, including their animations and media. Default: `1`.
Use `2` for double speed or `0.5` for half speed. The value must be positive and finite, and must remain constant across frames.
`from` and `durationInFrames` are measured in the parent timeline. Changing `playbackRate` does not change when the sequence mounts or unmounts.
-`trimBefore` is measured in the children's frames and is applied after the speed change:
+`trimBefore` and `trimAfter` are measured in the children's frames and are applied after the speed change:
```tsx twoslash title="MyVideo.tsx"
import {Sequence, useCurrentFrame} from 'remotion';
diff --git a/packages/docs/docs/studio/make-component-interactive.mdx b/packages/docs/docs/studio/make-component-interactive.mdx
index 42da5b42a8..9b15b5c2ea 100644
--- a/packages/docs/docs/studio/make-component-interactive.mdx
+++ b/packages/docs/docs/studio/make-component-interactive.mdx
@@ -15,9 +15,10 @@ For code that remains editable after visual changes, follow [Interactivity best
An [`InteractivitySchema`](/docs/interactivity-schema) describes which props the Studio may edit.
-Use [`Interactive.baseSchema`](/docs/interactive#interactivebaseschema) for props inherited from [`<Sequence>`](/docs/sequence), such as [`from`](/docs/sequence#from), [`trimBefore`](/docs/sequence#trimbefore), [`playbackRate`](/docs/sequence#playbackrate), [`durationInFrames`](/docs/sequence#durationinframes), [`freeze`](/docs/sequence#freeze), [`hidden`](/docs/sequence#hidden), [`name`](/docs/sequence#name) and [`showInTimeline`](/docs/sequence#showintimeline).
+Use [`Interactive.baseSchema`](/docs/interactive#interactivebaseschema) for props inherited from [`<Sequence>`](/docs/sequence), such as [`from`](/docs/sequence#from), [`trimBefore`](/docs/sequence#trimbefore), [`trimAfter`](/docs/sequence#trimafter), [`playbackRate`](/docs/sequence#playbackrate), [`loop`](/docs/sequence#loop), [`durationInFrames`](/docs/sequence#durationinframes), [`freeze`](/docs/sequence#freeze), [`hidden`](/docs/sequence#hidden), [`name`](/docs/sequence#name) and [`showInTimeline`](/docs/sequence#showintimeline).
-As a rule, include `Interactive.baseSchema` in every schema passed to `Interactive.withSchema()` so trimming and visibility controls remain available.
+As a rule, include `Interactive.baseSchema` in every schema passed to `Interactive.withSchema()` so trimming, looping and visibility controls remain available. Forward all of these props to the `<Sequence>` your component renders.
+If the output of your component does not change with the frame, use [`Interactive.baseSchemaWithoutLoop`](/docs/interactive#interactivebaseschemawithoutloop) instead so that no inert `Loop` control is shown.
Use [`Interactive.transformSchema`](/docs/interactive#interactivetransformschema) if your component accepts a `style` prop and applies it to the rendered element.
@@ -294,6 +295,10 @@ The component now has a timeline row, inherits the common `<Sequence>` props and
Use it when the component renders a `<Sequence>` internally.
+### `Interactive.baseSchemaWithoutLoop`<AvailableFrom v="4.0.530" />
+
+Use it instead of `Interactive.baseSchema` when the output of the component does not change with the frame, so that no `Loop` control is shown.
+
### `Interactive.transformSchema`
Use it when the component accepts a `style` prop and applies it to the rendered element.
diff --git a/packages/docs/docs/timing.mdx b/packages/docs/docs/timing.mdx
index 39c9ecba2e..198ff4f375 100644
--- a/packages/docs/docs/timing.mdx
+++ b/packages/docs/docs/timing.mdx
@@ -93,63 +93,89 @@ export const MyComposition = () => (
);
```
-## `trimAfter`: Trim video or audio
+## `trimAfter`: Trim the end<AvailableFrom v="4.0.530" />
-`<Audio>` and `<Video>` also accept `trimAfter` to select where playback stops in the video or audio.
+`trimAfter` selects where the content ends, measured in the same clock as `trimBefore`: the frame that the children receive from [`useCurrentFrame()`](/docs/use-current-frame).
-At 30 composition FPS, `trimBefore={45}` and `trimAfter={105}` select the source from 1.5 to 3.5 seconds.
+The item shows the child frames from `trimBefore` up to, but not including, `trimAfter`. It therefore ends after `(trimAfter - trimBefore) / playbackRate` parent frames, unless `durationInFrames` or the parent ends it earlier.
<TimingTimeline from={30} trimBefore={45} trimAfter={105} playbackRate={2} loop={false} />
```tsx twoslash title="MyComposition.tsx"
-import {staticFile} from 'remotion';
-import {Video} from '@remotion/media';
+import {Interactive, useCurrentFrame} from 'remotion';
+
+const Counter = () => <div>{useCurrentFrame()}</div>;
export const MyComposition = () => (
- <Video
- src={staticFile('video.mp4')}
+ <Interactive.Div
from={30}
trimBefore={45}
trimAfter={105}
playbackRate={2}
- />
+ >
+ <Counter />
+ </Interactive.Div>
);
```
-## `loop`: Repeat video or audio
+For `<Video>` and `<Audio>`, the child clock is the position in the source file in composition frames.
+At 30 composition FPS, `trimBefore={45}` and `trimAfter={105}` select the source from 1.5 to 3.5 seconds.
+If `trimAfter` is omitted, playback continues until the end of the file.
+
+Before 4.0.530, `trimAfter` was only available on `<Video>` and `<Audio>`.
-`loop` on `<Audio>` and `<Video>` repeats the source range selected by `trimBefore` and `trimAfter`.
+## `loop`: Repeat an item<AvailableFrom v="4.0.530" />
-If `trimAfter` is omitted, it loops from `trimBefore` after the end of the source.
+`loop` repeats the range selected by `trimBefore` and `trimAfter`.
+
+`durationInFrames` limits the total length of the item. Without it, the item loops until its parent ends.
<TimingTimeline from={30} durationInFrames={90} trimBefore={45} trimAfter={105} playbackRate={2} loop />
```tsx twoslash title="MyComposition.tsx"
-import {staticFile} from 'remotion';
-import {Video} from '@remotion/media';
+import {Interactive, useCurrentFrame} from 'remotion';
+
+const Counter = () => <div>{useCurrentFrame()}</div>;
export const MyComposition = () => (
- <Video
- src={staticFile('video.mp4')}
+ <Interactive.Div
from={30}
trimBefore={45}
trimAfter={105}
playbackRate={2}
loop
durationInFrames={90}
- />
+ >
+ <Counter />
+ </Interactive.Div>
);
```
-At 30 FPS, this repeats the selected 60 source frames three times over 90 composition frames.
+This repeats the selected 60 child frames three times over 90 composition frames.
+
+`<Video>` and `<Audio>` have an intrinsic duration, so they may omit `trimAfter` and loop the whole file after `trimBefore`.
+All other items require `trimAfter` to loop, because they have no intrinsic end.
+
+[`<Loop>`](/docs/loop) is a shorthand for the same behavior with a period measured in the parent timeline: `<Loop durationInFrames={50} times={3}>` is equivalent to `trimAfter={50}`, `durationInFrames={150}` and `loop`.
+
+Before 4.0.530, `loop` was only available on `<Video>` and `<Audio>`.
+
+## Why both `trimAfter` and `durationInFrames`?
+
+`trimAfter` is a position in the child clock, `durationInFrames` is a length in the parent timeline.
+
+They only coincide if `trimBefore` is `0`, `playbackRate` is `1` and the item does not loop.
+
+`loop` needs both: the trim range defines one cycle, `durationInFrames` defines how long the item stays.
+A range of 60 child frames repeated over 90 parent frames cannot be expressed with one prop.
## Order of operations
Think of the timing props as applying to the content in this order:
-1. `from`, `trimBefore` and `trimAfter` defines the selected source range
+1. `from`, `trimBefore` and `trimAfter` define the selected range
2. `playbackRate` stretches or compresses the selected range
-3. `loop` (on `<Audio>` and `<Video>`) loops the stretched range
+3. `loop` repeats the stretched range
4. `durationInFrames` is applied last and defines the duration of the item in the timeline.
For an item with children, [`useCurrentFrame()`](/docs/use-current-frame) at parent frame `t` is:
@@ -158,8 +184,15 @@ For an item with children, [`useCurrentFrame()`](/docs/use-current-frame) at par
trimBefore + (t - from) * playbackRate
```
+If the item loops, the elapsed child frames wrap around at the end of the range:
+
+```
+trimBefore + ((t - from) * playbackRate) % (trimAfter - trimBefore)
+```
+
## See also
- [`<Sequence>`](/docs/sequence)
+- [`<Loop>`](/docs/loop)
- [Trimming audio](/docs/audio/trimming)
- [Adding a video](/docs/videos/)
diff --git a/packages/docs/elements/guidelines.mdx b/packages/docs/elements/guidelines.mdx
index 11c1af31de..d348d836d3 100644
--- a/packages/docs/elements/guidelines.mdx
+++ b/packages/docs/elements/guidelines.mdx
@@ -53,9 +53,9 @@ Wrap the exported component with [`Interactive.withSchema()`](/docs/interactive-
Export the schema-enabled component directly and wire up the common props:
- Add [`Interactive.baseSchema`](/docs/interactive#interactivebaseschema) to every `Interactive.withSchema()` schema and pass `controls` unchanged to the component's `<Sequence>`.
-- Pass `from`, `durationInFrames`, `trimBefore`, `playbackRate`, `freeze`, `hidden`, `name`, and `showInTimeline` to that Sequence.
+- Pass `from`, `durationInFrames`, `trimBefore`, `trimAfter`, `playbackRate`, `loop`, `freeze`, `hidden`, `name`, and `showInTimeline` to that Sequence.
- Pass `style` and the ref to the rendered outline. When the Sequence uses `layout="none"`, pass [`outlineRef`](/docs/sequence#outlineref) to it. Set the dimensions in the component because Element metadata does not pass them as props.
-- Render frame-dependent animation in a child below the Sequence. This makes [`useCurrentFrame()`](/docs/use-current-frame) respect `from`, `trimBefore`, `playbackRate`, and `freeze`. If the component that returns the Sequence calls it, it reads the parent clock.
+- Render frame-dependent animation in a child below the Sequence. This makes [`useCurrentFrame()`](/docs/use-current-frame) respect `from`, `trimBefore`, `trimAfter`, `playbackRate`, `loop`, and `freeze`. If the component that returns the Sequence calls it, it reads the parent clock.
Export the schema-enabled component directly, without a forwarding wrapper. See the [oscilloscope](/elements/audio/oscilloscope), [waveform progress](/elements/audio/waveform-progress), and [mirrored spectrum](/elements/audio/mirrored-spectrum) Elements.
diff --git a/packages/docs/static/system-prompt.txt b/packages/docs/static/system-prompt.txt
index 97142bba19..1ec24c709f 100644
--- a/packages/docs/static/system-prompt.txt
+++ b/packages/docs/static/system-prompt.txt
@@ -168,6 +168,8 @@ export const MyComp: React.FC = () => {
The "from" prop specifies the frame number where the element should appear.
The "trimBefore" prop trims the start of the component timeline by a number of frames.
+The "trimAfter" prop ends the component timeline at a frame, measured in the same clock as "trimBefore".
+The "loop" prop repeats the range between "trimBefore" and "trimAfter"; it requires "trimAfter" except on <Video> and <Audio>, which loop the whole file.
The "durationInFrames" prop specifies how long the element should appear.
diff --git a/packages/gif/src/Gif.tsx b/packages/gif/src/Gif.tsx
index 6086499d31..81848d7bc2 100644
--- a/packages/gif/src/Gif.tsx
+++ b/packages/gif/src/Gif.tsx
@@ -19,7 +19,7 @@ import type {RemotionGifProps} from './props';
const {useMemoizedEffectDefinitions, useMemoizedEffects} = Internals;
-export type GifProps = InteractiveBaseProps &
+export type GifProps = Omit<InteractiveBaseProps, 'loop'> &
InteractiveCropProps &
InteractivePremountProps &
InteractiveTransformProps &
@@ -32,7 +32,8 @@ export type GifProps = InteractiveBaseProps &
* @see [Documentation](https://remotion.dev/docs/gif)
*/
export const gifSchema: InteractivitySchema = {
- ...Internals.baseSchema,
+ // Looping is controlled by `loopBehavior`, which repeats the intrinsic duration.
+ ...Internals.baseSchemaWithoutLoop,
...Internals.premountSchema,
...Internals.transformSchema,
...Interactive.backgroundSchema,
@@ -84,7 +85,13 @@ const GifInner = ({
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore: sequenceProps.trimBefore,
+ trimAfter: sequenceProps.trimAfter,
+ playbackRate,
+ loop: undefined,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
diff --git a/packages/lottie/src/Lottie.tsx b/packages/lottie/src/Lottie.tsx
index b87fae4db1..7dc2027658 100644
--- a/packages/lottie/src/Lottie.tsx
+++ b/packages/lottie/src/Lottie.tsx
@@ -182,6 +182,7 @@ export const Lottie = ({
from,
durationInFrames,
trimBefore,
+ trimAfter,
freeze,
hidden,
name,
@@ -203,7 +204,13 @@ export const Lottie = ({
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate: undefined,
+ loop: undefined,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
@@ -218,6 +225,7 @@ export const Lottie = ({
from={from}
durationInFrames={durationInFrames}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
freeze={freeze}
hidden={hidden}
name={name ?? '<Lottie>'}
diff --git a/packages/lottie/src/types.ts b/packages/lottie/src/types.ts
index 86d7e400dc..aa1d8ec9fa 100644
--- a/packages/lottie/src/types.ts
+++ b/packages/lottie/src/types.ts
@@ -20,7 +20,7 @@ export type AspectRatioConstraint =
| 'xMinYMax'
| 'xMidYMax';
-export type LottieProps = Omit<InteractiveBaseProps, 'playbackRate'> &
+export type LottieProps = Omit<InteractiveBaseProps, 'playbackRate' | 'loop'> &
InteractivePremountProps & {
/**
* JSON object with the animation data.
diff --git a/packages/mac-cursors/src/MacOSCursor.tsx b/packages/mac-cursors/src/MacOSCursor.tsx
index de81318e8f..b3cb956642 100644
--- a/packages/mac-cursors/src/MacOSCursor.tsx
+++ b/packages/mac-cursors/src/MacOSCursor.tsx
@@ -15,7 +15,7 @@ import {
} from 'remotion';
import {macOSCursorNames, resolveCursor} from './resolve-cursor';
-export type MacOSCursorProps = InteractiveBaseProps &
+export type MacOSCursorProps = Omit<InteractiveBaseProps, 'loop'> &
InteractivePremountProps & {
readonly cursor?: string;
readonly customCursor?: string;
@@ -24,7 +24,8 @@ export type MacOSCursorProps = InteractiveBaseProps &
};
export const macOSCursorSchema: InteractivitySchema = {
- ...Interactive.baseSchema,
+ // A cursor image does not advance with the frame, so looping it would be inert.
+ ...Interactive.baseSchemaWithoutLoop,
...Interactive.premountSchema,
cursor: {
type: 'enum',
@@ -64,6 +65,7 @@ const MacOSCursorInner: React.FC<
styleWhilePremounted,
styleWhilePostmounted,
trimBefore,
+ trimAfter,
playbackRate,
freeze,
hidden,
@@ -92,7 +94,13 @@ const MacOSCursorInner: React.FC<
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop: undefined,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
@@ -106,6 +114,7 @@ const MacOSCursorInner: React.FC<
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
diff --git a/packages/maptiler/src/MapHeatmap.tsx b/packages/maptiler/src/MapHeatmap.tsx
index 4e520f7c1b..184a5a48e9 100644
--- a/packages/maptiler/src/MapHeatmap.tsx
+++ b/packages/maptiler/src/MapHeatmap.tsx
@@ -212,7 +212,9 @@ export const MapHeatmap = (props: MapHeatmapProps) => {
name,
showInTimeline,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
} = props;
const {
@@ -224,7 +226,13 @@ export const MapHeatmap = (props: MapHeatmapProps) => {
postmountingActive,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: null,
@@ -238,7 +246,9 @@ export const MapHeatmap = (props: MapHeatmapProps) => {
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/maptiler/src/MapLayer.tsx b/packages/maptiler/src/MapLayer.tsx
index 915fcdede5..ef81b77188 100644
--- a/packages/maptiler/src/MapLayer.tsx
+++ b/packages/maptiler/src/MapLayer.tsx
@@ -147,7 +147,9 @@ export const MapLayer = ({
from,
durationInFrames,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
hidden,
name,
@@ -165,7 +167,13 @@ export const MapLayer = ({
postmountingActive,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: null,
@@ -181,7 +189,9 @@ export const MapLayer = ({
from={from}
durationInFrames={durationInFrames}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
freeze={freeze}
hidden={hidden}
showInTimeline={showInTimeline ?? false}
diff --git a/packages/maptiler/src/MapOverlay.tsx b/packages/maptiler/src/MapOverlay.tsx
index 53a78f0290..471d3ae542 100644
--- a/packages/maptiler/src/MapOverlay.tsx
+++ b/packages/maptiler/src/MapOverlay.tsx
@@ -131,7 +131,9 @@ const MapOverlayRefForwardingFunction: ForwardRefRenderFunction<
styleWhilePremounted,
styleWhilePostmounted,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
hidden,
name,
@@ -166,7 +168,13 @@ const MapOverlayRefForwardingFunction: ForwardRefRenderFunction<
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: {opacity, ...style},
@@ -180,7 +188,9 @@ const MapOverlayRefForwardingFunction: ForwardRefRenderFunction<
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/maptiler/src/MapPoint.tsx b/packages/maptiler/src/MapPoint.tsx
index 133970d35e..e51841569e 100644
--- a/packages/maptiler/src/MapPoint.tsx
+++ b/packages/maptiler/src/MapPoint.tsx
@@ -286,7 +286,9 @@ export const MapPoint = (props: MapPointProps) => {
name,
showInTimeline,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
} = props;
const {
@@ -298,7 +300,13 @@ export const MapPoint = (props: MapPointProps) => {
postmountingActive,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: null,
@@ -312,7 +320,9 @@ export const MapPoint = (props: MapPointProps) => {
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/maptiler/src/MapPolygon.tsx b/packages/maptiler/src/MapPolygon.tsx
index 4c5d8c0ca2..197aa86b0c 100644
--- a/packages/maptiler/src/MapPolygon.tsx
+++ b/packages/maptiler/src/MapPolygon.tsx
@@ -376,7 +376,9 @@ export const MapPolygon = (props: MapPolygonProps) => {
name,
showInTimeline,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
} = props;
const {
@@ -388,7 +390,13 @@ export const MapPolygon = (props: MapPolygonProps) => {
postmountingActive,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: null,
@@ -402,7 +410,9 @@ export const MapPolygon = (props: MapPolygonProps) => {
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/maptiler/src/MapPolyline.tsx b/packages/maptiler/src/MapPolyline.tsx
index 23c3f1c889..1dc0b641d2 100644
--- a/packages/maptiler/src/MapPolyline.tsx
+++ b/packages/maptiler/src/MapPolyline.tsx
@@ -446,7 +446,9 @@ export const MapPolyline = (props: MapPolylineProps) => {
name,
showInTimeline,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
} = props;
const {
@@ -458,7 +460,13 @@ export const MapPolyline = (props: MapPolylineProps) => {
postmountingActive,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: null,
@@ -472,7 +480,9 @@ export const MapPolyline = (props: MapPolylineProps) => {
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/maptiler/src/MapRegion.tsx b/packages/maptiler/src/MapRegion.tsx
index 7a3e43f950..96c21f177a 100644
--- a/packages/maptiler/src/MapRegion.tsx
+++ b/packages/maptiler/src/MapRegion.tsx
@@ -364,7 +364,9 @@ const MapRegionRefForwardingFunction: ForwardRefRenderFunction<
premountFor,
postmountFor,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
hidden,
name,
@@ -386,7 +388,13 @@ const MapRegionRefForwardingFunction: ForwardRefRenderFunction<
postmountingActive,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: null,
@@ -400,7 +408,9 @@ const MapRegionRefForwardingFunction: ForwardRefRenderFunction<
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/maptiler/src/MapRoute.tsx b/packages/maptiler/src/MapRoute.tsx
index 7b974982dd..ade7034b71 100644
--- a/packages/maptiler/src/MapRoute.tsx
+++ b/packages/maptiler/src/MapRoute.tsx
@@ -293,7 +293,9 @@ const MapRouteRefForwardingFunction: ForwardRefRenderFunction<
premountFor,
postmountFor,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
hidden,
name,
@@ -315,7 +317,13 @@ const MapRouteRefForwardingFunction: ForwardRefRenderFunction<
postmountingActive,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: null,
@@ -329,7 +337,9 @@ const MapRouteRefForwardingFunction: ForwardRefRenderFunction<
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
hidden={hidden}
diff --git a/packages/maptiler/src/MapSource.tsx b/packages/maptiler/src/MapSource.tsx
index 93c6b38c22..a6771e5076 100644
--- a/packages/maptiler/src/MapSource.tsx
+++ b/packages/maptiler/src/MapSource.tsx
@@ -99,7 +99,9 @@ export const MapSource = ({
from,
durationInFrames,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
hidden,
name,
@@ -117,7 +119,13 @@ export const MapSource = ({
postmountingActive,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: null,
@@ -133,7 +141,9 @@ export const MapSource = ({
from={from}
durationInFrames={durationInFrames}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
freeze={freeze}
hidden={hidden}
showInTimeline={showInTimeline ?? false}
diff --git a/packages/maptiler/src/MapViewport.tsx b/packages/maptiler/src/MapViewport.tsx
index 91597eb1a7..0860e1c3ff 100644
--- a/packages/maptiler/src/MapViewport.tsx
+++ b/packages/maptiler/src/MapViewport.tsx
@@ -552,7 +552,9 @@ const MapViewportRefForwardingFunction: ForwardRefRenderFunction<
durationInFrames,
from,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
hidden,
name,
@@ -576,7 +578,13 @@ const MapViewportRefForwardingFunction: ForwardRefRenderFunction<
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: null,
@@ -592,7 +600,9 @@ const MapViewportRefForwardingFunction: ForwardRefRenderFunction<
durationInFrames={durationInFrames}
from={from}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
freeze={freeze}
hidden={hidden}
showInTimeline={showInTimeline}
diff --git a/packages/media/src/audio/audio.tsx b/packages/media/src/audio/audio.tsx
index eaf388f03c..87ba7c721c 100644
--- a/packages/media/src/audio/audio.tsx
+++ b/packages/media/src/audio/audio.tsx
@@ -57,7 +57,6 @@ export const audioSchema: InteractivitySchema = {
keyframable: false,
},
muted: {type: 'boolean', default: false, description: 'Muted'},
- loop: {type: 'boolean', default: false, description: 'Loop'},
} as const satisfies InteractivitySchema;
const AudioInner: React.FC<
diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsx
index c480ed035f..69686c1103 100644
--- a/packages/media/src/video/video.tsx
+++ b/packages/media/src/video/video.tsx
@@ -63,7 +63,6 @@ export const videoSchema: InteractivitySchema = {
keyframable: false,
},
muted: {type: 'boolean', default: false, description: 'Muted'},
- loop: {type: 'boolean', default: false, description: 'Loop'},
...Internals.transformSchema,
...Interactive.backgroundSchema,
...Interactive.borderSchema,
diff --git a/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx b/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx
index fd2d16a68e..0633880576 100644
--- a/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx
+++ b/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx
@@ -2,6 +2,7 @@ import React, {useCallback} from 'react';
import {
Freeze,
HtmlInCanvas,
+ Internals,
type HtmlInCanvasProps,
type HtmlInCanvasOnPaint,
useCurrentFrame,
@@ -10,7 +11,7 @@ import {
export type HtmlInCanvasMotionBlurProps = Pick<
HtmlInCanvasProps,
- 'from' | 'durationInFrames' | 'trimBefore' | 'playbackRate'
+ 'from' | 'durationInFrames' | 'trimBefore' | 'trimAfter' | 'playbackRate'
> & {
readonly children: React.ReactNode;
readonly width: number;
@@ -83,6 +84,7 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({
from,
durationInFrames,
trimBefore,
+ trimAfter,
playbackRate,
}) => {
const {durationInFrames: compositionDurationInFrames} = useVideoConfig();
@@ -110,7 +112,13 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({
const visibleDuration = Math.max(
0,
Math.min(
- durationInFrames ?? Infinity,
+ Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop: undefined,
+ }),
compositionDurationInFrames - (from ?? 0),
),
);
@@ -187,6 +195,7 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({
from={from}
durationInFrames={durationInFrames}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
name="<HtmlInCanvasMotionBlur>"
onPaint={onPaint}
diff --git a/packages/rive/src/RemotionRiveCanvas.tsx b/packages/rive/src/RemotionRiveCanvas.tsx
index 7203fb4ce3..656fb5b149 100644
--- a/packages/rive/src/RemotionRiveCanvas.tsx
+++ b/packages/rive/src/RemotionRiveCanvas.tsx
@@ -450,7 +450,9 @@ const RemotionRiveCanvasInnerForwardRefFunction: React.ForwardRefRenderFunction<
styleWhilePremounted,
styleWhilePostmounted,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
showInTimeline,
hidden,
@@ -476,7 +478,13 @@ const RemotionRiveCanvasInnerForwardRefFunction: React.ForwardRefRenderFunction<
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
@@ -499,7 +507,9 @@ const RemotionRiveCanvasInnerForwardRefFunction: React.ForwardRefRenderFunction<
layout="none"
from={from}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
freeze={freeze}
hidden={hidden}
showInTimeline={showInTimeline}
diff --git a/packages/rough-notation/src/Annotation.tsx b/packages/rough-notation/src/Annotation.tsx
index c507ce8945..1773767e31 100644
--- a/packages/rough-notation/src/Annotation.tsx
+++ b/packages/rough-notation/src/Annotation.tsx
@@ -37,7 +37,7 @@ type SharedAnnotationComponentProps = Readonly<
type AnnotationInteractiveProps<Config> = SharedAnnotationComponentProps &
Readonly<Config> &
- InteractiveBaseProps &
+ Omit<InteractiveBaseProps, 'loop'> &
InteractivePremountProps;
export type HighlightProps = AnnotationInteractiveProps<HighlightConfig>;
@@ -52,7 +52,7 @@ export type CircleProps = AnnotationInteractiveProps<
>;
type InternalAnnotationProps = SharedAnnotationComponentProps &
- InteractiveBaseProps &
+ Omit<InteractiveBaseProps, 'loop'> &
InteractivePremountProps & {
readonly color?: string;
readonly strokeWidth?: number;
@@ -216,7 +216,8 @@ const textContentSchema = {
} as const satisfies InteractivitySchema;
const sharedSchema = (defaultRoughness: number): InteractivitySchema => ({
- ...Interactive.baseSchema,
+ // The drawing is driven by `progress`, so a loop control would be inert.
+ ...Interactive.baseSchemaWithoutLoop,
...Interactive.premountSchema,
progress: {
type: 'number',
@@ -369,6 +370,7 @@ const makeAnnotationComponent = ({
styleWhilePremounted,
styleWhilePostmounted,
trimBefore,
+ trimAfter,
playbackRate,
freeze,
hidden,
@@ -422,7 +424,13 @@ const makeAnnotationComponent = ({
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop: undefined,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: null,
@@ -436,6 +444,7 @@ const makeAnnotationComponent = ({
layout="none"
from={from ?? 0}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
durationInFrames={durationInFrames ?? Infinity}
freeze={freeze}
diff --git a/packages/shapes/src/components/render-svg.tsx b/packages/shapes/src/components/render-svg.tsx
index f6199c204c..fea4581ab3 100644
--- a/packages/shapes/src/components/render-svg.tsx
+++ b/packages/shapes/src/components/render-svg.tsx
@@ -15,7 +15,7 @@ import {
} from 'remotion';
import {doesReactSupportTransformOriginProperty} from '../utils/does-react-support-canary';
-type ShapeSequenceProps = InteractiveBaseProps &
+type ShapeSequenceProps = Omit<InteractiveBaseProps, 'loop'> &
InteractivePremountProps & {
readonly controls?: SequenceControls;
};
@@ -45,6 +45,7 @@ const RenderSvgWithTiming = ({
styleWhilePostmounted,
from,
trimBefore,
+ trimAfter,
playbackRate,
freeze,
hidden,
@@ -78,7 +79,13 @@ const RenderSvgWithTiming = ({
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop: undefined,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: actualStyle,
@@ -92,6 +99,7 @@ const RenderSvgWithTiming = ({
layout="none"
from={from}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
freeze={freeze}
hidden={hidden}
@@ -135,6 +143,7 @@ export const RenderSvg = ({
styleWhilePremounted,
styleWhilePostmounted,
trimBefore,
+ trimAfter,
playbackRate,
freeze,
hidden,
@@ -301,6 +310,7 @@ export const RenderSvg = ({
styleWhilePostmounted={styleWhilePostmounted}
from={from}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
freeze={freeze}
hidden={hidden}
diff --git a/packages/shapes/src/components/schema.ts b/packages/shapes/src/components/schema.ts
index c3815821a8..40c93a5f7b 100644
--- a/packages/shapes/src/components/schema.ts
+++ b/packages/shapes/src/components/schema.ts
@@ -85,7 +85,8 @@ export const makeShapeSchema = (
shapeFields: InteractivitySchema,
): InteractivitySchema => {
return {
- ...Internals.baseSchema,
+ // Shapes do not advance with the frame, so a loop control would be inert.
+ ...Internals.baseSchemaWithoutLoop,
...Internals.premountSchema,
...shapeFields,
fill: colorField({
diff --git a/packages/shapes/src/test/effects.test.tsx b/packages/shapes/src/test/effects.test.tsx
index aa237327a5..ca8c44dac7 100644
--- a/packages/shapes/src/test/effects.test.tsx
+++ b/packages/shapes/src/test/effects.test.tsx
@@ -89,12 +89,29 @@ mock.module('remotion', () => {
durationInFrames: {},
from: {},
trimBefore: {},
+ trimAfter: {},
+ loop: {},
+ freeze: {},
+ hidden: {},
+ name: {},
+ showInTimeline: {},
+ },
+ baseSchemaWithoutLoop: {
+ durationInFrames: {},
+ from: {},
+ trimBefore: {},
+ trimAfter: {},
freeze: {},
hidden: {},
name: {},
showInTimeline: {},
},
transformSchema: {},
+ resolveSequenceDuration: ({
+ durationInFrames,
+ }: {
+ readonly durationInFrames: number | undefined;
+ }) => durationInFrames ?? Infinity,
usePremounting: ({
style,
}: {
diff --git a/packages/skills/skills/remotion-interactivity/SKILL.md b/packages/skills/skills/remotion-interactivity/SKILL.md
index e9101af763..901c49809a 100644
--- a/packages/skills/skills/remotion-interactivity/SKILL.md
+++ b/packages/skills/skills/remotion-interactivity/SKILL.md
@@ -305,7 +305,7 @@ Render separate elements if one version should have effects and another should n
## Making your own component interactive
-When using `Interactive.withSchema()`, include `Interactive.baseSchema` in the schema so standard timeline controls such as trimming and visibility remain available.
+When using `Interactive.withSchema()`, include `Interactive.baseSchema` in the schema so standard timeline controls such as trimming, looping and visibility remain available, and forward `from`, `durationInFrames`, `trimBefore`, `trimAfter`, `playbackRate`, `loop`, `freeze`, `hidden`, `name` and `showInTimeline` to the `<Sequence>` the component renders. Use `Interactive.baseSchemaWithoutLoop` if the output does not change with the frame.
To make a custom userland component interactive, use:
[Make a component interactive](https://www.remotion.dev/docs/studio/make-component-interactive.md)
diff --git a/packages/skills/skills/remotion-markup/SKILL.md b/packages/skills/skills/remotion-markup/SKILL.md
index 73bff1f89e..26e93f61d0 100644
--- a/packages/skills/skills/remotion-markup/SKILL.md
+++ b/packages/skills/skills/remotion-markup/SKILL.md
@@ -203,6 +203,29 @@ Useful for components whose internal clock should start later:
<Sequence trimBefore={10 * fps} {/* ... */} />
```
+### `trimAfter`
+
+Ends the internal clock at a frame. Measured in the same clock as `trimBefore`, so the layer lasts `(trimAfter - trimBefore) / playbackRate` frames in the timeline unless `durationInFrames` is shorter:
+
+```tsx
+// Play the footage from second 2 to second 5
+<Video trimBefore={2 * fps} trimAfter={5 * fps} {/* ... */} />
+
+// Children see frames `10 * fps` through `15 * fps - 1`
+<Sequence trimBefore={10 * fps} trimAfter={15 * fps} {/* ... */} />
+```
+
+### `loop`
+
+Repeats the range between `trimBefore` and `trimAfter`. `durationInFrames` sets the total length. `<Video>` and `<Audio>` may omit `trimAfter` and loop the whole file; other layers need `trimAfter` because they have no intrinsic end:
+
+```tsx
+<Video loop durationInFrames={20 * fps} {/* ... */} />
+<Sequence trimAfter={2 * fps} durationInFrames={20 * fps} loop {/* ... */} />
+```
+
+`<Img>`, `<CanvasImage>`, `<Solid>` and shapes do not support `loop` because their output does not change over time.
+
### Fallback
If a component does not support these props, wrap it in`<Sequence>` from `remotion`, which has them.
diff --git a/packages/studio-server/src/test/discriminated-union-update.test.ts b/packages/studio-server/src/test/discriminated-union-update.test.ts
index 77dd2df7e5..e02c3fb2f7 100644
--- a/packages/studio-server/src/test/discriminated-union-update.test.ts
+++ b/packages/studio-server/src/test/discriminated-union-update.test.ts
@@ -25,7 +25,11 @@ test('Should correctly separate discriminated union for layout', () => {
propStatuses: {},
getDragOverrides: () => ({}),
});
- expect(schemaFields?.map((s) => s.key)).toEqual(['playbackRate', 'layout']);
+ expect(schemaFields?.map((s) => s.key)).toEqual([
+ 'playbackRate',
+ 'loop',
+ 'layout',
+ ]);
});
test('Should expose absolute-fill variant fields when active', () => {
@@ -46,6 +50,7 @@ test('Should expose absolute-fill variant fields when active', () => {
});
expect(schemaFields?.map((s) => s.key)).toEqual([
'playbackRate',
+ 'loop',
'style.transformOrigin',
'style.translate',
'style.scale',
diff --git a/packages/studio-server/src/test/get-all-schema-keys.test.ts b/packages/studio-server/src/test/get-all-schema-keys.test.ts
index 0df7cc84ff..aaa434164e 100644
--- a/packages/studio-server/src/test/get-all-schema-keys.test.ts
+++ b/packages/studio-server/src/test/get-all-schema-keys.test.ts
@@ -37,7 +37,9 @@ test('getAllSchemaKeys returns every key across all enum variants', () => {
'durationInFrames',
'from',
'trimBefore',
+ 'trimAfter',
'playbackRate',
+ 'loop',
'freeze',
].sort(),
);
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index 43c12b708c..702063870d 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -40,6 +40,7 @@ import {
TIMELINE_PADDING,
} from '../../helpers/timeline-layout';
import {useMediaMetadata} from '../../helpers/use-media-metadata';
+import {useRuntimeValueSelector} from '../../helpers/use-runtime-values';
import {SetSelectedModalContext} from '../../state/modals';
import {AudioWaveform} from '../AudioWaveform';
import {useConfirmationDialog} from '../ConfirmationDialog';
@@ -73,6 +74,7 @@ import {
import {TimelineSequenceFrame} from './TimelineSequenceFrame';
import {
canResizeTimelineSequenceDuration,
+ getTimelineSequenceEndField,
getTimelineSequenceMediaDurationDragLimits,
isCascadingSequence,
isTimelineSequenceDurationDraggable,
@@ -534,10 +536,19 @@ const TimelineSequenceInner: React.FC<{
isStudioInteractivityEnabled() &&
propStatusesForOverride?.durationInFrames?.status === 'static',
);
+ const endField = useRuntimeValueSelector({
+ controls: s.controls,
+ selector: (runtimeValues) =>
+ getTimelineSequenceEndField({sequence: s, runtimeValues}),
+ isEqual: (first, second) =>
+ first.fieldKey === second.fieldKey &&
+ first.trimBefore === second.trimBefore &&
+ first.playbackRate === second.playbackRate,
+ });
const durationCanResize = Boolean(
isStudioInteractivityEnabled() &&
canResizeTimelineSequenceDuration({
- status: propStatusesForOverride?.durationInFrames,
+ status: propStatusesForOverride?.[endField.fieldKey],
}),
);
const fromCanUpdate = Boolean(
@@ -1064,7 +1075,7 @@ const TimelineSequenceInner: React.FC<{
nodePath !== null &&
validatedLocation !== null &&
durationCanResize &&
- (!isMedia || mediaDurationDragLimits !== null);
+ (!isMedia || Boolean(s.loopDisplay) || mediaDurationDragLimits !== null);
const showLeftEdgeDragHandle =
isTimelineSequenceLeftEdgeDraggable(s) &&
nodePath !== null &&
diff --git a/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx b/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx
index 11779af216..6c16c80cb2 100644
--- a/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx
@@ -1,6 +1,7 @@
import {
isSchemaFieldHoldOnly,
isSchemaFieldKeyframable,
+ stringifySequenceSubscriptionKey,
} from '@remotion/studio-shared';
import React, {useCallback, useContext, useMemo, useRef} from 'react';
import {unstable_batchedUpdates} from 'react-dom';
@@ -195,6 +196,59 @@ const Value: React.FC<{
sequencesRef,
],
);
+ // Items without an intrinsic duration need `trimAfter` to loop. Filling it
+ // with the current end keeps the output unchanged until the clip is extended.
+ const getLoopTrimAfter = useCallback(
+ (value: unknown): number | null => {
+ if (field.key !== 'loop' || value !== true || !schema.trimAfter) {
+ return null;
+ }
+
+ const trimAfterStatus = Internals.getPropStatusesCtx(
+ propStatusesRef.current,
+ nodePath,
+ )?.trimAfter;
+ if (
+ trimAfterStatus?.status !== 'static' ||
+ trimAfterStatus.codeValue !== undefined
+ ) {
+ return null;
+ }
+
+ const key = stringifySequenceSubscriptionKey(nodePath);
+ const sequence = sequencesRef.current.find((candidate) => {
+ const overrideId = candidate.controls?.overrideId;
+ const path = overrideId
+ ? overrideIdToNodePathMappings[overrideId]
+ : undefined;
+ return path && stringifySequenceSubscriptionKey(path) === key;
+ });
+ if (
+ !sequence ||
+ sequence.type === 'audio' ||
+ sequence.type === 'video' ||
+ !Number.isFinite(sequence.duration) ||
+ sequence.duration <= 0
+ ) {
+ return null;
+ }
+
+ const runtimeTrimBefore =
+ sequence.controls?.runtimeValues.getSnapshot().trimBefore;
+ const trimBefore =
+ typeof runtimeTrimBefore === 'number' ? runtimeTrimBefore : 0;
+
+ return trimBefore + sequence.duration * sequence.sequencePlaybackRate;
+ },
+ [
+ field.key,
+ nodePath,
+ overrideIdToNodePathMappings,
+ propStatusesRef,
+ schema.trimAfter,
+ sequencesRef,
+ ],
+ );
const getAdjustedDuration = useCallback(
(playbackRate: unknown): number | null => {
if (
@@ -303,6 +357,7 @@ const Value: React.FC<{
const stringifiedValue = JSON.stringify(value);
const fieldLabel = field.description ?? field.key;
const adjustedDuration = getAdjustedDuration(value);
+ const loopTrimAfter = getLoopTrimAfter(value);
const keyframeChanges = getPlaybackRateChanges(value);
if (value === propStatus.codeValue) {
@@ -353,6 +408,18 @@ const Value: React.FC<{
schema,
},
]),
+ ...(loopTrimAfter === null
+ ? []
+ : [
+ {
+ fileName: validatedLocation.source,
+ nodePath,
+ fieldKey: 'trimAfter',
+ value: loopTrimAfter,
+ defaultValue: null,
+ schema,
+ },
+ ]),
],
setPropStatuses,
clientId,
@@ -369,6 +436,7 @@ const Value: React.FC<{
field.fieldSchema.type,
field.key,
getAdjustedDuration,
+ getLoopTrimAfter,
getPlaybackRateChanges,
nodePath,
schema,
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
index 10ed7746ba..598ccda0ef 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
@@ -184,6 +184,15 @@ const baseStyle: React.CSSProperties = {
touchAction: 'none',
};
+// The right edge edits whichever prop currently defines the end of the item.
+// `trimAfter` is measured in the child clock, so parent-frame drags are
+// converted through `trimBefore` and `playbackRate`.
+export type TimelineSequenceEndField = {
+ readonly fieldKey: 'durationInFrames' | 'trimAfter';
+ readonly trimBefore: number;
+ readonly playbackRate: number;
+};
+
export type TimelineSequenceDurationDragTarget = {
readonly parentPlaybackRate: number;
readonly fileName: string;
@@ -192,6 +201,7 @@ export type TimelineSequenceDurationDragTarget = {
readonly minimumDuration: number;
readonly nodePath: SequencePropsSubscriptionKey;
readonly schema: InteractivitySchema;
+ readonly endField: TimelineSequenceEndField;
};
export type TimelineSequenceLeftEdgeDragTarget = {
@@ -205,6 +215,9 @@ export type TimelineSequenceLeftEdgeDragTarget = {
readonly playbackRate: number;
readonly positionField: 'from' | null;
readonly schema: InteractivitySchema;
+ // When `trimAfter` defines the end, moving `trimBefore` keeps the end in
+ // place by itself and `durationInFrames` stays untouched.
+ readonly writesDuration: boolean;
};
export type TimelineSequenceFromDragTarget = {
@@ -346,6 +359,8 @@ const getMinimumSequenceDuration = ({
);
};
+// Only `<Loop>` registers a loop display without controls. Items with controls
+// own their `loop` prop, so their edges stay editable while looping.
export const isTimelineSequenceDurationDraggable = (sequence: TSequence) => {
const isInteractiveCascadingSequence = isCascadingSequence(sequence);
@@ -354,7 +369,6 @@ export const isTimelineSequenceDurationDraggable = (sequence: TSequence) => {
sequence.type === 'image' ||
sequence.type === 'audio' ||
sequence.type === 'video') &&
- !sequence.loopDisplay &&
(!sequence.isInsideSeries || isInteractiveCascadingSequence) &&
Boolean(sequence.controls)
);
@@ -386,7 +400,13 @@ const playbackRateComponentIdentities = new Set([
'dev.remotion.remotion.AnimatedImage',
]);
-const getTrimBeforePlaybackRate = (sequence: TSequence) => {
+const getTrimPlaybackRate = ({
+ sequence,
+ runtimeValues,
+}: {
+ readonly sequence: TSequence;
+ readonly runtimeValues: Readonly<Record<string, unknown>>;
+}) => {
const componentIdentity = sequence.controls?.componentIdentity;
if (
componentIdentity === null ||
@@ -396,17 +416,52 @@ const getTrimBeforePlaybackRate = (sequence: TSequence) => {
return sequence.sequencePlaybackRate;
}
- const runtimePlaybackRate =
- sequence.controls?.runtimeValues.getSnapshot().playbackRate;
+ const runtimePlaybackRate = runtimeValues.playbackRate;
return typeof runtimePlaybackRate === 'number' ? runtimePlaybackRate : 1;
};
+export const getTimelineSequenceEndField = ({
+ sequence,
+ runtimeValues,
+}: {
+ readonly sequence: TSequence;
+ readonly runtimeValues: Readonly<Record<string, unknown>>;
+}): TimelineSequenceEndField => {
+ const playbackRate = getTrimPlaybackRate({sequence, runtimeValues});
+ const trimBefore =
+ typeof runtimeValues.trimBefore === 'number' ? runtimeValues.trimBefore : 0;
+ const {trimAfter, durationInFrames} = runtimeValues;
+ // A looped item repeats its trim range, so its end is always the duration.
+ const endsAtTrimAfter =
+ runtimeValues.loop !== true &&
+ typeof trimAfter === 'number' &&
+ Number.isFinite(trimAfter) &&
+ (typeof durationInFrames !== 'number' ||
+ (trimAfter - trimBefore) / playbackRate <= durationInFrames);
+
+ return {
+ fieldKey: endsAtTrimAfter ? 'trimAfter' : 'durationInFrames',
+ trimBefore,
+ playbackRate,
+ };
+};
+
+export const getTimelineSequenceEndFieldValue = ({
+ endField,
+ durationInFrames,
+}: {
+ readonly endField: TimelineSequenceEndField;
+ readonly durationInFrames: number;
+}) => {
+ if (endField.fieldKey === 'durationInFrames') {
+ return durationInFrames;
+ }
+
+ return endField.trimBefore + durationInFrames * endField.playbackRate;
+};
+
const isFromDraggableSequence = (sequence: TSequence) => {
- return (
- !sequence.loopDisplay &&
- !sequence.isInsideSeries &&
- Boolean(sequence.controls)
- );
+ return !sequence.isInsideSeries && Boolean(sequence.controls);
};
export const getTimelineSequenceDurationDragValue = ({
@@ -510,7 +565,10 @@ export const getTimelineSequenceLeftEdgeDragChanges = ({
});
}
- if (nextValues.durationInFrames !== target.initialDuration) {
+ if (
+ target.writesDuration &&
+ nextValues.durationInFrames !== target.initialDuration
+ ) {
changes.push({
fileName: target.fileName,
nodePath: target.nodePath,
@@ -559,8 +617,11 @@ export const getTimelineSequenceDurationDragChanges = ({
{
fileName: target.fileName,
nodePath: target.nodePath,
- fieldKey: 'durationInFrames',
- value: nextValue,
+ fieldKey: target.endField.fieldKey,
+ value: getTimelineSequenceEndFieldValue({
+ endField: target.endField,
+ durationInFrames: nextValue,
+ }),
defaultValue: null,
schema: target.schema,
},
@@ -803,10 +864,19 @@ export const getTimelineSequenceDurationDragTargets = ({
}
const nodePath = track.nodePathInfo.sequenceSubscriptionKey;
+ const {controls} = originalSequence;
+ if (!controls) {
+ return null;
+ }
+
+ const endField = getTimelineSequenceEndField({
+ sequence: originalSequence,
+ runtimeValues: controls.runtimeValues.getSnapshot(),
+ });
const durationStatus = Internals.getPropStatusesCtx(
propStatuses,
nodePath,
- )?.durationInFrames;
+ )?.[endField.fieldKey];
if (
!canResizeTimelineSequenceDuration({
status: durationStatus,
@@ -815,16 +885,15 @@ export const getTimelineSequenceDurationDragTargets = ({
return null;
}
- const {controls} = originalSequence;
- if (!controls) {
- return null;
- }
-
const key = stringifySequenceSubscriptionKey(nodePath);
if (!targets.has(key)) {
const selectionKey = getTimelineSequenceSelectionKey(nodePathInfo);
+ // A looped media item repeats its source, so its natural length does
+ // not cap the duration.
const isMedia =
- originalSequence.type === 'audio' || originalSequence.type === 'video';
+ (originalSequence.type === 'audio' ||
+ originalSequence.type === 'video') &&
+ !originalSequence.loopDisplay;
const isDraggedSequence = selectionKey === draggedSelectionKey;
const mediaDurationDragLimits = isMedia
? isDraggedSequence
@@ -865,6 +934,7 @@ export const getTimelineSequenceDurationDragTargets = ({
minimumDuration,
nodePath,
schema: controls.schema,
+ endField,
});
}
}
@@ -944,6 +1014,7 @@ export const getTimelineSequenceLeftEdgeDragTargets = ({
const key = stringifySequenceSubscriptionKey(nodePath);
if (!targets.has(key)) {
+ const runtimeValues = controls.runtimeValues.getSnapshot();
targets.set(key, {
parentPlaybackRate: getParentSequencePlaybackRate(
originalSequence,
@@ -961,9 +1032,17 @@ export const getTimelineSequenceLeftEdgeDragTargets = ({
sequences,
}),
nodePath,
- playbackRate: getTrimBeforePlaybackRate(originalSequence),
+ playbackRate: getTrimPlaybackRate({
+ sequence: originalSequence,
+ runtimeValues,
+ }),
positionField,
schema: controls.schema,
+ writesDuration:
+ getTimelineSequenceEndField({
+ sequence: originalSequence,
+ runtimeValues,
+ }).fieldKey === 'durationInFrames',
});
}
}
@@ -1433,11 +1512,14 @@ const TimelineSequenceLeftEdgeDragHandleInner: React.FC<{
);
}
- latestRef.current.setDragOverrides(
- target.nodePath,
- 'durationInFrames',
- Internals.makeStaticDragOverride(nextValues.durationInFrames),
- );
+ if (target.writesDuration) {
+ latestRef.current.setDragOverrides(
+ target.nodePath,
+ 'durationInFrames',
+ Internals.makeStaticDragOverride(nextValues.durationInFrames),
+ );
+ }
+
latestRef.current.setDragOverrides(
target.nodePath,
'trimBefore',
@@ -2080,8 +2162,13 @@ const TimelineSequenceRightEdgeDragHandleInner: React.FC<{
latestRef.current.setDragOverrides(
target.nodePath,
- 'durationInFrames',
- Internals.makeStaticDragOverride(previewValue),
+ target.endField.fieldKey,
+ Internals.makeStaticDragOverride(
+ getTimelineSequenceEndFieldValue({
+ endField: target.endField,
+ durationInFrames: previewValue,
+ }),
+ ),
);
}
diff --git a/packages/studio/src/components/Timeline/split-selected-timeline-item.ts b/packages/studio/src/components/Timeline/split-selected-timeline-item.ts
index 6bab6c234d..66f352db05 100644
--- a/packages/studio/src/components/Timeline/split-selected-timeline-item.ts
+++ b/packages/studio/src/components/Timeline/split-selected-timeline-item.ts
@@ -81,6 +81,15 @@ export const getTimelineSequenceSplitEligibility = ({
};
}
+ // `trimBefore` is both where playback starts and where the loop restarts,
+ // so the right half could not resume mid-cycle.
+ if (sequence.loopDisplay) {
+ return {
+ canSplit: false,
+ reason: 'Looping sequences cannot be split',
+ };
+ }
+
if (
!hasSequenceTimingTraits(
selection.nodePathInfo.sequenceSubscriptionKey.sequenceKeys,
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index b62575c695..845ad0d9c5 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -2002,6 +2002,7 @@ test('Timeline duration drag supports interactive video clips', () => {
minimumDuration: 1,
nodePath: nodePathInfo.sequenceSubscriptionKey,
schema: Internals.baseSchema,
+ endField: {fieldKey: 'durationInFrames', trimBefore: 0, playbackRate: 1},
},
]);
});
diff --git a/packages/template-vibe-code/src/editor/model/schemas.ts b/packages/template-vibe-code/src/editor/model/schemas.ts
index d232a40553..5e13fb1437 100644
--- a/packages/template-vibe-code/src/editor/model/schemas.ts
+++ b/packages/template-vibe-code/src/editor/model/schemas.ts
@@ -250,8 +250,10 @@ const timingKeys = new Set([
"from",
"durationInFrames",
"trimBefore",
+ "trimAfter",
"offset",
"playbackRate",
+ "loop",
"freeze",
"hidden",
"premountFor",
@@ -307,6 +309,8 @@ const fieldLabels: Record<string, string> = {
from: "From",
durationInFrames: "Duration",
trimBefore: "Trim before",
+ trimAfter: "Trim after",
+ loop: "Loop",
freeze: "Freeze frame",
hidden: "Hidden",
playbackRate: "Speed",
diff --git a/packages/three/src/ThreeCanvas.tsx b/packages/three/src/ThreeCanvas.tsx
index 525d01c5d0..0e74cc6fdb 100644
--- a/packages/three/src/ThreeCanvas.tsx
+++ b/packages/three/src/ThreeCanvas.tsx
@@ -159,7 +159,9 @@ export const ThreeCanvasInternals = ({
from,
durationInFrames,
trimBefore,
+ trimAfter,
playbackRate,
+ loop,
freeze,
hidden,
name,
@@ -181,7 +183,13 @@ export const ThreeCanvasInternals = ({
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames,
+ trimBefore,
+ trimAfter,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
@@ -196,7 +204,9 @@ export const ThreeCanvasInternals = ({
from={from}
durationInFrames={durationInFrames}
trimBefore={trimBefore}
+ trimAfter={trimAfter}
playbackRate={playbackRate}
+ loop={loop}
freeze={freeze}
hidden={hidden}
name={name ?? '<ThreeCanvas>'}
diff --git a/packages/transitions/src/TransitionSeries.tsx b/packages/transitions/src/TransitionSeries.tsx
index adcffa9783..350a11afb1 100644
--- a/packages/transitions/src/TransitionSeries.tsx
+++ b/packages/transitions/src/TransitionSeries.tsx
@@ -132,7 +132,9 @@ type SeriesSequenceProps = PropsWithChildren<
| 'freeze'
| 'hidden'
| 'trimBefore'
+ | 'trimAfter'
| 'playbackRate'
+ | 'loop'
>
>;
@@ -153,7 +155,9 @@ const transitionSeriesSequenceSchema = {
showInTimeline: Internals.sequenceSchema.showInTimeline,
freeze: Internals.freezeField,
trimBefore: Internals.sequenceSchema.trimBefore,
+ trimAfter: Internals.sequenceSchema.trimAfter,
playbackRate: Internals.sequenceSchema.playbackRate,
+ loop: Internals.sequenceSchema.loop,
layout: Internals.sequenceSchema.layout,
} as const satisfies InteractivitySchema;