HLS and DASH for quality that adapts to bandwidth
Adaptive streaming automatically adjusts video quality based on the user's network conditions. Instead of one large file, the video is split into small segments at multiple quality levels.
Developed by Apple, HLS is the most widely supported adaptive streaming format.
It uses .m3u8 playlist files that reference video segments.
// Native HLS support in Safari
<video controls>
<source src="video.m3u8" type="application/x-mpegURL" />
</video>
// For other browsers, use hls.js library
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script>
const video = document.getElementById('video');
const hlsUrl = 'https://example.com/video.m3u8';
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(hlsUrl);
hls.attachMedia(video);
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
// Native HLS support (Safari)
video.src = hlsUrl;
}
</script>
An open standard alternative to HLS, DASH uses .mpd manifest files.
// Using dash.js library
<script src="https://cdn.dashjs.org/latest/dash.all.min.js"></script>
<script>
const player = dashjs.MediaPlayer().create();
player.initialize(
document.querySelector('video'),
'https://example.com/video.mpd',
true
);
</script>
Don't use for: Short videos (<30s), hero backgrounds, or when file size is already optimized.