Skip to content

🎥 Make videos programmatically with React

player doc

Jonny Burger24299939b778
diff --git a/packages/docs/components/Experimental.tsx b/packages/docs/components/Experimental.tsx
new file mode 100644
index 0000000000..2fcfe5d5c2
--- /dev/null
+++ b/packages/docs/components/Experimental.tsx
@@ -0,0 +1,33 @@
+import React from "react";
+
+export const ExperimentalBadge: React.FC = () => {
+ return (
+ <div
+ style={{
+ backgroundColor: "rgba(246, 229, 141,0.3)",
+ borderRadius: 8,
+ padding: "12px 12px",
+ marginBottom: 24,
+ }}
+ >
+ <div
+ style={{
+ backgroundColor: "#f6e58d",
+ display: "inline-block",
+ padding: "3px 10px",
+ fontWeight: "bold",
+ borderRadius: 5,
+ fontSize: 14,
+ marginBottom: 10,
+ color: "black",
+ }}
+ >
+ EXPERIMENTAL
+ </div>
+ <div>
+ This component may change in minor version updates. Watch this
+ documentation to see breaking changes when upgrading.
+ </div>
+ </div>
+ );
+};
diff --git a/packages/docs/components/Player.tsx b/packages/docs/components/Player.tsx
new file mode 100644
index 0000000000..0ae9cd75a0
--- /dev/null
+++ b/packages/docs/components/Player.tsx
@@ -0,0 +1,22 @@
+import { Player } from "@remotion/player";
+import React from "react";
+import { useCurrentFrame } from "../../babel-loader/node_modules/remotion/dist";
+
+const Comp: React.FC = () => {
+ const frame = useCurrentFrame();
+ return <div style={{ fontSize: 200 }}>{frame}</div>;
+};
+
+export const PlayerExample: React.FC = () => {
+ return (
+ <Player
+ component={Comp}
+ width={900}
+ height={500}
+ controls
+ durationInFrames={100}
+ fps={30}
+ loop
+ ></Player>
+ );
+};
diff --git a/packages/docs/docs/config.md b/packages/docs/docs/config.mdx
similarity index 100%
rename from packages/docs/docs/config.md
rename to packages/docs/docs/config.mdx
diff --git a/packages/docs/docs/player.md b/packages/docs/docs/player.md
new file mode 100644
index 0000000000..f8d61d91f1
--- /dev/null
+++ b/packages/docs/docs/player.md
@@ -0,0 +1,229 @@
+---
+id: player
+title: <Player />
+---
+
+import Tabs from '@theme/Tabs';
+import TabItem from '@theme/TabItem';
+import { PlayerExample } from "../components/Player.tsx";
+import { ExperimentalBadge } from "../components/Experimental.tsx";
+
+<ExperimentalBadge/>
+
+Using the Remotion Player you can embed Remotion videos in any React app and customize the video content at runtime.
+
+## Installation
+
+To install the player, run the following command in a React project:
+
+<Tabs
+defaultValue="npm"
+values={[
+{ label: 'npm', value: 'npm', },
+{ label: 'yarn', value: 'yarn', },
+]
+}>
+<TabItem value="npm">
+
+```bash
+npm i @remotion/player
+```
+
+ </TabItem>
+
+ <TabItem value="yarn">
+
+```bash
+yarn add @remotion/player
+```
+
+ </TabItem>
+</Tabs>
+
+## `<Player />`
+
+A react component which takes the following props
+
+### `component` or `lazyComponent`
+
+Pass a React component in directly **or** pass a function that returns a dynamic import. Passing neither or both of the props is an error.
+
+### `durationInFrames`
+
+The duration of the video in frames. Must be an integer and greater than 0.
+
+### `fps`
+
+The frame rate of the video.
+
+### `width`
+
+The width of the composition in pixels.
+
+### `height`
+
+The height of the composition in pixels.
+
+### `loop`
+
+_optional_
+
+Whether the video should restart when it ends. Default `false`.
+
+### `autoPlay`
+
+_optional_
+
+Whether the video should start immediately after loaded. Default `false`.
+
+### `controls`
+
+_optional_
+
+Whether the video should display a seek bar and a play/pause button. Default `false.`
+
+## `PlayerRef`
+
+You may attach a ref to the player and control it in an imperative manner.
+
+```tsx {14}
+import {Player, PlayerRef} from '@remotion/player';
+
+const MyComp: React.FC = () => {
+ const playerRef = useRef<PlayerRef>(null);
+
+ useEffect(() => {
+ if (playerRef.current) {
+ console.log(playerRef.current.getCurrentFrame());
+ }
+ }, []);
+
+ return (
+ <Player
+ ref={playerRef}
+ // other props
+ />
+ );
+}
+```
+
+The following methods are available on the player ref:
+
+### `pause()`
+
+Pause the video. Nothing happens if the video is already not playing.
+
+### `play()`
+
+Play the video. Nothing happens if the video is already playing.
+
+### `toggle()`
+
+Pauses the video if it's playing. Plays the video if it's paused.
+
+### `getCurrentFrame()`
+
+Gets the current postition expressed as the current frame. Divide by the `fps` you passed to get the time in seconds.
+
+### `seekTo()`
+
+#### Arguments
+
+- `frame`: `number`
+
+Move the position in the video to the frame that you specify. If the video is playing, it will pause for a brief moment, then start playing again after the seek is completed.
+
+### `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 [player ref](#playerref), you can bind event listeners to get notified of certain events of the player.
+
+```tsx
+const ref = useRef<PlayerRef>(null);
+
+useEffect(() => {
+ if (!ref.current) {
+ return null;
+ }
+ ref.current.addEventListener('play', () => {
+ console.log('playing');
+ });
+ ref.current.addEventListener('pause', () => {
+ console.log('pausing');
+ });
+ ref.current.addEventListener('seeked', (e) => {
+ console.log('seeked to ' + e.detail.frame);
+ });
+ ref.current.addEventListener('ended', (e) => {
+ console.log('ended');
+ });
+ ref.current.addEventListener('error', (e) => {
+ console.log('error', e.detail.error);
+ });
+}, []);
+```
+
+### `seeked`
+
+Fired when the time position changes. You may get the current frame by reading it from `e.detail.frame`.
+
+```tsx
+ref.current.addEventListener('seeked', (e) => {
+ console.log('seeked to ' + e.detail.frame); // seeked to 120
+});
+```
+
+This event fires on every single frame. If you link it to a state update, you may want to throttle the updates to avoid over-rendering.
+
+### `ended`
+
+Fires when the video has ended and looping is disabled.
+
+### `play`
+
+Fires when the video has started playing or has resumed from a pause.
+
+### `pause`
+
+Fires when the video has paused or ended.
+
+### `error`
+
+Fires when an error or uncaught exception has happened in the video.
+
+You may get the error by reading the `e.detail.error` value:
+
+```tsx
+ref.current.addEventListener('error', (e) => {
+ console.log('error ', e.detail.error); // error [Error: undefined is not a function]
+});
+```
+
+## Handling errors
+
+Since videos are written in React, they are prone to crashing.
+When a video throws an exception, you may handle the error using the [`error` event](#error).
+The video will unmount and show an error UI, but not the whole app website will crash.
+It is up to you to handle the error and to re-mount the video (for example by changing the `key` prop in React).
+
+This feature is implemented using an [error boundary](https://reactjs.org/docs/error-boundaries.html), so only errors in the render function will be caught. Errors in event handlers and asynchronous code will not be reported and will not cause the video to unmount.
+
+## Known issues
+
+Before we mark the player as stable, we are looking to improve in the following areas:
+
+- Allow the player size to be a different size than the composition size.
+- Better loading state than the current "Loading..." text.
+- Implement keyboard controls.
+- Fullscreen support.
+- Better props validation
+- Customize error UI
+
+<PlayerExample />
diff --git a/packages/docs/package-lock.json b/packages/docs/package-lock.json
index fb4968b9bc..a474f63af9 100644
--- a/packages/docs/package-lock.json
+++ b/packages/docs/package-lock.json
@@ -11,9 +11,7 @@
"@splitbee/web": "^0.2.4",
"@types/react": "^17.0.4",
"clsx": "^1.1.1",
- "prism-react-renderer": "^1.1.1",
- "react": "^16.8.4",
- "react-dom": "^16.8.4"
+ "prism-react-renderer": "^1.1.1"
}
},
"node_modules/@algolia/autocomplete-core": {
@@ -9317,19 +9315,6 @@
"rc": "cli.js"
}
},
- "node_modules/react": {
- "version": "16.14.0",
- "resolved": "https://registry.npmjs.org/react/-/react-16.14.0.tgz",
- "integrity": "sha512-0X2CImDkJGApiAlcf0ODKIneSwBPhqJawOa5wCtKbu7ZECrmS26NvtSILynQ66cgkT/RJ4LidJOc3bUESwmU8g==",
- "dependencies": {
- "loose-envify": "^1.1.0",
- "object-assign": "^4.1.1",
- "prop-types": "^15.6.2"
- },
- "engines": {
- "node": ">=0.10.0"
- }
- },
"node_modules/react-base16-styling": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/react-base16-styling/-/react-base16-styling-0.6.0.tgz",
@@ -9486,17 +9471,6 @@
"node": ">= 6"
}
},
- "node_modules/react-dom": {
- "version": "16.14.0",
- "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-16.14.0.tgz",
- "integrity": "sha512-1gCeQXDLoIqMgqD3IO2Ah9bnf0w9kzhwN5q4FGnHZ67hBm9yePzB5JJAIQCc8x3pFnNlwFq4RidZggNAAkzWWw==",
- "dependencies": {
- "loose-envify": "^1.1.0",
- "object-assign": "^4.1.1",
- "prop-types": "^15.6.2",
- "scheduler": "^0.19.1"
- }
- },
"node_modules/react-error-overlay": {
"version": "6.0.9",
"resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.9.tgz",
@@ -10356,15 +10330,6 @@
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
"integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
},
- "node_modules/scheduler": {
- "version": "0.19.1",
- "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.19.1.tgz",
- "integrity": "sha512-n/zwRWRYSUj0/3g/otKDRPMh6qv2SYMWNq85IEa8iZyAv8od9zDYpGSnpBEjNgcMNq6Scbu5KfIPxNF72R/2EA==",
- "dependencies": {
- "loose-envify": "^1.1.0",
- "object-assign": "^4.1.1"
- }
- },
"node_modules/schema-utils": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.0.0.tgz",
@@ -20860,16 +20825,6 @@
"strip-json-comments": "~2.0.1"
}
},
- "react": {
- "version": "16.14.0",
- "resolved": "https://registry.npmjs.org/react/-/react-16.14.0.tgz",
- "integrity": "sha512-0X2CImDkJGApiAlcf0ODKIneSwBPhqJawOa5wCtKbu7ZECrmS26NvtSILynQ66cgkT/RJ4LidJOc3bUESwmU8g==",
- "requires": {
- "loose-envify": "^1.1.0",
- "object-assign": "^4.1.1",
- "prop-types": "^15.6.2"
- }
- },
"react-base16-styling": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/react-base16-styling/-/react-base16-styling-0.6.0.tgz",
@@ -20999,17 +20954,6 @@
}
}
},
- "react-dom": {
- "version": "16.14.0",
- "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-16.14.0.tgz",
- "integrity": "sha512-1gCeQXDLoIqMgqD3IO2Ah9bnf0w9kzhwN5q4FGnHZ67hBm9yePzB5JJAIQCc8x3pFnNlwFq4RidZggNAAkzWWw==",
- "requires": {
- "loose-envify": "^1.1.0",
- "object-assign": "^4.1.1",
- "prop-types": "^15.6.2",
- "scheduler": "^0.19.1"
- }
- },
"react-error-overlay": {
"version": "6.0.9",
"resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.9.tgz",
@@ -21767,15 +21711,6 @@
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
"integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
},
- "scheduler": {
- "version": "0.19.1",
- "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.19.1.tgz",
- "integrity": "sha512-n/zwRWRYSUj0/3g/otKDRPMh6qv2SYMWNq85IEa8iZyAv8od9zDYpGSnpBEjNgcMNq6Scbu5KfIPxNF72R/2EA==",
- "requires": {
- "loose-envify": "^1.1.0",
- "object-assign": "^4.1.1"
- }
- },
"schema-utils": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.0.0.tgz",
diff --git a/packages/docs/package.json b/packages/docs/package.json
index 2f084fc5f7..5ab46200f6 100644
--- a/packages/docs/package.json
+++ b/packages/docs/package.json
@@ -15,12 +15,11 @@
"@docusaurus/core": "2.0.0-alpha.75",
"@docusaurus/preset-classic": "2.0.0-alpha.75",
"@mdx-js/react": "^1.6.21",
+ "@remotion/player": "^2.0.7",
"@splitbee/web": "^0.2.4",
"@types/react": "^17.0.4",
"clsx": "^1.1.1",
- "prism-react-renderer": "^1.1.1",
- "react": "^16.8.4",
- "react-dom": "^16.8.4"
+ "prism-react-renderer": "^1.1.1"
},
"browserslist": {
"production": [
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 6a973333e2..597e3fe0ac 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -79,6 +79,7 @@ module.exports = {
"visualize-audio",
],
},
+ "player",
{
type: "category",
label: "API - @remotion/renderer",
diff --git a/packages/player/src/use-element-size.ts b/packages/player/src/use-element-size.ts
index 84648dda37..2c17bb2bee 100644
--- a/packages/player/src/use-element-size.ts
+++ b/packages/player/src/use-element-size.ts
@@ -15,6 +15,11 @@ export const useElementSize = (
() =>
new ResizeObserver((entries) => {
const newSize = entries[0].target.getClientRects();
+ if (!newSize) {
+ setSize(null);
+ return;
+ }
+
setSize({
width: newSize[0].width,
height: newSize[0].height,
@@ -31,6 +36,7 @@ export const useElementSize = (
const rect = ref.current.getClientRects();
if (!rect[0]) {
+ setSize(null);
return;
}