Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

could any one show me how in php i can get different bitrate(resolution) sub m3u8 urls if we have the main playlist m3u8 using get_data method?The following is data i have from get_data method but i want to get m3u8 urls for each resolution. Could any one show me how this can be done?Thanks in advance.

$returned_content = get_data(''.$m3u8Url);
/* gets the data from a URL */
function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;

main playlist m3u8:

  #EXTM3U
    #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1628000,RESOLUTION=852x480,CODECS="avc1.77.30,mp4a.40.2"
    http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/erewewrwrtf34324343443243434344/test1.mpegts/playlist-dfasdfasdfaw4q3243241.m3u8
    #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=128000,RESOLUTION=256x144,CODECS="avc1.66.30,mp4a.40.2"
    http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgdhgfhgjhghfdsdf45454545345435/test1.mpegts/playlist-adfdfghgjdt5t45454542.m3u8
    #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=464000,RESOLUTION=426x240,CODECS="avc1.77.30,mp4a.40.2"
    http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/764563564565445fsdf4r3dfdfdffdf/test1.mpegts/playlist-eertyeryry564534rrtr3.m3u8
    #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=828000,RESOLUTION=640x360,CODECS="avc1.77.30,mp4a.40.2"
    http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgsfdgdfgfdg5435345456745264554/test1.mpegts/playlist-fgsfghdghjt4353454544.m3u8
    #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2128000,RESOLUTION=1024x576,CODECS="avc1.77.30,mp4a.40.2"
    http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/sfdgsdfgfdgfdgfdgfd465436546576/test1.mpegts/playlist-fghdjhygjujdfgsaf4455.m3u8
    #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=3692000,RESOLUTION=1280x720,CODECS="avc1.64001f,mp4a.40.2"
    http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/sfdghgjyuktyurty546565466453645/test1.mpegts/playlist-safdghhgfjjyj45345546.m3u8
                they're just strings, so... basic string operations and maybe a judicious bit of explode() and whatnot.
– Marc B
                May 16, 2014 at 19:19

First off, you need to get data source, then process them (explode() the values, as your sample data is in line breaks), then group them by two's, and in the end loop them. Consider this example:

$curl_output = '#EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1628000,RESOLUTION=852x480,CODECS="avc1.77.30,mp4a.40.2" http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/erewewrwrtf34324343443243434344/test1.mpegts/playlist-dfasdfasdfaw4q3243241.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=128000,RESOLUTION=256x144,CODECS="avc1.66.30,mp4a.40.2" http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgdhgfhgjhghfdsdf45454545345435/test1.mpegts/playlist-adfdfghgjdt5t45454542.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=464000,RESOLUTION=426x240,CODECS="avc1.77.30,mp4a.40.2" http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/764563564565445fsdf4r3dfdfdffdf/test1.mpegts/playlist-eertyeryry564534rrtr3.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=828000,RESOLUTION=640x360,CODECS="avc1.77.30,mp4a.40.2" http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgsfdgdfgfdg5435345456745264554/test1.mpegts/playlist-fgsfghdghjt4353454544.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2128000,RESOLUTION=1024x576,CODECS="avc1.77.30,mp4a.40.2" http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/sfdgsdfgfdgfdgfdgfd465436546576/test1.mpegts/playlist-fghdjhygjujdfgsaf4455.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=3692000,RESOLUTION=1280x720,CODECS="avc1.64001f,mp4a.40.2" http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/sfdghgjyuktyurty546565466453645/test1.mpegts/playlist-safdghhgfjjyj45345546.m3u8'; // process the string $pieces = explode("\n", $curl_output); // make an array out of curl return value unset($pieces[0]); // remove #EXTM3U $pieces = array_map('trim', $pieces); // remove unnecessary space $pieces = array_chunk($pieces, 2); // group them by two's

Formatted pieces should look something like this:

Array
    [0] => Array
        [0] => #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1628000,RESOLUTION=852x480,CODECS="avc1.77.30,mp4a.40.2"
        [1] => http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/erewewrwrtf34324343443243434344/test1.mpegts/playlist-dfasdfasdfaw4q3243241.m3u8
    [1] => Array
        [0] => #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=128000,RESOLUTION=256x144,CODECS="avc1.66.30,mp4a.40.2"
        [1] => http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgdhgfhgjhghfdsdf45454545345435/test1.mpegts/playlist-adfdfghgjdt5t45454542.m3u8

Then, on the html loop and them, and inside the loop process the links:

<?php foreach($pieces as $key => $value): ?>
    <a href="<?php echo $value[1]; ?>">Watch this in 
        $value[0] = explode(',', $value[0]);
        foreach($value[0] as $index => $element) {
            if(stripos($element, 'RESOLUTION') !== false) {
                echo $element;
    </a><br/>
<?php endforeach; ?>

The HTML Markup should now look something like this:

<a href="http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/erewewrwrtf34324343443243434344/test1.mpegts/playlist-dfasdfasdfaw4q3243241.m3u8">Watch this in 
    RESOLUTION=852x480  </a>
<a href="http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgdhgfhgjhghfdsdf45454545345435/test1.mpegts/playlist-adfdfghgjdt5t45454542.m3u8">Watch this in 
    RESOLUTION=256x144  </a>
                thanks but i couldnt make it to work. I tried to echo $pieces to see if the lines are grouped in arrray but no output!
– user1788736
                May 17, 2014 at 4:40
                @user1788736 its hard for me to guess how to parse the data that came from the curl (maybe is \n or \r\n) but if it is just string, you can just modify the answer to suite it accordingly to that data that you have
– user1978142
                May 17, 2014 at 4:45
                kevinabelita i grouped the array successfully. only i dont know how to run your last part of the code(for each). Could you check that part there are some echo missing it think ?
– user1788736
                May 17, 2014 at 5:22
                @user1788736 as provided on the answer, once they are grouped (like the one in the formatted part), you can just foreach them below the php part (the one that processed the data).
– user1978142
                May 17, 2014 at 5:35
                hyperlinks printed correctly.only part now i dont know how to reference a specific resolution(for example RESOLUTION=640x360) m3u8 url and use that m3u8 url else where in my code!For example load it to jw player!
– user1788736
                May 17, 2014 at 5:43

If I understood the quetion right you need to parse a string and get resolution.

function findResolution($string){
    $array = explode(",",$string);
    foreach ($array as $item){
        if (strpos($item,"RESOLUTION")!==false){
            return str_replace("RESOLUTION=","",$item);
                thanks for reply. I want to output resolution value and the url after it. could you tell me how ? i want to create hyper link for each diffrent resolution
– user1788736
                May 16, 2014 at 20:06
    if($handle) {
        while(($line = fgets($handle)) !== FALSE) {
            if(strpos($line,"EXT-X-STREAM-INF") !== FALSE) {
                if ($c=preg_match_all ("/.*?(BANDWIDTH)(.*?)(,)(RESOLUTION)(.*?)(,)/is", $line, $matches)) {
                    $return['data'][$i]['bandwidth'] = str_replace("=","",$matches[2][0]);
                    $return['data'][$i]['resolution'] = str_replace("=","",$matches[5][0]);
            if(strpos($line,".ts") !== FALSE) {
                $return['data'][$i]['url'] = str_replace(array("\r","\n"),"",$line);
                $i++;
        fclose($handle);
    return $return;

That gets you an array with bandwidth, resolution and url keys for each iteration in the original file.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.