Fetch album art PHP script

Page last updated: 2010-01-30

Author: Peter Mescalchin

About

A script for scanning folders in an mp3 music library and then automatically fetching missing album artwork using source images from the Amazon product database music section via the Amazon Web Services API, written in PHP5. As well as fetching the actual artwork, the script will resize/crop all images to a standard image size (using ImageMagick) and then save resulting images within the source mp3 album folder for use with music players such as Clementine Music Player, foobar2000 or Rhythmbox.

The script will not modify mp3 files, or overwrite existing album folder artwork images, meaning it can be safely re-run over a music library as new album content is added. All album information is extracted from the album folder name, not the MP3 files themselves (e.g. no ID3 tag reading), so a well structured folder naming scheme is important for effective results.

Updates

Requirements

Configuration

All configuration options are kept in config.php, below is a summary of each configuration directive and suggested values:

Config key Comments Example value
AMAZON_ACCESSKEY Amazon Web Services access key - 20 characters. Obtain from your AWS login at http://aws.amazon.com/ under Security Credentials / Access keys
AMAZON_SECRETKEY Amazon Web Services secret key - 40 characters. Obtain from your AWS login at http://aws.amazon.com/ under Security Credentials / Access keys
IMAGEMAGICK_CONVERT Full path to the installed ImageMagick convert CLI program "C:\\Program Files\\ImageMagick\\convert.exe"
IMAGEMAGICK_SAVE_QUALITY Save quality for album art images. 80 seems to work well. Values between 1 (low quality) to 100 (high quality) 80
TEMP_IMAGE_PATH Where temporary album image file from Amazon will be saved before resizing by ImageMagick. Usually won't need to change the default "tmpimage.jpg"
TARGETIMAGE_FILENAME Target image filename, will be saved under full/album/path/TARGETIMAGE_FILENAME. "cover.jpg" works for foobar2000, some media players prefer/use "folder.jpg" "cover.jpg"
TARGETIMAGE_WIDTH Target image save width. The script will skip source Amazon images that are smaller than the desired width, so don't set the width too wide or fewer album art images will be downloaded. I find a 300 pixel width works well. 300
TARGETIMAGE_HEIGHT Same requirements/rules as TARGETIMAGE_WIDTH for the image height. 300
LOG_FILE Log file for script execution progress. log.txt
LOGERROR_FILE On error, or album art image not found, messages will be logged here. logerror.txt

Final configuration step is to setup the extractartistalbum->extract() method, which will extract artist name/album name details from your mp3 folder paths. The extract() method is expected to return either an array of artist/album, or an empty array in the case of failure - meaning the mp3 folder will be skipped.

The supplied class will handle path names in the following formats:

Since extractartistalbum->extract() accepts the full album path it should be trivial to convert to a /music/artistname/albumname/ extractor using some inbuilt PHP string manipulation functions.

Running

To execute script call PHP CLI with the full script path, followed with the full base path to your MP3 album folders without any trailing wildcard. E.g.

/usr/bin/php /script/to/run.php /mp3/albumpath/

Then sit back, be patient and let the script do its work. Any issues with running the script or suggestions for improvement please contact me.

Download / View source

Links