Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 381
yujas Avatar asked Jul 07 '11 16:07

yujas


1 Answers

#!/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

like image 175
dbogdan Avatar answered Oct 03 '22 00:10

dbogdan