Home | | Ruby | | Share This Page |
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).
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:
Data Comment#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.
Here are the steps required to organize your music collection:
Group your music collection into directories, each directory being given the album title. Like this:
Music
Album_Title_1
Track_1.mp3
Track_2.mp3
Track_3.mp3
Album_Title_2
Track_1.mp3
Track_2.mp3
Track_3.mp3
Album_Title_3
Track_1.mp3
Track_2.mp3
Track_3.mp3
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` info.sub!(/.*Length:\s*([\d|:]+).*/m,"\\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 } end end print "Press Enter to close:" STDIN.readline
Home | | Ruby | | Share This Page |