Reading SHOUTcast/Icecast metadata from a radio stream with Python
Has anyone had success with reading SHOUTcast/Icecast metadata from a remote radio stream?
There are several libraries that can read 开发者_StackOverflowmetadata 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.
#!/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
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
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?
精彩评论