0

My script has no problems extracting the images from FLAC files, however when given an MP3 file, it will only grab the first image and not the rest.

Things I've tried:

  • Converting ID3v2.3 to ID3v2.4
  • Converting ID3v2.3 to APEv2
  • Printing out the full array of track['pictures']. Does not go past range of 0.

The script takes two command line arguments. The first is the mount name which is the subdirectory that the images will be written to. The second is the path to the file.

Example: python extract_art.py "classic" "hello.mp3"

# -*- coding: UTF-8 -*-

# This script gets called from metadata.liq
# Run this script using Python 2

import sys
import random
import os
import codecs
from mutagenwrapper import read_tags

# Name of the stream (Exaples: anything-goes, anime)
mount_name = sys.argv[1]

# Name of the file to be opened
filename = sys.argv[2]

# Points to the path where images will be written to
full_path = "/mnt/album_art/" + mount_name + "/"

track = read_tags(filename)

try:
    # Check if there is album art
    album_art = track['pictures'][0]
except:
    # If album art cannot be read, return a null string to flag deletion on album art that pervious song wrote.
    album_art = ""

if album_art == "":
    try:
        # If no album art was found, attempt to delete album art that may have been previously written by another song
        os.remove(full_path + 'albumart.jpg')
        print("Deleted album art")
    except:
        # If there is no album art to delete
        print("No album art to delete")
else:
    #If album art was found in the current track, write the new album art and create the proper paths if necessary
    if not os.path.exists(os.path.dirname(full_path)):
            os.makedirs(os.path.dirname(full_path))
    with open(full_path + 'albumart.jpg', 'wb') as f:
            f.write(album_art)
            print("Wrote new album art")
            f.close()   

# Same as writing album art, except with background image

background_image = ""

try:
    background_image = track['pictures'][1]
except:
    if background_image == "":
        background_image = album_art
        print("No background image found, using album art as background iamge.")
    else:
        background_image = ""

if background_image == "":
    try:
        os.remove(full_path + 'backgroundimage.jpg')
        print("Deleted background image")
    except:
        print("No background image to delete")
else:
    if not os.path.exists(os.path.dirname(full_path)):
            os.makedirs(os.path.dirname(full_path))
    with open(full_path + 'backgroundimage.jpg', 'wb') as f:
            f.write(background_image)
            print("Wrote new background image")
            f.close()

1 Answers1

1

As the documentation says:

Note that this module is still in its early development stage. Many features are not supported…

Later on, it explains limitations with (among other things) album art and handling multi-valued tags, so it's not all that surprising that it has problems with multiple album art tags in one format. This is only version 0.0.5…

If you want a deeper explanation, you'll probably need to dig into the source or ask the author. (Possibly by filing a bug.) But I'm willing to bet the only solution is either "fix it yourself" or "wait until someone else cares enough to implement it".

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Ah, okay thanks. I'll probably have the script use regular Mutagen for MP3 handling then and see if that works. – John Broderick Nov 02 '14 at 03:33
  • @JohnBroderick: By the way, you may also want to look at the Mutagen wrappers embedded in MusicBrainz Picard and Quod Libet for ideas. – abarnert Nov 03 '14 at 19:04