无法从一些冰流中提取元数据

时间:2016-10-21 10:23:52

标签: php stream metadata icecast

我正在尝试从流中提取icecast元数据。 我的代码适用于某些流而不适用于其他流。 问题是某些流不返回icymetaint值,而这是代码丢失的地方。

我无法从此流中获取icymetaint标头: http://radio.hbr1.com:19800/tronic.ogg

但是当我把它放在VLC媒体播放器中时,它显示元就好了。 那么我到底错过了什么?还有哪些方法可以让icecast流传输metdata?流版本是Icecast 2.3.3

这是用于检索元数据和标题的类中的代码:

public function GetDataFromStream($parsedUrl)
{
    $returnData = array();
    $addr = $parsedUrl['host'];

    $addr = gethostbyname($addr);

    $sock = fsockopen($addr, $parsedUrl['port'], $errno, $errstr, 5);
    $path = isset($parsedUrl['path'])?$parsedUrl['path']:'/';

    if ($sock)
    {
        $request = 'GET '. $path .' HTTP/1.0' . CRLF .
            'Host: ' . $parsedUrl['host'] . CRLF .
            'Connection: Close' . CRLF .
            'User-Agent: ' . $this->useragent . CRLF .
            'Accept: */*' . CRLF .
            'icy-metadata: 1'.CRLF.
            'icy-prebuffer: 65536'.CRLF.
            (isset($parsedUrl['user']) ? 'Authorization: Basic ' .
            base64_encode($parsedUrl['user'] . ':' . $parsedUrl['pass']) . CRLF : '').
            'X-TipOfTheDay: Winamp "Classic" rulez all of them.' . CRLF . CRLF;

        if (fwrite($sock, $request))
        {
            $theaders = $line = '';

            while (!feof($sock))
            {
                $line = fgets($sock, 4096);

                if('' == trim($line))
                    break;
                $theaders .= $line;
            }

            $theaders = explode(CRLF, $theaders);

            foreach ($theaders as $header)
            {
                $t = explode(':', $header);


                if (isset($t[0]) && trim($t[0]) != '')
                {
                    $name = preg_replace('/[^a-z][^a-z0-9]*/i','', strtolower(trim($t[0])));
                    array_shift($t);
                    $value = trim(implode(':', $t));

                    if ($value != '')
                    {
                        if (is_numeric($value))
                            $this->headers[$name] = (int)$value;
                        else
                            $this->headers[$name] = $value;
                    }
                }
            }

            if (isset($this->headers['icymetaint']))
            {
                $metainterval = $this->headers['icymetaint'];
                $intervals = 0;
                $metadata = '';

                while(1)
                {
                    $data = '';

                    while(!feof($sock))
                    {
                        $data .= fgetc($sock);

                        if (strlen($data) >= $metainterval)
                            break;
                    }

                    $len = join(unpack('c', fgetc($sock))) * 16;

                    if ($len > 0)
                    {
                        $metadata = str_replace("\0", '', fread($sock, $len));
                        break;
                    }
                    else
                    {
                        $intervals++;
                        if ($intervals > 100) break;
                    }
                }

                $metarr = explode(';', $metadata);

                foreach ($metarr as $meta)
                {
                    $t = explode('=', $meta);

                    if (isset($t[0]) && trim($t[0]) != '')
                    {
                        $name = preg_replace('/[^a-z][^a-z0-9]*/i','', strtolower(trim($t[0])));

                        array_shift($t);

                        $value = trim(implode('=', $t));

                        if (substr($value, 0, 1) == '"' || substr($value, 0, 1) == "'")
                            $value = substr($value, 1);

                        if (substr($value, -1) == '"' || substr($value, -1) == "'")
                            $value = substr($value, 0, -1);

                        if ($value != '')
                        {
                            $tmp = &$this->metadata;
                            $tmp[$name] = $value;
                        }
                    }
                }
                $this->valid = true;
            }
            else
            {
                $this->valid = false;
            }

            fclose($sock);
        }
        else
            echo 'unable to write.';
    }
    else
        //echo 'no socket '.$errno.' - '.$errstr.'.';
        ;

}

2 个答案:

答案 0 :(得分:2)

您可以使用.xspf mountpoint扩展,获取XML并解析它:

<?php
$stream_url = "http://radio.hbr1.com:19800/tronic.ogg";
$xspf_url = $stream_url . ".xspf";
$xml = file_get_contents($xspf_url);
if($xml){
    $data = simplexml_load_string($xml);
    // Track artist
    print $data->trackList->track->creator;
    // Track title
    print $data->trackList->track->title;
}
?>

以下是.xspf数据的显示方式(我使用lynx来读取网址内容):

$ lynx -mime_header http://radio.hbr1.com:19800/tronic.ogg.xspf
HTTP/1.0 200 OK
Content-Type: application/xspf+xml
Content-Length: 615

<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="http://xspf.org/ns/0/" version="1">
  <title/>
  <creator/>
  <trackList>
    <track>
      <location>http://radio.hbr1.com:19800/tronic.ogg</location>
      <creator>Res Q</creator>
      <title>Fakesleep (2012)</title>
      <annotation>Stream Title: HBR1 - Tronic Lounge
Stream Description: Music on Futurenet
Content Type:application/ogg
Bitrate: Quality 0,00
Current Listeners: 28
Peak Listeners: 45
Stream Genre: Tech House, Progressive House, Electro, Minimal</annotation>
      <info>http://www.hbr1.com</info>
    </track>
  </trackList>
</playlist>

正如您所看到的,/playlist/trackList/track/title XML节点是您的歌曲标题,/playlist/trackList/track/creator通常是艺术家。

答案 1 :(得分:0)

那是因为你只是试图解析Nullsoft在Shoutcast中引入的大脑死亡古代元数据滑流。

正确的流使用容器(例如Ogg或WebM)而不是丢弃原始数据。

较新的Icecast服务器提供JSON API(版本2.4.1及更高版本)。这比仅为元数据提取整个流更有用。

如果你正在解码流,那么你应该考虑使用适当的库来解析流,libogg,libopus,libvorbis。