Skip to content

🎥 Make videos programmatically with React

alright

Jonny Burger62795d857cf9
diff --git a/packages/promo-pages/package.json b/packages/promo-pages/package.json
index 239cf11528..863dad4bae 100644
--- a/packages/promo-pages/package.json
+++ b/packages/promo-pages/package.json
@@ -17,7 +17,7 @@
"@remotion/eslint-config-internal": "workspace:*",
"@remotion/shapes": "workspace:*",
"create-video": "workspace:*",
- "hls.js": "^0.14.17",
+ "hls.js": "1.5.19",
"polished": "4.3.1",
"react": "19.0.0",
"react-dom": "19.0.0",
@@ -27,7 +27,7 @@
},
"devDependencies": {
"@eslint/eslintrc": "3.1.0",
- "@types/hls.js": "^0.13.3",
+ "@types/hls.js": "1.0.0",
"@types/react": "19.0.0",
"@types/react-dom": "19.0.0",
"eslint": "9.14.0",
diff --git a/packages/promo-pages/src/components/homepage/Demo/index.tsx b/packages/promo-pages/src/components/homepage/Demo/index.tsx
index d58fcecad8..02449b496a 100644
--- a/packages/promo-pages/src/components/homepage/Demo/index.tsx
+++ b/packages/promo-pages/src/components/homepage/Demo/index.tsx
@@ -44,7 +44,6 @@ const playerWrapper: CSSProperties = {
export const Demo: React.FC = () => {
const {colorMode} = useColorMode();
- console.log({colorMode});
const [data, setData] = useState<LocationAndTrending | null>(null);
const ref = useRef<PlayerRef>(null);
diff --git a/packages/promo-pages/src/components/homepage/MuxVideo.tsx b/packages/promo-pages/src/components/homepage/MuxVideo.tsx
index 12865b1899..808889bef8 100644
--- a/packages/promo-pages/src/components/homepage/MuxVideo.tsx
+++ b/packages/promo-pages/src/components/homepage/MuxVideo.tsx
@@ -1,8 +1,9 @@
-import React, {forwardRef, useRef} from 'react';
+import Hls from 'hls.js';
+import React, {forwardRef, useEffect, useImperativeHandle, useRef} from 'react';
import {VideoPlayerWithControls} from './VideoPlayerWithControls';
const getVideoToPlayUrl = (muxId: string) => {
- return `https://stream.mux.com/${muxId}.mp4`;
+ return `https://stream.mux.com/${muxId}.m3u8`;
};
const MuxVideoForward: React.ForwardRefRenderFunction<
@@ -13,11 +14,37 @@ const MuxVideoForward: React.ForwardRefRenderFunction<
> & {
readonly muxId: string;
}
-> = ({muxId, ...props}) => {
+> = ({muxId, ...props}, ref) => {
const videoRef = useRef<HTMLVideoElement>(null);
const vidUrl = getVideoToPlayUrl(muxId);
- return <video ref={videoRef} src={vidUrl} {...props} />;
+ useImperativeHandle(ref, () => videoRef.current!, []);
+
+ useEffect(() => {
+ let hls: Hls;
+ if (videoRef.current) {
+ const {current} = videoRef;
+ if (current.canPlayType('application/vnd.apple.mpegurl')) {
+ // Some browers (safari and ie edge) support HLS natively
+ current.src = vidUrl;
+ } else if (Hls.isSupported()) {
+ // 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();
+ }
+ };
+ }, [vidUrl, videoRef]);
+
+ return <video ref={videoRef} {...props} />;
};
export const MuxVideo = forwardRef(MuxVideoForward);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 88f8830ae4..1a78e01f1c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1663,8 +1663,8 @@ importers:
specifier: workspace:*
version: link:../create-video
hls.js:
- specifier: ^0.14.17
- version: 0.14.17
+ specifier: 1.5.19
+ version: 1.5.19
polished:
specifier: 4.3.1
version: 4.3.1
@@ -1694,8 +1694,8 @@ importers:
specifier: 4.0.0-beta.9
version: 4.0.0-beta.9([email protected])
'@types/hls.js':
- specifier: ^0.13.3
- version: 0.13.3
+ specifier: 1.0.0
+ version: 1.0.0
'@types/react':
specifier: 19.0.0
version: 19.0.0
@@ -14882,6 +14882,13 @@ packages:
resolution: {integrity: sha512-Po8ZPCsAcPPuf5OODPEkb6cdWJ/w4BdX1veP7IIOc2WG0x1SW4GEQ1+FHKN1AMG2AePJfNUceJbh5PKtP92yRQ==}
+ resolution: {integrity: sha512-EGY2QJefX+Z9XH4PAxI7RFoNqBlQEk16UpYR3kbr82CIgMX5SlMe0PjFdFV0JytRhyVPQCiwSyONuI6S1KdSag==}
+ deprecated: This is a stub types definition. hls.js provides its own type definitions, so you do not need this installed.
+ dependencies:
+ hls.js: 0.14.17
+ dev: true
+
resolution: {integrity: sha512-iMIqiko6ooLrTh1joXodJK5X9xeEALT1kM5G3ZLhD3hszxBdIEd5C75U834D9mLcINgD4OyZf5uQXjkuYydWvA==}
dependencies:
@@ -20376,7 +20383,6 @@ packages:
resolution: {integrity: sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==}
- dev: false
resolution: {integrity: sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==}
@@ -21708,12 +21714,15 @@ packages:
dependencies:
eventemitter3: 4.0.7
url-toolkit: 2.2.3
- dev: false
resolution: {integrity: sha512-wA66nnYFvQa1o4DO/BFgLNRKnBTVXpNeldGRBJ2Y0SvFtdwvFKCbqa9zhHoZLoxHhZ+jYsj3aIBkWQQCPNOhMw==}
dev: false
+ resolution: {integrity: sha512-C020dKWEJcyvLnrqsFKW4q6D/6IEzKWdhktIS5bgoyEFE8lHgrFBq4RIngdy113abJOlIruhv8qjg7UX8hwxOw==}
+ dev: false
+
resolution: {integrity: sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==}
dependencies:
@@ -30235,7 +30244,6 @@ packages:
resolution: {integrity: sha512-Da75SQoxsZ+2wXS56CZBrj2nukQ4nlGUZUP/dqUBG5E1su5GKThgT94Q00x81eVII7AyS1Pn+CtTTZ4Z0pLUtQ==}
- dev: false
resolution: {integrity: sha512-7/hqDrWa0iMnCoET9W1T07EmD4Eg/Wmoj/X8TGBc++ECRK4m5yTsjP4O6s0yagbxfqIOuUkIxe2/sA+VR2GxZA==}