3

如果我们有使用 get_data 方法的主播放列表 m3u8,谁能告诉我如何在 php 中获得不同的比特率(分辨率)子 m3u8 url?以下是我从 get_data 方法获得的数据,但我想为每个分辨率获取 m3u8 url。谁能告诉我如何做到这一点?提前谢谢。

$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;
}

主播放列表 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
4

3 回答 3

3

首先,您需要获取数据源,然后处理它们(explode()值,因为您的示例数据在换行符中),然后将它们按两个分组,最后循环它们。考虑这个例子:

<?php

$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

?>

格式化的片段应如下所示:

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
    )
    ...

然后,在 html 循环和它们上,在循环内部处理链接:

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

HTML 标记现在应该如下所示:

<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>
于 2014-05-17T00:11:02.550 回答
0

如果我理解正确的问题,您需要解析一个字符串并获得解决方案。

function findResolution($string){
    $array = explode(",",$string);
    foreach ($array as $item){
        if (strpos($item,"RESOLUTION")!==false){
            return str_replace("RESOLUTION=","",$item);
        }
    }
}
于 2014-05-16T19:32:13.037 回答
0

为什么不使用 fopen/fgets?

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

这将为您提供一个数组,其中包含原始文件中每次迭代的带宽、分辨率和 url 键。

于 2015-03-12T22:29:33.153 回答