Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I play a m3u8 (file) video using the HTML5 <video> element?

I came across this stackoverflow link that talks about playing a m3u8 file: Playing m3u8 Files with HTML Video Tag

I've tried doing something similar to play the video link in the m3u8 file like on phpfiddle:

    echo '<video width="352" height="198" controls>
<source src="https://udemy-adaptive-streaming-prod.udemy.com/9287/72689/2012-04-30_04-09-49-f5ad53b1736807ee7f8837b37115aeeb/hls/677cda5a7077be8d22348b5edebd77db.m3u8?sign=%252BCIehx2LKCxUcNSU33mWdfm5SbA%253D&mign=EEsJDxEabAoMa1kFRgIfbEkIDw8RHGwKDGtZXAFYS3lHSwgIGEoJUl57U1sfTBQlBTYIFRkNEVlZfVtaAl5Dc15fAQ==&quality=HD" type="application/x-mpegURL"></video>';

But it's not working. It seems to show the video element but the video doesn't get loaded in it. Is it possible to play m3u8 files this way? The m3u8 file that I want to play is inside the "https://www.udemy.com/excel-tutorial/

Thanks for any help.

like image 510
vjks Avatar asked Dec 07 '16 09:12

vjks


People also ask

How do I stream M3U8 in HTML?

Stream HLS or m3u8 files using above code. it works for desktop: ms edge browser (not working with desktop chrome) and mobile: chrome,opera mini browser. if you want to play direct . m3u8 (HLS file), then add chrome extension called Native HLS Playback from chrome web app.

How do I play an M3U8 video?

If you're looking for a good program to open and use M3U8 files, try VLC, Apple's iTunes, or Songbird. Another way to open this file format on Linux is with XMMS, while Mac users should be able to use CocoModX (in addition to some of the those Windows-compatible programs).

How do I open a M3U8 file in my browser?

Castr Live Streaming Player Just put the . m3u8 file into the URL box and click Play Stream button, you can easily view M3U8 on web. You should make sure that, your streaming URL must be HTTPS compatible. When you stream HLS or DASH, you need to enable CORS.

Can HTML5 play HLS?

There are actually a few HTML5 players available which are capable of playing back HLS streams. One example would be the Bitmovin Player, which offers professional support as well as a fallback for legacy browsers. It's a commercial product, but they also provides a free plan. Also open-source projects like hls.


1 Answers

Use the JavaScript HLS client hls.js package found on github. Used by many established organizations. Works on all browsers.

A quick example page:

<html>
  <body>
    <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
    <video id="video" controls></video>
    <script>
    if(Hls.isSupported())
    {
        var video = document.getElementById('video');
        var hls = new Hls();
        hls.loadSource('playlist.m3u8');
        hls.attachMedia(video);
        hls.on(Hls.Events.MANIFEST_PARSED,function()
        {
            video.play();
        });
    }
    else if (video.canPlayType('application/vnd.apple.mpegurl'))
    {
        video.src = 'playlist.m3u8';
        video.addEventListener('canplay',function()
        {
            video.play();
        });
    }
    </script>
  </body>
</html>

Replace 'playlist.m3u8' with your playlist.

like image 68
Jahmic Avatar answered Oct 20 '22 08:10

Jahmic