Python:使用 mplayer 解析流标题

标签 python parsing stream subprocess mplayer

我正在用 Python 编写一个简单的前端来使用 mplayer(在子进程中)播放和录制互联网广播 channel (例如来自 shoutcast)。当用户单击一个站点时,将运行以下代码:


url = http://77.111.88.131:8010 # only an example
cmd = "mplayer %s" % url
p = subprocess.Popen(cmd.split(), shell=False)
wait = os.waitpid(p.pid, 1)
return int(p.pid)

这非常有效,流开始正常播放。虽然我想以某种方式解析流的标题。看来我需要从 mplayer 输出中获取标题。这是我在终端中播放流时的输出:

$ mplayer http://77.111.88.131:8010
MPlayer 1.0rc4-4.4.5 (C) 2000-2010 MPlayer Team
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing http://77.111.88.131:8010.
Resolving 77.111.88.131 for AF_INET6...
Couldn't resolve name for AF_INET6: 77.111.88.131
Connecting to server 77.111.88.131[77.111.88.131]: 8010...
Name   : Justmusic.Fm
Genre  : House
Website: http://www.justmusic.fm
Public : yes
Bitrate: 192kbit/s
Cache size set to 320 KBytes
Cache fill:  0.00% (0 bytes)   
ICY Info: StreamTitle='(JustMusic.FM) Basement - Zajac, Migren live at Justmusic 2010-10-09';StreamUrl='http://www.justmusic.fm';
Cache fill: 17.50% (57344 bytes)   
Audio only file format detected.

然后它会一直运行直到停止。所以问题是,如何检索“(JustMusic.FM) Basement - Zajac, Migren live at Justmusic 2010-10-09”并且仍然让进程运行?我不认为 subprocess() 实际上存储了输出,但我可能错了。非常感谢任何帮助:)

最佳答案

stdout 参数设置为 PIPE,您将能够收听命令的输出:

p= subprocess.Popen(['mplayer', url], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout:
    if line.startswith('ICY Info:'):
        info = line.split(':', 1)[1].strip()
        attrs = dict(re.findall("(\w+)='([^']*)'", info))
        print 'Stream title: '+attrs.get('StreamTitle', '(none)')

关于Python:使用 mplayer 解析流标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3929318/

相关文章:

python - a[ :]=b and a=b[:] 之间有什么区别

python - 如何使用索引更新张量的元素?

java - 如何在 J2ME 中解析 URL

java - 缓冲输入流中标记读取限制的用途是什么

c++ - iostream是否占用堆栈空间?

node.js - node.js 流的错误处理

python - 为什么我在 Django formset_factory 中得到单个字段值?

python - 如何使用loadtxt在python中的同一个数组中加载多个文件?

python - 在python中解析Parse格式字符串

java - 将json转换为对应的HashMap