Code / PHP Snippets / iTunes .itc Extraction and Conversion

  1. <?
  2. // iTunes .itc Artwork Extraction Script v0.1
  3. // (c) Scott Wilcox 2007/08
  4. //
  5. // You will need to download and install 'jpegextractor', imagemagick
  6. // and java in order for this to successfully work. You'll  also need SCP
  7. // as well. Also uses rm, cd, find and cmp. Report bugs/errors to
  8. // sc0tt@x0f.org or the evil pixies will come to get you. Takes about 25
  9. // seconds to get through about 140 albums (approx!).
  10. //
  11. // You can see the end results at http://nixbox.org/albums in a pretty grid.
  12. //
  13. // Things you'll need:
  14. // jpegextractor                http://schmidt.devlib.org/software/jpeg-extractor.html
  15. // imagemagick                  http://www.imagemagick.org
  16. // java                                                 http://java.com
  17. //
  18. // This has been tested on Mac OSX, Slackware Linux and FreeBSD. I'm not
  19. // responsbile if this blows your house up though, kills your dog or does
  20. // anything else deemed to be bad.
  21.  
  22. // Path to jpegextractor directory, with a trailing slash.
  23. $jpegextractor = "/usr/local/jpegextractor/";
  24.  
  25. // Path to java
  26. $java = "/usr/bin/java";
  27.  
  28. // Path to ImageMagick's mogrify binary
  29. $imagemagick = "/usr/local/ImageMagick-6.3.7/bin/mogrify";
  30.  
  31. // Path to iTunes "Album Art" directory, no trailing slash
  32. $itunes_path = "/Users/MyUser/Music/iTunes/Album Artwork";
  33.  
  34. // Path to a temporary directory we can write too, make sure
  35. // you add a trailing slash, ie /tmp/albumart/
  36. $tmp = "/tmp/albumart/";
  37.  
  38. // Path to SCP binary
  39. $scp = "/usr/bin/scp";
  40.  
  41. // SCP Login details (user@host:path)
  42. $scp_auth = "user@server:/path/to/album/art";
  43.  
  44. // Path to standard 'find', 'rm' and 'cmp' binaries
  45. $find = "/usr/bin/find";
  46. $rm = "/bin/rm";
  47. $cmp = "/usr/bin/cmp";
  48.  
  49. // Check to see that they've configured it somewhat
  50. if ((empty($tmp)) || (empty($itunes_path)) || (empty($imagemagick)) || (empty($jpegextractor))) {
  51.         die("Error: Configuration not completed.");
  52. }
  53.  
  54. // First, we need to check that our temporary directory exists
  55. // and we can write to it
  56. if (is_writeable($tmp)) {
  57.         // We'll use the standard find command to find our list
  58.         // of itunes .itc files
  59.  
  60.         // Clear the directory out of jpg's
  61.         @shell_exec("$rm ".$tmp."*");
  62.  
  63.         // Check for .itc's
  64.         echo "Checking for iTunes .itc files...n";
  65.         $itcs = shell_exec("$find $itunes_path -name *.itc* ");
  66.         if ($itcs) {
  67.                 // If we found any .itc files, then put them into an array
  68.                 // and we'll copy them over to a temp directory so that
  69.                 // jpegextractor can process them all in one go.
  70.                 $itcs = explode("n",$itcs);
  71.                 echo "nFound ".count($itcs)." .itc files.nn";
  72.  
  73.                 // Initilise some variables and an array
  74.                 $fail = 0; $blank = 0; $copied = array();
  75.                
  76.                 foreach ($itcs as $itc) {
  77.                         if ($itc != "") {
  78.                                 // Copy each file to our temporary directory
  79.                                 if (copy($itc,$tmp.basename($itc))) {
  80.                                         $copied[] = $tmp.basename($itc);
  81.                                 } else {
  82.                                         $fail++;
  83.                                         echo "File '$itc' failed to copy n";
  84.                                 }
  85.                         } else {
  86.                                 $blank++;
  87.                         }
  88.                 }
  89.  
  90.                 // Notify user of how many copied successfully, and how
  91.                 // many failed/were blanks
  92.                 echo number_format(count($copied))." files successfully copied. ";
  93.                 echo number_format($fail)." files failed to copy. ";
  94.                 echo "There were ".number_format($blank)." blank entries. n";
  95.  
  96.                 // Notify user that we're about to start jpegextraction
  97.                 echo "nStarting extraction of jpeg images (this may take a while!)n";
  98.                
  99.                 // Now switch into the jpegextractor directory and run
  100.                 // jpegextractor on temporary directory of .itcs
  101.                 $result = shell_exec("cd $jpegextractor ; $java jpegextractor -D $tmp -o -d 4 -q -p album
  102.                  ".$tmp."*.itc ; rm ".$tmp."*.itc");
  103.  
  104.                 // Now we need to create an resized image using imagemagick
  105.                 // to resize them all.
  106.                 echo "Resizing images... n";
  107.                 $result = shell_exec("$imagemagick -resize 100x100 ".$tmp."*");
  108.  
  109.                 // Weed out any duplicates!
  110.                 echo "nTrying to remove duplicates (this may take a while too!)... n";
  111.                 chdir($tmp);$bash = shell_exec('ALL=`'.$find.' . -type f`; for ONE in $ALL; do [ -f $ONE ] ||
  112.                 continue; for TWO in $ALL; do [ -f $TWO ] || continue; case $ONE in $TWO) continue;; esac;
  113.                 '.$cmp.' -s $ONE $TWO && echo Duplicate: $TWO && '.$rm.' -f $TWO; done; done');
  114.                 echo $bash." n";
  115.  
  116.                
  117.                 echo "nUploading via SCP (You'll be prompted for a password!)... n";
  118.                 // Now SCP them over to the webserver
  119.                 $result = shell_exec($scp." ".$tmp."* ".$scp_auth);
  120.  
  121.                 // Clean up and delete all left files
  122.                 shell_exec("$rm ".$tmp."*");
  123.                
  124.         } else {
  125.                 echo "No .itc files found. n";
  126.         }
  127. } else {
  128.         echo "Unable to write to temporary directory '$tmp' n";
  129. }
  130. ?>

Download as Text : January 6th, 2009