How to make your Sansa m200-series work with iTunes under OSX


Introduction
General solution
Enough already--how do I fix this problem?
Troubleshooting

Introduction

I picked up a Sansa m260 player the other day for cheap, and was almost immediately disappointed. It worked okay, but it didn't seem to want to play any of my albums in order. Now, this is admittedly a bit of an idiosyncracy with me--I like to play my music an album-at-a-time, and I like the tracks to play in order. The Sansa didn't seem to want to do that; its default behaviour was to to play the tracks in alphabetical order either by their "title" ID3 tag, or in alphabetical order by their filename. Clearly unacceptable, and definitely annoying. (Other than that, the Sansa is a decent player, and a great value for the money. In SanDisk's defense, they do not claim iTunes compatibility--but an MP3 is an MP3, right?)

After a bit of searching on the web, I discovered that the Sansa will read the "track" ID3 tag, and will preferentially sort the music within an album by the track tag. Unfortunately, the Sansa m200-series players do not recognize the "track" format that iTunes uses. iTunes, when it encodes an MP3, will make the "track" ID3 tag of the third track on an album with 12 tracks look like this: "3/12". The Sansa doesn't quite know what to do with this, as it expects to see only digits in the "track" tag. Problem.

General Solution

What can we do to fix this? Well, the right way would probably be to tweak the firmware on the Sansa to be a bit more flexible. Unfortunately, I possess neither the source code for the Sansa, nor the ability to modify it. I was stuck with making the ID3 tags look more like what the Sansa expected to see.

This, it turns out, is pretty trivial to do. There are plenty of ID3 track editors out there, and lots of 'em are command-line driven. As an added bonus, I have iTunes configured to stick the two-digit track number at the beginning of the name of the MP3 file it generates. So, the procedure I followed was:

This was a bit of an adventure--I'd recently switched to running bash instead of tcsh, so I decided that this would be my first bash script. Whee!

Enough already--how do I fix the problem?

  1. Make a backup copy of your iTunes folder.
  2. Locate, download, and compile a command-line ID3 editor. I used id3v2. Make sure the directory containing your ID3 editor is in your path.
  3. Copy-and-paste the following Perl script into a text file somewhere on your Mac:
    #!/usr/bin/perl -w
    
    # Sets the track in the ID3 tag to the first part of the
    # filename.  If this is a multi-disc album, it will discard the dash.
    # That's not the best behaviour, but I have only a few multi-disc albums,
    # and would rather fix them by hand than make the script more intelligent.
    # :)
    
    $file = $ARGV[0];
    chomp $file;
    print $file;
    $track = $file;
    $track =~ s/^(\d+) .*$/$1/;
    if ( $track =~ /^\d\-\d+/ ) {
      $track =~ s/^\d\-(\d+).*$/$1/;
    }
    print "Setting track ID to $track for \"$file\"\n";
    $command = "id3v2 -T $track \"$file\"";
    `$command`;
    

    Then make that text file executable ("chmod a+x [filename]").

  4. Copy-and-paste the following bash script into a text file somewhere on your Mac:
    #!/bin/bash
    #
    # Enumerates the MP3s stored in my iTunes folder.  Structure is
    #  iTunes root:Artist:Album:Track
    # so from the root, I have to go down two directories before
    # I can get to the tracks.
    
    for artist in *
    do 
      if [ -d "$artist" ] 
      then
        cd "$artist";
        for album in *
        do
          if [ -d "$album" ]
          then
            cd "$album";
            for track in *
            do
              ~Dylan/Desktop/fix_id3.pl "$track";
            done
            cd ..;
            continue
          fi
        done
        cd ..;
        continue
      fi
    done
    

    Then make that text file executable ("chmod a+x [filename]").

    For the "~Dylan/Desktop/fix_id3.pl" bit, you should substitute the path to and filename of the Perl script you copied in step #3.

  5. Change to the root folder of your backup copy of your iTunes folder, and run the bash script. id3v2 generates lots of mallocs on my system, but it still works just fine. :)
  6. Copy the MP3s from your backup copy onto your Sansa.
  7. Enjoy your properly-ordered tracks

It would be a very smart idea to make sure you understand what all this is doing before you try to use it--I've presented this in a cookbook copy-and-paste format, but if your system is set up differently than mine, or if I've neglected to take something into account, you could munge your ID3 tags. On that note, I am most explicitly NOT responsible if anything goes wrong: Use this procedure at your own risk.

Troubleshooting

You did run this on a backup copy of your iTunes folder, right? So any problems shouldn't affect your music library . . .

Check to make sure you did the usual things--made the files executable (use "chmod a+x [filename]"), make sure they run, and that all the paths are correct. If you use a tag editor other than id3v2, or your tag editor isn't in your path, you'll probably need to adjust the syntax of the $command string in the Perl script in step #3.