Skip to content

Instantly share code, notes, and snippets.

@dustinmartin
Forked from aperezdc/id3-picture-tidy.py
Created June 13, 2012 20:00
Show Gist options
  • Select an option

  • Save dustinmartin/2926112 to your computer and use it in GitHub Desktop.

Select an option

Save dustinmartin/2926112 to your computer and use it in GitHub Desktop.

Revisions

  1. Adrian Perez revised this gist Feb 1, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions id3-picture-tidy.py
    Original 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)
  2. Adrian Perez revised this gist Feb 1, 2012. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions id3-picture-tidy.py
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,9 @@
    # Most distributions ship in a package called “python-mutagen“.
    #

    from mutagen.id3 import ID3
    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:
    tags = ID3(fpath)
    fileinfo = mutagen.File(fpath)
    except Exception, e:
    print >> sys.stderr, "%s" % e
    continue

    for tag in PIC_TAGS:
    if tag in tags:
    pic_data = tags.get(tag)
    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)
    continue

    if not P.isfile(pic_path):
    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:
    tags.delall(tag)
    tags.save()
    fileinfo.tags.delall(tag)
    fileinfo.tags.save()
    fileinfo.save()


    def walk_and_handle(path, cb=handle_dir):
  3. Adrian Perez revised this gist Feb 1, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion id3-picture-tidy.py
    Original 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()
    return


    def walk_and_handle(path, cb=handle_dir):
  4. Adrian Perez created this gist Nov 15, 2011.
    68 changes: 68 additions & 0 deletions id3-picture-tidy.py
    Original 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])