Reputation: 71
Has anyone had success with reading SHOUTcast/Icecast metadata from a remote radio stream?
There are several libraries that can read metadata from a local MP3 file, but none seem designed to work with a radio stream (which is essentially a never-ending MP3 file on a remote server).
Other recommendations suggest to download a finite number of bits from the beginning of the mp3 stream, but this often leads to a bunch of hex output with nothing resembling text metadata.
Anyone know of a more successful solution? Thanks.
Upvotes: 7
Views: 7025
Reputation: 5531
For anyone else finding themselves here, 10 years later, here is the python3 version of @dbogdan's code. It may be notable that content[metaint:].split("'")[1]
is extremely unreliable. Also, as soon as you come across "Queensrÿche", a non-english title, etc... the title is going to be full of bytes where the special characters are. You can't decode
the entire tag, so there is a little bit of "jumping through hoops" to whittle the tag down to only a title. I did not grab sample rate
and bitrate
from the response headers because, they are constantly wrong. Get that data from the MP3 head. To be fair, the tag Shoutcast/Icecast gives you can be wrong in every way! It only takes one time of coming across "C C Revival - I hearded through the grapevine" (which really happened) to realize there is nothing official or reliable about these tags. This can create huge problems if, for instance: you use Shout/Ice cast tags as search parameters for MusicBrainz searches.
from urllib import request as urequest
import re
SRCHTITLE = re.compile(br'StreamTitle=\\*(?P<title>[^;]*);').search
def get_stream_title(tag:bytes) -> str:
title = ''
if m := SRCHTITLE(tag):
#decode, strip, unescape and remove surrounding quotes (may not even be the same type of quote)
title = m.group('title').decode('utf-8').strip().replace('\\', '')[1:-1]
return title
def id3(url:str) -> dict:
request = urequest.Request(url, headers={'Icy-MetaData': 1})
with urequest.urlopen(request) as resp:
metaint = int(resp.headers.get('icy-metaint', '-1'))
if metaint<0: return False
resp.read(metaint) #this isn't seekable so, arbitrarily read to the point we want
tagdata = dict(
site_url = resp.headers.get('icy-url' ) ,
name = resp.headers.get('icy-name' ).title(),
genre = resp.headers.get('icy-genre').title(),
title = get_stream_title(resp.read(255)) )
return tagdata
Upvotes: 0
Reputation: 152
I used a bit of @dbogdan's code and created a library I use for over 4 thousands streams daily. It works good and is stable and support metadata such as song title, artist name, bitrate and content-type.
you can find it at https://github.com/Dirble/streamscrobbler-python
Upvotes: 0
Reputation: 99
#!/usr/bin/env python
import urllib2
stream_url = 'http://pub1.di.fm/di_classictrance'
request = urllib2.Request(stream_url)
try:
request.add_header('Icy-MetaData', 1)
response = urllib2.urlopen(request)
icy_metaint_header = response.headers.get('icy-metaint')
if icy_metaint_header is not None:
metaint = int(icy_metaint_header)
read_buffer = metaint+255
content = response.read(read_buffer)
title = content[metaint:].split("'")[1]
print title
except:
print 'Error'
For more details check this link
Upvotes: 6
Reputation: 6752
Since mp3 is a proprietary format, the specification is not so easy to come by. This website gives a good overview, I think.
In normal mp3 files, the ID3v1 metadata tag goes at the very end of the file, it makes up the last 128 bytes. This is actually a bad design. The ID3 system was added as an afterthought to mp3, so I guess there was no other way to do it without breaking backward-compatibility. This means that if the radio stream is provided like a never-ending mp3 file, there can be no ID3 tag in the normal sense.
I would check with the people who run the radio station; perhaps they put the ID3 tag in a non-standard place?
Upvotes: -1