问题描述
如何从互联网广播流中获取歌曲名称?
How can I get song name from internet radio stream?
Python:获取shoutcast/互联网广播的名称从url接收电台我在这里看,但是只有广播电台的名称.但是,如何获得正在播放的歌曲的名称?这是我要获取歌曲名称的流链接. http://pool.cdn.lagardere.cz/fm-evropa2-128
Python: Get name of shoutcast/internet radio station from url I looked here, but there is only getting name of radio station. But how to get name of the playing song? Here is stream link from where I want to get name of song. http://pool.cdn.lagardere.cz/fm-evropa2-128
我应该怎么做?你能帮我吗?
How should I do it? Can you help me please?
推荐答案
要获取流标题,您需要请求元数据.请参见 shoutcast/icecast协议说明:
To get the stream title, you need to request metadata. See 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'))
在这种情况下,流标题为空.
The stream title is empty in this case.
这篇关于Python 3从互联网广播流中获取歌曲名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!