Python 检查互联网广播流是否可用/直播的方法 [英] Python Way to check whether internet radio stream is avaliable/live
问题描述
我致力于收集网络广播流文件,例如 m3u,其中包含指向 内部流的链接(例如 http://aska.ru-hoster.com:8053/autodj).
I work on collecting internet radio stream files such as m3u with a link to stream inside (for example http://aska.ru-hoster.com:8053/autodj).
我没有找到关于如何检查链接是否可用/有效的示例.
I didn`t find example on how it is possible to check if the link is avaliable/live.
任何帮助感激!
UPD:
也许主要问题应该是这样的:
Maybe the main question should sound like:
会不会是一条流被破坏了?如果是,该流的链接是否仍然可用,或者浏览器中只会出现 404 错误?如果流已死链接仍然可以打开,还有什么方法可以检查流?
Could be a stream broken? If yes, will the link for that stream be still available or there will be simply 404 error in browser? If link still available to open even stream is dead, what are other methods to check the stream?
推荐答案
您是否正在尝试检查流 URL 是否存在?
如果是,它就像检查任何其他 url 是否存在一样.
Are you trying to check if the streaming URL exists?
If yes, it will be just like checking any other url if it exists.
一种方法是尝试使用 urllib
获取 url 并检查返回的状态代码.
One way will be to try getting url using urllib
and check for returned status code.
200 - 存在
其他任何内容(例如 404) - 不存在或您无法访问它.
200 - Exists
Anything else (e.g. 404) -Doesn't exist or you can't access it.
例如:
import urllib
url = 'http://aska.ru-hoster.com:8053/autodj'
code = urllib.urlopen(url).getcode()
#if code == 200: #Edited per @Brad's comment
if str(code).startswith('2') or str(code).startswith('3') :
print 'Stream is working'
else:
print 'Stream is dead'
EDIT-1
虽然上面会捕获 URL 是否存在.如果 URL 存在且媒体链接已损坏,则不会捕获.
While above will catch if a URL exists or not. It will not catch if URL exists and media link is broken.
使用 vlc
的一种可能解决方案是从 url 获取媒体,尝试播放它并在播放时获取其状态.如果媒体不存在,我们将得到可用于确定链接状态的错误.
One possible solution using vlc
is to get the media from url, try to play it and get its status while playing. If media doesnt exists, we will get error which can be used to determine link status.
通过我们得到的有效网址
With working URL we get
url = 'http://aska.ru-hoster.com:8053/autodj'
>>>
Stream is working. Current state = State.Playing
我们得到损坏的 URL,
With broken URL we get,
url = 'http://aska.ru-hoster.com:8053/autodj12345'
>>>
Stream is dead. Current state = State.Error
下面是实现上述的基本逻辑.您可能需要查看 VLC 站点以捕获其他错误类型和更好的方法.
Below is basic logic to achieve above. You may want to check VLC site to catch other error types and better methods.
import vlc
import time
url = 'http://aska.ru-hoster.com:8053/autodj'
#define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')
#Define VLC player
player=instance.media_player_new()
#Define VLC media
media=instance.media_new(url)
#Set player media
player.set_media(media)
#Play the media
player.play()
#Sleep for 5 sec for VLC to complete retries.
time.sleep(5)
#Get current state.
state = str(player.get_state())
#Find out if stream is working.
if state == "vlc.State.Error" or state == "State.Error":
print 'Stream is dead. Current state = {}'.format(state)
player.stop()
else:
print 'Stream is working. Current state = {}'.format(state)
player.stop()
这篇关于Python 检查互联网广播流是否可用/直播的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!