Skip to content

Instantly share code, notes, and snippets.

@sonyarianto
Created December 27, 2020 08:35
Show Gist options
  • Save sonyarianto/c46196df1e5f393a2859240d8dde332a to your computer and use it in GitHub Desktop.
Save sonyarianto/c46196df1e5f393a2859240d8dde332a to your computer and use it in GitHub Desktop.
Read Shoutcast or Icecast Stream Metadata
<?php
function getStreamMetadata($streamUrl) {
$needle = 'StreamTitle=';
$ua = 'Dailymate Radio/1.0';
$opts = ['http' => ['method' => 'GET',
'header' => 'Icy-MetaData: 1',
'user_agent' => $ua]
];
$context = stream_context_create($opts);
$icyMetaIntFound = false;
$icyInterval = -1;
$offset = 0;
if(($headers = get_headers($streamUrl, 0, $context))) {
foreach($headers as $h) {
if(!(strpos(strtolower($h), 'icy-metaint:') === false)) {
$icyMetaIntFound = true;
$icyInterval = explode(':', $h)[1];
break;
}
}
}
if(!$icyMetaIntFound) {
echo "icy-metaint header not exists!";
return;
}
if($stream = fopen($streamUrl, 'r', false, $context)) {
while($buffer = stream_get_contents($stream, $icyInterval, $offset)) {
if(strpos($buffer, $needle) !== false) {
fclose($stream);
$title = explode($needle, $buffer)[1];
return substr($title, 1, strpos($title, ';') - 2);
}
$offset += $icyInterval;
}
}
}
echo getStreamMetadata('http://sukmben.radiogentara.com:8080/gentarahd');
// var_dump(getStreamMetadata('https://freeuk16.listen2myradio.com/live.mp3?typeportmount=s1_23369_stream_600894294'));
// var_dump(getStreamMetadata('http://pu.klikhost.com:7720/;'));
@starapple2
Copy link

What's Dailymate Radio/1.0 and what does it do?

@sonyarianto
Copy link
Author

@starapple2 it just user agent string

@starapple2
Copy link

@starapple2 it just user agent string

Thanks @sonyarianto . What is the importance in this script? Is it a Web standard?

@starapple2
Copy link

Nice script @sonyarianto. I called it in an AJAX request, put the result in a span and applied CSS scrolling and ended up with a nice player that is updated every 15 secs with JavaScript setInterval:
Media_Player

@sonyarianto
Copy link
Author

@starapple2 it just user agent string

Thanks @sonyarianto . What is the importance in this script? Is it a Web standard?

ya it just to tell what User Agent that access that URL, you can type anything

@sonyarianto
Copy link
Author

Nice script @sonyarianto. I called it in an AJAX request, put the result in a span and applied CSS scrolling and ended up with a nice player that is updated every 15 secs with JavaScript setInterval: Media_Player

wow this is very nice

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