Skip to content

🎥 Make videos programmatically with React

improved

Jonny Burgerebf034586b7c
diff --git a/packages/docs/components/LandingPage/CodeExample.tsx b/packages/docs/components/LandingPage/CodeExample.tsx
index 4de2781fbf..6c0e00425e 100644
--- a/packages/docs/components/LandingPage/CodeExample.tsx
+++ b/packages/docs/components/LandingPage/CodeExample.tsx
@@ -17,6 +17,7 @@ const Demo: React.FC = () => {
justifyContent: "center",
alignItems: "center",
flex: 1,
+ backgroundColor: "black",
}}
>
<img
diff --git a/packages/docs/components/LandingPage/GithubDemo.tsx b/packages/docs/components/LandingPage/GithubDemo.tsx
new file mode 100644
index 0000000000..68cb379bd0
--- /dev/null
+++ b/packages/docs/components/LandingPage/GithubDemo.tsx
@@ -0,0 +1,97 @@
+import React from "react";
+import {
+ AbsoluteFill,
+ Img,
+ interpolate,
+ measureSpring,
+ spring,
+ useCurrentFrame,
+ useVideoConfig,
+} from "remotion";
+import styled from "styled-components";
+
+const Title = styled.h1`
+ font-size: 5em;
+ color: var(--text-color);
+ margin-bottom: 0;
+ margin-top: 0;
+ line-height: 1.4;
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
+ Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
+`;
+
+const Description = styled.p`
+ font-size: 3em;
+ margin-bottom: 0;
+ margin-top: 0;
+ line-height: 1.4;
+ color: var(--text-color);
+
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
+ Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
+`;
+
+export type GithubResponse = {
+ login: string;
+ avatar_url: string;
+ followers: number;
+};
+export const GithubDemo: React.FC<{
+ data: GithubResponse | null;
+}> = ({ data }) => {
+ const frame = useCurrentFrame();
+ const { fps } = useVideoConfig();
+ const progress = spring({
+ frame,
+ fps,
+ config: {
+ damping: 200,
+ },
+ });
+
+ const springDur = measureSpring({ fps, config: { damping: 200 } });
+
+ const titleTranslation = interpolate(progress, [0, 1], [700, 0]);
+ const subtitleOpacity = interpolate(
+ frame,
+ [springDur + 15, springDur + 40],
+ [0, 1]
+ );
+
+ if (!data) {
+ return null;
+ }
+
+ return (
+ <AbsoluteFill
+ style={{
+ backgroundColor: "var(--ifm-background-color)",
+ justifyContent: "center",
+ alignItems: "center",
+ }}
+ >
+ <div
+ style={{ display: "flex", flexDirection: "row", alignItems: "center" }}
+ >
+ <Img
+ src={data.avatar_url}
+ style={{
+ borderRadius: 150,
+ height: 300,
+ width: 300,
+ transform: `scale(${progress})`,
+ }}
+ />
+ <div style={{ width: 60 }} />
+ <div>
+ <Title style={{ transform: `translateY(${titleTranslation}px)` }}>
+ Hi {data.login}!
+ </Title>
+ <Description style={{ opacity: subtitleOpacity }}>
+ You have {data.followers} followers.
+ </Description>
+ </div>
+ </div>
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/docs/components/LandingPage/Programmatic.tsx b/packages/docs/components/LandingPage/Programmatic.tsx
new file mode 100644
index 0000000000..7a34af160c
--- /dev/null
+++ b/packages/docs/components/LandingPage/Programmatic.tsx
@@ -0,0 +1,58 @@
+import { Player, PlayerRef } from "@remotion/player";
+import React, { useCallback, useEffect, useRef, useState } from "react";
+import { GithubDemo, GithubResponse } from "./GithubDemo";
+
+export const ProgrammaticContent: React.FC<{ data: GithubResponse | null }> = ({
+ data,
+}) => {
+ const containerRef = useRef<HTMLDivElement>(null);
+ const playerRef = useRef<PlayerRef>(null);
+
+ const [isIntersecting, setIsIntersecting] = useState(false);
+
+ const callback: IntersectionObserverCallback = useCallback((data) => {
+ const { isIntersecting } = data[0];
+ setIsIntersecting(isIntersecting);
+ if (isIntersecting) {
+ playerRef.current?.play();
+ } else {
+ playerRef.current?.pause();
+ }
+ }, []);
+
+ useEffect(() => {
+ const { current } = containerRef;
+ if (!current) {
+ return;
+ }
+ const observer = new IntersectionObserver(callback, {
+ root: null,
+ threshold: 1,
+ });
+ observer.observe(current);
+
+ return () => observer.unobserve(current);
+ }, []);
+
+ useEffect(() => {
+ if (isIntersecting) {
+ playerRef.current?.play();
+ }
+ }, [data]);
+
+ return (
+ <div ref={containerRef}>
+ <Player
+ ref={playerRef}
+ component={GithubDemo}
+ compositionHeight={720}
+ compositionWidth={1280}
+ durationInFrames={100}
+ fps={30}
+ controls
+ style={{ width: 500 }}
+ inputProps={{ data }}
+ ></Player>
+ </div>
+ );
+};
diff --git a/packages/docs/components/LandingPage/editor.tsx b/packages/docs/components/LandingPage/editor.tsx
index 7684ad3eac..8dcefaa434 100644
--- a/packages/docs/components/LandingPage/editor.tsx
+++ b/packages/docs/components/LandingPage/editor.tsx
@@ -77,7 +77,7 @@ export const LightningFastEditor: React.FC = () => {
playsInline
playing={isIntersecting}
loop
- style={{ width: 500, borderRadius: 4, overflow: "hidden" }}
+ style={{ width: 500, borderRadius: 7, overflow: "hidden" }}
></Video>
</div>
<div style={{ flex: 1 }}>
diff --git a/packages/docs/components/LandingPage/parametrize.tsx b/packages/docs/components/LandingPage/parametrize.tsx
index 7c8f38cfdd..992461d245 100644
--- a/packages/docs/components/LandingPage/parametrize.tsx
+++ b/packages/docs/components/LandingPage/parametrize.tsx
@@ -1,10 +1,13 @@
-import React from "react";
+import React, { useEffect, useReducer, useRef, useState } from "react";
import styled from "styled-components";
+import { GithubResponse } from "./GithubDemo";
+import { ProgrammaticContent } from "./Programmatic";
const Row = styled.div`
display: flex;
flex-direction: row;
text-align: left;
+ align-items: center;
`;
const Title = styled.h2`
@@ -22,10 +25,112 @@ const Mp4 = styled.span`
-webkit-background-clip: text;
`;
+const SubmitButton = styled.button`
+ appearance: none;
+ padding: 14px 16px;
+ background-color: rgb(66, 144, 245);
+ color: white;
+ border: none;
+ font-weight: bold;
+ border-radius: 3px;
+ font-size: 14px;
+ cursor: pointer;
+`;
+
+const Input = styled.input`
+ color: var(--text-color);
+`;
+
+const cache: { [key: string]: GithubResponse } = {};
+
+const makeRequest = async (username: string): Promise<GithubResponse> => {
+ if (cache[username]) {
+ return cache[username];
+ }
+ const response = await fetch(`https://api.github.com/users/${username}`);
+ const json = await response.json();
+ if (response.status !== 200) {
+ throw new Error(json.message);
+ }
+ cache[username] = json;
+ return json;
+};
+
+type Actions =
+ | {
+ type: "fetch-video";
+ }
+ | {
+ type: "receive-video";
+ response: GithubResponse;
+ }
+ | {
+ type: "receive-error";
+ error: Error;
+ };
+
+type State =
+ | {
+ type: "error";
+ error: Error;
+ }
+ | {
+ type: "loaded";
+ response: GithubResponse;
+ }
+ | {
+ type: "loading";
+ };
+
+const reducer = (state: State, action: Actions): State => {
+ switch (action.type) {
+ case "fetch-video":
+ return {
+ type: "loading",
+ };
+ case "receive-video":
+ return {
+ type: "loaded",
+ response: action.response,
+ };
+ case "receive-error":
+ return {
+ type: "error",
+ error: action.error,
+ };
+ default:
+ return state;
+ }
+};
+
export const Parametrize: React.FC = () => {
+ const ref = useRef<HTMLInputElement>(null);
+ const [username, setUsername] = useState("JonnyBurger");
+
+ const [state, dispatch] = useReducer(reducer, { type: "loading" });
+
+ useEffect(() => {
+ dispatch({
+ type: "fetch-video",
+ });
+ makeRequest(username)
+ .then((response) => {
+ dispatch({
+ type: "receive-video",
+ response,
+ });
+ })
+ .catch((error) => {
+ dispatch({
+ type: "receive-error",
+ error,
+ });
+ });
+ }, [username]);
+
return (
<Row>
- <div>
+ <div style={{ flex: 1 }}>
<Title>
<Mp4>Programmatic</Mp4> content <br />
and rendering
@@ -34,7 +139,33 @@ export const Parametrize: React.FC = () => {
Fetch data from an API and use it as the content. <br />
Render videos programmatically using our server-side APIs.
</p>
+ <span style={{ fontSize: 13 }}>
+ Customize the video by entering your Github username.
+ </span>
+ <div style={{ height: 10 }}></div>
+ <div>
+ <Input
+ autoFocus
+ ref={ref}
+ type="text"
+ placeholder="Your Github username"
+ ></Input>{" "}
+ <div style={{ width: 8, display: "inline-block" }}></div>
+ <SubmitButton
+ disabled={state.type === "loading"}
+ style={{ opacity: state.type === "loading" ? 0.5 : 1 }}
+ onClick={() => setUsername(ref.current?.value as string)}
+ >
+ Show video
+ </SubmitButton>
+ </div>
+ {state.type === "error" ? (
+ <div style={{ color: "red" }}>{state.type}</div>
+ ) : null}
</div>
+ <ProgrammaticContent
+ data={state.type === "loaded" ? state.response : null}
+ />
</Row>
);
};
diff --git a/packages/example/src/GithubDemo/index.tsx b/packages/example/src/GithubDemo/index.tsx
new file mode 100644
index 0000000000..984a26cfeb
--- /dev/null
+++ b/packages/example/src/GithubDemo/index.tsx
@@ -0,0 +1,85 @@
+import React from 'react';
+import {
+ AbsoluteFill,
+ Img,
+ interpolate,
+ measureSpring,
+ spring,
+ useCurrentFrame,
+ useVideoConfig,
+} from 'remotion';
+import styled from 'styled-components';
+
+const Title = styled.h1`
+ font-size: 5em;
+ margin-bottom: 0;
+ margin-top: 0;
+ line-height: 1.4;
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
+ Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
+`;
+
+const Description = styled.p`
+ font-size: 3em;
+ margin-bottom: 0;
+ margin-top: 0;
+ line-height: 1.4;
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
+ Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
+`;
+
+export const GithubDemo: React.FC<{
+ data: GithubResponse;
+}> = ({data}) => {
+ const frame = useCurrentFrame();
+ const {fps} = useVideoConfig();
+ const progress = spring({
+ frame,
+ fps,
+ config: {
+ damping: 200,
+ },
+ });
+
+ const springDur = measureSpring({fps, config: {damping: 200}});
+
+ const titleTranslation = interpolate(progress, [0, 1], [700, 0]);
+ const subtitleOpacity = interpolate(
+ frame,
+ [springDur + 15, springDur + 40],
+ [0, 1]
+ );
+
+ return (
+ <AbsoluteFill
+ style={{
+ backgroundColor: 'white',
+ justifyContent: 'center',
+ alignItems: 'center',
+ }}
+ >
+ <div
+ style={{display: 'flex', flexDirection: 'row', alignItems: 'center'}}
+ >
+ <Img
+ src={data.avatar_url}
+ style={{
+ borderRadius: 150,
+ height: 300,
+ width: 300,
+ transform: `scale(${progress})`,
+ }}
+ />
+ <div style={{width: 60}} />
+ <div>
+ <Title style={{transform: `translateY(${titleTranslation}px)`}}>
+ Hi {data.login}!
+ </Title>
+ <Description style={{opacity: subtitleOpacity}}>
+ You have {data.followers} followers.
+ </Description>
+ </div>
+ </div>
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 9360aca03a..4d34c05e2a 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import {Composition, getInputProps} from 'remotion';
import {ColorInterpolation} from './ColorInterpolation';
import {Framer} from './Framer';
+import {GithubDemo} from './GithubDemo';
import {MissingImg} from './MissingImg';
import RemoteVideo from './RemoteVideo';
import {TenFrameTester} from './TenFrameTester';
@@ -241,6 +242,14 @@ export const Index: React.FC = () => {
fps={30}
durationInFrames={100}
/>
+ <Composition
+ id="github-demo"
+ component={GithubDemo}
+ width={1280}
+ height={720}
+ fps={30}
+ durationInFrames={100}
+ />
</>
);
};