I needed a nicer way to take a bunch of songs with me, and discovered
that if I make a playlist in my media player that’s already been
properly arranged and shuffled for me, and load the playlist on my
handheld, it loads faster than if I have to have it load a pile of
music files and shuffle them.  However, the playlist’s directory
structure was wrong…

# mvmusic.rb <input-playlist> <target-directory>
# Converts playlist to point at files by stripping its directory
# names from the filenames in <input-playlist> and copying the corrected
# playlist, as well as the appropriate files, to a single <target-directory>.
# The original design is to make it easier to prepare music for use in a
# portable medium or player device.
# -ch 11/20/2005

# Make sure we had two arguements
unless ARGV.length == 2
  puts “Usage:”
  puts ”  mvmusic.rb <input-playlist> <target-directory>”
  exit 1

infile = File.open ARGV[0]
lines = infile.readlines

# Make sure it’s .m3u format
unless lines[0] =~ /^#EXTM3U$/
  puts “Input file is not in .m3u format.”
  exit 1

# Check for trailing slant on directory name
if ARGV[1] =~ //$/ # ends in slant
  outdir = ARGV[1]
  outdir = ARGV[1] + “/”
output = [] # The outgoing playlist

lines.each do |l|
  unless l =~ /^#.*/ # Lines that start with hashmark aren’t filenames
    puts File.basename(l)
    out = “#{outdir}” + File.basename(l)
    output.push out
    l.chomp! # Clip the newline so the ‘cp’ won’t break
    `cp — “#{l}” #{outdir}`
  else # Lines that start with hashmark can go through verbatim
    output.push l

# Finally, write out the finished playlist.
outfile = File.new(“#{outdir}” + File.basename(ARGV[0]), “w+”)
outfile.write output

# That’s all folks.
exit 0


One thought on “

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s