Skip to content

🎥 Make videos programmatically with React

Docs: Add example for how to use HLS in preview

Jonny Burgerb25310815ca3
diff --git a/packages/docs/docs/miscellaneous/snippets/hls.md b/packages/docs/docs/miscellaneous/snippets/hls.md
new file mode 100644
index 0000000000..2640d1968c
--- /dev/null
+++ b/packages/docs/docs/miscellaneous/snippets/hls.md
@@ -0,0 +1,65 @@
+---
+image: /generated/articles-docs-miscellaneous-snippets-hls.png
+title: "HLS support (HTTP Live Streaming)"
+id: hls
+crumb: "Video"
+---
+
+## No native support
+
+Remotion does not currently support HLS / HTTP Live streaming / `.m3u8` files natively.
+It is a technically difficult feature to add because:
+
+- There is no native support for it in the browser.
+- FFmpeg does not treat it like other video formats when extracting frames using the low-level API.
+
+For Remotion to support HLS and take full advantage of only taking the relevant portion of the video while rendering, we'd need to implement our custom parser which takes a lot of time given the long specification.
+
+## Using hls.js to play HLS videos during Preview
+
+You can play HLS videos during preview in the [`<Player>`](/docs/player) and in the Remotion Studio.
+
+Note the following caveats:
+
+<Step>1</Step> This code only shows how to connect the video tag to the HLS stream, it has not been tested on a real project. <br/>
+<Step>2</Step> Videos with audio will not render using this code. Use an alternative source during rendering. See <a href="/docs/miscellaneous/snippets/offthread-video-while-rendering">&lt;OffthreadVideo&gt; while rendering
+</a> and <a href="/docs/getremotionenvironment"><code>getRemotionEnvironment()</code></a> for how to use different components based on whether you are rendering or previewing.<br/><br/>
+
+```tsx twoslash title="HlsDemo.tsx"
+import Hls from "hls.js";
+import React, { useEffect, useRef } from "react";
+import { AbsoluteFill, RemotionVideoProps, Video } from "remotion";
+
+const HlsVideo: React.FC<RemotionVideoProps> = ({ src }) => {
+ const videoRef = useRef<HTMLVideoElement>(null);
+
+ useEffect(() => {
+ if (!src) {
+ throw new Error("src is required");
+ }
+
+ const hls = new Hls();
+
+ hls.loadSource(src);
+ hls.attachMedia(videoRef.current!);
+
+ return () => {
+ hls.destroy();
+ };
+ }, [src]);
+
+ return <Video ref={videoRef} src={src} />;
+};
+
+export const HlsDemo: React.FC = () => {
+ return (
+ <AbsoluteFill>
+ <HlsVideo src="https://stream.mux.com/nqGuji1CJuoPoU3iprRRhiy3HXiQN0201HLyliOg44HOU.m3u8" />
+ </AbsoluteFill>
+ );
+};
+```
+
+## See also
+
+- [GitHub issue for this feature request](https://github.com/remotion-dev/remotion/issues/2930)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 8ae040b6b7..05a55c5032 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -714,6 +714,7 @@ module.exports = {
"miscellaneous/snippets/use-delay-render",
"miscellaneous/snippets/adding-animations",
"miscellaneous/snippets/offthread-video-while-rendering",
+ "miscellaneous/snippets/hls",
],
},
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index ccf804098b..7a14a2235a 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1539,6 +1539,13 @@ export const articles = [
compId: "articles-docs-miscellaneous-snippets-fps-converter",
crumb: "Snippets",
},
+ {
+ id: "hls",
+ title: "HLS support (HTTP Live Streaming)",
+ relativePath: "docs/miscellaneous/snippets/hls.md",
+ compId: "articles-docs-miscellaneous-snippets-hls",
+ crumb: "Video",
+ },
{
id: "miscellaneous/snippets/offthread-video-while-rendering",
title: "<OffthreadVideo /> while rendering",
diff --git a/packages/docs/static/generated/articles-docs-miscellaneous-snippets-hls.png b/packages/docs/static/generated/articles-docs-miscellaneous-snippets-hls.png
new file mode 100644
index 0000000000..521b5192ef
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-miscellaneous-snippets-hls.png differ
diff --git a/packages/example/package.json b/packages/example/package.json
index 208db7b178..1bc6cbf9d5 100644
--- a/packages/example/package.json
+++ b/packages/example/package.json
@@ -37,6 +37,7 @@
"@remotion/gif": "workspace:*",
"@remotion/google-fonts": "workspace:*",
"@remotion/lambda": "workspace:*",
+ "@remotion/layout-utils": "workspace:*",
"@remotion/lottie": "workspace:*",
"@remotion/media-utils": "workspace:*",
"@remotion/motion-blur": "workspace:*",
@@ -49,8 +50,8 @@
"@remotion/three": "workspace:*",
"@remotion/transitions": "workspace:*",
"@remotion/zod-types": "workspace:*",
- "@remotion/layout-utils": "workspace:*",
"@types/express": "^4.17.11",
+ "@types/hls.js": "^0.13.3",
"@types/node": "18.14.6",
"@types/opentype.js": "^1.3.4",
"@types/react": "18.0.26",
@@ -58,6 +59,7 @@
"@types/web": "0.0.78",
"babel-loader": "^8.2.2",
"dotenv": "9.0.2",
+ "hls.js": "^0.14.17",
"lottie-web": "5.12.2",
"opentype.js": "^1.3.4",
"polished": "^4.0.5",
diff --git a/packages/example/src/Hls/HlsDemo.tsx b/packages/example/src/Hls/HlsDemo.tsx
new file mode 100644
index 0000000000..4177a0a23b
--- /dev/null
+++ b/packages/example/src/Hls/HlsDemo.tsx
@@ -0,0 +1,32 @@
+import Hls from 'hls.js';
+import React, {useEffect, useRef} from 'react';
+import {AbsoluteFill, RemotionVideoProps, Video} from 'remotion';
+
+const HlsVideo: React.FC<RemotionVideoProps> = ({src}) => {
+ const videoRef = useRef<HTMLVideoElement>(null);
+
+ useEffect(() => {
+ if (!src) {
+ throw new Error('src is required');
+ }
+
+ const hls = new Hls();
+
+ hls.loadSource(src);
+ hls.attachMedia(videoRef.current!);
+
+ return () => {
+ hls.destroy();
+ };
+ }, [src]);
+
+ return <Video ref={videoRef} src={src} />;
+};
+
+export const HlsDemo: React.FC = () => {
+ return (
+ <AbsoluteFill>
+ <HlsVideo src="https://stream.mux.com/nqGuji1CJuoPoU3iprRRhiy3HXiQN0201HLyliOg44HOU.m3u8" />
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index da420df9a5..1eff4da0ba 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -23,6 +23,7 @@ import {Expert} from './Expert';
import {FontDemo} from './Fonts';
import {Framer} from './Framer';
import {FreezeExample} from './Freeze/FreezeExample';
+import {HlsDemo} from './Hls/HlsDemo';
import {HugePayload, hugePayloadSchema} from './HugePayload';
import {ManyAudio} from './ManyAudio';
import {MissingImg} from './MissingImg';
@@ -459,6 +460,14 @@ export const Index: React.FC = () => {
/>
</Folder>
<Folder name="video-tests">
+ <Composition
+ id="hls"
+ component={HlsDemo}
+ width={1920}
+ height={1080}
+ durationInFrames={100}
+ fps={30}
+ />
<Composition
id="video-testing-mp4"
component={VideoTesting}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d1d1d1010b..6ec78ba4c7 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -843,6 +843,9 @@ importers:
'@types/express':
specifier: ^4.17.11
version: 4.17.13
+ '@types/hls.js':
+ specifier: ^0.13.3
+ version: 0.13.3
'@types/node':
specifier: 18.14.6
version: 18.14.6
@@ -864,6 +867,9 @@ importers:
dotenv:
specifier: 9.0.2
version: 9.0.2
+ hls.js:
+ specifier: ^0.14.17
+ version: 0.14.17
lottie-web:
specifier: 5.12.2
version: 5.12.2
@@ -9178,7 +9184,6 @@ packages:
resolution: {integrity: sha512-Po8ZPCsAcPPuf5OODPEkb6cdWJ/w4BdX1veP7IIOc2WG0x1SW4GEQ1+FHKN1AMG2AePJfNUceJbh5PKtP92yRQ==}
- dev: true
resolution: {integrity: sha512-iMIqiko6ooLrTh1joXodJK5X9xeEALT1kM5G3ZLhD3hszxBdIEd5C75U834D9mLcINgD4OyZf5uQXjkuYydWvA==}