![如何让 Conky 正确显示 VLC 数据](https://linux22.com/image/1062075/%E5%A6%82%E4%BD%95%E8%AE%A9%20Conky%20%E6%AD%A3%E7%A1%AE%E6%98%BE%E7%A4%BA%20VLC%20%E6%95%B0%E6%8D%AE.png)
我有一个 Conky 配置,可以获取 python 脚本的输出:
#!/usr/bin/python
import subprocess
import sys
import getopt
def cmd_output(args, **kwds):
kwds.setdefault("stdout", subprocess.PIPE)
kwds.setdefault("stderr", subprocess.STDOUT)
p = subprocess.Popen(args, **kwds)
return p.communicate()[0]
def main(argv):
try:
# name = "org.mpris.MediaPlayer2.vlc" #get this from "qdbus | grep vlc" and change the 3 lines below
data = str(cmd_output("qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Metadata".split()))
status = str(cmd_output("qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus".split()))
current_seconds = int(cmd_output("qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Position".split()))/1000
except ValueError:
if not data:
print("No Song Playing")
sys.exit()
except:
print("VLC Not Running")
sys.exit()
status = status[:-1]
start = data.find("title:")
end = data.find("xesam:tracknumber:")
if end==-1:
end = data.find("xesam:url:")
title = data[start+7:end-1]
if len(title)>40:
title = "Unknown"
start = data.find("artist:")
end = data.find("xesam:title:")
artist = data[start+8:end-1]
if len(artist)>40:
artist = "Unknown"
start = data.find("album:")
end = data.find("xesam:artist:")
album = data[start+7:end-1]
if len(album)>40:
album = "Unknown"
start = data.find("vlc:length:")
end = data.find("vlc:publisher:")
length_seconds = int(data[start+11:end])/1000
current_seconds = int(current_seconds / 1000)
current_position_percent = str(int((float(current_seconds) / float(length_seconds))*100))
length = str(length_seconds/60).rjust(1,"0")+":"+str(length_seconds%60).rjust(2,"0")
current_position = str(int(current_seconds/60)).rjust(1,"0")+":"+str(int(current_seconds%60)).rjust(2,"0")
if len(argv) < 1:
print("Error: No arguments specified. Type -h for help.")
try:
opts, args = getopt.getopt(argv,"tarslcph")
except getopt.GetoptError:
print("Error: Unknown argument. Type -h for help.")
sys.exit(2)
for opt, arg in opts:
if opt == "-t":
print(title)
sys.exit()
elif opt == "-a":
print(album)
sys.exit()
elif opt == "-r":
print(artist)
sys.exit()
elif opt == "-s":
print(status)
sys.exit()
elif opt == "-l":
print(length)
sys.exit()
elif opt == "-c":
print(current_position)
sys.exit()
elif opt == "-p":
print(current_position_percent)
sys.exit()
else:
print("Provide at least one of the following arguments")
print("-t Display playing title")
print("-s Display playback status")
print("-l Display length of the file playing")
print("-c Display current position")
print("-p Display percent of elapsed time")
sys.exit()
if __name__ == "__main__":
main(sys.argv[1:])
如果 VLC 正在运行,它就可以完美运行,但如果没有运行,它根本无法运行,只会占用 CPU 时间。当 VLC 未打开并正在播放时,如何防止 current_seconds 值未提供有效数字的错误?
答案1
您是否尝试过用 来封装您调用此脚本的地方(在您的 conky 配置文件中)if_running
?
来自文档:
如果进程正在运行,则显示所有内容 $if_running 和匹配的 $endif。这使用“pidof”命令,因此也支持 -x 开关。
例如,您可以像这样尝试:$(if_running vlc)<python script calls>$endif
。