Last active
July 23, 2024 19:53
-
-
Save fracasula/5781710 to your computer and use it in GitHub Desktop.
How to get the MP3 metadata (StreamTitle) from a streaming URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; |
Hello. Great script!)
Please tell me how I can grab the stream name from Metadata? (Stream-Name)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
`
grk_api_func.php
`<?php
chdir('..');
include "grk_functions.php";
/**
*
*/
class array2xml extends DomDocument
{
} // 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…