如何使用 python 从 pianobar 检索当前歌曲名称和剩余时间?

How do I retrieve the current song name and time left from pianobar using python?

我正在 raspberry pi 上使用 TKinter 和 python 创建一个带图形用户界面的闹钟。当闹钟响起时,我希望它开始播放钢琴吧。我可以很容易地做到这一点,但我还想在 GUI 上显示歌曲的名称,而且我不知道如何获取当前正在播放的歌曲。我试过使用管道“|”,并使用“>”重定向,但我一无所获。任何建议都会有所帮助。

您需要使用事件命令界面。来自手册页:

An example script can be found in the contrib/ directory of pianobar's source distribution.

~/.config/pianobar:

user = <username>
password = <password> (although I'd suggest password_command)
event_command = ~/.config/pianobar/event_command.py

~/config/event_command.py

#!/usr/bin/env python

import os
import sys
from os.path import expanduser, join

path = os.environ.get('XDG_CONFIG_HOME')
if not path:
    path = expanduser("~/.config")
else:
    path = expanduser(path)
fn = join(path, 'pianobar', 'nowplaying')

info = sys.stdin.readlines()
cmd = sys.argv[1]

if cmd == 'songstart':
    with open(fn, 'w') as f:
        f.write("".join(info))

这会在新歌开始时将歌曲信息写入 ~/.config/pianobar/nowplaying(联机帮助页中还有其他事件可用)。然后,您可以使用您选择的工具对其进行解析以获取歌曲名称。