6 # Advanced CD-ROM ripping program
8 # Copyright 2010, Colin McCabe
13 require 'optparse/time'
16 #-----------------------------------------------------------------
18 #-----------------------------------------------------------------
19 $cd_dev = "/dev/cdrom"
21 #-----------------------------------------------------------------
23 #-----------------------------------------------------------------
26 system(cmd) unless $opts.dry_run == true
27 ($?.exitstatus == 0) or raise "#{cmd} failed"
30 def die_unless_installed(cmd)
31 system("which #{cmd} > /dev/null")
32 ($?.exitstatus == 0) or raise "you need to install the #{cmd} program"
35 def get_number_of_tracks_on_cd
36 look_for_tracks = false
38 IO.popen("cdda2wav -v summary -J dev=#{$cd_dev} 2>&1", "r") do |io|
39 io.readlines.each do |line|
42 if (line =~ /^AUDIOtrack/) then
43 look_for_tracks = true
44 elsif (look_for_tracks == true) then
45 look_for_tracks = false
46 line =~ /[ \t]*1-([1234567890][1234567890]*)[^1234567890]/ \
47 or raise "couldn't understand cdda2wav output!"
52 raise "couldn't find what we were looking for in cdda2wav output! \
53 output:#{lines.join('\n')}"
56 # Process the WAV file into an MP3 and FLAC file.
57 # This is done in a background process.
58 def process_wav(track)
59 FileUtils.mkdir_p(track.flac_dir, $fu_args)
60 my_system("flac -f '#{track.wav_file_name}' \
61 --output-name='#{track.flac_file_name}' &>/dev/null")
62 my_system("flac --test '#{track.flac_file_name}' &>/dev/null")
63 FileUtils.mkdir_p(track.mp3_dir, $fu_args)
64 my_system("lame -q 1 -b 192 '#{track.wav_file_name}' \
65 '#{track.mp3_file_name}' &>/dev/null")
66 FileUtils.rm_f(track.wav_file_name, $fu_args)
69 def audiorip(tnum, track)
71 my_system("nice -1 cdparanoia -w -d #{$cd_dev} #{tnum}")
73 raise "failed to rip track #{tnum} (#{track.name})"
75 # cdparanoia always outputs to cdda.wav
76 FileUtils.mv("cdda.wav", track.wav_file_name, $fu_args)
78 # TODO: if there are more than N processes, wait for one of them to terminate
90 #-----------------------------------------------------------------
92 #-----------------------------------------------------------------
97 $fu_args = { :verbose => true }
100 parser = OptionParser.new do |myparser|
101 myparser.banner = "Usage: #{ File.basename($0) } [opts]"
102 myparser.separator("Specific options:")
103 myparser.on("--dry-run", "-d",
104 "Show what would be done, without doing it.") do |a|
106 $fu_args = { :verbose => true, :noop => true }
108 myparser.on("--tracklist [FILE]", "-t",
109 "Provide a list of tracks to use.") do |file|
110 opts.manifest_file = file
113 myparser.on("--partial-tracklist [FILE]", "-T",
114 "Provide a partial list of tracks to use.") do |file|
115 opts.manifest_file = file
121 raise "you must provide a tracklist" unless opts.manifest_file != nil
127 attr_accessor :name, :flac_dir, :flac_file_name, :mp3_dir, :mp3_file_name,
130 if name =~ /\[LL\]/ then
131 raise "you can't include [LL] in a track name"
133 if name =~ /\.mp3/ then
134 raise "don't include .mp3 in the track name; that will be added"
136 if name =~ /\.flac/ then
137 raise "don't include .flac in the track name; that will be added"
139 (name =~ /([^\/][^\/]*)\/([^\/]*[^\/])/) or \
140 raise "track name must be of the form 'foo/bar'"
142 @flac_dir = "#{$1} [LL]"
143 @flac_file_name = "#{@flac_dir}/#{$2}.flac"
145 @mp3_file_name = "#{@mp3_dir}/#{$2}.mp3"
146 @wav_file_name = "#{$1}__#{$2}.wav"
150 "track(\"#{@name}\")"
155 def initialize(filename)
157 eval(File.new(filename).read)
158 @t.each do |key, val|
159 @t[key] = Track.new(val)
161 # TODO: implement some shortcuts that make manifests easier to type.
162 # Probably avoiding the necessity to continue typing the album name if it is the same as the
163 # previous track's name would make things a lot easier without complicating everything too much.
166 def validate(num_tracks)
168 raise "you must define some tracks"
170 @t.each { |t| t.validate }
171 if (not $opts.partial) then
172 (1..num_tracks).each do |t|
173 if not @t[t].defined?
174 raise "don't know what to do with track #{t}"
178 # TODO: make sure that tracks inside albums are in order
179 # i.e. we don't map track 2 to a name that sorts to before track 1
183 (1..num_tracks).each do |tnum|
184 next unless @t.has_key?(tnum)
185 audiorip(tnum, @t[tnum])
191 @t.keys.sort.each do |key|
192 ret = "#{ret}#{key}:'#{@t[key].inspect()}'\n"
198 #-----------------------------------------------------------------
200 #-----------------------------------------------------------------
204 $opts = MyOptions.parse(ARGV)
205 rescue ArgumentError => msg
206 $stderr.puts("#{msg} Type --help to see usage information.\n")
211 die_unless_installed("lame")
212 die_unless_installed("flac")
213 die_unless_installed("cdparanoia")
214 die_unless_installed("cdda2wav")
216 manifest = Manifest.new($opts.manifest_file)
217 puts manifest.inspect
218 num_tracks = get_number_of_tracks_on_cd()
219 puts "found #{num_tracks} tracks"
220 manifest.rip(num_tracks)