Sagarika Panda
Sagarika Panda

Reputation: 41

how to parse a m3u8 playlist to get the bandwidths using javascript?

I have a .m3u8 file.I want to parse it using javascript to get the bandwidths.the playlist is attached below

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=200000, RESOLUTION=720x480
http://ALPHA.mycompany.com/lo/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=200000, RESOLUTION=720x480
http://BETA.mycompany.com/lo/prog_index.m3u8

#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=500000, RESOLUTION=1920x1080
http://ALPHA.mycompany.com/md/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=500000, RESOLUTION=1920x1080
http://BETA.mycompany.com/md/prog_index.m3u8

Upvotes: 3

Views: 7918

Answers (3)

Raman Plaha
Raman Plaha

Reputation: 56

why not use hls.js they have a api which can do the above for you.

Check the link out https://github.com/video-dev/hls.js/blob/master/docs/API.md#hlslevels

Instantiate the hls library and then console.log(hls.levels) This will give you an array of data that contains all of the parsed .m3u8 details.

Hope that helps

Upvotes: 2

Sagarika Panda
Sagarika Panda

Reputation: 41

<script>
            $.get( "sl.m3u8", function( data ) {
                    $content=data;
                    $arr=data.split("\n");
                    $removeItem = $arr[0];
                        $arr = jQuery.grep($arr, function(value) {
                          return value !== $removeItem;
                             });
                             $arr1=[];
                             $arr2=[];
                    $.each($arr, function( index, value ) {
                            var pattern = /#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=[0-9]*/;
                            if(pattern.test(value))
                            {
                                var band=value.split("BANDWIDTH=")[1];
                                $arr1.push(band);
                            }
                            else
                            {
                                $arr2.push(value);
                            }
                    });
                    for (var i=0;i<$arr1.length;i++){
                        $('<option/>').val(i).html($arr1[i]).appendTo('#items');
                     }
                   $('#items').change(function(v){
                       myFunction($arr2[v.target.value]);
                  });

                });

       </script>

Upvotes: 1

Daniel
Daniel

Reputation: 1402

try this code:

var lines = file.split(/[\r\n]/);
for (var i in lines){
    var line = lines[i];
    if (/BANDWIDTH/.test(line)) {
        var keyVal = line.split(/ /)[1],
            val = keyVal.split(/[=,]/)[1];
        console.log(val);
    }
}

hope it helps.

Upvotes: 2

Related Questions