Loading twitter status...
/ 19.Mar.2007
I’m a simple person. I don’t need much when it comes to burning CD’s. Which is why I don’t really care that the CD burning support in linux isn’t all that spectacular. If there’s one thing that Ubuntu doesn’t do well, it’s burn CD’s. For their own sake, I hope they fix up Serpentine for the release of Feisty, because right now it’s terrible. It takes 10 minutes just to import a few mp3’s and then another 10 minutes to convert them.. and then we have to cross our fingers and hope the burn works or that Ubuntu see’s the blank CD that’s in the CDRW.
Luckily, this is the only thing I really use a CD burner for. Sometimes I need to burn some songs to an audio CD to listen to in the car. Since I was tired or working with Rails for an evening, I took a couple of hours to code a small script in Ruby that automates the process for me. There’s no GUI. It’s very simple. But it doesn’t need to be complicated, so who cares? It requires Ruby (of course) and the ruby-mp3info ruby gem. It also requires cdrdao. Which I’m pretty sure is standard in most Ubuntu systems, but I could be wrong about that.
gem install ruby-mp3info
Here’s the code. Have fun with it. Maybe I’ll slap a GUI on it when I feel like playing with Ruby and GTK some day. This shouldn’t have any problems running on any linux disto that has cdrao, ruby, and ruby-mp3info installed.
#!/usr/bin/env ruby
#
# Burn a folder of MP3's to an audio CD
# -------------------------------------
# This script requires the ruby-mp3info
# gem. install it with gem install.
#
# burn_audiocd /path/to/mp3/files
#
# Change this if you don't have permissions
TempDir = "/tmp/burn_audiocd"
require "rubygems"
require "mp3info"
require "ftools"
def main
dir = parse_directory(ARGV[0])
calculate_time(dir)
prepare_temp(dir)
convert_to_wav(dir)
prepare_cue_sheet(dir)
burn_cd
end
# Display some help.
def output_help
puts 'burn_audiocd: USAGE: burn_audiocd <mp3_directory>'
exit
end
# Simple checks to make sure the directory has MP3 files in it.
def parse_directory(dir)
output_help if dir.nil? || dir == "" || !File.directory?(dir)
print 'Checking Directory... '
num_mp3s = 0
Dir.foreach(dir) {|filename| num_mp3s += 1 if filename =~ /\.mp3$/i}
if num_mp3s == 0
puts 'No mp3 files found!'
exit
end
puts "#{num_mp3s} mp3 files found."
return dir
end
# We want to see how long the MP3's are (roughly) so we know
# if we want to add more songs or not.
def calculate_time(dir)
puts 'Estimating total number of minutes for this CD...'
time = 0
Dir.foreach(dir) { |filename|
Mp3Info.open("#{dir}/#{filename}") {|mp3info| time += mp3info.length} if filename =~ /\.mp3$/i
}
if time/60 > 80
printf "Total CD Time is estimated at %5.2f minutes.\nThis will not fit on a regular 80 minute CD.", time/60
exit
end
printf "Estimated CD Time: %5.2f minutes\nContinue? [n/Y] ", time/60
exit if $stdin.gets =~ /n|no/i
true
end
# We'll store all the converted files in a temp dir. So we'll
# have to create it.
def prepare_temp(dir)
if File.directory?(TempDir)
puts 'Cleaning Up Temporary Directory...'
FileUtils.rm Dir["#{TempDir}/*"]
else
puts 'Creating Temporary Directory...'
Dir.mkdir(TempDir)
end
end
# Convert the mp3 files to .wav files so they can be burned as
# an audio cd.
def convert_to_wav(dir)
puts 'Converting Files...'
Dir.foreach(dir) { |filename|
if filename =~ /\.mp3$/i
puts " #{filename}"
`lame --quiet --decode #{dir.escape_spaces}/#{filename.escape_spaces} #{TempDir}/#{File.basename(filename.downcase.convert_spaces, ".mp3")}.wav 2>/dev/null`
end
}
end
# Create the file that will be used as a cue sheet
def prepare_cue_sheet(dir)
puts 'Creating a CUE sheet...'
cue = File.new("#{TempDir}/cuesheet.toc", "w+")
cue << "CD_DA\nCD_TEXT {\n LANGUAGE_MAP {\n 0 : EN\n }\n LANGUAGE 0 {\n TITLE \"Ruby Created CD!\"\n }\n}\n\n"
Dir.foreach(dir) { |filename|
if filename =~ /\.mp3$/
cue << "TRACK AUDIO\nCD_TEXT {\n LANGUAGE 0 {\n "
Mp3Info.open("#{dir}/#{filename}") {|mp3info|
cue << "TITLE \"#{mp3info.tag.title}\"\n "
cue << "PERFORMER \"#{mp3info.tag.artist}\"\n }\n}\n\n"
}
cue << "FILE \"#{TempDir}/#{File.basename(filename.downcase.convert_spaces, ".mp3")}.wav\" 0\n"
end
}
cue.close
end
# Actually burn the CD.
def burn_cd
puts 'Starting Burn Process...'
`cdrdao write #{TempDir}/cuesheet.toc`
puts 'Cleaning up Temporary Files...'
FileUtils.rm Dir["#{TempDir}/*"]
puts 'CD Creation Complete!'
end
class String
def escape_spaces
self.gsub(/\s/, '\ ')
end
def convert_spaces
self.gsub(/\s+/, '_')
end
end
main
micro theme by seaofclouds, edited by me, and powered with Mephisto
Sorry, comments are closed for this article.