-
-
Save dustinmartin/2926112 to your computer and use it in GitHub Desktop.
Revisions
-
Adrian Perez revised this gist
Feb 1, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ #! /usr/bin/env python2 # -*- coding: utf-8 -*- # # Traverse a directory tree, picking embedded pictures from .MP3 and .M4A audio # saving them to a “cover.{png,jpg,bmp}” (as long as the file does *not* exist) -
Adrian Perez revised this gist
Feb 1, 2012 . 1 changed file with 9 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,9 +8,9 @@ # Most distributions ship in a package called “python-mutagen“. # from os import path as P import os, sys import mutagen PIC_TAGS = ( "PIC", u"PIC", u"PIC:", @@ -21,15 +21,16 @@ def handle_dir(path, files): for fpath in (P.join(path, f) for f in files): try: fileinfo = mutagen.File(fpath) except Exception, e: print >> sys.stderr, "%s" % e continue for tag in PIC_TAGS: if tag in fileinfo.tags: pic_data = fileinfo.tags.get(tag) pic_mime = pic_data.mime.lower() pic_path = None if pic_mime == "image/png": pic_path = P.join(path, "cover.png") elif pic_mime == "image/bmp": @@ -38,17 +39,17 @@ def handle_dir(path, files): pic_path = P.join(path, "cover.jpg") else: print >> sys.stderr, " - %r unrecognized type %r" % (fpath, pic_mime) if pic_path is not None and not P.isfile(pic_path): print "Writing %r" % pic_path with file(pic_path, "wb") as pic_fd: pic_fd.write(pic_data.data) print "Removing picture from %r" % fpath for tag in PIC_TAGS: fileinfo.tags.delall(tag) fileinfo.tags.save() fileinfo.save() def walk_and_handle(path, cb=handle_dir): -
Adrian Perez revised this gist
Feb 1, 2012 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -49,7 +49,6 @@ def handle_dir(path, files): for tag in PIC_TAGS: tags.delall(tag) tags.save() def walk_and_handle(path, cb=handle_dir): -
Adrian Perez created this gist
Nov 15, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ #! /usr/bin/env python2 # # Traverse a directory tree, picking embedded pictures from .MP3 and .M4A audio # saving them to a “cover.{png,jpg,bmp}” (as long as the file does *not* exist) # and then removing the embedded pictures from the audio file. # # Requires the Mutagen tag edition library: https://code.google.com/p/mutagen/ # Most distributions ship in a package called “python-mutagen“. # from mutagen.id3 import ID3 from os import path as P import os, sys PIC_TAGS = ( "PIC", u"PIC", u"PIC:", "APIC", u"APIC", u"APIC:", ) def handle_dir(path, files): for fpath in (P.join(path, f) for f in files): try: tags = ID3(fpath) except Exception, e: print >> sys.stderr, "%s" % e continue for tag in PIC_TAGS: if tag in tags: pic_data = tags.get(tag) pic_mime = pic_data.mime.lower() if pic_mime == "image/png": pic_path = P.join(path, "cover.png") elif pic_mime == "image/bmp": pic_path = P.join(path, "cover.bmp") elif pic_mime == "image/jpeg" or pic_mime == "image/jpg": pic_path = P.join(path, "cover.jpg") else: print >> sys.stderr, " - %r unrecognized type %r" % (fpath, pic_mime) continue if not P.isfile(pic_path): print "Writing %r" % pic_path with file(pic_path, "wb") as pic_fd: pic_fd.write(pic_data.data) print "Removing picture from %r" % fpath for tag in PIC_TAGS: tags.delall(tag) tags.save() return def walk_and_handle(path, cb=handle_dir): for dirpath, dirnames, filenames in os.walk(path): audios = [f for f in filenames if f.lower().endswith(".mp3") or f.lower().endswith(".m4a")] if len(audios): cb(dirpath, audios) if __name__ == "__main__": import sys if len(sys.argv) == 2: walk_and_handle(sys.argv[1]) else: raise SystemExit("Usage: %s <path>" % sys.argv[0])