Skip to content Skip to sidebar Skip to footer

Python 3 Get Song Name From Internet Radio Stream

How can I get song name from internet radio stream? Python: Get name of shoutcast/internet radio station from url I looked here, but there is only getting name of radio station. Bu

Solution 1:

To get the stream title, you need to request metadata. See shoutcast/icecast protocol description:

#!/usr/bin/env pythonfrom __future__ import print_function
import re
import struct
import sys
try:
    import urllib2
except ImportError:  # Python 3import urllib.request as urllib2

url = 'http://pool.cdn.lagardere.cz/fm-evropa2-128'# radio stream
encoding = 'latin1'# default: iso-8859-1 for mp3 and utf-8 for ogg streams
request = urllib2.Request(url, headers={'Icy-MetaData': 1})  # request metadata
response = urllib2.urlopen(request)
print(response.headers, file=sys.stderr)
metaint = int(response.headers['icy-metaint'])
for _ inrange(10): # # title may be empty initially, try several times
    response.read(metaint)  # skip to metadata
    metadata_length = struct.unpack('B', response.read(1))[0] * 16# length byte
    metadata = response.read(metadata_length).rstrip(b'\0')
    print(metadata, file=sys.stderr)
    # extract title from the metadata
    m = re.search(br"StreamTitle='([^']*)';", metadata)
    if m:
        title = m.group(1)
        if title:
            breakelse: 
    sys.exit('no title found')
print(title.decode(encoding, errors='replace'))

The stream title is empty in this case.

Post a Comment for "Python 3 Get Song Name From Internet Radio Stream"