Home | Ruby |     Share This Page
Creating Music Playlists with Ruby
Version 1.0, 08/19/2007


This is a Linux project to organize collections of music that are in computer media form.

During my yearly boat trips to the wilds of Alaska, I eventually get out of range of satellite radio, so I make my navigation computer play MP3 music instead. This in turn has gotten me into thinking about how music libraries are organized, and how existing music players read data from a music collection.

This article describes what seems to be the easiest and most common way to organize a music collection. Once some playlist files have been created using the provided Ruby script, any number of Linux music playing programs will automatically recognize the playlists and organize your music collection on the fly. This makes it easy to move between different music programs and decide which one you like.

The two Linux music programs I particularly like are Juk and Amarok, and both of them will automatically index your music collection once they have been given the directory path where your music is stored (and where the playlists have been generated).

The Details

After some study I have come to realize there is a simple playlist file format (defined here) with the suffix "M3U" that seems to be automatically recognized by many of the music players available on Linux. Internally, the file looks like this:

#EXTM3U First file line: mandatory file header that identifies the format
#EXTINF:455,Track 1 Name "Extra information" for the first music file, including the duration of the music in seconds (455) and an optional track name.
track_01.mp3 Relative path to the first music file. If the M3U file is located in the same directory as the file, the file name is all that is needed.
#EXTINF:283,Track 2 Name Information for the second file.
track_02.mp3 Relative path for the second file
And so forth, for any required number of music files.

Click here to see a sample M3U file (this file has a suffix of ".txt" to allow it to be downloaded as plain text, but your M3U files should have a suffix of ".m3u").

It's important to say that my collection represents the simplest possible case — the M3U playlist files are located in the same directory as the music files (which makes the file paths very short), I don't have track titles because my collection was copied and converted to MP3s from a big collection of old classical music CDs, and I gave each music directory the album title as its name.

The Process

Here are the steps required to organize your music collection:

  • Make sure you have Ruby installed. If it has not been installed, get it here or by way of your favorite package management tool.
  • Install a copy of a utility named "mp3info," either here or by using your favorite package utility. This program is used to acquire play times from the MP3 files.
  • Group your music collection into directories, each directory being given the album title. Like this:

  • Download the playlist generator Ruby script from this page (see below).
  • Edit the script to include the path to your music collection.
  • Make the script file executable and run it. If you give the script file a suffix of ".sh" instead of ".rb", on most Linux systems this will allow it to be run by simply clicking it with your mouse.
  • Run one of the nicer Linux music playing programs, like Juk or Amarok, either of which will automatically index your collection after being given the path to the primary music directory (named "Music" above).
  • This project can be looked on as a simple starting point and tutorial for a more complex project in which song titles, artists, etc. are available and can be integrated into the M3U playlist according to the specification outlined above.


    Here is the Ruby script listing. You may simply copy it from your browser's display, or click here for a plain-text copy.

    #!/usr/bin/ruby -w
    # This script is (c) Copyright 2007, P. Lutus
    # and is released under the GPL
    # relaunch in window
    exec("konsole -e #{$0} #{ARGV.join(' ')}") if ENV['TERM'] == "dumb"
    # create m3u format playlists for each music directory
    base="/netbackup/music" # change this path to suit your needs
    albums = Dir["#{base}/*"]
    albums.sort.each do |path|
       data = "#EXTM3U\n"
       # get the directory name, last element in path
       name = path.sub(/.*\/(.*)/,"\\1")
       name.gsub!(/_/," ")
       name.gsub!(/(\A|\s)\w/) { |c| c.upcase }
       puts name + " ..."
       tracks = Dir["#{path}/*.mp3"]
       tracks.sort.each do |track|
          track_name = track.sub(/.*\/(.*)/,"\\1")
          info =`mp3info -x #{track} 2>&1`
          m,s = info.split(":")
          secs = m.to_i * 60 + s.to_i
          data += "#EXTINF:#{secs},\n" + track_name + "\n"
          File.open("#{path}/#{name}.m3u","w") { |f| f.write data }
    print "Press Enter to close:"
    • Examine the script listing above. Before running the script, you will need to change the value of "base," the variable that contains the path to your music collection.
    • This simple script may serve as a starting point for a more ambitious project that integrates more information than I have available in my music collection.
    • This is a completely typical Ruby project — it took about an hour from conception to completion (not counting the effort of writing this Web page). If I need to change the script some months from now, I expect that I will still understand it perfectly because of Ruby's transparent syntax. And it solved a rather important problem I faced — I had come to realize that Amarok wasn't going to organize my music collection unless I generated some playlists, and I certainly wasn't going to write them by hand.
    Revision History
    • Version 1.0 08/19/2007. Initial Public Release.
    The Ruby music organizer is Copyright 2007, P. Lutus, and is released under the GPL.

    Home | Ruby |     Share This Page