#!/usr/bin/python #This script extracts cover images from the Amarok database and #creates cover.bmp images for use by Rockbox. It almost certainly #relies on undocumented parts of the Amarok database and file system #implementation, so your mileage may vary. # #Python is not my primary language, so apologies if this code is not #as elegant as it could be. # #Written in Aug. 2009 by Matt McHenry # from sqlite3 import connect import hashlib import os.path #-- configuration vars #the location of your music collection, relative to your home directory collectionroot = "/Collection" #size of cover images to create, in pixels #(I think this varies by theme) coversize = "120" #debug = False debug = True #-- end configuration home = os.path.expanduser("~") amarokdir = home + "/.kde/share/apps/amarok" #now: amarokdir = home + "/.kde4/share/apps/amarok" #read album info from database c = connect(amarokdir + "/collection.db").cursor() #TODO this is now a mysql database ... :-/ #mysql WIP: "select images.path as image, albums.name as album, artists.name as artist from images left join albums on images.id=albums.image left join artists on albums.artist=artists.id group by album, artist order by artist, album" data = c.execute("select tags.dir as dir, album.name as album, artist.name as artist from tags left join artist on tags.artist=artist.id left join album on tags.album=album.id group by album, artist").fetchall() covers = {} albums = set() #determine which albums have covers for row in data: directory = row[0][1:] album = row[1] artist = row[2] albums.add(album) #compilations leave out the artist for digestsrc in [artist.lower() + album.lower(), album.lower()]: m = hashlib.md5() m.update(digestsrc) digest = m.hexdigest() coverfile = amarokdir + "/albumcovers/large/" + digest if os.path.exists(coverfile): #some covers mistakenly think they live in the root, #which leads them to show up for a bunch of untagged songs if directory[-1*len(collectionroot):] == collectionroot: print "omitting cover for album '" + album + "' which claims to live in root directory" continue if directory not in covers: covers[directory] = coverfile elif debug: print "no cover found for " + digestsrc + " (" + digest + ")" existed = 0 created = 0 print "processing covers for " + str(len(covers)) + " of " + str(len(albums)) + " albums" #maps hashes of cover images to directories, #so we can detect dirs with the same cover uniqcovs = {} for directory, coversrc in covers.iteritems(): #rockbox: coverdest = directory + "/cover.bmp" #android: coverdest = directory + "/AlbumArt.jpg" if debug: covhasher = hashlib.md5() covhasher.update(open(coversrc, "r").read()) covhash = covhasher.hexdigest() if covhash in uniqcovs: print "same covers: " + directory print " " + uniqcovs[covhash] #print " " + coversrc #print " " + covhash else: uniqcovs[covhash] = directory if os.path.exists(coverdest): existed += 1 else: result = os.spawnlp(os.P_WAIT, "convert", "convert", coversrc, #rockbox: "-geometry", coversize+"x"+coversize, coverdest) if result != 0: print "error creating cover for " + coverdest + ": " + str(result) else: created += 1 print "created " + str(created) + " cover images (" + str(existed) + " already existed)"