superrip: Be less verbose in flac and lame programs
[cmccabe-bin] / superrip.rb
1 #!/usr/bin/ruby -w
2
3 #
4 # superrip.rb
5 #
6 # Advanced CD-ROM ripping program
7
8 # Copyright 2010, Colin McCabe
9 #
10
11 require 'fileutils'
12 require 'optparse'
13 require 'optparse/time'
14 require 'ostruct'
15
16 #-----------------------------------------------------------------
17 # constants
18 #-----------------------------------------------------------------
19 $cd_dev = "/dev/cdrom"
20
21 #-----------------------------------------------------------------
22 # functions
23 #-----------------------------------------------------------------
24 def my_system(cmd)
25   puts cmd
26   system(cmd) unless $opts.dry_run == true
27   ($?.exitstatus == 0) or raise "#{cmd} failed"
28 end
29
30 def die_unless_installed(cmd)
31   system("which #{cmd} > /dev/null")
32   ($?.exitstatus == 0) or raise "you need to install the #{cmd} program"
33 end
34
35 def get_number_of_tracks_on_cd
36   look_for_tracks = false
37   IO.popen("cdda2wav -v summary -J dev=#{$cd_dev} 2>&1", "r") do |io|
38     io.readlines.each do |line|
39       line.chomp!
40       if (line =~ /^AUDIOtrack/) then
41         look_for_tracks = true
42       elsif (look_for_tracks == true) then
43         look_for_tracks = false
44         line =~ /[ \t]*1-([1234567890][1234567890]*)[^1234567890]/ \
45           or raise "couldn't understand cdda2wav output!"
46         return $1.to_i
47       end
48     end
49   end
50   raise "couldn't find what we were looking for in cdda2wav output!"
51 end
52
53 def audiorip(tnum, track)
54   begin
55     my_system("nice -1 cdparanoia -w -d #{$cd_dev} #{tnum}")
56   rescue
57     raise "failed to rip track #{tnum} (#{track.name})"
58   end
59   # cdparanoia always outputs to cdda.wav
60   FileUtils.mv("cdda.wav", track.wav_file_name, $fu_args)
61
62   # TODO: spawn a thread to do this stuff in the background
63   FileUtils.mkdir_p(track.flac_dir, $fu_args)
64   my_system("flac -f '#{track.wav_file_name}' \
65 --output-name='#{track.flac_file_name}' &>/dev/null")
66   begin
67     my_system("flac --test '#{track.flac_file_name}' &>/dev/null")
68   rescue
69     raise "failed to encode #{track.flac_file_name}"
70   end
71   FileUtils.mkdir_p(track.mp3_dir, $fu_args)
72   my_system("lame -q 1 -b 192 '#{track.wav_file_name}' \
73 '#{track.mp3_file_name}' &>/dev/null")
74   FileUtils.rm_f(track.wav_file_name, $fu_args)
75 end
76
77 #-----------------------------------------------------------------
78 # classes
79 #-----------------------------------------------------------------
80 class MyOptions
81   def self.parse(args)
82     opts = OpenStruct.new
83     opts.dry_run = false
84     $fu_args = { :verbose => true }
85
86     # Fill in opts values
87     parser = OptionParser.new do |myparser|
88       myparser.banner = "Usage: #{ File.basename($0) } [opts]"
89       myparser.separator("Specific options:")
90       myparser.on("--dry-run", "-d",
91             "Show what would be done, without doing it.") do |a|
92         opts.dry_run = true
93         $fu_args = { :verbose => true, :noop => true }
94       end
95       myparser.on("--tracklist [FILE]", "-t",
96             "Provide a list of tracks to use.") do |file|
97         opts.manifest_file = file
98         opts.partial = false
99       end
100       myparser.on("--partial-tracklist [FILE]", "-T",
101             "Provide a partial list of tracks to use.") do |file|
102         opts.manifest_file = file
103         opts.partial = true
104       end
105     end
106     parser.parse!(args)
107
108     raise "you must provide a tracklist" unless opts.manifest_file != nil
109     return opts
110   end
111 end
112
113 class Track
114   attr_accessor :name, :flac_dir, :flac_file_name, :mp3_dir, :mp3_file_name,
115     :wav_file_name
116   def initialize(name)
117     if name =~ /\[LL\]/ then
118       raise "you can't include [LL] in a track name" 
119     end
120     if name =~ /\.mp3/ then
121       raise "don't include .mp3 in the track name; that will be added"
122     end
123     if name =~ /\.flac/ then
124       raise "don't include .flac in the track name; that will be added"
125     end
126     (name =~ /([^\/][^\/]*)\/([^\/]*[^\/])/) or \
127       raise "track name must be of the form 'foo/bar'"
128     @name = name
129     @flac_dir = "#{$1} [LL]"
130     @flac_file_name = "#{@flac_dir}/#{$2}.flac"
131     @mp3_dir = "#{$1}"
132     @mp3_file_name = "#{@mp3_dir}/#{$2}.mp3"
133     @wav_file_name = "#{$1}__#{$2}.wav"
134   end
135
136   def inspect
137     "track(\"#{@name}\")"
138   end
139 end
140
141 class Manifest
142   def initialize(filename)
143     @t = Hash.new
144     eval(File.new(filename).read)
145     @t.each do |key, val|
146       @t[key] = Track.new(val)
147     end
148     # TODO: implement some shortcuts that make manifests easier to type.
149     # Probably avoiding the necessity to continue typing the album name if it is the same as the
150     # previous track's name would make things a lot easier without complicating everything too much.
151   end
152
153   def validate(num_tracks)
154     if (@t.empty?) then
155       raise "you must define some tracks"
156     end
157     @t.each { |t| t.validate }
158     if (not $opts.partial) then
159       (1..num_tracks).each do |t|
160         if not @t[t].defined?
161           raise "don't know what to do with track #{t}"
162         end
163       end
164     end
165     # TODO: make sure that tracks inside albums are in order
166     # i.e. we don't map track 2 to a name that sorts to before track 1
167   end
168
169   def rip(num_tracks)
170     (1..num_tracks).each do |tnum|
171       next unless @t.has_key?(tnum)
172       audiorip(tnum, @t[tnum])
173     end
174   end
175
176   def inspect
177     ret = ""
178     @t.keys.sort.each do |key|
179       ret = "#{ret}#{key}:'#{@t[key].inspect()}'\n"
180     end
181     return ret
182   end
183 end
184
185 #-----------------------------------------------------------------
186 # main
187 #-----------------------------------------------------------------
188 # Parse options.
189 begin
190   begin
191     $opts = MyOptions.parse(ARGV)
192   rescue ArgumentError => msg
193   $stderr.puts("#{msg} Type --help to see usage information.\n")
194   exit 1
195   end
196 end
197
198 die_unless_installed("lame")
199 die_unless_installed("flac")
200 die_unless_installed("cdparanoia")
201 die_unless_installed("cdda2wav")
202
203 manifest = Manifest.new($opts.manifest_file)
204 puts manifest.inspect
205 num_tracks = get_number_of_tracks_on_cd()
206 puts "found #{num_tracks} tracks"
207 manifest.rip(num_tracks)
208 exit 0