hey, remember that perl script i hacked together to rip cds? this afternoon i decided to rewrite it in python. complete with cddb lookups! it needs
pycddb,
discid, and
lame. i'm not sure if pycddb and discid are in the ubuntu repositories, but i'm sure they'd be easy to compile if they aren't.
the code:
Code:
import os, commands, PyCDDB
def cddblookup():
# get the disc id, create a PyCDDB object, and do the lookup
discid = commands.getoutput("discid")
db = PyCDDB.PyCDDB()
try:
matches = db.query(discid)
except ValueError:
# since i'm not testing the value returned from
# discid this is necessary (in case the drive has no disc)
print "no cd in tha drive!? :("
exit()
# did i find anything?
if len(matches) > 0:
if len(matches) > 1:
# shit: multiple matches.. give the user a choice
print "multiple matches for this cd. choose of the following:"
for i in range(len(matches)):
artist, album = matches[i]['title'].split(" / ", 1)
print "%d - %s by %s (%s)" % (i + 1, album, artist, matches[i]['category'])
matchidx = input("enter the number of the correct match: ")
matchidx = matchidx - 1
else:
# easy: one match (hope it's correct!)
matchidx = 0
albuminfo = db.read(matches[matchidx])
else:
# no results.. for now i'll be lazy and exit()
print "cddb lookup failed, or no results found :["
exit()
return albuminfo
def ripandencode(albuminfo, enccmd):
# rip *.wav files with cdparanoia
ripstatus = os.system("cdparanoia -B")
#print "cdparanoia -B"
#raw_input()
# encode mp3s using lame
for track in range(len(albuminfo['TTITLE'])):
if track < 10:
tnum = "0" + str(track + 1)
else:
tnum = str(track + 1)
lamecmd = enccmd.replace("%NUM%", tnum)
lamecmd = lamecmd.replace("%TITLE%", albuminfo['TTITLE'][track])
lamecmd = lamecmd.replace("%ALBUM%", album)
lamecmd = lamecmd.replace("%ARTIST%", artist)
encstatus = os.system(lamecmd)
#print lamecmd
#raw_input()
print "done!"
if __name__ == "__main__":
lamecmd = "lame -V 4 --tn %NUM% --tt \"%TITLE%\" --tl \"%ALBUM%\" --ta \"%ARTIST%\" track%NUM%.cdda.wav \"%NUM% - %TITLE%.mp3\""
albuminfo = cddblookup()
# i have the shit from cddb--ask if it's good before i rip
artist, album = albuminfo['DTITLE'].split(" / ", 1)
print "%s by %s" % (album, artist)
for track in range(len(albuminfo['TTITLE'])):
print "%02d. %s" % (track + 1, albuminfo['TTITLE'][track])
gogogo = raw_input("rip and encode this cd? (y/n): ")
if gogogo == "y":
# rip with cdparanoia and encode mp3z with lame
ripandencode(albuminfo, lamecmd)
else:
exit()
it's a bit ugly right now, but it gets the job done. there are some spots where i ought to return some sort of error code, or throw an exception (from the cddblookup() function) rather than just do an exit(), but i got lazy.
if you comment the two calls to os.system(...) and uncomment the print and raw_input() statements below each, you can see all the commands it will run without actually ripping anything.
to-do:
- change it so it handles multiple cd devices (right now you'd have to edit the calls to discid and cdparanoia in the script)
- add some sort of configuration file so a user can customize what sort of command is used to encode the *.wav files that cdparanoia produces (you could use a custom lame, oggenc, etc. command), and so they can customize the file naming scheme
- delete the *.wav files when it's done (maybe put an option in the config file to keep them)
- make the output nicer
- maybe think about doing up a gui (i hate coding that shit )