]> diplodocus.org Git - flac-archive/blob - fa-rip
Use album.release where possible, restrict
[flac-archive] / fa-rip
1 #! /usr/bin/env python2.4
2
3 '''
4 =head1 NAME
5
6 B<fa-rip> - rip a CD for B<fa-flacd>
7
8 =head1 SYNOPSIS
9
10 B<fa-rip> [B<-d> I<device>] [B<-p> I<post-processor> [B<-t> I<track-count>]
11
12 =head1 DESCRIPTION
13
14 B<fa-rip> creates a temporary directory for storage of its
15 intermediate files, uses MusicBrainz to create the "cue" file and
16 candidate tags files, and runs C<cdparanoia(1)> to rip the CD to the
17 "wav" file.
18
19 In order for this CD to be processed by B<fa-flacd>, you must create a
20 "tags" file. This is usually done by renaming one of the
21 candidate-tags files and deleting the others. Don't forget to fill in
22 the DATE tag in the selected candidate before renaming it. If
23 B<fa-rip> could not find any tag information from MusicBrainz, you'll
24 have to fill out the candidate-tags-0 template.
25
26 =head1 OPTIONS
27
28 =over 4
29
30 =item B<-d> [B<--device>] I<device>
31
32 Use I<device> as the CD-ROM device, instead of the default
33 "/dev/cdrom" or the environment variable CDDEV.
34
35 =item B<-p> [B<--post-processor>] I<post-processor>
36
37 Create a "post-processor" file in the temporary directory containing
38 the line 'I<post-processor> "$@"'. See B<fa-flacd>'s man page for
39 information about this hook.
40
41 =item B<-t> [B<--tracks>] I<track-count>
42
43 Archive only the first I<track-count> tracks. This is handy for
44 ignoring data tracks.
45
46 =back
47
48 =head1 ENVIRONMENT
49
50 =over 4
51
52 =item CDDEV
53
54 B<fa-rip> uses this to rip audio and save the cuesheet for a CD.
55 MusicBrainz::Client can usually figure this out automatically.
56
57 =back
58
59 =head1 AUTHORS
60
61 Written by Eric Gillespie <epg@pretzelnet.org>.
62
63 flac-archive is free software; you may redistribute it and/or modify
64 it under the same terms as Perl itself.
65
66 =cut
67
68 ''' #' # python-mode is sucks
69
70 import os, sys, tempfile, traceback
71 from optparse import OptionParser
72
73 import musicbrainz2.disc
74 import musicbrainz2.webservice
75
76 from org.diplodocus.util import catch_EnvironmentError as c
77
78 def mkcue(disc, trackcount=None):
79 fp = c(file, 'cue', 'w')
80 c(fp.write, 'FILE "dummy.wav" WAVE\n')
81 c(fp.write, ' TRACK 01 AUDIO\n')
82 c(fp.write, ' INDEX 01 00:00:00\n')
83
84 if trackcount == None:
85 trackcount = disc.lastTrackNum
86 else:
87 trackcount = min(trackcount, disc.lastTrackNum)
88
89 pregap = disc.tracks[0][0]
90 for i in xrange(disc.firstTrackNum, trackcount):
91 offset = disc.tracks[i][0]
92 offset -= pregap
93
94 minutes = seconds = 0
95 sectors = offset % 75
96 if offset >= 75:
97 seconds = offset / 75
98 if seconds >= 60:
99 minutes = seconds / 60
100 seconds = seconds % 60
101
102 c(fp.write, ' TRACK %02d AUDIO\n' % (i + 1,))
103 c(fp.write,
104 ' INDEX 01 %02d:%02d:%02d\n' % (minutes, seconds, sectors))
105
106 c(fp.close)
107
108 return trackcount
109
110 def tags_file(fn, trackcount, various, artist=None, album=None,
111 release_dates={}, tracks=[]):
112 fp = c(file, fn, 'w')
113 c(fp.write, 'ARTIST=')
114 if artist != None:
115 c(fp.write, artist)
116 c(fp.write, '\nALBUM=')
117 if album != None:
118 c(fp.write, album)
119 c(fp.write, '\n')
120
121 have_date = False
122 for (country, date) in release_dates.items():
123 have_date = True
124 c(fp.write, 'DATE[%s]=%s\n' % (country, date))
125 have_date or c(fp.write, 'DATE=\n')
126
127 for i in xrange(1, trackcount + 1):
128 try:
129 track = tracks.pop(0)
130 title = track.title
131 artist = track.artist
132 except IndexError:
133 title = ''
134 artist = ''
135 various and c(fp.write, 'ARTIST[%d]=%s\n' % (i, artist))
136 c(fp.write, 'TITLE[%d]=%s\n' % (i, title))
137
138 c(fp.close)
139
140 def tags(disc, trackcount, mb=True):
141 results = []
142 seen_various = False
143
144 tags_file('candidate-tags-0', trackcount, False)
145
146 if not mb:
147 return
148
149 include = musicbrainz2.webservice.ReleaseIncludes(tracks=True)
150 q = musicbrainz2.webservice.Query()
151 filter = musicbrainz2.webservice.ReleaseFilter(discId=disc.getId())
152
153 i = 0
154 for album in q.getReleases(filter):
155 i += 1
156 various = not album.release.isSingleArtistRelease()
157
158 if various and not seen_various:
159 seen_various = True
160 tags_file('candidate-tags-0v', trackcount, True)
161
162 tags_file('candidate-tags-' + str(i), trackcount, various,
163 album.release.artist.name, album.release.title,
164 album.release.getReleaseEventsAsDict(),
165 q.getReleaseById(album.release.id, include).tracks)
166
167 def rip(device, trackcount, single_file):
168 if device == None:
169 device = '/dev/cdrom'
170
171 argv = ['cdparanoia', '-d', device]
172
173 if single_file:
174 argv.extend(['1-' + str(trackcount), 'wav'])
175 else:
176 argv.append('-B')
177
178 c(os.execvp, argv[0], argv)
179
180 def make_post_processor(command):
181 if command == None:
182 return
183
184 fd = c(os.open, 'post-processor', os.O_CREAT | os.O_WRONLY, 0555)
185 fp = c(os.fdopen, fd, 'w')
186 c(fp.write, command +' "$@"\n')
187 c(fp.close)
188
189 def main(argv):
190 # Control the exit code for any uncaught exceptions.
191 try:
192 parser = OptionParser()
193 parser.disable_interspersed_args()
194 parser.add_option('-d', '--device')
195 parser.add_option('-m', '--no-musicbrainz',
196 action='store_true', default=False)
197 parser.add_option('-p', '--post-processor')
198 parser.add_option('-s', '--single-file',
199 action='store_true', default=False)
200 parser.add_option('-t', '--tracks', type='int', default=99)
201 except:
202 traceback.print_exc()
203 return 2
204
205 try:
206 # Raises SystemExit on invalid options in argv.
207 (options, args) = parser.parse_args(argv[1:])
208 except Exception, error:
209 if isinstance(error, SystemExit):
210 return 1
211 traceback.print_exc()
212 return 2
213
214 try:
215 device = options.device
216 trackcount = options.tracks
217
218 tempdir = c((lambda x: tempfile.mkdtemp(prefix=x, dir='.')),
219 'flac-archive.')
220 c(os.chdir, tempdir)
221
222 make_post_processor(options.post_processor)
223
224 disc = musicbrainz2.disc.readDisc(device)
225
226 trackcount = mkcue(disc, trackcount)
227 tags(disc, trackcount, mb=not options.no_musicbrainz)
228 rip(device, trackcount, options.single_file)
229 except Exception, error:
230 if isinstance(error, SystemExit):
231 raise
232 # check all print_exc and format_exc in fa-flacd.py; i think
233 # for some i don't do this msg print check
234 sys.stderr.write(getattr(error, 'msg', ''))
235 traceback.print_exc()
236 return 2
237
238 return 0
239
240 if __name__ == '__main__':
241 sys.exit(main(sys.argv))