Mark Gilbert's Blog

Science and technology, served light and fluffy.

How I got my groove back – Music Files, Playlists, and the Sansa Clip

Before a couple of months ago, I had only really been using my MP3 player, a Sansa Clip, to listen to music while I was at work, but then I started finding other uses for it.  For example, I can connect it as an input to my guitar amp, and then play along with whatever song I cue up. I also found myself plugging it in at home, finding it far easier to use than Windows Media Player (WMP).

WMP works fine for playing music, but managing my collection is another matter.  I’d drop a new MP3 into a folder, and then fight for 15 minutes with WMP to get it to actually recognize it.  Sometimes it would appear under "Songs" but not "Albums".  Sometimes I’d drag it into a playlist, only to have it get duplicated.  Sometimes the file wouldn’t sync to my player at all: no errors, but no transferring bits either.  These are probably just cases of me just not doing it the "WMP-Way", but whatever that is is not intuitive.

The more I thought about it, the more I realized that the three most common things I was still using WMP for were:

  1. Ripping CDs and syncing music to the player.
  2. Syncing music to the Sansa Clip.
  3. Burning podcasts onto CD so I can listen to them in my car.

I haven’t ripped a CD in months because I’ve been buying all my recent music online.  Burning podcasts onto CD is actually very painless in WMP, so I will probably continue using it for that.

But syncing?  Could I manage the music on the player directly?  Plugging the player into a USB port registers it as another storage device, available in Windows Explorer.  Could I just drag music onto it?  The short answer is "yes", but to really make this useful, I’d need to do a few more things:

  1. Reorganize the media files to clean up where Windows Media Player originally dropped them.
  2. Edit the media tags on the files so that Artist and Song Titles are accurate and simple.
  3. Maximize the number of songs I could fit onto the player by converting everything to MP3 format.
  4. Organize them into separate playlists to accommodate my current given mood.
  5. Sit back and enjoy the sweet sounds of victory.

Reorganize the media files

Most of my digital collection was actually ripped from my CD collection using Windows Media Player, which organizes it into a folder structure that looks like this:

Folder Structure

I’m really only interested in Artist and Song Title.  If I’m in the mood for John Williams, for example, I want to hear all of his work – I don’t care if it came from the "The Spielberg/Williams Collaboration", "Harry Potter and The Sorcerer’s Stone: Soundtrack", or one of the Star Wars albums I own.  I just want to hear the music of John Williams.  So, I decided to flatten the music by removing the Album level:

Folder Structure Flattened

Next, the track numbers that prefaced the song titles were making me twitch, so I removed them:

Folder Structure No Track Numbers

The next step was to resolve all of the "Unknown Artist", "Various Artists", and other folders that had been created over time, and move those music files into folders with a real artist name.  Some of these became obvious just from the name of the song – "Takin’ Care of Business" by Bachman-Turner Overdrive, for example.  Some of these, especially the classical pieces like "Violin Concerto No. 1", took a little more work to track down.  A lot of these required me to look at the media tags attached to the file, which we’ll address next.

Edit the media tags

Each audio file has a series of tags such as Artist, Album, Song Title, Track #, etc.  I originally used these to help reorganize the music into their proper artist folders, but many of these needed to be cleaned up themselves.  Why?  Because my Sansa Clip would use organize the music by these tags.  Putting the files in a folder in Windows Explorer called "Hans Zimmer" wouldn’t be enough – the song’s Artist media tag would need to reflect that name.

Originally I thought I needed an application to allow me modify these, but I discovered that Windows Explorer can do it.  When you select a music file in Windows Explorer, the window shows a series of controls at the bottom:

Media Tag Controls

All you have to do to change these is click the tag you want to edit, type over it, and hit Enter:

Media Tags - Editing

So, my first task was going through and cleaning up the "Contributing Artists", "Album artist", and "Title" for each of my music files.  After updating a few, I realized how tedious this was going to.  I don’t have an enormous digital music collection, but it’s large enough that I figured I could write something to automate the process faster than just doing it manually.

So I did.

I had already organized each music file into a folder named after the artist responsible, and had renamed the files themselves to clean up the song title (Several songs were named things like "Satisfied* [bonus tracks].mp3", so I cleaned it up to just be "Satisfied.mp3").  What if I could write a Powershell script (my shiny new tool in my development toolbox) to rework the media tags for each file based on this information?

After consulting my good friend, Google, I found people here and here were already managing media tags from Powershell.  Using TagLib# (available from GitHub: https://github.com/mono/taglib-sharp ), it was very easy to walk through my entire music collection, updating media tags as I went:

[Reflection.Assembly]::LoadFrom( (Resolve-Path ".\taglib-sharp.dll") )

$BaseMusicPath = "C:\Users\Mark\Desktop\Music"

Get-ChildItem -Path $BaseMusicPath -Filter "*.mp3" -Recurse | ForEach-Object {
    Write-Host "Processing:" $_.FullName
    $CurrentMediaFile = [TagLib.File]::Create($_.FullName)
   
    # Set the song title to the file name
    $CurrentMediaFile.Tag.Title = [IO.FileInfo]$_.Name
   
    # Make the AlbumArtists match the Artists (contributing artists)
    $CurrentMediaFile.Tag.AlbumArtists = $CurrentMediaFile.Tag.Artists
   
    # Save the new album name into the file
    $CurrentMediaFile.Save()
}

The script looks through my music folders recursively for every MP3, opens it, sets the "Title" media tag to the file name and the "AlbumArtists" media tag to the "Artists" tag.  The latter corresponds to the "Contributing Artists" tag that appears in Windows Explorer.

The script worked like a charm.  It ran through my entire collection in a matter of seconds, and took me about half an hour to piece it together.  Overall, I estimate it saved me at least an hour of drudgery, and gave me a great excuse to do something in Powershell.

 

Maximize the number of songs

I still had a mix of WMA and MP3 files at this point.  In the course of updating the media tags, I noticed there was a pretty large gap between the average file size of a WMA file and the average file size of an MP3 – WMAs were much larger than the MP3s.  I found a free converter from KoyoteSoft that could process my entire music collection in batch – converting all WMA files to MP3 in place.  I didn’t think to capture before and after totals, but the size savings was tremendous: 30% smaller files were very common.

I actually put the media tag editing on pause to convert everything over to MP3s.  That is why the Powershell script above only handles MP3s.  By the time I got around to writing it, EVERYTHING was an MP3.

 

Organize them into Playlists

The next, and what ended up being the biggest challenge, was figuring out how I could create my own playlists.  To be fair, I had not tried this with the Sansa Clip before.  What got me thinking about it was that there was a "Playlists" option on the Clip, hinting that it was supported and that I only had to figure out how to do it.

My good friend, Google, turned out to be a good start down this path.  I found this post on the Sansa Clip forums that pointed to a couple of possible paths:

  1. If I browsed to the folder on the Clip in Windows Explorer, and right clicked on a folder or music file, I had an option for "Create Playlist".  I tried selecting multiple folders and created a playlist from them.  That dropped a .PLA file in the folder, and the player seemed to like it.  The weird thing was that this file was 0 bytes long.  Examining the file properties (again through Windows Explorer) revealed a tab called "References" that listed out all of the songs I just dropped in.  That tab would allow me to remove songs, or reorder them, but there did not appear to be any way to add new ones to an existing playlist.  If I added a new song, I’d have to reselect all of the other songs AND the new one to effectively update the playlist.  That would become unwieldy fast.
  2. The other option I found in this forum post talked about the M3U playlist file format.  This was billed as a simple text file format, which seemed much more likely to be manageable going forward.

I ended up consulting several other internet destinations to figure out what this file needed to look like, and how to get it to work on the Clip:

In addition to these posts, I did a fair amount of my own experimentation to figure out the following procedure:

  1. Create a Windows 1252 (ANSI) text file and name it with a ".m3u" file extension.
  2. Add this as the very first line of the file: #EXTM3U
  3. Add one or more relative paths to the music files to be included in the playlist.  These would be relative to the "Music" folder on the Clip where the Artist folders would be housed:

        #EXTM3U
        Antonio Vivaldi\12 Violin Concerto, for violin, strings & continuo in E major (‘La Primave.mp3
        Antonio Vivaldi\Concerto For 2 Violins In A Minor, Op. 3 No. 8 – Allegro (Mouvement 1).mp3
        Antonio Vivaldi\Four Seasons- Spring Allegro.mp3
        Émile Waldteufel\Skaters Waltz.mp3
        Franz Liszt\Hungarian Rhapsody No 2.mp3
        Franz Schubert\Moment Musical.mp3
        Frédéric Chopin\Minute Waltz.mp3
        Georges Bizet\Carmen Suite 1 Les Toreadors.mp3

    This seemed to be the minimum contents needed to get the playlist to be recognized.

    For the most part, if I kept the files in a subfolder below the Artist name, the player would not recognize them.  My decision to flatten the music files to just one level down proved to be beneficial here.  I say "for the most part" because I did have one instance where a file was 2 levels down, in an "album" folder below the Artist folder, and the player found it.  I couldn’t explain why this worked, or why moving the other files up to the Artist folder caused them to suddenly be recognized by the player.  I thought it might have something to do with the length of the overall path, but as you can see from the above samples, some of the songs I have are quite long, and the player found those just fine.

  4. Switch the player to "MTP" mode.  For the Clip, this is found under Settings\USB Mode.  My player had been set to "Auto Detect".  At least two of the posts I found mentioned the other mode, "MSC", as being completely unusable for transferring playlist files to the player.  I have not tried changing this back to "Auto Detect" or trying "MSC", and then copying the playlist files over and seeing if they still worked.  I also didn’t dig into what these two modes are.  I had been working on the playlist issue for the better part of the week, and honestly, was just interested to see it resolved rather than exploring every nook and cranny.  Perhaps another day.
  5. Place this file in the root of the "Music" folder.  I tried a few different other locations for the playlist files on the player, including the "Playlists" folder, but this was the only one where it worked.

At this point, assuming that the music files were already on the player, the "Playlists" option on the player will now show the new playlist, and let you play from it.  I decided to go one step further with the playlists, not wanting to manage the playlists file by hand, and created a small WinForms application called "Playlist Forge" that would allow me to drag and drop individual music files, or entire folders, and construct the playlist file myself.

Playlist Forge

If you drag an M3U file onto Playlist Forge, it opens it.

Dragging a single music file (MP3 or WMA) onto it adds it to the playlist, including the name of the file and the parent folder.  (Playlist Forge assumes the folder structure I mentioned previously, where the actual music files are in a folder named after the Artist.)

Dragging a folder onto Playlist Forge will recursively find all MP3 or WMA files, and include them in the playlist, regardless of their depth.  It would still only include the file name and the folder it was actually in, but it would dig down as deeply as needed in the folder structure to pull out all of the music files.

Once you have the right files in there, you hit "CTRL-S" to save it.  If you had opened an M3U file originally, it would overwrite it.  If you had just started dragging music files onto it, it will create a new file called "NewPlaylist.m3u" on your desktop.

Finally, you can hit “CTRL-N” to clear the utility out and start a new playlist from scratch.

While this is definitely rough, it proved to be much faster to write this utility and use it than trying to pull all of the paths and files out manually.  It will also allow me to easily edit the files later, as I add music to my collection.

The utility – both the source and the compiled application – are found in the PlaylistForge.zip archive found at http://tinyurl.com/MarkGilbertSource if you are interested.  (And yes, I did see that other people had built apps like this already, but this seemed like a fun little app to write.)

 

Sit back and enjoy the sweet sounds of victory

A lot of research and work for this, but after all of it I am much happier about the state of my music collection and the prospects for managing it going forward.

About these ads

January 8, 2013 - Posted by | Powershell, Visual Studio/.NET

Sorry, the comment form is closed at this time.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: