6 # Advanced CD-ROM ripping program
8 # Copyright 2010, Colin McCabe
13 require 'optparse/time'
16 #-----------------------------------------------------------------
18 #-----------------------------------------------------------------
19 $cd_dev = "/dev/cdrom"
22 #-----------------------------------------------------------------
24 #-----------------------------------------------------------------
27 system(cmd) unless $opts.dry_run == true
28 ($?.exitstatus == 0) or raise "#{cmd} failed"
31 def die_unless_installed(cmd)
32 system("which #{cmd} > /dev/null")
33 ($?.exitstatus == 0) or raise "you need to install the #{cmd} program"
36 def get_number_of_tracks_on_cd
37 look_for_tracks = false
39 IO.popen("cdda2wav -v summary -J dev=#{$cd_dev} 2>&1", "r") do |io|
40 io.readlines.each do |line|
43 if (line =~ /^AUDIOtrack/) then
44 look_for_tracks = true
45 elsif (look_for_tracks == true) then
46 look_for_tracks = false
47 line =~ /[ \t]*1-([1234567890][1234567890]*)[^1234567890]/ \
48 or raise "couldn't understand cdda2wav output!"
53 raise "couldn't find what we were looking for in cdda2wav output! \
54 output:#{lines.join('\n')}"
57 # Process the WAV file into an MP3 and FLAC file.
58 # This is done in a background process.
59 def process_wav(track)
60 FileUtils.mkdir_p(track.flac_dir, $fu_args)
61 my_system("flac -f '#{track.wav_file_name}' \
62 --output-name='#{track.flac_file_name}' &>/dev/null")
63 my_system("flac --test '#{track.flac_file_name}' &>/dev/null")
64 FileUtils.mkdir_p(track.mp3_dir, $fu_args)
65 my_system("lame -q 1 -b 192 '#{track.wav_file_name}' \
66 '#{track.mp3_file_name}' &>/dev/null")
67 FileUtils.rm_f(track.wav_file_name, $fu_args)
70 def audiorip(tnum, track)
72 my_system("nice -1 cdparanoia -w -d #{$cd_dev} #{tnum}")
74 raise "failed to rip track #{tnum} (#{track.name})"
76 # cdparanoia always outputs to cdda.wav
77 FileUtils.mv("cdda.wav", track.wav_file_name, $fu_args)
79 # If there are too many processes, wait for one of them to terminate
80 if ($children.keys.length > $opts.max_children) then
81 pid = Process.wait(-1)
98 #-----------------------------------------------------------------
100 #-----------------------------------------------------------------
103 opts = OpenStruct.new
105 opts.max_children = 4
106 $fu_args = { :verbose => true }
108 # Fill in opts values
109 parser = OptionParser.new do |myparser|
110 myparser.banner = "Usage: #{ File.basename($0) } [opts]"
111 myparser.separator("Specific options:")
112 myparser.on("--dry-run", "-d",
113 "Show what would be done, without doing it.") do |a|
115 $fu_args = { :verbose => true, :noop => true }
117 myparser.on("--tracklist [FILE]", "-t",
118 "Provide a list of tracks to use.") do |file|
119 opts.manifest_file = file
122 myparser.on("--partial-tracklist [FILE]", "-T",
123 "Provide a partial list of tracks to use.") do |file|
124 opts.manifest_file = file
127 myparser.on("--max-children [NCHILD]", "-j",
128 "The maximum number of child processes to allow at any \
129 given time") do |nchild|
130 opts.max_children = nchild.to_i
131 if (opts.max_children < 1) then
132 raise "can't set max_children to #{opts.max_children}"
138 raise "you must provide a tracklist" unless opts.manifest_file != nil
144 attr_accessor :name, :flac_dir, :flac_file_name, :mp3_dir, :mp3_file_name,
147 if name =~ /\[LL\]/ then
148 raise "you can't include [LL] in a track name"
150 if name =~ /\.mp3/ then
151 raise "don't include .mp3 in the track name; that will be added"
153 if name =~ /\.flac/ then
154 raise "don't include .flac in the track name; that will be added"
156 (name =~ /([^\/][^\/]*)\/([^\/]*[^\/])/) or \
157 raise "track name must be of the form 'foo/bar'"
159 @flac_dir = "#{$1} [LL]"
160 @flac_file_name = "#{@flac_dir}/#{$2}.flac"
162 @mp3_file_name = "#{@mp3_dir}/#{$2}.mp3"
163 @wav_file_name = "#{$1}__#{$2}.wav"
167 "track(\"#{@name}\")"
172 def initialize(filename)
174 eval(File.new(filename).read)
175 @t.each do |key, val|
176 @t[key] = Track.new(val)
178 # TODO: implement some shortcuts that make manifests easier to type.
179 # Probably avoiding the necessity to continue typing the album name if it is the same as the
180 # previous track's name would make things a lot easier without complicating everything too much.
183 def validate(num_tracks)
185 raise "you must define some tracks"
187 @t.each { |t| t.validate }
188 if (not $opts.partial) then
189 (1..num_tracks).each do |t|
190 if not @t[t].defined?
191 raise "don't know what to do with track #{t}"
195 # TODO: make sure that tracks inside albums are in order
196 # i.e. we don't map track 2 to a name that sorts to before track 1
200 (1..num_tracks).each do |tnum|
201 next unless @t.has_key?(tnum)
202 audiorip(tnum, @t[tnum])
209 @t.keys.sort.each do |key|
210 ret = "#{ret}#{key}:'#{@t[key].inspect()}'\n"
216 #-----------------------------------------------------------------
218 #-----------------------------------------------------------------
222 $opts = MyOptions.parse(ARGV)
223 rescue ArgumentError => msg
224 $stderr.puts("#{msg} Type --help to see usage information.\n")
229 die_unless_installed("lame")
230 die_unless_installed("flac")
231 die_unless_installed("cdparanoia")
232 die_unless_installed("cdda2wav")
234 manifest = Manifest.new($opts.manifest_file)
235 puts manifest.inspect
236 num_tracks = get_number_of_tracks_on_cd()
237 puts "found #{num_tracks} tracks"
238 manifest.rip(num_tracks)
239 puts "*** FINISHED ***"