I often get email from people asking if there any examples in the book about how to play HLS video in the browser and my answer is always the same: no. In this short post, I’ll try to explain why not with an example of how to play encrypted HLS video in the browser, which is also something I get asked about a lot.
The main reason there aren’t any specific examples in the book that show how to play HLS video in the browser is because most of the time, all you need to do is tell the player where the playlist is and hit play; it takes care of the rest. Plus, there are a lot of players out there that support HLS video.
Most of the effort involved in playing HLS video goes into encoding the videos, segmenting them, preparing the playlist, and so on, not configuring the front-end. And this is what the book focuses on.
Let me highlight this with an example. I’m going to use Video.js, but it should work in just about every other HLS capable player. To play HLS video using Video.js you’ll need to use the Video.js HLS source handler. I’ve already written something about how to encrypt video for HLS so I won’t go over the details here – I did say this was going to be a short post.
Here’s some HTML that plays the video:
<head> <link href="http://vjs.zencdn.net/6.4.0/video-js.css" rel="stylesheet"> </head> <body> <video id="example-video" class="video-js vjs-default-skin" controls="controls" width="640" height="360"> <source src="https://s3.eu-west-2.amazonaws.com/net.hlsbook/prog_index.m3u8" type="application/x-mpegURL" /> </video> <script src="http://vjs.zencdn.net/6.4.0/video.js"></script> <script src="https://s3.eu-west-2.amazonaws.com/net.hlsbook/videojs-contrib-hls.min.js"></script> <script> var player = videojs('example-video'); </script> </body>
As you can see (highlighted) all you have to do to play the video is just tell the player where the playlist is. Even though this particular video is encrypted, there is nothing more you have to do on the front-end.
In this instance, the playlist and the encrypted video are stored in an S3 bucket. (If you are interested, I wrote a post recently about how to serve HLS video from an S3 bucket that goes into more detail.)
And finally, here’s an example: