So you have an HLS stream and its M3U8 link, and you want to display it on your website. You might think it's as simple as embedding a YouTube video, but if you've tried using a standard HTML <video>
tag, you've probably discovered it doesn't always work. Playing M3U8 streams reliably across all browsers requires a specialized video player. But don't worry, the process is surprisingly straightforward. This guide will show you exactly how to embed a robust M3U8 player on your site.
HLS (HTTP Live Streaming), which uses M3U8 playlists, is the dominant streaming technology today. While native browser support for HLS is growing (Safari supports it out of the box), it's not universal. Browsers like Chrome and Firefox on desktop require a little help to understand and play M3U8 files.
This is where JavaScript-based players come in. They act as a compatibility layer, fetching the M3U8 playlist, downloading the video segments, and feeding them into the browser's video element. This ensures your stream works for all visitors, regardless of their browser.
Several excellent, free, and open-source players can handle M3U8 streams. Choosing one depends on your needs for customization, features, and ease of use. Here's a comparison of two of the most popular choices.
Player | Key Feature | Best For | Ease of Use |
---|---|---|---|
Video.js | Extremely customizable with a huge plugin ecosystem. | Projects that need a full-featured, skinnable player that can be extended. | Easy for basic setup, moderate for deep customization. |
HLS.js | Lightweight and focused solely on HLS playback. | Adding M3U8 support to a standard <video> tag with minimal overhead. |
Very easy, requires only a few lines of code. |
For this guide, we will use Video.js because it's a fantastic all-in-one solution that provides a great-looking player with minimal setup.
Follow these three simple steps to get your M3U8 player up and running in minutes.
First, you need to include the Video.js stylesheet and JavaScript file in the <head>
section of your HTML page. The easiest way is to link to them from a CDN (Content Delivery Network).
<!-- Video.js CSS -->
<link href="https://vjs.zencdn.net/8.10.0/video-js.css" rel="stylesheet" />
<!-- Video.js JS -->
<script src="https://vjs.zencdn.net/8.10.0/video.min.js"></script>
Next, add a <video>
element where you want the player to appear. Give it the class video-js
and other standard video attributes like controls
and preload
.
<video
id="my-m3u8-player"
class="video-js vjs-default-skin"
controls
preload="auto"
width="640"
height="360"
data-setup="{}">
</video>
Finally, you need to tell Video.js what to play. Add a <source>
tag inside your <video>
element. The src
attribute should be your M3U8 link, and the type
must be set to application/x-mpegURL
.
<video
id="my-m3u8-player"
class="video-js vjs-default-skin"
controls
preload="auto"
width="640"
height="360"
data-setup="{}">
<!-- This is where you add your M3U8 source -->
<source src="YOUR_M3U8_LINK_HERE.m3u8" type="application/x-mpegURL">
</video>
Simply replace YOUR_M3U8_LINK_HERE.m3u8
with the actual URL of your stream. That's it! When you load your page, Video.js will automatically detect the M3U8 source and present a beautiful, functional player.
While the term M3U8 player might sound intimidating, modern tools have made it incredibly simple to implement. By leveraging a powerful library like Video.js, you can embed high-quality, adaptive HLS streams on your website with just a few lines of code. This ensures a smooth, professional viewing experience for your audience on any device or browser, unlocking the full potential of your video content.