检查互联网广播流是否可用/实时的Python方法 [英] Python Way to check whether internet radio stream is avaliable/live

查看:73
本文介绍了检查互联网广播流是否可用/实时的Python方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我致力于收集互联网广播流文件,例如 m3u ,并提供指向内部流的链接(例如

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获取网址并检查返回的状态码.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆