Python3从互联网广播流中获取歌曲名称

2024-05-21 07:54:55 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从网络广播流中获取歌曲名称?在

Python: Get name of shoutcast/internet radio station from url我看了这里,但只有电台的名字。但如何得到播放歌曲的名字呢?这里是流链接,我想从这里得到歌曲的名字。http://pool.cdn.lagardere.cz/fm-evropa2-128

我该怎么做?你能帮帮我吗?在


Tags: ofnamefrom网络urlget链接名字
1条回答
网友
1楼 · 发布于 2024-05-21 07:54:55

要获得流标题,需要请求元数据。见shoutcast/icecast protocol description

#!/usr/bin/env python
from __future__ import print_function
import re
import struct
import sys
try:
    import urllib2
except ImportError:  # Python 3
    import 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 _ in range(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:
            break
else: 
    sys.exit('no title found')
print(title.decode(encoding, errors='replace'))

在本例中,流标题为空。在

相关问题 更多 >