Skip to content

🎥 Make videos programmatically with React

put on homepage

Jonny Burgerc431d72c9676
diff --git a/packages/docs/components/DocsDark.tsx b/packages/docs/components/DocsDark.tsx
index c606bf9d3c..feb288584c 100644
--- a/packages/docs/components/DocsDark.tsx
+++ b/packages/docs/components/DocsDark.tsx
@@ -1,240 +1,234 @@
-import { useColorMode } from "@docusaurus/theme-common";
-import { PlayerInternals } from "@remotion/player";
-import React, {
- useCallback,
- useEffect,
- useMemo,
- useRef,
- useState,
-} from "react";
-import { AbsoluteFill } from "remotion";
-import { Spacer } from "./layout/Spacer";
+import {useColorMode} from '@docusaurus/theme-common';
+import {PlayerInternals} from '@remotion/player';
+import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
+import {AbsoluteFill} from 'remotion';
+import {Spacer} from './layout/Spacer';
export const SingleVideoDemo: React.FC<{
- dark: string;
- light: string;
-}> = ({ dark, light }) => {
- const { colorMode } = useColorMode();
-
- const container = useRef<HTMLDivElement>(null);
- const ref1 = useRef<HTMLVideoElement>(null);
-
- const style: React.CSSProperties = useMemo(
- () => ({
- display: "flex",
- }),
- [],
- );
-
- const videoContainer: React.CSSProperties = useMemo(() => {
- return {
- border:
- "1px solid " +
- (colorMode === "dark"
- ? "var(--ifm-color-emphasis-200)"
- : "var(--ifm-color-emphasis-300)"),
- flex: 1,
- display: "block",
- overflow: "hidden",
- borderRadius: "var(--ifm-code-border-radius)",
- };
- }, [colorMode]);
-
- useEffect(() => {
- ref1.current.addEventListener(
- "canplay",
- () => {
- ref1.current.play();
- },
- {
- once: true,
- },
- );
- }, [colorMode]);
-
- return (
- <div ref={container} style={style}>
- <div style={videoContainer}>
- <video
- ref={ref1}
- preload="metadata"
- src={colorMode === "dark" ? dark : light}
- muted
- loop
- playsInline
- />
- </div>
- </div>
- );
+ readonly dark: string;
+ readonly light: string;
+}> = ({dark, light}) => {
+ const {colorMode} = useColorMode();
+
+ const container = useRef<HTMLDivElement>(null);
+ const ref1 = useRef<HTMLVideoElement>(null);
+
+ const style: React.CSSProperties = useMemo(
+ () => ({
+ display: 'flex',
+ }),
+ [],
+ );
+
+ const videoContainer: React.CSSProperties = useMemo(() => {
+ return {
+ border:
+ '1px solid ' +
+ (colorMode === 'dark'
+ ? 'var(--ifm-color-emphasis-200)'
+ : 'var(--ifm-color-emphasis-300)'),
+ flex: 1,
+ display: 'block',
+ overflow: 'hidden',
+ borderRadius: 'var(--ifm-code-border-radius)',
+ };
+ }, [colorMode]);
+
+ useEffect(() => {
+ ref1.current.addEventListener(
+ 'canplay',
+ () => {
+ ref1.current.play();
+ },
+ {
+ once: true,
+ },
+ );
+ }, [colorMode]);
+
+ return (
+ <div ref={container} style={style}>
+ <div style={videoContainer}>
+ <video
+ ref={ref1}
+ preload="metadata"
+ src={colorMode === 'dark' ? dark : light}
+ muted
+ loop
+ playsInline
+ />
+ </div>
+ </div>
+ );
};
export const DualVideoDemo: React.FC<{
- leftDark: string;
- leftLight: string;
- rightDark: string;
- rightLight: string;
-}> = ({ leftDark, leftLight, rightDark, rightLight }) => {
- const { colorMode } = useColorMode();
- const [played, setPlayed] = useState(false);
-
- const container = useRef<HTMLDivElement>(null);
- const ref1 = useRef<HTMLVideoElement>(null);
- const ref2 = useRef<HTMLVideoElement>(null);
-
- const size = PlayerInternals.useElementSize(container, {
- triggerOnWindowResize: false,
- shouldApplyCssTransforms: true,
- });
-
- const mobile = size?.width < 700;
-
- const style: React.CSSProperties = useMemo(
- () => ({
- display: "flex",
- flexDirection: mobile ? "column" : "row",
- }),
- [mobile],
- );
-
- const videoContainer: React.CSSProperties = useMemo(() => {
- return {
- border:
- "1px solid " +
- (colorMode === "dark"
- ? "transparent"
- : "var(--ifm-color-emphasis-300)"),
- flex: mobile ? 0 : 1,
- display: "block",
- overflow: "hidden",
- borderRadius: "var(--ifm-code-border-radius)",
- aspectRatio: "16/9",
- position: "relative",
- };
- }, [colorMode, mobile]);
-
- const playAll = useCallback(() => {
- ref1.current.play();
- ref2.current.play();
- setPlayed(true);
- }, []);
-
- useEffect(() => {
- Promise.all([
- new Promise<void>((resolve) => {
- ref1.current.addEventListener("canplay", () => resolve(), {
- once: true,
- });
- }),
- new Promise<void>((resolve) => {
- ref2.current.addEventListener("canplay", () => resolve(), {
- once: true,
- });
- }),
- ]).then(() => {
- playAll();
- });
- }, [colorMode, playAll]);
-
- return (
- <div ref={container} style={style}>
- <div style={videoContainer}>
- <AbsoluteFill>
- <video
- ref={ref1}
- preload="metadata"
- src={colorMode === "dark" ? leftDark : leftLight}
- muted
- loop
- playsInline
- />
- </AbsoluteFill>
- <AbsoluteFill>
- <button
- type="button"
- style={{
- width: 36,
- height: 36,
- bottom: 6,
- left: 6,
- position: "absolute",
- appearance: "none",
- border: "1px solid rgba(255, 255, 255, 0.1)",
- outline: "1px solid rgba(0, 0, 0, 0.1)",
- borderRadius: 4,
- display: "flex",
- justifyContent: "center",
- alignItems: "center",
- cursor: "pointer",
- backgroundColor: "transparent",
- pointerEvents: played ? "none" : "auto",
- opacity: played ? 0 : 1,
- transition: "opacity 0.2s",
- }}
- onClick={playAll}
- >
- <svg
- style={{
- height: 24,
- }}
- viewBox="0 0 384 512"
- >
- <path
- fill="currentColor"
- d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"
- />
- </svg>
- </button>
- </AbsoluteFill>
- </div>
- <Spacer />
- <Spacer />
- <div style={videoContainer}>
- <video
- ref={ref2}
- preload="metadata"
- src={colorMode === "dark" ? rightDark : rightLight}
- muted
- loop
- playsInline
- />
- </div>
- </div>
- );
+ readonly leftDark: string;
+ readonly leftLight: string;
+ readonly rightDark: string;
+ readonly rightLight: string;
+}> = ({leftDark, leftLight, rightDark, rightLight}) => {
+ const {colorMode} = useColorMode();
+ const [played, setPlayed] = useState(false);
+
+ const container = useRef<HTMLDivElement>(null);
+ const ref1 = useRef<HTMLVideoElement>(null);
+ const ref2 = useRef<HTMLVideoElement>(null);
+
+ const size = PlayerInternals.useElementSize(container, {
+ triggerOnWindowResize: false,
+ shouldApplyCssTransforms: true,
+ });
+
+ const mobile = size?.width < 700;
+
+ const style: React.CSSProperties = useMemo(
+ () => ({
+ display: 'flex',
+ flexDirection: mobile ? 'column' : 'row',
+ }),
+ [mobile],
+ );
+
+ const videoContainer: React.CSSProperties = useMemo(() => {
+ return {
+ border:
+ '1px solid ' +
+ (colorMode === 'dark'
+ ? 'transparent'
+ : 'var(--ifm-color-emphasis-300)'),
+ flex: mobile ? 0 : 1,
+ display: 'block',
+ overflow: 'hidden',
+ borderRadius: 'var(--ifm-code-border-radius)',
+ aspectRatio: '16/9',
+ position: 'relative',
+ };
+ }, [colorMode, mobile]);
+
+ const playAll = useCallback(() => {
+ ref1.current.play();
+ ref2.current.play();
+ setPlayed(true);
+ }, []);
+
+ useEffect(() => {
+ Promise.all([
+ new Promise<void>((resolve) => {
+ ref1.current.addEventListener('canplay', () => resolve(), {
+ once: true,
+ });
+ }),
+ new Promise<void>((resolve) => {
+ ref2.current.addEventListener('canplay', () => resolve(), {
+ once: true,
+ });
+ }),
+ ]).then(() => {
+ playAll();
+ });
+ }, [colorMode, playAll]);
+
+ return (
+ <div ref={container} style={style}>
+ <div style={videoContainer}>
+ <AbsoluteFill>
+ <video
+ ref={ref1}
+ preload="metadata"
+ src={colorMode === 'dark' ? leftDark : leftLight}
+ muted
+ loop
+ playsInline
+ />
+ </AbsoluteFill>
+ <AbsoluteFill>
+ <button
+ type="button"
+ style={{
+ width: 36,
+ height: 36,
+ bottom: 6,
+ left: 6,
+ position: 'absolute',
+ appearance: 'none',
+ border: '1px solid rgba(255, 255, 255, 0.1)',
+ outline: '1px solid rgba(0, 0, 0, 0.1)',
+ borderRadius: 4,
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+ cursor: 'pointer',
+ backgroundColor: 'transparent',
+ pointerEvents: played ? 'none' : 'auto',
+ opacity: played ? 0 : 1,
+ transition: 'opacity 0.2s',
+ }}
+ onClick={playAll}
+ >
+ <svg
+ style={{
+ height: 24,
+ }}
+ viewBox="0 0 384 512"
+ >
+ <path
+ fill="currentColor"
+ d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"
+ />
+ </svg>
+ </button>
+ </AbsoluteFill>
+ </div>
+ <Spacer />
+ <Spacer />
+ <div style={videoContainer}>
+ <video
+ ref={ref2}
+ preload="metadata"
+ src={colorMode === 'dark' ? rightDark : rightLight}
+ muted
+ loop
+ playsInline
+ />
+ </div>
+ </div>
+ );
};
export const AnimatingProperties: React.FC = () => {
- return (
- <div>
- <DualVideoDemo
- leftDark="/img/animating-properties-left-dark.mp4"
- leftLight="/img/animating-properties-left-light.mp4"
- rightDark="/img/animating-properties-right-dark.mp4"
- rightLight="/img/animating-properties-right-light.mp4"
- />
- </div>
- );
+ return (
+ <div>
+ <DualVideoDemo
+ leftDark="/img/animating-properties-left-dark.mp4"
+ leftLight="/img/animating-properties-left-light.mp4"
+ rightDark="/img/animating-properties-right-dark.mp4"
+ rightLight="/img/animating-properties-right-light.mp4"
+ />
+ </div>
+ );
};
export const Springs: React.FC = () => {
- return (
- <div>
- <DualVideoDemo
- leftDark="/img/spring-left-dark.mp4"
- leftLight="/img/spring-left-light.mp4"
- rightDark="/img/spring-right-dark.mp4"
- rightLight="/img/spring-right-light.mp4"
- />
- </div>
- );
+ return (
+ <div>
+ <DualVideoDemo
+ leftDark="/img/spring-left-dark.mp4"
+ leftLight="/img/spring-left-light.mp4"
+ rightDark="/img/spring-right-dark.mp4"
+ rightLight="/img/spring-right-light.mp4"
+ />
+ </div>
+ );
};
export const Transforms: React.FC = () => {
- return (
- <div>
- <SingleVideoDemo
- dark="/img/transforms-dark.mp4"
- light="/img/transforms-light.mp4"
- />
- </div>
- );
+ return (
+ <div>
+ <SingleVideoDemo
+ dark="/img/transforms-dark.mp4"
+ light="/img/transforms-light.mp4"
+ />
+ </div>
+ );
};
diff --git a/packages/docs/src/components/Demo/ActionRow.tsx b/packages/docs/src/components/Demo/ActionRow.tsx
new file mode 100644
index 0000000000..6142f0f9dd
--- /dev/null
+++ b/packages/docs/src/components/Demo/ActionRow.tsx
@@ -0,0 +1,34 @@
+import {useColorMode} from '@docusaurus/theme-common';
+import React from 'react';
+import styles from './action-row.module.css';
+
+export const ActionRow: React.FC = () => {
+ const {colorMode} = useColorMode();
+
+ return (
+ <div className={styles.row}>
+ <div className={styles.action}>
+ <h2 className={styles['action-title']}>Drag & Drop</h2>
+ <p>
+ The video is interactive. Drag the cards around to change the video.
+ </p>
+ </div>
+ <div className={styles.action}>
+ <h2 className={styles['action-title']}>
+ {colorMode === 'dark' ? 'Light theme' : 'Dark theme'}
+ </h2>
+ <p>
+ Switch to {colorMode === 'dark' ? 'light' : 'dark'} mode and see the
+ video adjust!
+ </p>
+ </div>
+ <div className={styles.action}>
+ <h2 className={styles['action-title']}>Render a video</h2>
+ <p>
+ Invoke a distributed render using{' '}
+ <a href="/lambda">Remotion Lambda</a>.
+ </p>
+ </div>
+ </div>
+ );
+};
diff --git a/packages/docs/src/components/Demo/Demo.tsx b/packages/docs/src/components/Demo/Demo.tsx
new file mode 100644
index 0000000000..365284391e
--- /dev/null
+++ b/packages/docs/src/components/Demo/Demo.tsx
@@ -0,0 +1,38 @@
+import {useColorMode} from '@docusaurus/theme-common';
+import {Player} from '@remotion/player';
+import React from 'react';
+import {HomepageVideoComp} from '../../remotion/HomepageVideo/Comp';
+import {ActionRow} from './ActionRow';
+
+export const Demo: React.FC = () => {
+ const {colorMode} = useColorMode();
+ return (
+ <div>
+ <br />
+ <br />
+ <br />
+ <h1>Try it out</h1>
+ <Player
+ component={HomepageVideoComp}
+ compositionWidth={640}
+ compositionHeight={360}
+ durationInFrames={120}
+ fps={30}
+ autoPlay
+ controls
+ clickToPlay={false}
+ style={{
+ border: '2px solid black',
+ borderBottom: '4px solid black',
+ borderRadius: 8,
+ width: '100%',
+ }}
+ inputProps={{
+ theme: colorMode,
+ }}
+ loop
+ />
+ <ActionRow />
+ </div>
+ );
+};
diff --git a/packages/docs/src/components/Demo/action-row.module.css b/packages/docs/src/components/Demo/action-row.module.css
new file mode 100644
index 0000000000..215814bf93
--- /dev/null
+++ b/packages/docs/src/components/Demo/action-row.module.css
@@ -0,0 +1,25 @@
+.row {
+ display: flex;
+ flex-direction: row;
+ align-items: center;
+ margin-top: 40px;
+}
+
+.action {
+ flex: 1;
+ text-wrap: balance;
+}
+
+.action-title {
+ font-family: 'GT Planar';
+}
+
+@media screen and (max-width: 900px) {
+ .row {
+ flex-direction: column;
+ align-items: flex-start;
+ }
+ .action {
+ margin-top: 20px;
+ }
+}
diff --git a/packages/docs/src/pages/index.tsx b/packages/docs/src/pages/index.tsx
index 0125c252d2..bc7768daf4 100644
--- a/packages/docs/src/pages/index.tsx
+++ b/packages/docs/src/pages/index.tsx
@@ -14,6 +14,7 @@ import {LightningFastEditor} from '../../components/LandingPage/editor';
import {IfYouKnowReact} from '../../components/LandingPage/if-you-know-react';
import {Parametrize} from '../../components/LandingPage/parametrize';
import {RealMP4Videos} from '../../components/LandingPage/real-mp4-videos';
+import {Demo} from '../components/Demo/Demo';
import styles from './landing.module.css';
const NewLanding: React.FC = () => {
@@ -41,6 +42,8 @@ const NewLanding: React.FC = () => {
<IfYouKnowReact />
<br />
<br />
+
+ <Demo />
<br />
<br />
<br />
diff --git a/packages/docs/src/remotion/HomepageVideo/Card.tsx b/packages/docs/src/remotion/HomepageVideo/Card.tsx
index 7da7894c5b..a1b0355383 100644
--- a/packages/docs/src/remotion/HomepageVideo/Card.tsx
+++ b/packages/docs/src/remotion/HomepageVideo/Card.tsx
@@ -313,11 +313,11 @@ export const Cards: React.FC<{
const index = indices[i];
const content =
index === 0 ? (
- <CurrentCountry theme={theme} />
+ <TrendingRepos theme={theme} />
) : index === 1 ? (
<Temperature theme={theme} />
) : index === 2 ? (
- <TrendingRepos theme={theme} />
+ <CurrentCountry theme={theme} />
) : (
'🐹'
);
diff --git a/packages/docs/src/remotion/HomepageVideo/Comp.tsx b/packages/docs/src/remotion/HomepageVideo/Comp.tsx
index bf74280aab..ce37321f39 100644
--- a/packages/docs/src/remotion/HomepageVideo/Comp.tsx
+++ b/packages/docs/src/remotion/HomepageVideo/Comp.tsx
@@ -7,7 +7,9 @@ export const schema = z.object({
theme: z.enum(['light', 'dark']),
});
-export const Comp: React.FC<z.infer<typeof schema>> = ({theme}) => {
+export const HomepageVideoComp: React.FC<z.infer<typeof schema>> = ({
+ theme,
+}) => {
const [state, setRerenders] = useState({
rerenders: 0,
indices: [0, 1, 2, 3],
diff --git a/packages/docs/src/remotion/HomepageVideo/TrendingRepos.tsx b/packages/docs/src/remotion/HomepageVideo/TrendingRepos.tsx
index 4b275503c6..0fa191b660 100644
--- a/packages/docs/src/remotion/HomepageVideo/TrendingRepos.tsx
+++ b/packages/docs/src/remotion/HomepageVideo/TrendingRepos.tsx
@@ -43,7 +43,7 @@ export const TrendingRepos: React.FC<{
marginBottom: 10,
}}
>
- Trending repository
+ Trending repositories
</div>
<div style={item}>1. maybe-finance/maybe</div>
<div style={item}>2. excalidraw/excalidraw</div>
diff --git a/packages/docs/src/remotion/Root.tsx b/packages/docs/src/remotion/Root.tsx
index f11349849e..916c6751c9 100644
--- a/packages/docs/src/remotion/Root.tsx
+++ b/packages/docs/src/remotion/Root.tsx
@@ -6,7 +6,7 @@ import {experts} from '../data/experts';
import {AllTemplates} from './AllTemplates';
import {Article} from './Article';
import {Expert} from './Expert';
-import {Comp, schema} from './HomepageVideo/Comp';
+import {HomepageVideoComp, schema} from './HomepageVideo/Comp';
import {TemplateComp} from './Template';
export const RemotionRoot: React.FC = () => {
@@ -67,7 +67,7 @@ export const RemotionRoot: React.FC = () => {
id="template-all"
/>
<Composition
- component={Comp}
+ component={HomepageVideoComp}
id="HomepageVideo"
width={640}
height={360}