获取音频流元数据的问题

时间:2021-05-25 09:29:29

标签: php audio-streaming

我在这里使用以下 PHP 代码:Pulling Track Info From an Audio Stream Using PHP 从音频流中提取元数据,如下所示,但无法从流中获取元数据,而且我无法弄清楚其他正常工作的 URL 之间的区别以及为什么这个失败。

我已经尝试分析(尽我所能)进入流中的信息,我可以看到元数据在流中,但由于某种原因,它无法带回任何数据,只是坐在那里永远加载.对于其他流,它似乎工作正常,但不是这个。

<?php

/**
 * Please be aware. This gist requires at least PHP 5.4 to run correctly.
 * Otherwise consider downgrading the $opts array code to the classic "array" syntax.
 */
function getMp3StreamTitle($streamingUrl, $interval, $offset = 0, $headers = true)
{
    $needle = 'StreamTitle=';
    $ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36';

    $opts = [
            'http' => [
            'method' => 'GET',
            'header' => 'Icy-MetaData: 1',
            'user_agent' => $ua
        ]
    ];

    if (($headers = get_headers($streamingUrl))) {
        foreach ($headers as $h) {
            if (strpos(strtolower($h), 'icy-metaint') !== false && ($interval = explode(':', $h)[1])) {
                break;
            }
        }
    }

    $context = stream_context_create($opts);

    if ($stream = fopen($streamingUrl, 'r', false, $context)) {
        $buffer = stream_get_contents($stream, $interval, $offset);
        fclose($stream);

        if (strpos($buffer, $needle) !== false) {
            $title = explode($needle, $buffer)[1];
            return substr($title, 1, strpos($title, ';') - 2);
        } else {
            return getMp3StreamTitle($streamingUrl, $interval, $offset + $interval, false);
        }
    } else {
        throw new Exception("Unable to open stream [{$streamingUrl}]");
    }
}

var_dump(getMp3StreamTitle('http://ais-sa2.cdnstream1.com/2210_128.mp3', 19200));

工作流和上面的(不起作用)都包含这一行:

ICY Info: StreamTitle='Artist Info - Track Info';

但我不明白为什么上面例子中的流不起作用。

有什么想法吗?谢谢。

0 个答案:

没有答案