Store, stream, and embed video in your SvelteKit application with the Mux API and components. Use this guide when you add video pages, user uploads, or background video to a SvelteKit site.
When adding video to your SvelteKit app, you'll encounter some common hurdles. First, videos are large. Storing them in your public directory can lead to excessive bandwidth consumption and poor Git repository performance. Next, it's important to compress and optimize your videos for the web. Then, as network conditions change, you might want to adapt the quality of your video to ensure a smooth playback experience for your users. Finally, you may want to integrate additional features like captions, thumbnails, and analytics.
You might consider using Mux's APIs and components to handle these challenges, and more.
The quickest way to add a video to your site is with Video.js. In Svelte, you use the Video.js HTML custom elements. Install them with the Mux video media and the Mux Data extension:
npm install @videojs/html @videojs/mux-video @videojs/mux-dataThen add a player to a page:
<script lang="ts">
import '@videojs/html/video/player';
import '@videojs/html/video/skin';
import '@videojs/html/media/mux-video';
import '@videojs/html/extensions/mux-data';
</script>
<video-player>
<video-skin style="display: block; width: 100%; aspect-ratio: 16 / 9;">
<mux-video
src="https://stream.mux.com/jwmIE4m9De02B8TLpBHxOHX7ywGnjWxYQxork1Jn5ffE.m3u8"
playsinline
crossorigin="anonymous"
></mux-video>
<mux-data metadata={{ video_title: 'Test VOD', viewer_user_id: 'user-id-007' }}></mux-data>
</video-skin>
</video-player>The Mux video media plays the video and loads its poster and timeline hover previews. The Mux Data extension monitors playback. Metadata is an object, so Svelte sets it as a JavaScript property on <mux-data>. The server-rendered HTML still shows metadata="[object Object]", which you can ignore: the property takes over once the element upgrades in the browser.
Keep the imports static. SvelteKit renders the custom elements on the server, and the browser upgrades them when the page loads. See Use Video.js with Svelte and SvelteKit for more.
If your site has just a few videos, you might upload them to Mux directly through the dashboard. In the Mux Dashboard, on your video assets page, select "Create New Asset". On the next screen, you can upload a video directly to Mux.

You'll then be able to see your new asset on your video assets page. When you click on the asset, you can find the asset's playback ID in the "Playback and Thumbnails" tab. Put this playback ID in the src URL of the <mux-video> element.

To set signed tokens, a custom domain or DRM, see Use Video.js with Mux. To change the look and feel of the player, see the Video.js documentation.
If you're managing more videos, you might take a look at our CMS integrations.
Finally, if you need more control over your video workflow, read on.
If you're looking to build your own video workflow that enables uploading, playback, and more in your application, you can use the Mux API with Video.js for playback and Mux Uploader for uploads.
One reason you might want to build your own video workflow is when you want to allow users to upload video to your app.
Let's start by adding the Mux TypeScript SDK to your project. We'll be using this a lot.
import Mux from '@mux/ts';
import { MUX_TOKEN_ID, MUX_TOKEN_SECRET } from '$env/static/private';
const mux = new Mux({
tokenId: MUX_TOKEN_ID,
tokenSecret: MUX_TOKEN_SECRET
});
export default mux;Now, we can add a new page where users can upload videos. This will involve using the Mux Uploader component, which will upload videos to a Mux Direct Uploads URLAPI.
We'll start by creating an upload URL using the Direct Uploads URL API.
import mux from '$lib/mux';
export const load = async () => {
// Create an endpoint for MuxUploader to upload to
const upload = await mux.video.uploads.create({
new_asset_settings: {
playback_policy: ['public'],
video_quality: 'basic'
},
// in production, you'll want to change this origin to your-domain.com
cors_origin: '*'
});
return { id: upload.id, url: upload.url };
}In production, you'll want to apply additional security measures to your upload URL. Consider protecting the route with authentication to prevent unauthorized users from uploading videos. Also, use cors_origin and consider playback_policies to further restrict where uploads can be performed and who can view uploaded videos.
Then, we'll pass that URL to the Mux Uploader component, which will handle uploading for us.
<script>
import '@mux/mux-uploader';
export let data;
</script>
<mux-uploader endpoint={data.url} />Next, we'll create an API endpoint that will listen for Mux webhooks. When we receive the notification that the video has finished uploading and is ready for playback, we'll add the video's metadata to our database.
import mux from '$lib/mux';
import { json } from '@sveltejs/kit';
import { MUX_WEBHOOK_SIGNING_SECRET } from '$env/static/private';
export const POST = async ({ request }) => {
const body = await request.text();
// mux.webhooks.unwrap will validate that the given payload was sent by Mux and parse the payload.
// Generate MUX_WEBHOOK_SIGNING_SECRET in the Mux dashboard
// https://dashboard.mux.com/settings/webhooks
const event = await mux.webhooks.unwrap(body, request.headers, MUX_WEBHOOK_SIGNING_SECRET);
// you can also unwrap the payload yourself:
// const event = await request.json();
switch (event.type) {
case 'video.upload.asset_created':
// we might use this to know that an upload has been completed
// and we can save its assetId to our database
break;
case 'video.asset.ready':
// we might use this to know that a video has been encoded
// and we can save its playbackId to our database
break;
// there are many more Mux webhook events
// check them out at https://www.mux.com/docs/webhook-reference
default:
break;
}
return json({ message: 'ok' });
};Finally, let's make a playback page. We retrieve the video metadata from our database in the page's load function, return its playbackId and title, and play it with Video.js:
<script>
// npm install @videojs/html @videojs/mux-video @videojs/mux-data
import '@videojs/html/video/player';
import '@videojs/html/video/skin';
import '@videojs/html/media/mux-video';
import '@videojs/html/extensions/mux-data';
export let data;
</script>
<video-player>
<video-skin>
<mux-video
src={`https://stream.mux.com/${data.playbackId}.m3u8`}
playsinline
crossorigin="anonymous"
></mux-video>
<mux-data metadata={{ video_title: data.title }}></mux-data>
</video-skin>
</video-player>
<style>
video-skin {
display: block;
width: 100%;
aspect-ratio: 16 / 9;
}
</style>And we've got upload and playback. Nice!
What's next? You can integrate with your CMS. You can set signed tokens, a custom domain or DRM. Or get started with the example project below:
This is a bare-bones starter application with SvelteKit that uses: