Python:用mplayer解析流标题
作者:互联网
我正在Python中编写一个简单的前端,使用mplayer(在子进程中)播放和录制互联网广播频道(例如来自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)地下室 – Zajac,Migren住在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,subprocess,stream,parsing,mplayer 来源: https://codeday.me/bug/20190606/1189634.html