Skip to content

🎥 Make videos programmatically with React

write a doc page

Jonny Burger3bdc0503485a
diff --git a/packages/docs/components/Premounting/NoPremounting.tsx b/packages/docs/components/Premounting/NoPremounting.tsx
new file mode 100644
index 0000000000..679c9e2178
--- /dev/null
+++ b/packages/docs/components/Premounting/NoPremounting.tsx
@@ -0,0 +1,52 @@
+import React from "react";
+
+export const NoPremounting: React.FC = () => {
+ return (
+ <svg
+ width="414"
+ height="142"
+ style={{
+ maxWidth: "100%",
+ }}
+ viewBox="0 0 414 142"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <rect
+ x="1.5"
+ y="39.5"
+ width="204"
+ height="37"
+ rx="7.5"
+ fill="#0B84F3"
+ stroke="black"
+ strokeWidth="3"
+ />
+ <rect
+ x="208.5"
+ y="79.5"
+ width="204"
+ height="37"
+ rx="7.5"
+ fill="#9B59B6"
+ stroke="black"
+ strokeWidth="3"
+ />
+ <path
+ d="M217 92.8333C217 91.2706 218.295 90 219.889 90H231.444C233.038 90 234.333 91.2706 234.333 92.8333V104.167C234.333 105.729 233.038 107 231.444 107H219.889C218.295 107 217 105.729 217 104.167V92.8333ZM242.237 91.5849C242.707 91.8328 243 92.3109 243 92.8333V104.167C243 104.689 242.707 105.167 242.237 105.415C241.768 105.663 241.199 105.636 240.752 105.344L236.419 102.511L235.778 102.09V101.333V95.6667V94.9096L236.419 94.4891L240.752 91.6557C241.194 91.368 241.763 91.337 242.237 91.5849Z"
+ fill="white"
+ />
+ <path
+ d="M185.554 25.4949L186.358 142"
+ stroke="#F02C00"
+ strokeWidth="3.27837"
+ />
+ <path
+ d="M198.463 21.1739L187.572 29.0151C186.885 29.5099 185.965 29.5342 185.252 29.0762L172.947 21.1655C172.357 20.7861 172 20.1327 172 19.4311V4.06187C172 2.92313 172.923 2 174.062 2H197.258C198.397 2 199.32 2.92313 199.32 4.06187V19.5006C199.32 20.1638 199.001 20.7864 198.463 21.1739Z"
+ fill="#F02C00"
+ stroke="#F02C00"
+ strokeWidth="2.06187"
+ />
+ </svg>
+ );
+};
diff --git a/packages/docs/components/Premounting/Premounting.tsx b/packages/docs/components/Premounting/Premounting.tsx
new file mode 100644
index 0000000000..dafa487c69
--- /dev/null
+++ b/packages/docs/components/Premounting/Premounting.tsx
@@ -0,0 +1,69 @@
+import React from "react";
+
+export const Premounting: React.FC = () => {
+ return (
+ <svg
+ width="414"
+ height="142"
+ style={{
+ maxWidth: "100%",
+ }}
+ viewBox="0 0 414 142"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <rect
+ x="1.5"
+ y="39.5"
+ width="204"
+ height="37"
+ rx="7.5"
+ fill="#0B84F3"
+ stroke="black"
+ strokeWidth="3"
+ />
+ <rect
+ x="149.5"
+ y="82.5"
+ width="263"
+ height="37"
+ rx="7.5"
+ fill="#9B59B6"
+ stroke="black"
+ strokeWidth="3"
+ />
+ <path
+ d="M148 90C148 85.0294 152.029 81 157 81H203V121H157C152.029 121 148 116.971 148 112V90Z"
+ fill="#C8A0D8"
+ />
+ <path
+ d="M148 90C148 85.0294 152.029 81 157 81H203V121H157C152.029 121 148 116.971 148 112V90Z"
+ stroke="black"
+ />
+ <rect
+ x="149.5"
+ y="82.5"
+ width="263"
+ height="37"
+ rx="7.5"
+ stroke="black"
+ strokeWidth="3"
+ />
+ <path
+ d="M217 95.8333C217 94.2706 218.295 93 219.889 93H231.444C233.038 93 234.333 94.2706 234.333 95.8333V107.167C234.333 108.729 233.038 110 231.444 110H219.889C218.295 110 217 108.729 217 107.167V95.8333ZM242.237 94.5849C242.707 94.8328 243 95.3109 243 95.8333V107.167C243 107.689 242.707 108.167 242.237 108.415C241.768 108.663 241.199 108.636 240.752 108.344L236.419 105.511L235.778 105.09V104.333V98.6667V97.9096L236.419 97.4891L240.752 94.6557C241.194 94.368 241.763 94.337 242.237 94.5849Z"
+ fill="white"
+ />
+ <path
+ d="M185.554 25.4949L186.358 142"
+ stroke="#F02C00"
+ strokeWidth="3.27837"
+ />
+ <path
+ d="M198.463 21.1739L187.572 29.0151C186.885 29.5099 185.965 29.5342 185.252 29.0762L172.947 21.1655C172.357 20.7861 172 20.1327 172 19.4311V4.06187C172 2.92313 172.923 2 174.062 2H197.258C198.397 2 199.32 2.92313 199.32 4.06187V19.5006C199.32 20.1638 199.001 20.7864 198.463 21.1739Z"
+ fill="#F02C00"
+ stroke="#F02C00"
+ strokeWidth="2.06187"
+ />
+ </svg>
+ );
+};
diff --git a/packages/docs/components/TableOfContents/player.tsx b/packages/docs/components/TableOfContents/player.tsx
index 516fcfbea1..91d4fb769c 100644
--- a/packages/docs/components/TableOfContents/player.tsx
+++ b/packages/docs/components/TableOfContents/player.tsx
@@ -59,6 +59,10 @@ export const PlayerGuide: React.FC = () => {
<strong>Avoiding flickers</strong>
<div>Troubleshooting for flickers due to unloaded assets</div>
</TOCItem>
+ <TOCItem link="/docs/troubleshooting/player-flicker">
+ <strong>Premounting</strong>
+ <div>Mount components earlier to allow them to load</div>
+ </TOCItem>
</Grid>
</div>
);
diff --git a/packages/docs/docs/player/premounting.mdx b/packages/docs/docs/player/premounting.mdx
new file mode 100644
index 0000000000..62a667c0eb
--- /dev/null
+++ b/packages/docs/docs/player/premounting.mdx
@@ -0,0 +1,157 @@
+---
+title: Premounting
+slug: /player/premounting
+crumb: "@remotion/player"
+---
+
+import { NoPremounting } from "../../components/Premounting/NoPremounting";
+import { Premounting } from "../../components/Premounting/Premounting";
+
+# Premounting<AvailableFrom v="4.0.140"/>
+
+Remotion only ever renders the current frame.
+This means when a video or another asset is about to come up, it is not loaded by default.
+
+<NoPremounting />
+<br />
+<p style={{ lineHeight: 1.2 }}>
+ <sub>
+ <em>
+ Even though the video is soon about to appear, it is not yet loaded
+ because Remotion only ever renders the current time.
+ </em>
+ </sub>
+</p>
+
+## What is premounting?
+
+Premounting is the practice of mounting a component with assets to load earlier to prepare the scene for entrance.
+
+<Premounting />
+<br />
+<p style={{ lineHeight: 1.2 }}>
+ <sub>
+ <em>
+ The video is mounted earlier to give it some time to load.
+ <br />
+ It carries the style <code>opacity: 0</code> to make it invisible.
+ </em>
+ </sub>
+</p>
+
+## Preloading a `<Sequence>`
+
+Use premounting sparingly since having more elements mounted will slow down the page.
+Add the `premountFor` prop to the `<Sequence>` component to enable premounting.
+
+The number you pass is the number in frames you premount the component for.
+
+```tsx twoslash title="Premounted.tsx"
+import { Sequence, staticFile, Video } from "remotion";
+
+// ---cut---
+const MyComp: React.FC = () => {
+ return (
+ <Sequence premountFor={100}>
+ <Video src={staticFile("bigbuckbunny.mp4")}></Video>
+ </Sequence>
+ );
+};
+```
+
+In the [Remotion Studio](/docs/studio), a premounted component is indicated with diagonal stripes:
+
+<img src="/img/premount.png" />
+<br />
+<br />
+
+A premounted [`<Sequence>`](/docs/sequence) does carry the styles `opacity: 0` and `pointer-events: none`.
+
+It is not possible to premount a [`<Sequence>`](/docs/sequence) with `layout="none"` because such a sequence does not have a container to apply the styles to.
+
+## With `<Series>` and `<TransitionSeries>`
+
+`premountFor` is also available on [`<Series.Sequence>`](/docs/series) and [`<TransitionSeries.Sequence>`](/docs/transitions/transitionseries).
+It is not yet possible to premount a whole [`<Series>`](/docs/series) or [`<TransitionSeries>`](/docs/transitions/transitionseries).
+
+## Custom premount component
+
+Premounting can also be implemented with the public Remotion APIs.
+Use the following component like you would `<Sequence>`:
+
+```tsx twoslash title="PremountedSequence.tsx"
+import React, { forwardRef, useMemo } from "react";
+import {
+ Freeze,
+ getRemotionEnvironment,
+ Sequence,
+ SequenceProps,
+ useCurrentFrame,
+} from "remotion";
+
+export type PremountedSequenceProps = SequenceProps & {
+ premountFor: number;
+};
+
+const PremountedSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
+ HTMLDivElement,
+ {
+ premountFor: number;
+ } & SequenceProps
+> = ({ premountFor, ...props }, ref) => {
+ const frame = useCurrentFrame();
+
+ if (props.layout === "none") {
+ throw new Error('`<Premount>` does not support layout="none"');
+ }
+
+ const { style: passedStyle, from = 0, ...otherProps } = props;
+ const active =
+ frame < from &&
+ frame >= from - premountFor &&
+ !getRemotionEnvironment().isRendering;
+
+ const style: React.CSSProperties = useMemo(() => {
+ return {
+ ...passedStyle,
+ opacity: active ? 0 : 1,
+ // @ts-expect-error Only in the docs - it will not give a type error in a Remotion project
+ pointerEvents: active ? "none" : passedStyle?.pointerEvents ?? "auto",
+ };
+ }, [active, passedStyle]);
+
+ return (
+ <Freeze frame={from} active={active}>
+ <Sequence
+ ref={ref}
+ name={`<PremountedSequence premountFor={${premountFor}}>`}
+ from={from}
+ style={style}
+ {...otherProps}
+ />
+ </Freeze>
+ );
+};
+
+export const PremountedSequence = forwardRef(
+ PremountedSequenceRefForwardingFunction,
+);
+```
+
+:::note
+One caveat of this custom component: In a premounted sequence, the native buffer state can still be triggered.
+It is your task to disable triggering the native buffer state (e.g set [`pauseWhenBuffering`](/docs/offthreadvideo#pausewhenbuffering) to false) when in a premounted sequence.
+You can achieve this for example by using a React context.
+:::
+
+## Usage together with the buffer state
+
+If you also use the [buffer state](/docs/player/buffer-state), the [`<Audio>`](/docs/audio#pausewhenbuffering), [`<Video>`](/docs/video#pausewhenbuffering), [`<OffthreadVideo>`](/docs/offthreadvideo#pausewhenloading) and [`<Img>`](/docs/img#pausewhenloading) tags are aware of premounting and don't trigger the buffer state while in a premounted [`<Sequence>`](/docs/sequence).
+
+Otherwise it would lead to pausing playback while a scene is not even visible yet.
+
+## See also
+
+- [Buffer state](/docs/player/buffer-state)
+- [Preloading](/docs/player/preloading)
+- [Avoiding flickering](/docs/troubleshooting/player-flicker)
diff --git a/packages/docs/docs/troubleshooting/video-flicker.md b/packages/docs/docs/troubleshooting/video-flicker.md
index 415d3989c7..d1af92f8f7 100644
--- a/packages/docs/docs/troubleshooting/video-flicker.md
+++ b/packages/docs/docs/troubleshooting/video-flicker.md
@@ -236,3 +236,4 @@ You are responsible yourself for making the video invisible while it is being pr
- [`@remotion/preload` vs `prefetch()`](/docs/player/preloading#remotionpreload-vs-prefetch)
- [`preloadVideo()`](/docs/preload/preload-video)
- [Flickering (during rendering)](/docs/flickering)
+- [Premounting](/docs/player/premounting)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index b569200e80..e806061c75 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -618,10 +618,11 @@ module.exports = {
"player/integration",
"player/autoplay",
"player/current-time",
+ "troubleshooting/player-flicker",
+ "player/buffer-state",
"player/preloading",
+ "player/premounting",
"player/best-practices",
- "player/buffer-state",
- "troubleshooting/player-flicker",
],
},
{
diff --git a/packages/docs/static/img/premount.png b/packages/docs/static/img/premount.png
new file mode 100644
index 0000000000..b487b7ddb8
Binary files /dev/null and b/packages/docs/static/img/premount.png differ