Skip to content

🎥 Make videos programmatically with React

player API

Jonny Burger576c1384059c
diff --git a/packages/docs/docs/player/api.md b/packages/docs/docs/player/api.md
index 553b2b0b86..62b6b28f09 100644
--- a/packages/docs/docs/player/api.md
+++ b/packages/docs/docs/player/api.md
@@ -1,6 +1,6 @@
---
-id: api
title: "<Player>"
+slug: /player/player
---
A component which can be rendered in a regular React App (for example: [Create React App](https://create-react-app.dev/), [Next.JS](https://nextjs.org)) to display a Remotion video.
@@ -176,7 +176,7 @@ _optional_
A callback function that allows you to return a custom UI that gets displayed while the player is loading.
-The first parameter contains the `height` and `width` of the player as it gets rendered.
+The first parameter of the callback function contains the `height` and `width` of the player as it gets rendered.
```tsx twoslash
import { Player, RenderLoading } from "@remotion/player";
diff --git a/packages/docs/docs/player/player.md b/packages/docs/docs/player/index.md
similarity index 94%
rename from packages/docs/docs/player/player.md
rename to packages/docs/docs/player/index.md
index 350382143b..c08171fab7 100644
--- a/packages/docs/docs/player/player.md
+++ b/packages/docs/docs/player/index.md
@@ -1,7 +1,5 @@
---
-id: player
title: "@remotion/player"
-slug: /player
---
import { PlayerExampleWithControls } from "../../components/PlayerExampleWithControls.tsx";
diff --git a/packages/docs/docs/player/thumbnail.md b/packages/docs/docs/player/thumbnail.md
index 6e99840cac..449fe0514f 100644
--- a/packages/docs/docs/player/thumbnail.md
+++ b/packages/docs/docs/player/thumbnail.md
@@ -18,15 +18,15 @@ import { MyVideo } from "./remotion/MyVideo";
export const App: React.FC = () => {
return (
<Thumbnail
- inputProps={{
- title: "Foo",
- }}
component={MyVideo}
compositionWidth={600}
compositionHeight={600}
frameToDisplay={30}
durationInFrames={120}
fps={30}
+ inputProps={{
+ title: "Foo",
+ }}
/>
);
};
@@ -63,6 +63,73 @@ To make it smaller, pass a `style` prop to give the Thumbnail a different width:
The height of the canvas in pixels.
The height you would like the video to have when rendered as an MP4. Use `style={{height: <height>}}` to define a height to be assumed in the browser.
+### `durationInFrames`
+
+The duration of the video in frames. Must be an integer and greater than 0.
+
+:::note
+This prop is required for the Thumbnail component because your component may render differently based on what `useVideoConfig()` returns.
+:::
+
+### `fps`
+
+The frame rate of the video. Must be a number.
+
+:::note
+This prop is required for the Thumbnail component because your component may render differently based on what `useVideoConfig()` returns.
+:::
+
+### `errorFallback`
+
+_optional_
+
+A callback for rendering a custom error message. See [Handling errors](#handling-errors) section for an example.
+
+### `renderLoading`
+
+_optional_
+
+A callback function that allows you to return a custom UI that gets displayed while the thumbnail is loading.
+
+The first parameter of the callback function contains the `height` and `width` of the thumbnail as it gets rendered.
+
+```tsx twoslash
+import { RenderLoading, Thumbnail } from "@remotion/player";
+import { useCallback } from "react";
+import { AbsoluteFill } from "remotion";
+
+const Component: React.FC = () => null;
+
+// ---cut---
+
+const MyApp: React.FC = () => {
+ // `RenderLoading` type can be imported from "@remotion/player"
+ const renderLoading: RenderLoading = useCallback(({ height, width }) => {
+ return (
+ <AbsoluteFill style={{ backgroundColor: "gray" }}>
+ Loading thumbnail ({height}x{width})
+ </AbsoluteFill>
+ );
+ }, []);
+
+ return (
+ <Thumbnail
+ fps={30}
+ component={Component}
+ durationInFrames={100}
+ compositionWidth={1080}
+ compositionHeight={1080}
+ frameToDisplay={30}
+ renderLoading={renderLoading}
+ />
+ );
+};
+```
+
+:::info
+A thumbnail needs to be loaded if it contains elements that use React Suspense, or if the `lazyComponent` prop is being used.
+:::
+
### `inputProps`
_optional_
@@ -73,23 +140,138 @@ Pass props to the component that you have specified using the `component` prop.
_optional_
-A regular `style` prop for a HTMLDivElement. You can pass a different height and width if you would like different dimensions for the player than the original composition dimensions.
+A regular `style` prop for a HTMLDivElement. You can pass a different height and width if you would like different dimensions for the thumbnail than the original composition dimensions.
-### `durationInFrames`
+### `className`
-The duration of the video in frames. Must be an integer and greater than 0.
+_optional - available since v3.1.3_
-:::note
-This prop is required for the Thumbnail component because your component may render differently based on what `useVideoConfig()` returns.
-:::
+A HTML class name to be applied to the container.
-### `fps`
+## ThumbnailRef
-The frame rate of the video. Must be a number.
+You may attach a ref to the thumbnail and get some layout info.
-:::note
-This prop is required for the Thumbnail component because your component may render differently based on what `useVideoConfig()` returns.
-:::
+```tsx twoslash {15}
+// @allowUmdGlobalAccess
+
+// @filename: MyComposition.tsx
+export const MyComposition: React.FC = () => null;
+
+// @filename: index.tsx
+// ---cut---
+import { Thumbnail, ThumbnailRef } from "@remotion/player";
+import { useEffect, useRef } from "react";
+import { MyComposition } from "./MyComposition";
+
+const MyComp: React.FC = () => {
+ const thumbnailRef = useRef<ThumbnailRef>(null);
+
+ useEffect(() => {
+ if (thumbnailRef.current) {
+ console.log(thumbnailRef.current.getScale());
+ }
+ }, []);
+
+ return (
+ <Thumbnail
+ ref={thumbnailRef}
+ durationInFrames={30}
+ compositionWidth={1080}
+ compositionHeight={1080}
+ fps={30}
+ frameToDisplay={30}
+ component={MyComposition}
+ // Many other optional props are available.
+ />
+ );
+};
+```
+
+The following methods are available on the thumbnail ref:
+
+### `getContainerNode()`
+
+Gets the container `HTMLDivElement` of the thumbnail. Useful if you'd like to manually attach listeners to the thumbnail element.
+
+```tsx twoslash
+import { ThumbnailRef } from "@remotion/player";
+import { useEffect, useRef } from "react";
+// ---cut---
+const thumbnailRef = useRef<ThumbnailRef>(null);
+
+useEffect(() => {
+ if (!thumbnailRef.current) {
+ return;
+ }
+ const container = thumbnailRef.current.getContainerNode();
+ if (!container) {
+ return;
+ }
+
+ const onClick = () => {
+ console.log("thumbnail got clicked");
+ };
+
+ container.addEventListener("click", onClick);
+ return () => {
+ container.removeEventListener("click", onClick);
+ };
+}, []);
+```
+
+### `getScale()`
+
+Returns a number which says how much the content is scaled down compared to the natural composition size. For example, if the composition is `1920x1080`, but the thumbnail is 960px in width, this method would return `0.5`.
+
+### `addEventListener()`
+
+Start listening to an event. See the [Events](#events) section to see the function signature and the available events.
+
+### `removeEventListener()`
+
+Stop listening to an event. See the [Events](#events) section to see the function signature and the available events.
+
+## Events
+
+Using a [thumbnail ref](#thumbnailref), you can bind event listeners to get notified of certain events of the thumbnail.
+
+```tsx twoslash
+import { ThumbnailRef } from "@remotion/player";
+import { useEffect, useRef } from "react";
+// ---cut---
+const thumbnailRef = useRef<ThumbnailRef>(null);
+
+useEffect(() => {
+ if (!thumbnailRef.current) {
+ return;
+ }
+
+ thumbnailRef.current.addEventListener("error", (e) => {
+ console.log("error", e.detail.error);
+ });
+}, []);
+```
+
+### `error`
+
+Fires when an error or uncaught exception has happened in the thumbnail.
+
+You may get the error by reading the `e.detail.error` value:
+
+```tsx twoslash
+import { ThumbnailRef } from "@remotion/player";
+import { useRef } from "react";
+const ref = useRef<ThumbnailRef>(null);
+// ---cut---
+ref.current?.addEventListener("error", (e) => {
+ console.log("error ", e.detail.error); // error [Error: undefined is not a function]
+});
+```
+
+## Handling errors
+
+See: [`<Player>` -> Handling errors](/docs/player/api#handling-errors)
## See also
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 1f886abf77..a443496dc3 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -318,7 +318,7 @@ module.exports = {
type: "category",
link: {
type: "doc",
- id: "player/player",
+ id: "player/index",
},
label: "Player",
items: [
diff --git a/packages/player/src/index.tsx b/packages/player/src/index.tsx
index 0ec60fc3d4..3af83bf81b 100644
--- a/packages/player/src/index.tsx
+++ b/packages/player/src/index.tsx
@@ -8,7 +8,12 @@ import {usePlayer} from './use-player';
import {updateAllElementsSizes, useElementSize} from './utils/use-element-size';
export {Player, PlayerProps} from './Player';
-export {PlayerMethods, PlayerRef} from './player-methods';
+export {
+ PlayerMethods,
+ PlayerRef,
+ ThumbnailMethods,
+ ThumbnailRef,
+} from './player-methods';
export type {
RenderFullscreenButton,
RenderPlayPauseButton,
diff --git a/packages/player/src/player-methods.ts b/packages/player/src/player-methods.ts
index 1cde198edb..621f2ce4ba 100644
--- a/packages/player/src/player-methods.ts
+++ b/packages/player/src/player-methods.ts
@@ -23,4 +23,5 @@ export type PlayerMethods = ThumbnailMethods & {
unmute: () => void;
};
+export type ThumbnailRef = PlayerEmitter & ThumbnailMethods;
export type PlayerRef = PlayerEmitter & PlayerMethods;
diff --git a/vercel.json b/vercel.json
index 40867b42c6..afc741355c 100644
--- a/vercel.json
+++ b/vercel.json
@@ -109,6 +109,11 @@
"source": "/support",
"destination": "/docs/support",
"permanent": true
+ },
+ {
+ "source": "/docs/player/api",
+ "destination": "/docs/player/player",
+ "permanent": true
}
]
}