Skip to content

Instantly share code, notes, and snippets.

@fracasula
Last active February 26, 2024 21:45
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save fracasula/5781710 to your computer and use it in GitHub Desktop.
Save fracasula/5781710 to your computer and use it in GitHub Desktop.
How to get the MP3 metadata (StreamTitle) from a streaming 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://str30.creacast.com/r101_thema6', 19200));
echo "\n\n";
@kellito14
Copy link

I tried your code and for a while it worked but all of a sudden, it stopped working.

@brdn
Copy link

brdn commented Feb 10, 2019

curl version?

@ko-n
Copy link

ko-n commented Apr 5, 2020

curl version?
cURL version.

@two-t-fruity
Copy link

two-t-fruity commented Jun 25, 2020

Hi

Can anybody tell me why this code works on localhost, all stations I try, but 3 or 4 will not when on a server

PHP version 7.4 on localhost all stations work - server PHP version 5.5 about 3-4 do not work.

I tried all PHP versions available on the server and only 5.5 works at all

@rthymis
Copy link

rthymis commented Dec 11, 2021

Hello,
When I run the script it returns the Artist and Title correctly but it does not update automatically when the next track plays.
Is this possible or do I need extra code to achieve this?

@aigarspl
Copy link

@rthymis You can achieve this by 2 ways (okay, there are more ways, but I'm giving common ones) - create cron task on server or call this script with AJAX request.

@RadioActive913
Copy link

Hello, I’ve been using this snippet in my foreign music internet radio for few months now and its working great… !
I’m not a php programmer, a developer did this work a while ago. I have basic programming skills.
Right now, I ‘m trying to build a new internet radio that will play Greek music.
All the tracks that play there have Greek characters for Artist and Title in their tags.
AIMP player plays the stream and picks up the Artist / Title metadata (directly from the mp3 stream), and displays them correctly in Greek no problem as Artist and Title.
The Greek radio stream is : https://streamyourdream.org:8138/stream and the foreign music radio is https://streamyourdream.org:8050/radioactive

Using this php snippet (from fracasula) (which is a part of my API), see here https://radioactivefm.gr/live/api/getSongJson.php, I modified the calling code for the new Greek radio and created this https://radioactivefm.gr/live/api/getGreekSongJson.php but it never returns anything!

When I changed the MP3 tags to English characters in one track and called the getGreekSongJson.php then it did work fine, so the function isnt't working with the Greek character set.

Here's the phps that I use:
grk_functions.php

`<?php

$xml = simplexml_load_file("grk_config.xml");
$StreamingUrl = '';
$BackupUrl = '';
$ThirdUrl =  '';
$NoPlayerMessage = '';
$MessageSent = '';
$ReplyPageReturn = '';
$SongLiked = '';
$SongAlreadyLiked = '';
$MiniUrl = '';
$MiniMessage = '';
$SlideShowVisible = 'false';
$LikeFacebookMessage = '';
if (is_object($xml))
{
foreach($xml->children() as $child) {
	if ($child->getName() == 'Url') $StreamingUrl =  $child;
	if ($child->getName() == 'BackupUrl') $BackupUrl =  $child;
	if ($child->getName() == 'ThirdUrl') $ThirdUrl =  $child;
	if ($child->getName() == "NoPlayer") $NoPlayerMessage =  $child;
	if ($child->getName() == 'MessageSent') $MessageSent =  $child;
	if ($child->getName() == 'ReplyPageReturn') $ReplyPageReturn =  $child;
	if ($child->getName() == 'SongLiked') $SongLiked =  $child;
	if ($child->getName() == 'SongAlreadyLiked') $SongAlreadyLiked =  $child;
	if ($child->getName() == 'MiniUrl') $MiniUrl =  $child;
	if ($child->getName() == 'MiniMessage') $MiniMessage =  $child;
	if ($child->getName() == 'SlideShowVisible') $SlideShowVisible =  $child;
	if ($child->getName() == 'LikeFacebookMessage') $LikeFacebookMessage =  $child;
}
}


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}]");
	}
}

`

grk_api_func.php
`<?php
chdir('..');
include "grk_functions.php";
/**
*

  • Array 2 XML class
  • Convert an array or multi-dimentional array to XML
  • @author Kevin Waterson
  • @copyright 2009 PHPRO.ORG

*/
class array2xml extends DomDocument
{

public $nodeName;

private $xpath;

private $root;

private $node_name;


/**
* Constructor, duh
*
* Set up the DOM environment
*
* @param    string    $root        The name of the root node
* @param    string    $nod_name    The name numeric keys are called
*
*/
public function __construct($root='root', $node_name='node')
{
    parent::__construct();

    /*** set the encoding ***/
    $this->encoding = "utf-8";

    /*** format the output ***/
    $this->formatOutput = true;

    /*** set the node names ***/
    $this->node_name = $node_name;

    /*** create the root element ***/
    $this->root = $this->appendChild($this->createElement( $root ));

    $this->xpath = new DomXPath($this);
}

/*
* creates the XML representation of the array
*
* @access    public
* @param    array    $arr    The array to convert
* @aparam    string    $node    The name given to child nodes when recursing
*
*/
public function createNode( $arr, $node = null)
{
    if (is_null($node))
    {
        $node = $this->root;
    }
    foreach($arr as $element => $value) 
    {
        $element = is_numeric( $element ) ? $this->node_name : $element;

        $child = $this->createElement($element);
        $node->appendChild($child);

        if (is_array($value))
        {
            self::createNode($value, $child);
        }
		else if (strlen($value) > 0)
		{
			$child->appendChild($this->createCDataSection($value));
		}
    }
}
/*
* Return the generated XML as a string
*
* @access    public
* @return    string
*
*/
public function __toString()
{
    return $this->saveXML();
}

/*
* array2xml::query() - perform an XPath query on the XML representation of the array
* @param str $query - query to perform
* @return mixed
*/
public function query($query)
{
    return $this->xpath->evaluate($query);
}

} // end of class

?>`

getGreekSongJson.php

`<?
header('Access-Control-Allow-Origin: *');

include "grk_api_func.php";

$song = getMp3StreamTitle('https://streamyourdream.org:8138/stream', 19200);

$array = array(
array(
'songTitle'=>htmlentities($song))
);

try
{
echo json_encode($array, JSON_UNESCAPED_SLASHES);
}
catch (Exception $e)
{
echo $e->getMessage();
}

?>`

grk_config.xml

<?xml version="1.0" encoding="utf-8"?> <Config> <Url>https://streamyourdream.org:8138/stream</Url> <BackupUrl>https://streamyourdream.org:8138/stream</BackupUrl> <ThirdUrl>https://streamyourdream.org:8138/stream</ThirdUrl> <MiniUrl>https://streamyourdream.org:8138/stream</MiniUrl> <SlideShowVisible>true</SlideShowVisible> <NoPlayer>We are sorry but currently there's no streaming server available. Please try again later by refreshing this page</NoPlayer> <MessageSent>Message Sent!</MessageSent> <ReplyPageReturn>Click here to return</ReplyPageReturn> <SongLiked>was added to database. Thank you!</SongLiked> <SongAlreadyLiked>has been already added to database. Thank you!</SongAlreadyLiked> <LikeFacebookMessage>#GnisiosTracks</LikeFacebookMessage> <MiniMessage>This is the NON-Members Player!!!</MiniMessage> </Config>

I would appreciate any help in fixing this!

Thank you again…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment