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
end

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
end

# Check for trailing slant on directory name
if ARGV[1] =~ //$/ # ends in slant
  outdir = ARGV[1]
else
  outdir = ARGV[1] + “/”
end
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
    l.sub(/’/,”\'”)
    `cp — “#{l}” #{outdir}`
  else # Lines that start with hashmark can go through verbatim
    output.push l
  end
end

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

# That’s all folks.
exit 0

Advertisements

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