Skip to content

Instantly share code, notes, and snippets.

@dikaio
Created December 13, 2023 08:58
Show Gist options
  • Save dikaio/4201ab658008bce5b13b7da9b053c2ad to your computer and use it in GitHub Desktop.
Save dikaio/4201ab658008bce5b13b7da9b053c2ad to your computer and use it in GitHub Desktop.
Loading Videos on Vercel via Mix
import { preload } from "react-dom";
import { unstable_getImgProps as getImgProps } from "next/image";
type Props = {
playbackId: string;
loading: "lazy" | "eager";
resolution: "SD" | "HD";
};
export default function MuxVideo({ playBackId, loading, loading }: Props) {
const mp4Url = `https://stream.mux.com/${playbackId}/${
resolution === "SD" ? "medium" : "high"
}.mp4`;
const webmUrl = `https://stream.mux.com/${playbackId}/${
resolution === "SD" ? "medium" : "high"
}.webm`;
// Use `getImgProps` to convert the video poster image to WebP
const {
props: { src: poster },
} = getImgProps({
src: `https://image.mux.com/${playbackId}/thumbnail.webp?fit_mode=smartcrop&time=0`,
alt: "",
fill: true,
});
// Preload the poster when applicable
if (loading === "eager") {
preload(poster, {
as: "image",
fetchPriority: "high",
});
}
return (
<video
autoPlay
playsInline
loop
controls={false}
muted
preload="none"
>
<source src={mp4Url} type="video/mp4" />
<source src={webmUrl} type="video/webm" />
</video>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment