superrip: incremental improvements
[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
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(track, number)
54   begin
55     my_system("nice -1 cdparanoia -w -d #{$cd_dev} #{number}")
56   rescue
57     raise "failed to rip track #{number} (#{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 -c #{track.wav_file_name} > #{track.flac_file_name}")
65   my_system("lame -q 1 -b 192 #{track.wav_file_name} > #{track.mp3_file_name}")
66   FileUtils.rm_f(track.wav_file, $fu_args)
67 end
68
69 #-----------------------------------------------------------------
70 # classes
71 #-----------------------------------------------------------------
72 class MyOptions
73   def self.parse(args)
74     opts = OpenStruct.new
75     opts.dry_run = false
76     $fu_args = { :verbose => true }
77
78     # Fill in opts values
79     parser = OptionParser.new do |myparser|
80       myparser.banner = "Usage: #{ File.basename($0) } [opts]"
81       myparser.separator("Specific options:")
82       myparser.on("--dry-run", "-d",
83             "Show what would be done, without doing it.") do |a|
84         opts.dry_run = true
85         $fu_args = { :verbose => true, :noop => true }
86       end
87       myparser.on("--tracklist [FILE]", "-t",
88             "Provide a list of tracks to use.") do |file|
89         opts.manifest_file = file
90         opts.partial = false
91       end
92       myparser.on("--partial-tracklist [FILE]", "-T",
93             "Provide a partial list of tracks to use.") do |file|
94         opts.manifest_file = file
95         opts.partial = true
96       end
97     end
98     parser.parse!(args)
99
100     raise "you must provide a tracklist" unless opts.manifest_file != nil
101     return opts
102   end
103 end
104
105 class Track
106   attr_accessor :name, :flac_dir, :flac_file_name, :mp3_dir, :mp3_file_name
107   def initialize(name)
108     if name =~ /\[LL\]/ then
109       raise "you can't include [LL] in a track name" 
110     end
111     if name =~ /\.mp3/ then
112       raise "don't include .mp3 in the track name; that will be added"
113     end
114     if name =~ /\.flac/ then
115       raise "don't include .flac in the track name; that will be added"
116     end
117     (name =~ /([^\/][^\/]*)\/([^\/]*[^\/])/) or \
118       raise "track name must be of the form 'foo/bar'"
119     @name = name
120     @flac_dir = "#{1} [LL]"
121     @flac_file_name = "#{@flac_dir}/#{2}.flac"
122     @mp3_dir = "#{1}"
123     @mp3_file_name = "#{@mp3_dir}/#{2}.mp3"
124     @wav_file_name = "#{1}__#{2}.wav"
125   end
126
127   def inspect
128     "#{@name}"
129   end
130 end
131
132 class Manifest
133   def initialize(filename)
134     @t = Hash.new
135     eval(File.new(filename).read)
136     @t.each do |key, val|
137       @t[key] = Track.new(val)
138     end
139     # TODO: implement some shortcuts that make manifests easier to type.
140     # Probably avoiding the necessity to continue typing the album name if it is the same as the
141     # previous track's name would make things a lot easier without complicating everything too much.
142   end
143
144   def validate(num_tracks)
145     if (@t.empty?) then
146       raise "you must define some tracks"
147     end
148     @t.each { |t| t.validate }
149     if (not $opts.partial) then
150       (1..num_tracks).each do |t|
151         if not @t[t].defined?
152           raise "don't know what to do with track #{t}"
153         end
154       end
155     end
156     # TODO: make sure that tracks inside albums are in order
157     # i.e. we don't map track 2 to a name that sorts to before track 1
158   end
159
160   def rip(num_tracks)
161     (1..num_tracks).each do |t|
162       next unless @t.defined?(t)
163       audiorip(t)
164     end
165   end
166
167   def inspect
168     ret = ""
169     @t.keys.sort.each do |key|
170       ret = "#{ret}#{key}:'#{@t[key].inspect()}'\n"
171     end
172     return ret
173   end
174 end
175
176 #-----------------------------------------------------------------
177 # main
178 #-----------------------------------------------------------------
179 # Parse options.
180 begin
181   begin
182     $opts = MyOptions.parse(ARGV)
183   rescue ArgumentError => msg
184   $stderr.puts("#{msg} Type --help to see usage information.\n")
185   exit 1
186   end
187 end
188
189 die_unless_installed("lame")
190 die_unless_installed("flac")
191 die_unless_installed("cdparanoia")
192 die_unless_installed("cdda2wav")
193
194 manifest = Manifest.new($opts.manifest_file)
195 #puts manifest.inspect
196 num_tracks = get_number_of_tracks_on_cd()
197 puts "found #{num_tracks} tracks"
198 #manifest.rip(num_tracks)
199 exit 0