Python:使用 mplayer 解析流标题

发布于 2024-09-27 20:41:28 字数 1329 浏览 0 评论 0原文

我正在用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) Basement - Zajac, Migren live at Justmusic 2010-10-09”并仍然让进程运行?我不认为 subprocess() 实际上存储输出,但我可能是错的。非常感谢任何帮助:)

I'm writing a simple frontend in Python to play and record internet radio channels (e.g. from shoutcast) using mplayer (in a subprocess). When a user clicks a station the following code is run:


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)

This works perfectly, the stream starts playing as it should. Although I would like to somehow parse the title of the stream. It seems that I need to fetch the title from the mplayer output. This is the output when I play the stream in a terminal:

$ 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.

It then runs until it's stopped. So the question is, how can I retrieve "(JustMusic.FM) Basement - Zajac, Migren live at Justmusic 2010-10-09" and still let the process run? I don't think subprocess() actually stores the output, but I might be mistaken. Any help is deeply appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪花飘飘的天空 2024-10-04 20:41:28

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)')

Set the stdout argument to PIPE and you'll be able to listen to the output of the command:

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)')
忘年祭陌 2024-10-04 20:41:28
import re
import shlex
from subprocess import PIPE, Popen

URL = 'http://relay2.slayradio.org:8000/'

def get_exitcode_stdout_stderr(cmd):
    """
    Execute the external command and get its exitcode, stdout and stderr.
    """
    args = shlex.split(cmd)

    proc = Popen(args, stdout=PIPE, stderr=PIPE)
    out, err = proc.communicate()
    exitcode = proc.returncode
    #
    return exitcode, out, err

def get_title():
    cmd = "mplayer -endpos 1 -ao null {url}".format(url=URL)
    out = get_exitcode_stdout_stderr(cmd)[1]

    for line in out.split("\n"):
#        print(line)
        if line.startswith('ICY Info:'):
            match = re.search(r"StreamTitle='(.*)';StreamUrl=", line)
            title = match.group(1)
            return title

def main():
    print(get_title())

编辑:我在这里有一个不同的(更简单的)解决方案,但它停止工作,所以我更新了我的解决方案。想法:mplayer 在 1 秒后停止。 (-endpos 1)。

import re
import shlex
from subprocess import PIPE, Popen

URL = 'http://relay2.slayradio.org:8000/'

def get_exitcode_stdout_stderr(cmd):
    """
    Execute the external command and get its exitcode, stdout and stderr.
    """
    args = shlex.split(cmd)

    proc = Popen(args, stdout=PIPE, stderr=PIPE)
    out, err = proc.communicate()
    exitcode = proc.returncode
    #
    return exitcode, out, err

def get_title():
    cmd = "mplayer -endpos 1 -ao null {url}".format(url=URL)
    out = get_exitcode_stdout_stderr(cmd)[1]

    for line in out.split("\n"):
#        print(line)
        if line.startswith('ICY Info:'):
            match = re.search(r"StreamTitle='(.*)';StreamUrl=", line)
            title = match.group(1)
            return title

def main():
    print(get_title())

Edit: I had a different (simpler) solution here that stopped working so I updated my solution. The idea: mplayer stops after 1 sec. (-endpos 1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文