Skip to content

🎥 Make videos programmatically with React

feat: add postmountfor in core

Shankhadeep Dey4c82804cbb0f
diff --git a/packages/core/src/CompositionManager.tsx b/packages/core/src/CompositionManager.tsx
index c5a9c36703..1de3adaf40 100644
--- a/packages/core/src/CompositionManager.tsx
+++ b/packages/core/src/CompositionManager.tsx
@@ -120,6 +120,7 @@ export type TSequence = {
loopDisplay: LoopDisplay | undefined;
stack: string | null;
premountDisplay: number | null;
+ postmountDisplay: number | null;
} & EnhancedTSequenceData;
export type AudioOrVideoAsset = {
diff --git a/packages/core/src/Sequence.tsx b/packages/core/src/Sequence.tsx
index 505e0228c8..aa3858a729 100644
--- a/packages/core/src/Sequence.tsx
+++ b/packages/core/src/Sequence.tsx
@@ -342,16 +342,158 @@ const PremountedSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
};
const PremountedSequence = forwardRef(PremountedSequenceRefForwardingFunction);
+
+const PostmountedSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
+ HTMLDivElement,
+ SequenceProps
+> = (props, ref) => {
+ const frame = useCurrentFrame();
+
+ if (props.layout === 'none') {
+ throw new Error(
+ '`<Sequence>` with `postmountFor` prop does not support layout="none"',
+ );
+ }
+
+ const {
+ style: passedStyle,
+ from = 0,
+ durationInFrames = Infinity,
+ postmountFor = 0,
+ styleWhilePostmounted,
+ ...otherProps
+ } = props;
+
+ const endThreshold = Math.ceil(from + durationInFrames - 1);
+ const postmountingActive =
+ frame > endThreshold && frame <= endThreshold + postmountFor;
+
+ // Freeze at the last frame during postmount
+ const freezeFrame = durationInFrames - 1;
+
+ const style = useMemo(() => {
+ return {
+ ...passedStyle,
+ opacity: postmountingActive ? 0 : 1,
+ pointerEvents: postmountingActive
+ ? 'none'
+ : (passedStyle?.pointerEvents ?? undefined),
+ ...(postmountingActive ? styleWhilePostmounted : {}),
+ };
+ }, [passedStyle, postmountingActive, styleWhilePostmounted]);
+
+ return (
+ <Freeze frame={freezeFrame} active={postmountingActive}>
+ <Sequence
+ ref={ref}
+ from={from}
+ durationInFrames={durationInFrames}
+ style={style}
+ _remotionInternalPostmountDisplay={postmountFor}
+ _remotionInternalIsPostmounting={postmountingActive}
+ {...otherProps}
+ />
+ </Freeze>
+ );
+};
+
+const PostmountedSequence = forwardRef(
+ PostmountedSequenceRefForwardingFunction,
+);
+
+const PremountedPostmountedSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
+ HTMLDivElement,
+ SequenceProps
+> = (props, ref) => {
+ const frame = useCurrentFrame();
+
+ if (props.layout === 'none') {
+ throw new Error(
+ '`<Sequence>` with `premountFor` and `postmountFor` props does not support layout="none"',
+ );
+ }
+
+ const {
+ style: passedStyle,
+ from = 0,
+ durationInFrames = Infinity,
+ premountFor = 0,
+ postmountFor = 0,
+ styleWhilePremounted,
+ styleWhilePostmounted,
+ ...otherProps
+ } = props;
+
+ const endThreshold = Math.ceil(from + durationInFrames - 1);
+ const premountingActive = frame < from && frame >= from - premountFor;
+ const postmountingActive =
+ frame > endThreshold && frame <= endThreshold + postmountFor;
+
+ // Determine which freeze frame to use
+ const freezeFrame = premountingActive
+ ? 0
+ : postmountingActive
+ ? durationInFrames - 1
+ : 0;
+ const isFreezingActive = premountingActive || postmountingActive;
+
+ const style = useMemo(() => {
+ return {
+ ...passedStyle,
+ opacity: premountingActive || postmountingActive ? 0 : 1,
+ pointerEvents:
+ premountingActive || postmountingActive
+ ? 'none'
+ : (passedStyle?.pointerEvents ?? undefined),
+ ...(premountingActive ? styleWhilePremounted : {}),
+ ...(postmountingActive ? styleWhilePostmounted : {}),
+ };
+ }, [
+ passedStyle,
+ premountingActive,
+ postmountingActive,
+ styleWhilePremounted,
+ styleWhilePostmounted,
+ ]);
+
+ return (
+ <Freeze frame={freezeFrame} active={isFreezingActive}>
+ <Sequence
+ ref={ref}
+ from={from}
+ durationInFrames={durationInFrames}
+ style={style}
+ _remotionInternalPremountDisplay={premountFor}
+ _remotionInternalPostmountDisplay={postmountFor}
+ _remotionInternalIsPremounting={premountingActive}
+ _remotionInternalIsPostmounting={postmountingActive}
+ {...otherProps}
+ />
+ </Freeze>
+ );
+};
+
+const PremountedPostmountedSequence = forwardRef(
+ PremountedPostmountedSequenceRefForwardingFunction,
+);
+
const SequenceRefForwardingFunction: React.ForwardRefRenderFunction<
HTMLDivElement,
SequenceProps
> = (props, ref) => {
- if (
- props.layout !== 'none' &&
- props.premountFor &&
- !getRemotionEnvironment().isRendering
- ) {
- return <PremountedSequence {...props} ref={ref} />;
+ if (props.layout !== 'none' && !getRemotionEnvironment().isRendering) {
+ if (props.premountFor && props.postmountFor) {
+ // Handle both premount and postmount
+ return <PremountedPostmountedSequence {...props} ref={ref} />;
+ }
+
+ if (props.premountFor) {
+ return <PremountedSequence {...props} ref={ref} />;
+ }
+
+ if (props.postmountFor) {
+ return <PostmountedSequence {...props} ref={ref} />;
+ }
}
return <RegularSequence {...props} ref={ref} />;
diff --git a/packages/core/src/audio/AudioForPreview.tsx b/packages/core/src/audio/AudioForPreview.tsx
index 5feec93c64..bc3e558ed5 100644
--- a/packages/core/src/audio/AudioForPreview.tsx
+++ b/packages/core/src/audio/AudioForPreview.tsx
@@ -182,8 +182,10 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
stack: _remotionInternalStack,
showInTimeline,
premountDisplay: null,
+ postmountDisplay: null,
onAutoPlayError: null,
isPremounting: Boolean(sequenceContext?.premounting),
+ isPostmounting: Boolean(sequenceContext?.postmounting),
});
// putting playback before useVolume
@@ -196,6 +198,7 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
onlyWarnForMediaSeekingError: false,
acceptableTimeshift: acceptableTimeShiftInSeconds ?? null,
isPremounting: Boolean(sequenceContext?.premounting),
+ isPostmounting: Boolean(sequenceContext?.postmounting),
pauseWhenBuffering,
onAutoPlayError: null,
});
diff --git a/packages/core/src/use-media-buffering.ts b/packages/core/src/use-media-buffering.ts
index 437661b767..aba31b0b9b 100644
--- a/packages/core/src/use-media-buffering.ts
+++ b/packages/core/src/use-media-buffering.ts
@@ -8,6 +8,7 @@ export const useMediaBuffering = ({
element,
shouldBuffer,
isPremounting,
+ isPostmounting,
logLevel,
mountTime,
src,
@@ -15,6 +16,7 @@ export const useMediaBuffering = ({
element: React.RefObject<HTMLVideoElement | HTMLAudioElement | null>;
shouldBuffer: boolean;
isPremounting: boolean;
+ isPostmounting: boolean;
logLevel: LogLevel;
mountTime: number;
src: string | null;
@@ -35,7 +37,7 @@ export const useMediaBuffering = ({
return;
}
- if (isPremounting) {
+ if (isPremounting || isPostmounting) {
// Needed by iOS Safari which will not load by default
// and therefore not fire the canplay event.
@@ -45,7 +47,7 @@ export const useMediaBuffering = ({
// has no future data.
// Breaks on Firefox though: https://github.com/remotion-dev/remotion/issues/3915
- if (current.readyState < current.HAVE_FUTURE_DATA) {
+ if (isPremounting && current.readyState < current.HAVE_FUTURE_DATA) {
if (!navigator.userAgent.includes('Firefox/')) {
playbackLogging({
logLevel,
@@ -59,6 +61,7 @@ export const useMediaBuffering = ({
}
}
+ // Don't trigger buffering during premount or postmount
return;
}
@@ -181,7 +184,7 @@ export const useMediaBuffering = ({
// it gives the chance to load the new source.
// https://github.com/remotion-dev/remotion/issues/5218
- }, [buffer, src, element, isPremounting, logLevel, shouldBuffer, mountTime]);
+ }, [buffer, src, element, isPremounting, isPostmounting, logLevel, shouldBuffer, mountTime]);
return isBuffering;
};
diff --git a/packages/core/src/use-media-in-timeline.ts b/packages/core/src/use-media-in-timeline.ts
index f5d87bb26b..74c888d863 100644
--- a/packages/core/src/use-media-in-timeline.ts
+++ b/packages/core/src/use-media-in-timeline.ts
@@ -37,8 +37,10 @@ export const useMediaInTimeline = ({
stack,
showInTimeline,
premountDisplay,
+ postmountDisplay,
onAutoPlayError,
isPremounting,
+ isPostmounting,
}: {
volume: VolumeProp | undefined;
mediaVolume: number;
@@ -51,8 +53,10 @@ export const useMediaInTimeline = ({
stack: string | null;
showInTimeline: boolean;
premountDisplay: number | null;
+ postmountDisplay: number | null;
onAutoPlayError: null | (() => void);
isPremounting: boolean;
+ isPostmounting: boolean;
}) => {
const videoConfig = useVideoConfig();
const {rootId, audioAndVideoTags} = useContext(TimelineContext);
@@ -137,6 +141,7 @@ export const useMediaInTimeline = ({
playbackRate,
stack,
premountDisplay,
+ postmountDisplay,
});
return () => {
unregisterSequence(id);
@@ -162,6 +167,7 @@ export const useMediaInTimeline = ({
stack,
showInTimeline,
premountDisplay,
+ postmountDisplay,
]);
useEffect(() => {
@@ -173,7 +179,7 @@ export const useMediaInTimeline = ({
return;
}
- if (isPremounting) {
+ if (isPremounting || isPostmounting) {
return;
}
@@ -202,6 +208,7 @@ export const useMediaInTimeline = ({
onAutoPlayError,
imperativePlaying,
isPremounting,
+ isPostmounting,
logLevel,
mountTime,
]);
diff --git a/packages/core/src/use-media-playback.ts b/packages/core/src/use-media-playback.ts
index 1f3d518181..0536982c6e 100644
--- a/packages/core/src/use-media-playback.ts
+++ b/packages/core/src/use-media-playback.ts
@@ -36,6 +36,7 @@ export const useMediaPlayback = ({
acceptableTimeshift,
pauseWhenBuffering,
isPremounting,
+ isPostmounting,
onAutoPlayError,
}: {
mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement | null>;
@@ -46,6 +47,7 @@ export const useMediaPlayback = ({
acceptableTimeshift: number | null;
pauseWhenBuffering: boolean;
isPremounting: boolean;
+ isPostmounting: boolean;
onAutoPlayError: null | (() => void);
}) => {
const {playbackRate: globalPlaybackRate} = useContext(TimelineContext);
@@ -106,6 +108,7 @@ export const useMediaPlayback = ({
element: mediaRef,
shouldBuffer: pauseWhenBuffering,
isPremounting,
+ isPostmounting,
logLevel,
mountTime,
src: src ?? null,
@@ -154,7 +157,7 @@ export const useMediaPlayback = ({
playbackLogging({
logLevel,
tag: 'pause',
- message: `Pausing ${mediaRef.current?.src} because ${isPremounting ? 'media is premounting' : 'Player is not playing'}`,
+ message: `Pausing ${mediaRef.current?.src} because ${isPremounting ? 'media is premounting' : isPostmounting ? 'media is postmounting' : 'Player is not playing'}`,
mountTime,
});
mediaRef.current?.pause();
@@ -246,7 +249,7 @@ export const useMediaPlayback = ({
mediaRef: mediaRef.current,
time: shouldBeTime,
logLevel,
- why: `because time shift is too big. shouldBeTime = ${shouldBeTime}, isTime = ${mediaTagTime}, requestVideoCallbackTime = ${rvcTime}, timeShift = ${timeShift}${isVariableFpsVideo ? ', isVariableFpsVideo = true' : ''}, isPremounting = ${isPremounting}, pauseWhenBuffering = ${pauseWhenBuffering}`,
+ why: `because time shift is too big. shouldBeTime = ${shouldBeTime}, isTime = ${mediaTagTime}, requestVideoCallbackTime = ${rvcTime}, timeShift = ${timeShift}${isVariableFpsVideo ? ', isVariableFpsVideo = true' : ''}, isPremounting = ${isPremounting}, isPostmounting = ${isPostmounting}, pauseWhenBuffering = ${pauseWhenBuffering}`,
mountTime,
});
lastSeekDueToShift.current = lastSeek.current;
@@ -357,6 +360,7 @@ export const useMediaPlayback = ({
src,
onAutoPlayError,
isPremounting,
+ isPostmounting,
pauseWhenBuffering,
mountTime,
mediaTagCurrentTime,
diff --git a/packages/core/src/video/VideoForPreview.tsx b/packages/core/src/video/VideoForPreview.tsx
index 7c5a4b5daf..4397bf8104 100644
--- a/packages/core/src/video/VideoForPreview.tsx
+++ b/packages/core/src/video/VideoForPreview.tsx
@@ -142,8 +142,10 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
stack: _remotionInternalStack,
showInTimeline,
premountDisplay: null,
+ postmountDisplay: null,
onAutoPlayError: onAutoPlayError ?? null,
isPremounting: Boolean(parentSequence?.premounting),
+ isPostmounting: Boolean(parentSequence?.postmounting),
});
// putting playback before useVolume
@@ -156,6 +158,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
onlyWarnForMediaSeekingError,
acceptableTimeshift: acceptableTimeShiftInSeconds ?? null,
isPremounting: Boolean(parentSequence?.premounting),
+ isPostmounting: Boolean(parentSequence?.postmounting),
pauseWhenBuffering,
onAutoPlayError: onAutoPlayError ?? null,
});