Skip to content

🎥 Make videos programmatically with React

add video

Jonny Burger934df2a1d88e
diff --git a/packages/docs/components/layout/Button.tsx b/packages/docs/components/layout/Button.tsx
index 1c602a6b90..89a1343e2a 100644
--- a/packages/docs/components/layout/Button.tsx
+++ b/packages/docs/components/layout/Button.tsx
@@ -40,6 +40,7 @@ export const Button: React.FC<Props> = (props) => {
return (
<button
+ type="button"
className={styles.buttoncontainer}
disabled={actualDisabled}
{...other}
@@ -49,6 +50,7 @@ export const Button: React.FC<Props> = (props) => {
color: props.color,
cursor: props.disabled ? "default" : "pointer",
backgroundColor: props.background,
+ // @ts-expect-error
"--hover-color": props.hoverColor,
...(props.fullWidth ? { width: "100%" } : {}),
opacity: props.disabled ? 0.7 : 1,
diff --git a/packages/docs/src/components/MuxVideo.tsx b/packages/docs/src/components/MuxVideo.tsx
new file mode 100644
index 0000000000..1726a3c039
--- /dev/null
+++ b/packages/docs/src/components/MuxVideo.tsx
@@ -0,0 +1,54 @@
+import Hls from "hls.js";
+import React, {
+ forwardRef,
+ useEffect,
+ useImperativeHandle,
+ useRef,
+} from "react";
+
+const getVideoToPlayUrl = (muxId: string) => {
+ return `https://stream.mux.com/${muxId}.m3u8`;
+};
+
+const MuxVideoForward: React.ForwardRefRenderFunction<
+ HTMLVideoElement,
+ React.DetailedHTMLProps<
+ React.VideoHTMLAttributes<HTMLVideoElement>,
+ HTMLVideoElement
+ > & {
+ muxId: string;
+ }
+> = ({ muxId, ...props }, ref) => {
+ const videoRef = useRef<HTMLVideoElement>(null);
+ const vidUrl = getVideoToPlayUrl(muxId);
+
+ 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/packages/docs/src/components/VideoPlayerContent.tsx b/packages/docs/src/components/VideoPlayerContent.tsx
index e573fbbf35..6698ce2209 100644
--- a/packages/docs/src/components/VideoPlayerContent.tsx
+++ b/packages/docs/src/components/VideoPlayerContent.tsx
@@ -1,4 +1,3 @@
-import Hls from "hls.js";
import React, {
useCallback,
useEffect,
@@ -9,6 +8,7 @@ import React, {
import { ShowcaseVideo } from "../data/showcase-videos";
import { useElementSize } from "../helpers/use-el-size";
import { PausedIcon } from "../icons/arrows";
+import { MuxVideo } from "./MuxVideo";
import { Spinner } from "./Spinner";
import { SHOWCASE_MOBILE_HEADER_HEIGHT } from "./VideoPlayerHeader";
import { VideoSidebar } from "./VideoSidebar";
@@ -52,14 +52,6 @@ export const PAGINATE_ICON_PADDING = 20;
export const PAGINATE_BUTTONS_WIDTH =
(PAGINATE_ICON_WIDTH + PAGINATE_ICON_PADDING * 2) * 2;
-const getVideoToPlayUrl = (video: ShowcaseVideo) => {
- if (video.type === "mux_video") {
- return `https://stream.mux.com/${video.muxId}.m3u8`;
- }
-
- throw new Error("no url");
-};
-
export const VideoPlayerContent: React.FC<{
video: ShowcaseVideo;
userHasInteractedWithPage: boolean;
@@ -67,7 +59,6 @@ export const VideoPlayerContent: React.FC<{
const [loaded, setLoaded] = useState(false);
const videoRef = useRef<HTMLVideoElement>(null);
const container = useRef<HTMLDivElement>(null);
- const vidUrl = getVideoToPlayUrl(video);
const [paused, setPaused] = useState(!userHasInteractedWithPage);
@@ -93,30 +84,6 @@ export const VideoPlayerContent: React.FC<{
const height = ratio * video.height;
const width = ratio * video.width;
- 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]);
-
const onLoadedMetadata = useCallback(() => {
setLoaded(true);
}, []);
@@ -186,8 +153,9 @@ export const VideoPlayerContent: React.FC<{
<Spinner style={spinner} />
</div>
)}
- <video
+ <MuxVideo
ref={videoRef}
+ muxId={video.muxId}
style={videoCss}
onLoadedMetadata={onLoadedMetadata}
loop
diff --git a/packages/docs/success-stories/2021-07-31-makestories.md b/packages/docs/success-stories/2021-09-16-makestories.md
similarity index 93%
rename from packages/docs/success-stories/2021-07-31-makestories.md
rename to packages/docs/success-stories/2021-09-16-makestories.md
index 4ce2823299..57483a5b86 100644
--- a/packages/docs/success-stories/2021-07-31-makestories.md
+++ b/packages/docs/success-stories/2021-09-16-makestories.md
@@ -4,11 +4,15 @@ title: How MakeStories uses Remotion to render Web Stories
image: /img/makestories.png
---
+import {MuxVideo} from '../src/components/MuxVideo';
+
_This is the first article in our new series: Success Stories. We interview companies and creators that we find inspiring and which have successfully implemented Remotion in their stack. Let us know what you think!_
MakeStories is in the business of [Web Stories](https://stories.google/) - a format by Google to bring stories to the web. Appearing directly in Google Search, they are an opportunity for publishers to increase their audience massively.
-![Cover image](/img/makestories.png)
+<MuxVideo muxId="eB9pPF17zyOrmEas4kwsa3OvTeQV3cDlLQ5U01CHrfcg" style={{
+ width: '100%'
+}} controls poster="/img/makestories.png"/>
<p align="center"><em>MakeStories founder Pratik Ghela explains how they built a Remotion rendering service to export Web Stories to MP4s.</em></p>
@@ -23,7 +27,7 @@ MakeStories is a web based tool where you can create stories for websites. For c
We started to build a Web Story builder for social media. For that reason we were looking for a rendering engine and that was when we came across Remotion. Now a Web Story created on MakeStories is compatible with any platform - Google as well as social media.
-![Cover image](/img/makestories-export.png)
+![Screenshot of MakeStories' export feature](/img/makestories-export.png)
<p align="center"><em>MakeStories customers get two options to export their stories - either in AMP format to publish on Google or as MP4 to post to social media.</em></p>