PHP Parse HLS M3U8 TS Links

Input

encoded-03-31-16-thu-jun-2017.m3u8

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=654000,RESOLUTION=568×320,CODECS=”avc1.4d0029,mp4a.40.2″
1080p/encoded-03-31-16-thu-jun-2017.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2849000,RESOLUTION=568×320,CODECS=”avc1.42001f,mp4a.40.2″
720p/encoded-03-31-16-thu-jun-2017.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1033000,RESOLUTION=568×320,CODECS=”avc1.42001e,mp4a.40.2″
360p/encoded-03-31-16-thu-jun-2017.m3u8

encoded-03-31-16-thu-jun-2017.360.m3u8

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:13
#EXT-X-KEY:METHOD=AES-128,URI=”encoded-03-31-16-thu-jun-2017.key”,IV=0x5fd83c919c46159f14ec8da38a4d9ca7
#EXTINF:12.046444,
encoded-03-31-16-thu-jun-201700000.ts
#EXTINF:9.000000,
encoded-03-31-16-thu-jun-201700001.ts
#EXTINF:9.000000,
encoded-03-31-16-thu-jun-201700002.ts
#EXTINF:12.000000,
encoded-03-31-16-thu-jun-201700003.ts
.
.
.
#EXT-X-ENDLIST

Code

<?php

function parseHLS($file, $tag=".ts") {
$return = array();
$i = 0;
$handle = fopen($file, "r");
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($tag==".ts") {
if(strpos($line,".ts") !== FALSE) {
$return['data'][$i]['url'] = str_replace(array("\r","\n"),"",$line);
$i++;
}
}

if($tag==".key") {
if(strpos($line,".key") !== FALSE) {  

preg_match( '/URI="([^"]*)"/i', $line, $array );
//print_r( $array[1] ) ;

//$return['data'][$i]['url'] = str_replace(array("\r","\n"),"",$line);
$return['data'][$i]['url'] = str_replace(array("\r","\n"),"",$array[1]);

$i++;
}
}

if($tag==".m3u8") {
if(strpos($line,".m3u8") !== FALSE) {
$return['data'][$i]['url'] = str_replace(array("\r","\n"),"",$line);
$i++;
}
}

}
fclose($handle);
}
return $return;
}


echo "<pre>";

// Get other m3u8 files from m3u8 master manifest 
$u = parseHLS("encoded-03-31-16-thu-jun-2017.m3u8", ".m3u8");

print_r($u);

// Get TS segment files from m3u8 manifest 
$u = parseHLS("encoded-03-31-16-thu-jun-2017.360.m3u8", ".ts");

print_r($u);

// Get key file from m3u8 manifest 
$u = parseHLS("encoded-03-31-16-thu-jun-2017.360.m3u8", ".key");

print_r($u);

echo "</pre>";

?>

Output

Array
(
[data] => Array
(
[0] => Array
(
[bandwidth] => 654000
[resolution] => 568x320
[url] => 1080p/encoded-03-31-16-thu-jun-2017.m3u8
)

[1] => Array
(
[bandwidth] => 2849000
[resolution] => 568x320
[url] => 720p/encoded-03-31-16-thu-jun-2017.m3u8
)

[2] => Array
(
[bandwidth] => 1033000
[resolution] => 568x320
[url] => 360p/encoded-03-31-16-thu-jun-2017.m3u8
)

)

)

Array
(
[data] => Array
(
[0] => Array
(
[url] => encoded-03-31-16-thu-jun-201700000.ts
)

[1] => Array
(
[url] => encoded-03-31-16-thu-jun-201700001.ts
)

[2] => Array
(
[url] => encoded-03-31-16-thu-jun-201700002.ts
)

[3] => Array
(
[url] => encoded-03-31-16-thu-jun-201700003.ts
)
....
.....
)
)

Array
(
[data] => Array
(
[0] => Array
(
[url] => encoded-03-31-16-thu-jun-2017.key
)

)

)

Published by

Sandeep Verma

I’m an Entrepreneur. I’m proud to work as Blogger, LAMP Programmer, Linux Admin, Web Consultant, Cloud Manager, Apps Developer, Searcher. Concentrate > Observe > Imagine > Launch