Skip to content

🎥 Make videos programmatically with React

video player

Jonny Burgerb9eea748a75e
diff --git a/packages/docs/learn/2022-11-18-apple-fireworks.md b/packages/docs/learn/2022-11-18-apple-fireworks.md
index 783593f2a2..cfb5078402 100644
--- a/packages/docs/learn/2022-11-18-apple-fireworks.md
+++ b/packages/docs/learn/2022-11-18-apple-fireworks.md
@@ -14,11 +14,17 @@ authors:
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
+import {AppleFireworksTutorial} from '../src/components/AppleFireworksTutorial';
Learn how to create an animation that Apple has on its website during the holiday season with this beginner-friendly Remotion tutorial!
-
<img src="/img/apple-wow-tutorial/Final.gif"/>
+## Video version
+
+This tutorial is also available as a video version:
+
+<AppleFireworksTutorial />
+
## Getting started
Start a new Remotion project and select the blank template:
diff --git a/packages/docs/package.json b/packages/docs/package.json
index f1e4d287a0..31aac31160 100644
--- a/packages/docs/package.json
+++ b/packages/docs/package.json
@@ -56,8 +56,9 @@
"lottie-web": "^5.9.6",
"polished": "4.1.2",
"react": "18.0.0",
+ "plyr": "^3.7.3",
+ "mux-embed": "^4.16.0",
"react-dom": "18.0.0",
- "react-player": "^2.9.0",
"remotion": "3.3.15",
"three": "0.145.0",
"uuid": "^8.3.2",
diff --git a/packages/docs/src/components/AppleFireworksTutorial.tsx b/packages/docs/src/components/AppleFireworksTutorial.tsx
new file mode 100644
index 0000000000..441394c4aa
--- /dev/null
+++ b/packages/docs/src/components/AppleFireworksTutorial.tsx
@@ -0,0 +1,16 @@
+import React from "react";
+import { VideoPlayerWithControls } from "./VideoPlayerWithControls";
+
+export const AppleFireworksTutorial = () => {
+ return (
+ <VideoPlayerWithControls
+ playbackId="RDHz028sflhaMtuCm1ogQTdtq02aLTspWjLrnEIlDS1ZM"
+ poster="https://image.mux.com/RDHz028sflhaMtuCm1ogQTdtq02aLTspWjLrnEIlDS1ZM/thumbnail.png"
+ onError={(error) => {
+ console.log(error);
+ }}
+ onLoaded={() => undefined}
+ onSize={() => undefined}
+ />
+ );
+};
diff --git a/packages/docs/src/components/VideoPlayerWithControls.tsx b/packages/docs/src/components/VideoPlayerWithControls.tsx
new file mode 100644
index 0000000000..024ec3fe8a
--- /dev/null
+++ b/packages/docs/src/components/VideoPlayerWithControls.tsx
@@ -0,0 +1,180 @@
+import Hls from "hls.js";
+import Plyr from "plyr";
+import "plyr/dist/plyr.css";
+import type { MutableRefObject } from "react";
+import React, {
+ forwardRef,
+ useCallback,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
+import "./video-player.css";
+
+export interface HTMLVideoElementWithPlyr extends HTMLVideoElement {
+ plyr: Plyr;
+}
+
+const useCombinedRefs = function (
+ ...refs: (
+ | ((instance: HTMLVideoElementWithPlyr | null) => void)
+ | MutableRefObject<HTMLVideoElementWithPlyr | null>
+ | null
+ )[]
+): MutableRefObject<HTMLVideoElementWithPlyr | null> {
+ const targetRef = useRef(null);
+
+ useEffect(() => {
+ refs.forEach((ref) => {
+ if (!ref) return;
+
+ if (typeof ref === "function") {
+ ref(targetRef.current);
+ } else {
+ ref.current = targetRef.current;
+ }
+ });
+ }, [refs]);
+
+ return targetRef;
+};
+
+/*
+ * We need to set the width/height of the player depending on what the dimensions of
+ * the underlying video source is.
+ *
+ * On most platforms we know the dimensions on 'loadedmetadata'
+ * On Desktop Safari we don't know the dimensions until 'canplay'
+ *
+ * At first, I tried to get the dimensions of the video from these callbacks, that worked
+ * great except for on moble Safari. On Mobile Safari none of those callbacks fire until
+ * there is some user interaction :(
+ *
+ * BUT! There is a brilliant hack here. We can create a `display: none` `img` element in the
+ * DOM, load up the poster image.
+ *
+ * Since the poster image will have the same dimensions of the video, now we know if the video
+ * is vertical and now we can style the proper width/height so the layout doesn't have a sudden
+ * jump or resize.
+ *
+ */
+
+type Props = {
+ playbackId: string;
+ poster: string;
+ // eslint-disable-next-line react/require-default-props
+ currentTime?: number;
+ onLoaded: () => void;
+ onError: (error: ErrorEvent) => void;
+ onSize: (dim: { width: number; height: number }) => void;
+};
+
+type SizedEvent = {
+ target: {
+ width: number;
+ height: number;
+ };
+};
+
+export const VideoPlayerWithControls = forwardRef<
+ HTMLVideoElementWithPlyr,
+ Props
+>(({ playbackId, poster, currentTime, onLoaded, onError, onSize }, ref) => {
+ const videoRef = useRef<HTMLVideoElementWithPlyr>(null);
+ const metaRef = useCombinedRefs(ref, videoRef);
+ const playerRef = useRef<Plyr | null>(null);
+ const [playerInitTime] = useState(Date.now());
+
+ const videoError = useCallback(
+ (event: ErrorEvent) => onError(event),
+ [onError]
+ );
+
+ const onImageLoad = useCallback(
+ (event: SizedEvent) => {
+ const [w, h] = [event.target.width, event.target.height];
+ if (w && h) {
+ onSize({ width: w, height: h });
+ onLoaded();
+ } else {
+ onLoaded();
+ console.error("Error getting img dimensions", event); // eslint-disable-line no-console
+ }
+ },
+ [onLoaded, onSize]
+ );
+
+ /*
+ * See comment above -- we're loading the poster image just so we can grab the dimensions
+ * which determines styles for the player
+ */
+ useEffect(() => {
+ const img = new Image();
+ img.onload = (evt) => onImageLoad(evt as unknown as SizedEvent);
+ img.src = poster;
+ }, [onImageLoad, poster]);
+
+ useEffect(() => {
+ const video = videoRef.current;
+ const src = `https://stream.mux.com/${playbackId}.m3u8`;
+ let hls: Hls | null;
+ hls = null;
+ if (video) {
+ video.addEventListener("error", videoError);
+ playerRef.current = new Plyr(video, {
+ previewThumbnails: {
+ enabled: true,
+ src: `https://image.mux.com/${playbackId}/storyboard.vtt`,
+ },
+ storage: { enabled: false },
+ fullscreen: {
+ iosNative: true,
+ },
+ captions: { active: true, language: "auto", update: true },
+ });
+
+ if (video.canPlayType("application/vnd.apple.mpegurl")) {
+ // This will run in safari, where HLS is supported natively
+ video.src = src;
+ } else if (Hls.isSupported()) {
+ // This will run in all other modern browsers
+ hls = new Hls();
+ hls.loadSource(src);
+ hls.attachMedia(video);
+ hls.on(Hls.Events.ERROR, (_event, data) => {
+ if (data.fatal) {
+ videoError(new ErrorEvent("HLS.js fatal error"));
+ }
+ });
+ } else {
+ console.error(
+ // eslint-disable-line no-console
+ "This is an old browser that does not support MSE https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API"
+ );
+ }
+ }
+
+ return () => {
+ if (video) {
+ video.removeEventListener("error", videoError);
+ }
+
+ if (hls) {
+ hls.destroy();
+ }
+ };
+ }, [playbackId, playerInitTime, videoError, videoRef]);
+
+ useEffect(() => {
+ const video = videoRef.current;
+ if (currentTime && video) {
+ video.currentTime = currentTime;
+ }
+ }, [currentTime]);
+
+ return (
+ <div className="video-container">
+ <video ref={metaRef} poster={poster} controls playsInline />
+ </div>
+ );
+});
diff --git a/packages/docs/src/components/video-player.css b/packages/docs/src/components/video-player.css
new file mode 100644
index 0000000000..55d510f102
--- /dev/null
+++ b/packages/docs/src/components/video-player.css
@@ -0,0 +1,24 @@
+:global(:root) {
+ --plyr-color-main: #1b1b1b;
+ --plyr-range-fill-background: #ccc;
+}
+:global(.plyr__controls button),
+:global(.plyr__controls input) {
+ cursor: pointer;
+}
+.video-container {
+ margin-bottom: 40px;
+ margin-top: 40px;
+ border-radius: 30px;
+}
+:global(.plyr:fullscreen video) {
+ max-width: initial;
+ max-height: initial;
+ width: 100%;
+ height: 100%;
+}
+video {
+ display: block;
+ max-width: 100%;
+ cursor: pointer;
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index fad8590bf9..b86f7b2419 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -299,12 +299,13 @@ importers:
got: ^11
hls.js: ^0.14.17
lottie-web: ^5.9.6
+ mux-embed: ^4.16.0
+ plyr: ^3.7.3
polished: 4.1.2
prettier: ^2.7.1
prettier-plugin-organize-imports: ^2.3.4
react: 18.0.0
react-dom: 18.0.0
- react-player: ^2.9.0
remark-shiki-twoslash: ^3.1.0
remotion: 3.3.15
three: 0.145.0
@@ -349,10 +350,11 @@ importers:
got: 11.8.5
hls.js: 0.14.17
lottie-web: 5.9.6
+ mux-embed: 4.16.0
+ plyr: 3.7.3
polished: 4.1.2
react: 18.0.0
react-dom: [email protected]
- react-player: [email protected]
remark-shiki-twoslash: 3.1.0
remotion: link:../core
three: 0.145.0
@@ -11039,6 +11041,11 @@ packages:
requiresBuild: true
dev: false
+ /core-js/3.26.1:
+ resolution: {integrity: sha512-21491RRQVzUn0GGM9Z1Jrpr6PNPxPi+Za8OM9q4tksTSnlbXXGKK1nXNg/QvwFYettXvSX6zWKCtHHfjN4puyA==}
+ requiresBuild: true
+ dev: false
+
/core-util-is/1.0.2:
resolution: {integrity: sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=}
dev: false
@@ -11344,6 +11351,10 @@ packages:
/csstype/3.1.1:
resolution: {integrity: sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==}
+ /custom-event-polyfill/1.0.7:
+ resolution: {integrity: sha512-TDDkd5DkaZxZFM8p+1I3yAlvM3rSr1wbrOliG4yJiwinMZN8z/iGL7BTlDkrJcYTmgUSb4ywVCc3ZaUtOtC76w==}
+ dev: false
+
/d/1.0.1:
resolution: {integrity: sha512-m62ShEObQ39CfralilEQRjH6oAMtNCV1xJyEx5LpRYUVN+EviphDgUc/F3hnYbADmkiNs67Y+3ylmlG7Lnu+FA==}
dependencies:
@@ -15408,10 +15419,6 @@ packages:
type-fest: 0.6.0
dev: false
- /load-script/1.0.0:
- resolution: {integrity: sha1-BJGTngvuVkPuSUp+PaPSuscMbKQ=}
- dev: false
-
/loader-runner/4.2.0:
resolution: {integrity: sha512-92+huvxMvYlMzMt0iIOukcwYBFpkYJdpl2xsZ7LrlayO7E8SOv+JJUEK17B/dJIHAOLMfh2dZZ/Y18WgmGtYNw==}
engines: {node: '>=6.11.5'}
@@ -15447,6 +15454,10 @@ packages:
engines: {node: '>= 12.13.0'}
dev: false
+ /loadjs/4.2.0:
+ resolution: {integrity: sha512-AgQGZisAlTPbTEzrHPb6q+NYBMD+DP9uvGSIjSUM5uG+0jG15cb8axWpxuOIqrmQjn6scaaH8JwloiP27b2KXA==}
+ dev: false
+
/local-pkg/0.4.2:
resolution: {integrity: sha512-mlERgSPrbxU3BP4qBqAvvwlgW4MTg78iwJdGGnv7kibKjWcJksrG3t6LB5lXI93wXRDvG4NpUgJFmTG4T6rdrg==}
engines: {node: '>=14'}
@@ -16441,6 +16452,10 @@ packages:
resolution: {integrity: sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==}
dev: false
+ /mux-embed/4.16.0:
+ resolution: {integrity: sha512-3TN+RXOvp0qMTqyJKVKC5CitIgkgHMCi/wIX6Q0bEQ4TaPa4JSI7J7cjcRU5z+m6rNy3GqxvLPymnatbfQYHXw==}
+ dev: false
+
/nanocolors/0.1.12:
resolution: {integrity: sha512-2nMHqg1x5PU+unxX7PGY7AuYxl2qDx7PSrTRjizr8sxdd3l/3hBuWWaki62qmtYm2U5i4Z5E7GbjlyDFhs9/EQ==}
dev: false
@@ -17422,6 +17437,16 @@ packages:
base64-js: 1.5.1
xmlbuilder: 15.1.1
+ /plyr/3.7.3:
+ resolution: {integrity: sha512-ORULENBvEvvzMYXRQBALDmEi8P+wZt1Hr/NvHqchu/t7E2xJKNkRYWx0qCA1HETIGZ6zobrOVgqeAUqWimS7fQ==}
+ dependencies:
+ core-js: 3.26.1
+ custom-event-polyfill: 1.0.7
+ loadjs: 4.2.0
+ rangetouch: 2.0.1
+ url-polyfill: 1.1.12
+ dev: false
+
/polished/4.1.2:
resolution: {integrity: sha512-jq4t3PJUpVRcveC53nnbEX35VyQI05x3tniwp26WFdm1dwaNUBHAi5awa/roBlwQxx1uRhwNSYeAi/aMbfiJCQ==}
engines: {node: '>=10'}
@@ -18113,14 +18138,6 @@ packages:
read: 1.0.7
dev: false
- /prop-types/15.7.2:
- resolution: {integrity: sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==}
- dependencies:
- loose-envify: 1.4.0
- object-assign: 4.1.1
- react-is: 16.13.1
- dev: false
-
/prop-types/15.8.1:
resolution: {integrity: sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==}
dependencies:
@@ -18245,6 +18262,10 @@ packages:
resolution: {integrity: sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==}
engines: {node: '>= 0.6'}
+ /rangetouch/2.0.1:
+ resolution: {integrity: sha512-sln+pNSc8NGaHoLzwNBssFSf/rSYkqeBXzX1AtJlkJiUaVSJSbRAWJk+4omsXkN+EJalzkZhWQ3th1m0FpR5xA==}
+ dev: false
+
/raw-body/2.4.3:
resolution: {integrity: sha512-UlTNLIcu0uzb4D2f4WltY6cVjLi+/jEN4lgEUj3E04tpMDpUlkBo/eSn6zou9hum2VMNpCCUone0O0WeJim07g==}
engines: {node: '>= 0.8'}
@@ -18572,19 +18593,6 @@ packages:
- supports-color
- utf-8-validate
- /react-player/[email protected]:
- resolution: {integrity: sha512-jNUkTfMmUhwPPAktAdIqiBcVUKsFKrVGH6Ocutj6535CNfM91yrvWxHg6fvIX8Y/fjYUPoejddwh7qboNV9vGA==}
- peerDependencies:
- react: '>=16.6.0'
- dependencies:
- deepmerge: 4.2.2
- load-script: 1.0.0
- memoize-one: 5.2.1
- prop-types: 15.7.2
- react: 18.0.0
- react-fast-compare: 3.2.0
- dev: false
-
/react-reconciler/[email protected]:
resolution: {integrity: sha512-nK6kgY28HwrMNwDnMui3dvm3rCFjZrcGiuwLc5COUipBK5hWHLOxMJhSnSomirqWwjPBJKV1QcbkI0VJr7Gl1Q==}
engines: {node: '>=0.10.0'}
@@ -21406,6 +21414,10 @@ packages:
querystringify: 2.2.0
requires-port: 1.0.0
+ /url-polyfill/1.1.12:
+ resolution: {integrity: sha512-mYFmBHCapZjtcNHW0MDq9967t+z4Dmg5CJ0KqysK3+ZbyoNOWQHksGCTWwDhxGXllkWlOc10Xfko6v4a3ucM6A==}
+ dev: false
+
/url-toolkit/2.2.3:
resolution: {integrity: sha512-Da75SQoxsZ+2wXS56CZBrj2nukQ4nlGUZUP/dqUBG5E1su5GKThgT94Q00x81eVII7AyS1Pn+CtTTZ4Z0pLUtQ==}
dev: false