Home Updates Messages SandBox

Enqueuer for MPD

Today another simple utility program I have just written – there is probably already something similar available out there, but it was easier to write my own than to look for it. First a little background: I use Music Player Daemon most of the time to play my music – I keep all the music files in one directory and have them nicely indexed. But sometimes I want to play a file I just downloaded, outside of that directory, or I want to search for a file using Deskbar and play it. This means I need a small script that will be associated with music files and will perform the most appropriate action.

import os
import sys
import mpdclient2
import subprocess

basedir = "/home/sheep/music/"
if len(sys.argv)<2:
    sys.exit()
filename = os.path.abspath(sys.argv[1])
mpd = mpdclient2.connect()
if filename.startswith(basedir):
    musicname = filename[len(basedir):]
    mpd.add(musicname)
    mpd.play(len(mpd.playlistinfo())-1)
else:
    mpd.stop()
    subprocess.call(["/usr/bin/totem", filename])