- Description: This Python script outputs your MPD collection in CSV (comma-separated values) format (the default separator is a tab character, not a comma). You can output this to a file, which you can then further process with e.g. Openoffice.org Calc. MPD should be running in order for this script to function. Run it without arguments to display the default tags for each file, or specify the tags you want on the command line (like this: python mpd-collection2csv.py artist album). To write to a file, run as follows:
python mpd-collection2csv.py > collection.csv
- Maintainer(s): Frank Mulder < frankmulder2003 AT yahoo DOT com >
#!/usr/bin/python
import mpd
import sys
client = mpd.MPDClient() # create client object
client.connect("localhost", 6600) # connect to localhost:6600
def take_first_if_list(element):
# Takes the first element if the argument is a list; otherwise, it returns the argument itself. This is needed for tags which contain multiple fields.
if isinstance(element, list):
return element[0]
else:
return element
separator='\t'
# The default tags to be displayed for each file
tags_to_display = ['artist', 'album', 'title', 'genre', 'file']
# If arguments have been given, use them as the tags to be displayed
args = sys.argv[1:]
if len(args) > 0:
tags_to_display = args
for song in client.listallinfo():
if 'file' in song:
for tag in tags_to_display:
# Print the given tag, or a default string if the tag does not exist
print take_first_if_list(song.setdefault(tag, 'empty')),
print separator,
# Print a newline after each song
print
client.close() # send the close command
client.disconnect() # disconnect from the server