May 14, 2011

If it's too hard you're doing it wrong

Let's say you're a Linux user with an iPod that has a partially corrupt hard drive.  How would you take your music out of it and move them to a different player?  This post is how I handled the same situation.

First, I moved the entire iPod_Control directory from the iPod to a different, healthy hard disk to minimise data loss due to hardware failure.  Easiest thing to do is use gtkpod to export all the songs out of the iPod.  I added the copied iPod_Control directory as a new iPod to gtkpod and tried exporting the whole library.

For a number of reasons my iPod's music library was a little corrupted: (i) the library had been mutated by several different programs over years, and (ii) because the disk was damaged, some files were truncated.  gtkpod wasn't able to handle files that had issues.  It simply crashed when it encountered those files.

If you have ever looked at an iPod's iPod_Control directory, you will know that the file organisation is cryptic.  Unless you read the iTunes library file -- which is in a proprietary format -- you cannot make out what those files are.  gtkpod was reading the contents of my playlists from the iTunes library and copied the associated MP3s to the location I specified.  In the process it also renamed the MP3s so that it's easy for me to see which songs are where.

gtkpod was able to parse the library alright; it showed all my playlists and the songs in them.  So one solution could be, I figured, that I shouldn't let gtkpod do the renaming.  I exported my playlists as m3u files.  Now for each playlist I have a simple file with just a list of file paths.  I copied all the files into a directory using a shell for loop:

for f in $(grep -v '^#' /tmp/playlist.m3u); do
  # /path/to/ipod/iPod_Control/Music/Fxx/xyxy.mp3 -> Fxx-xyxy.mp3
  target=$(echo $f | sed 's#^\(.*\)/\([^/]*\)/\([^/]*\)$#\2-\3#')

  if [[ -f $target ]]; then
    echo >&2 "\nNot overwriting $f"
  else
    cp $f ./$target
    echo >&2 -n '.'
  fi
done

Then I used EasyTag to do the file renaming.  (EasyTag was good at handling corrupt MP3 files.)  After doing this all, only when I write this post, I realise there's an easier way: I can simply ask gtkpod to not rename files!  (In the file name format box, I would enter %o to prevent file renaming.)  Ah well, that's okay... writing that zsh script was fun!

No comments:

Post a Comment