🎥 Make videos programmatically with React
try out mux
Jonny Burgerf08f46943575
diff --git a/packages/docs/package-lock.json b/packages/docs/package-lock.jsonindex e50e1ae380..1ec113ffd9 100644--- a/packages/docs/package-lock.json+++ b/packages/docs/package-lock.json@@ -2280,6 +2280,11 @@"integrity": "sha512-S78QIYirQcUoo6UJZx9CSP0O2ix9IaeAXwQi26Rhr/+mg7qqPy8TzaxHSUut7eGjL8WmLccT7/MXf304WjqHcA==","dev": true},+ "@types/hls.js": {+ "version": "0.13.3",+ "resolved": "https://registry.npmjs.org/@types/hls.js/-/hls.js-0.13.3.tgz",+ "integrity": "sha512-Po8ZPCsAcPPuf5OODPEkb6cdWJ/w4BdX1veP7IIOc2WG0x1SW4GEQ1+FHKN1AMG2AePJfNUceJbh5PKtP92yRQ=="+ },"@types/hoist-non-react-statics": {"version": "3.3.1","resolved": "https://registry.npmjs.org/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz",@@ -6518,6 +6523,15 @@"value-equal": "^1.0.1"}},+ "hls.js": {+ "version": "0.14.17",+ "resolved": "https://registry.npmjs.org/hls.js/-/hls.js-0.14.17.tgz",+ "integrity": "sha512-25A7+m6qqp6UVkuzUQ//VVh2EEOPYlOBg32ypr34bcPO7liBMOkKFvbjbCBfiPAOTA/7BSx1Dujft3Th57WyFg==",+ "requires": {+ "eventemitter3": "^4.0.3",+ "url-toolkit": "^2.1.6"+ }+ },"hmac-drbg": {"version": "1.0.1","resolved": "https://registry.npmjs.org/hmac-drbg/-/hmac-drbg-1.0.1.tgz",@@ -13109,6 +13123,11 @@}}},+ "url-toolkit": {+ "version": "2.2.1",+ "resolved": "https://registry.npmjs.org/url-toolkit/-/url-toolkit-2.2.1.tgz",+ "integrity": "sha512-8+DzgrtDZYZGhHaAop5WGVghMdCfOLGbhcArsJD0qDll71FXa7EeKxi2hilPIscn2nwMz4PRjML32Sz4JTN0Xw=="+ },"use": {"version": "3.1.1","resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz",diff --git a/packages/docs/package.json b/packages/docs/package.jsonindex 621a1a14a2..34451fde7f 100644--- a/packages/docs/package.json+++ b/packages/docs/package.json@@ -16,9 +16,11 @@"@docusaurus/preset-classic": "2.0.0-alpha.70","@mdx-js/react": "^1.6.21","@splitbee/web": "^0.2.4",+ "@types/hls.js": "^0.13.3","@types/styled-components": "^5.1.9","chalk": "^4.1.0","clsx": "^1.1.1",+ "hls.js": "^0.14.17","prism-react-renderer": "^1.1.1","react": "^16.8.4","react-dom": "^16.8.4",diff --git a/packages/docs/src/components/VideoPlayer.tsx b/packages/docs/src/components/VideoPlayer.tsxindex 9cbb86db12..15df6bbb10 100644--- a/packages/docs/src/components/VideoPlayer.tsx+++ b/packages/docs/src/components/VideoPlayer.tsx@@ -1,6 +1,7 @@import React, { useEffect, useRef } from "react";import { ShowcaseVideo } from "../data/showcase-videos";import { VideoPlayerContent } from "./VideoPlayerContent";+const container: React.CSSProperties = {position: "fixed",top: 0,diff --git a/packages/docs/src/components/VideoPlayerContent.tsx b/packages/docs/src/components/VideoPlayerContent.tsxindex 21bab9dad4..9aaf70c461 100644--- a/packages/docs/src/components/VideoPlayerContent.tsx+++ b/packages/docs/src/components/VideoPlayerContent.tsx@@ -1,5 +1,6 @@import useBaseUrl from "@docusaurus/useBaseUrl";-import React, { useRef } from "react";+import Hls from "hls.js";+import React, { useEffect, useRef } from "react";import styled from "styled-components";import { ShowcaseVideo } from "../data/showcase-videos";import { useElementSize } from "../helpers/use-el-size";@@ -23,11 +24,22 @@ const Sidebar = styled.div`width: ${RESERVED_FOR_SIDEBAR}px;`;+const getVideoToPlayUrl = (video: ShowcaseVideo, vidUrl: string) => {+ if (video.type === "mux_video") {+ return `https://stream.mux.com/${video.muxId}.m3u8`;+ }+ return vidUrl;+};+export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({video,}) => {+ const videoRef = useRef<HTMLVideoElement>(null);const container = useRef<HTMLDivElement>(null);- const vidUrl = useBaseUrl(video.videoUrl);+ const baseVidUrl = useBaseUrl(+ video.type === "video_url" ? video.videoUrl : undefined+ );+ const vidUrl = getVideoToPlayUrl(video, baseVidUrl);const containerSize = useElementSize(document.body);@@ -42,9 +54,36 @@ export const VideoPlayerContent: React.FC<{ video: ShowcaseVideo }> = ({const height = ratio * video.height;const width = ratio * video.width;+ useEffect(() => {+ let hls;+ if (videoRef.current) {+ const current = videoRef.current;+ if (video.type === "video_url") {+ current.src = video.videoUrl;+ } else if (current.canPlayType("application/vnd.apple.mpegurl")) {+ // Some browers (safari and ie edge) support HLS natively+ current.src = vidUrl;+ } else if (Hls.isSupported()) {+ console.log("hi");+ // This will run in all other modern browsers+ hls = new Hls();+ hls.loadSource(vidUrl);+ hls.attachMedia(current);+ } else {+ console.error("This is a legacy browser that doesn't support MSE");+ }+ }++ return () => {+ if (hls) {+ hls.destroy();+ }+ };+ }, [videoRef]);+return (<Container ref={container}>- <Video src={vidUrl} height={height} width={width} autoPlay></Video>+ <Video ref={videoRef} height={height} width={width} autoPlay></Video><Sidebar><VideoSidebar video={video}></VideoSidebar></Sidebar>diff --git a/packages/docs/src/components/VideoSidebar.tsx b/packages/docs/src/components/VideoSidebar.tsxnew file mode 100644index 0000000000..8d5fda3ac0--- /dev/null+++ b/packages/docs/src/components/VideoSidebar.tsx@@ -0,0 +1,53 @@+import React from "react";+import styled from "styled-components";+import { ShowcaseLink, ShowcaseVideo } from "../data/showcase-videos";++const Container = styled.div`+ padding: 20px;+`;++const Title = styled.div`+ font-weight: bold;+ margin-bottom: 12px;+`;++const Description = styled.div`+ font-size: 14px;+ margin-bottom: 20px;+`;++const A = styled.a`+ font-size: 14px;+ font-weight: bold;+`;++const getLinkLabel = (linkType: ShowcaseLink) => {+ switch (linkType) {+ case "source_code":+ return "Source code";+ case "tutorial":+ return "Tutorial";+ case "website":+ return "Website";+ case "video":+ return "Video";+ }+};++export const VideoSidebar: React.FC<{+ video: ShowcaseVideo;+}> = ({ video }) => {+ return (+ <Container>+ <Title>{video.title}</Title>+ <Description>{video.description}</Description>+ {video.links.map((link) => {+ return (+ <div key={link.url}>+ <A href={link.url}>{getLinkLabel(link.type)}</A>+ </div>+ );+ })}+ </Container>+ );+};diff --git a/packages/docs/src/data/showcase-videos.tsx b/packages/docs/src/data/showcase-videos.tsxindex 0013ff2060..94899ceaa6 100644--- a/packages/docs/src/data/showcase-videos.tsx+++ b/packages/docs/src/data/showcase-videos.tsx@@ -1,18 +1,35 @@import React from "react";+export type ShowcaseLink = "tutorial" | "source_code" | "website" | "video";++type VideoType =+ | {+ type: "mux_video";+ muxId: string;+ }+ | {+ type: "video_url";+ videoUrl: string;+ };+export type ShowcaseVideo = {title: string;- videoUrl: string;imageUrl: string;description: React.ReactNode;height: number;width: number;-};+ links: {+ url: string;+ type: ShowcaseLink;+ }[];+ submittedOn: Date;+} & VideoType;export const showcaseVideos: ShowcaseVideo[] = [{title: "Spotify Wrapped",- videoUrl: "vid/showcase/1.mp4",+ type: "mux_video",+ muxId: "U5LZfw102AUygXZIH02VOvhvCks9L00j014yIvE6qZ01vdDo",imageUrl: "img/showcase/1.png",description: (<>@@ -22,9 +39,21 @@ export const showcaseVideos: ShowcaseVideo[] = [),height: 1280,width: 720,+ submittedOn: new Date("25-03-2021"),+ links: [+ {+ type: "source_code",+ url: "https://github.com/jonnyburger/remotion-wrapped",+ },+ {+ type: "video",+ url: "https://www.youtube.com/watch?v=I-y_5H9-3gk",+ },+ ],},{title: "AnySticker In App Assets",+ type: "video_url",videoUrl: "vid/showcase/2.mp4",imageUrl: "img/showcase/2.png",description: (@@ -32,13 +61,33 @@ export const showcaseVideos: ShowcaseVideo[] = [),height: 1920,width: 1080,+ submittedOn: new Date("25-03-2021"),+ links: [+ {+ type: "source_code",+ url: "https://github.com/JonnyBurger/anysticker-tutorials",+ },+ ],},{title: "Remotion Trailer",+ type: "video_url",+videoUrl: "vid/showcase/3.mp4",imageUrl: "img/showcase/3.png",description: <>This video Welcome you to Remotion project.</>,width: 1920,height: 1080,+ submittedOn: new Date("25-03-2021"),+ links: [+ {+ type: "source_code",+ url: "https://github.com/JonnyBurger/remotion-trailer",+ },+ {+ type: "video",+ url: "https://www.youtube.com/watch?v=gwlDorikqgY",+ },+ ],},];