Search code examples
phpffmpegvideo-streamingvideo.jshttp-live-streaming

Creating master playlist for HLS


I am trying to implement adaptive stream using HLS I have video encoded in 4 different resolution with .m3u8 extension

legend_240.m3u8
legend_360.m3u8
legend_480.m3u8
legend_720.m3u8

I encoded them using FFMPEG now I want to wrap them all in a master HLS playlist. How can I achieve this in an automated process?

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=17556000,RESOLUTION=428x240
legend_240.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=28556000,RESOLUTION=640x360
legend_360.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=56056000,RESOLUTION=854x480
legend_480.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=73056000,RESOLUTION=1280x720
legend_720.m3u8

Solution

  • I figured it out using file handling in php.

            $myfile = fopen($this->raw_path."/".$this->file_name.".m3u8", "w") or die("Unable to open file!");
    
            $txt = "#EXTM3U\n";
    
            fwrite($myfile, $txt);
    
            $txt = "#EXT-X-VERSION:3\n";
    
            fwrite($myfile, $txt);
            // fclose($myfile);
            if($convertedRes['720']){
    
            $txt = "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=73056000,RESOLUTION=1280x720\n";
            fwrite($myfile, $txt);
            $txt = $this->file_name."/".$this->file_name."-720.m3u8\n";
            fwrite($myfile, $txt);
    
            }
            if($convertedRes['480']){
    
            $txt = "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=5605600,RESOLUTION=854x480\n";
            fwrite($myfile, $txt);
            $txt = $this->file_name."/".$this->file_name."-480.m3u8\n";
            fwrite($myfile, $txt);
    
            }
    
            if($convertedRes['360']){
    
            $txt = "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2855600,RESOLUTION=640x360\n";
            fwrite($myfile, $txt);
            $txt = $this->file_name."/".$this->file_name."-360.m3u8\n";
            fwrite($myfile, $txt);
    
            }
    
            if($convertedRes['240']){
    
    
            $txt = "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1755600,RESOLUTION=428x240\n";
            fwrite($myfile, $txt);
            $txt = $this->file_name."/".$this->file_name."-240.m3u8\n";
            fwrite($myfile, $txt);
    
    
            }
    
    
    fclose($myfile);