🎥 Make videos programmatically with React
some improvements
Patric Salvisbergeea2602023ae
diff --git a/packages/docs/docs/temporary-url.md b/packages/docs/docs/temporary-url.mdindex 0ed24d8b55..3696a2cca6 100644--- a/packages/docs/docs/temporary-url.md+++ b/packages/docs/docs/temporary-url.md@@ -1,11 +1,11 @@---id: temporary-url-title: How to display a video in the Remotion Player before it's fully uploaded to a cloude storage.+title: How to display a video in the Remotion Player before it's fully uploaded to a cloud storage.---## Problem-Lets say you are building a webapp where you want to enable your users to upload a Video which then gets displayed by the Remotion Player in your webapp. You choose to store the videos in a Amazon S3 Bucket. Each time a video gets uploaded, several seconds, sometimes even minutes pass untill your video is fully uploaded to the cloud and can be displayed in the player. Definitely not a great user experience. You would like to have your video ready to watch as soon as it gets provided by the client, without having to wait for the cloud to completely finish the upload.+Lets say you are building a webapp where you want to enable your users to upload a Video which then gets displayed by the Remotion Player in your webapp. You choose to store the video in a cloud. Each time a video gets uploaded, several seconds, sometimes even minutes pass untill your video is fully uploaded to the cloud and can be displayed in the player. Definitely not a great user experience. You would like to have your video ready to watch as soon as it gets provided by the client, without having to wait for the cloud to completely finish the upload.Good news: This can be done pretty easily!@@ -13,7 +13,7 @@ Good news: This can be done pretty easily!Lets analyze the problem.-We will somwhere have a composition which returns a video tag that includes an url as source.+We will somewhere have a component which returns a video tag that includes an URL as source.```tsx twoslashconst upload = async (file: File) => {@@ -23,28 +23,29 @@ const upload = async (file: File) => {import { AbsoluteFill, Sequence, Video } from "remotion";type VideoProps = {- videoUrl: string | null;+ videoURL: string | null;};-export const MyComposition: React.FC<VideoProps> = ({ videoUrl }) => {- if (!videoUrl) {+export const MyComponent: React.FC<VideoProps> = ({ videoURL }) => {+ if (!videoURL) {return null;}return (<AbsoluteFill><Sequence>- <Video src={videoUrl} />+ <Video src={videoURL} /></Sequence></AbsoluteFill>);};```-The videoUrl will be passed from the Remotion Player to our composition.-As soon as a file is fully uploaded to S3, the url will be set and can be used by the composition to display the video.+The videoURL will be passed from the Remotion Player to our component.+As soon as a file is fully uploaded to the cloud, the URL will be set and can be used by the component to display the video.```tsx twoslash-const MyComposition: React.FC<{ videoUrl: string | null }> = (url) => {+import { useCallback } from "react";+const MyComposition: React.FC<{ videoURL: string | null }> = (URL) => {return null;};const upload = async (file: File) => {@@ -55,14 +56,22 @@ import { Player } from "@remotion/player";import { useState } from "react";export const RemotionPlayer: React.FC = () => {- const [videoUrl, setVideoUrl] = useState<string | null>(null);- const handleChange = async (event: any) => {- const file = event.target.files[0];- //upload is an example function & returns a url when a file is uploaded on S3.- const s3Url = await upload(file);- // E.g., s3Url = https://exampleBucketName.s3.ExampleAwsRegion.amazonaws.com- setVideoUrl(s3Url);- };+ const [videoURL, setVideoURL] = useState<string | null>(null);++ const handleChange = useCallback(+ async (event: React.ChangeEvent<HTMLInputElement>) => {+ if (event.target.files === null) {+ return;+ }++ const file = event.target.files[0];+ //upload is an example function & returns a URL when a file is uploaded on the cloud.+ const cloudURL = await upload(file);+ // E.g., cloudURL = https://exampleBucketName.s3.ExampleAwsRegion.amazonaws.com+ setVideoURL(cloudURL);+ },+ []+ );return (<div>@@ -72,7 +81,7 @@ export const RemotionPlayer: React.FC = () => {compositionWidth={1920}compositionHeight={1080}fps={30}- inputProps={{ videoUrl }}+ inputProps={{ videoURL }}/><input type="file" onChange={handleChange} />@@ -81,16 +90,12 @@ export const RemotionPlayer: React.FC = () => {};```-As mentioned, depending on the file size and available bandwidth, the uploading can take quite some time and we have to wait pretty long until the video can be displayed.--Therefore, lets see how we can solve this problem.-## Solution-When we start the upload of the file, we want to create a blob url which can be used by the composition to display the file residing on our clients system. Like this, the video can be displayed as soon as the file gets provided by the client. When the file is uploaded and we get the S3 url, the composition shall use the S3 url as source.+When we start the upload of the file, we want to create a blob URL which can be used by the composition to display the file residing on our clients system. Like this, the video can be displayed as soon as the file gets provided by the client. When the file is uploaded and we get the S3 URL, the composition shall use the S3 URL as source.```tsx twoslash-const MyComposition: React.FC<{ url: string | null }> = (url) => {+const MyComposition: React.FC<{ URL: string | null }> = (URL) => {return null;};const upload = async (file: File) => {@@ -101,28 +106,37 @@ const upload = async (file: File) => {import { Player } from "@remotion/player";import { useState } from "react";-type VideoUrl =+type VideoURL =| {type: "empty";- url: null;}| {- type: "blob" | "s3";- url: string;+ type: "blob" | "cloud";+ URL: string;};export const RemotionPlayer: React.FC = () => {- const [videoUrl, setVideoUrl] = useState<VideoUrl>({+ const [videoURL, setVideoURL] = useState<VideoURL>({type: "empty",- url: null,});- const handleChange = async (event: any) => {+ const handleChange = async (event: React.ChangeEvent<HTMLInputElement>) => {+ if (event.target.files === null) {+ return;+ }+const file = event.target.files[0];- const blobUrl = URL.createObjectURL(file);- setVideoUrl({ type: "blob", url: blobUrl });- const s3Url = await upload(file);- setVideoUrl({ type: "s3", url: s3Url });+ const blobURL = URL.createObjectURL(file);+ setVideoURL({ type: "blob", URL: blobURL });+ const cloudURL = await upload(file);+ setVideoURL({ type: "cloud", URL: cloudURL });+ URL.revokeObjectURL(blobURL);+ };+ const deriveURL = () => {+ if (videoURL.type === "empty") {+ return null;+ }+ return videoURL.URL;};return (@@ -133,7 +147,7 @@ export const RemotionPlayer: React.FC = () => {compositionWidth={1920}compositionHeight={1080}fps={30}- inputProps={{ url: videoUrl.url }}+ inputProps={{ URL: deriveURL() }}/><input type="file" onChange={handleChange} />@@ -144,8 +158,8 @@ export const RemotionPlayer: React.FC = () => {How does it work?-First, we create a new type called VideoUrl with which we can easily keep track what kind of url our videUrl state is at the moment (empty, blob or S3)+First, we create a new type called VideoURL with which we can easily keep track what kind of URL our videURL state is at the moment (empty, blob or cloud)-See how in `handleChange()` we now create a blob url simply with `URL.createObjectURL(file)` and set our `videoUrl` state-to type = "blob" and url = blobUrl. Like this, while we are still awaiting the S3 url, the Video will already be displayed in the Player with the blobUrl as source! Exactly what we wanted.-When the S3 url gets returned by our `upload()` function, the state will be updated with type = "S3" and url = S3url.+See how in `handleChange()` we now create a blob URL simply with `URL.createObjectURL(file)` and set our `videoURL` state+to type = "blob" and URL = blobURL. Like this, while we are still awaiting the cloud URL, the Video will already be displayed in the Player with the blobURL as source! Exactly what we wanted.+When the cloud URL gets returned by our `upload()` function, the state will be updated with type = "cloud" and URL = cloudURL and the player will automatically use the cloudURL as new source.