Author Topic: [Python] VLC for conky  (Read 2353 times)

0 Members and 1 Guest are viewing this topic.

Offline relax

  • Sir
  • ***
  • Posts: 562
  • Cookies: 114
  • The one and only
    • View Profile
[Python] VLC for conky
« on: September 09, 2012, 06:20:04 pm »
so i tryed to get vlc working with conky with 2 diffrent scripts from from the internet
unfortiuntly no one of them have worked :(
so i wrote my own witch is working
this script is very specific and might not work for you but it might and its really easy to change so it dose work.
you will need the latest beta version from vlc and dbus activated in vlc.

the python script

Code: [Select]
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:
            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:
            print("Unknown error")
            sys.exit()
        status = status[:-1]
        start = data.find("title:")
        end   = data.find("xesam:url:")
        title = data[start+7:end-1]
        if len(title)>40:
             title = "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("Try -h for help")
        try:
            opts, args = getopt.getopt(argv,"tslcph")
        except getopt.GetoptError:
            print("Try -h for help")
            sys.exit(2)
        for opt, arg in opts:
            if opt == "-t":
                print(title)
                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("To get vlc info pls provide some of the following arguments")
                print("-t, --title        To display playing title")
                print("-s, --status        To display playbackstatus")
                print("-l, --length        To display length of the file playing")
                print("-c, --current        To display current position")
                print("-p, --procent        To display procent of elapsed time")
if __name__ == "__main__":
   main(sys.argv[1:])

conky code

Code: [Select]
${if_running vlc}${font Sans:bold:size=8.25}${color aa0000}VLC  ${color3}${hr 2}
${image ~/images/vlc.png -p 0,630 -s 50x40}${goto 60}${voffset 5}${execpi 3 python ~/vlcc.py -t}
${goto 60}${voffset -3}${font Sans:Italic:size=8}${execpi 3 python ~/vlcc.py -s}$font

${execpi 1 python ~/vlcc.py -c}$alignr${execpi 3 python ~/vlcc.py -l}
${goto 200}${execpi 2 python ~/vlcc.py -p}
$endif


a screen dump


if you need help just tell me