我将使用互联网流作为DYI闹钟,并希望检查给定的流是否正确。通过正确我的意思是流数据存在,如果它实际上携带音频。
“有”部分由一个简单的电话处理:
import requests
urls = [
'http://statslive.infomaniak.ch/playlist/energy90s/energy90s-high.mp3/playlist.pls',
'http://stream.chantefrance.com/stream_chante_france.mp3',
'http://streaming.radio.rtl2.fr/rtl2-1-44-128',
'http://fakestream.example.com'
]
for url in urls:
try:
r = requests.get(url, stream=True)
except Exception as e:
print('failed to get stream: {e}'.format(e=e))
else:
if r.ok:
content_type = r.headers['content-type']
print('stream {url} is of type {content_type}'.format(url=url, content_type=content_type))
else:
print('error getting the stream: {error}'.format(error=r.text))
r = None # I do not know how I should really 'close' the stream
此输出指示我有哪种流,如果有的话
stream http://statslive.infomaniak.ch/playlist/energy90s/energy90s-high.mp3/playlist.pls is of type audio/x-scpls
stream http://stream.chantefrance.com/stream_chante_france.mp3 is of type audio/mpeg
stream http://streaming.radio.rtl2.fr/rtl2-1-44-128 is of type audio/mpeg
failed to get stream: ('Connection aborted.', gaierror(11001, 'getaddrinfo failed'))
我现在想检查(示例?)流以确保有音频数据。这是为了避免拥有正确的流但是会因某种原因而广播(请记住这是因为闹钟和生产关键数据:))。
如何检查此类信息?
我看到有sndhdr,但似乎是针对成熟的音频文件。