The Easiest Way to Save and Share Code Snippets on the web

Art Ripper

python

posted: Jul, 4th 2010 | jump to bottom

import sys
 
from mutagen.id3 import ID3
from mutagen.mp4 import MP4
 
__author__="Rich"
__date__ ="$Jun 11, 2010 9:27:47 PM$"
 
def write_image(artist, album, pic):
    safeAlbum = album.strip('"\'.,=/')
    filename = artist + "-" + safeAlbum + "." + pic.mime.lower()
    with open(filename, "wb") as out:
        out.write(pic.data)
    print "Wrote " + filename
 
def main():
    if len(sys.argv) < 2:
        print "usage: artripper.py mediafile"
        exit(0)
    srcpath = sys.argv[1]
    picIndex = 0
    picIndexForced = False
 
    if len(sys.argv) == 3:
        picIndexForced = True
        picIndex = int(sys.argv[2])
 
    file = ID3(srcpath)
    artist = unicode(file["TPE1"][0])
    album = unicode(file["TALB"])
    pics = file.getall("APIC")
    if len(pics) == 0:
        print "no pics"
    elif len(pics) == 1 or picIndexForced:
        write_image(artist, album, pics[picIndex])
    else:
        print "multiple pics"
 
 
if __name__ == "__main__":
    main()
 
56 views