Here are four approaches you can take to putting a video on the homepage of a SvelteKit website, depending on your needs for performance, styling, user experience, and backend integration:
1. Embed a Video Using the <video> HTML Element (Local or External File)
Best for direct control and basic video playback without third-party services.
svelte
<!-- src/routes/+page.svelte -->
<video autoplay muted loop playsinline class="hero-video">
<source src="/videos/intro.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
<style>
.hero-video {
width: 100%;
height: auto;
object-fit: cover;
}
</style>
✅ Good performance if video is optimized
✅ No third-party dependencies
⚠️ You must place the video file in /static/videos/ (or host externally and use CDN)
✅ Full control over autoplay, loop, quality, fallback, etc.
2. Use a YouTube or Vimeo Embed
Ideal if you want to stream videos efficiently and don’t want to host them yourself.
Setting preload="auto" or preload="none" on <video> tags as needed.
Serving videos in modern formats like WebM or H.265/HEVC.
Including a poster image for better UX.
Would you like code tailored to a specific approach above?
User
Let's lazy load the video once everything else has loaded (i.e. in an onMount)
Assistant
Great! If you want to lazy load a video on your SvelteKit homepageafter everything else has loaded, i.e., only after the app mounts on the client, you can use Svelte’s onMount lifecycle to defer loading the video. This is a powerful way to improve initial load performance, especially on media-rich pages.
Here's how to do it:
✅ Lazy Load Video Using onMount in SvelteKit
svelte
<!-- src/routes/+page.svelte -->
<script>
import { onMount } from 'svelte';
let showVideo = false;
onMount(() => {
// Wait until the main page content is settled before loading video
requestAnimationFrame(() => {
setTimeout(() => {
showVideo = true;
}, 0); // Adjust delay if needed
});
});
</script>
<section>
<h1>Welcome to My Website</h1>
<p>Enjoy your stay while the video loads in the background!</p>
{#if showVideo}
<video
autoplay
muted
loop
playsinline
preload="none"
class="hero-video"
>
<source src="/videos/intro.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
{/if}
</section>
<style>
.hero-video {
width: 100%;
height: auto;
object-fit: cover;
margin-top: 2rem;
}
</style>
🧠 Explanation:
onMount() ensures this only runs on the client, avoiding any server-side rendering issues.
showVideo controls whether the video element is rendered.
We use requestAnimationFrame + setTimeout to defer the work, giving priority to the initial render and browser rendering tasks.
You can load the video source from /static/videos/ by placing intro.mp4 in the static/videos/ directory, or use a CDN/external URL.
Would you like to extend this with IntersectionObserver for scroll-based loading or with a poster fade-in effect?
User
Which settings should I use on Handbrake to optimise the file?
Assistant
To optimize a video for use on your SvelteKit website using HandBrake, you'll want to strike a balance between file size, compatibility, and quality—especially since it's being lazy-loaded. Here are the recommended settings for web-optimized video output (e.g. background or hero videos):
✅ HandBrake Settings for Web Video Optimization
1. Format & Video Codec
Setting
Recommended Value
Format
MP4 (.mp4)
Video Codec
H.264 (x264)
✅ H.264 is the most widely supported codec across all browsers.
🔒 Avoid using H.265 (HEVC) or AV1 unless you're providing fallbacks.
2. Web Optimization
✅ Enable "Web Optimized"
(Found under the Summary tab)
Moves metadata to the beginning of the file.
Ensures fast playback/start times when the video is progressively loaded (important for web use).
3. Dimensions (Resolution)
Under the Dimensions tab:
❌ Uncheck "Auto Crop" unless you need it.
✂️ Optionally resize the video depending on where you’re using it:
Full Background: Max width ~1280px or 1920px
Inline Element: Max width ~640px
Leave aspect ratio locked.
4. Video Settings
Under the Video tab:
Setting
Recommended Value
Video Encoder
H.264 (x264)
Framerate (FPS)
Same as source or 24 fps
Constant Quality
RF 20 – 23
Encoder Preset
Very Fast (or faster)
✅ Lower RF = better quality but larger file. RF 20–23 is a typical sweet spot for the web.
✅ “Very Fast” is a good trade-off between encode time and compression efficiency.
5. Audio (Optional)
If the video will be muted (e.g., background videos), remove the audio track:
Under the Audio tab > Click the track > Remove Track
✅ Smaller file size and less processing.
6. Subtitles / Chapters
Not needed for background/hero videos.
❌ Remove all unless you need them.
Advanced Tips:
Name your file clearly (e.g. intro-optimized.mp4)
Serve WebM fallback if you want more modern compression alongside MP4.
You can also re-encode a higher-resolution version of the video for 2x displays, but optimize the default for bandwidth.
✅ Summary: HandBrake Key Checklist for Web Videos
Setting
Value
Format
MP4
Video Codec
H.264 (x264)
Web Optimized
✅ Enabled
Framerate
Same as source or 24 fps
Constant Quality (RF)
20–23
Audio
Remove (if muted)
Dimensions
Resize appropriately
Would you like a preset export JSON for HandBrake or a script using ffmpeg as an alternative?