]> diplodocus.org Git - flac-archive/blob - fa-rip
Untested: make multi-file the default with a single-file option.
[flac-archive] / fa-rip
1 #! /usr/bin/env perl
2
3 # $Id$
4 # $URL$
5
6 =head1 NAME
7
8 B<fa-rip> - rip a CD for B<fa-flacd>
9
10 =head1 SYNOPSIS
11
12 B<fa-rip> [B<-d> I<device>] [B<-p> I<post-processor> [B<-t> I<track-count>]
13
14 =cut
15
16 use strict;
17 use warnings;
18
19 use Env qw(
20 CDDEV
21 );
22
23 use Fcntl qw(O_CREAT O_WRONLY);
24 use File::Temp;
25 use Getopt::Long qw(:config gnu_getopt no_ignore_case);
26 use List::Util qw(min);
27 use POSIX ':sys_wait_h';
28 use Pod::Usage;
29
30 use MusicBrainz::Client;
31 use MusicBrainz::Queries qw(
32 MBQ_GetCDTOC
33 MBE_TOCGetFirstTrack
34 MBE_TOCGetLastTrack
35 MBE_TOCGetTrackSectorOffset
36 );
37 use MusicBrainz::Client::Simple;
38
39 sub mkcue {
40 my $device = shift;
41 my $trackcount = shift;
42
43 my $mb = MusicBrainz::Client->new;
44 defined($device) and $mb->set_device($device);
45
46 $mb->query(MBQ_GetCDTOC) or die($mb->get_query_error);
47
48 open(my $fh, '>cue') or die("open('>cue'): $!");
49 print($fh "FILE \"dummy.wav\" WAVE\n");
50 print($fh " TRACK 01 AUDIO\n");
51 print($fh " INDEX 01 00:00:00\n");
52
53 my $first = $mb->get_result_data(MBE_TOCGetFirstTrack) + 1;
54 $trackcount = min($trackcount, $mb->get_result_data(MBE_TOCGetLastTrack));
55 # There is frequently (always?) an offset of 150 sectors, so
56 # we'll subtract this offset from each track offset.
57 my $something = $mb->get_result_data1(MBE_TOCGetTrackSectorOffset, 2);
58
59 for my $track ($first .. $trackcount) {
60 my $off = $mb->get_result_data1(MBE_TOCGetTrackSectorOffset, $track+1);
61 $off -= $something;
62
63 my ($minutes,$seconds)=(0,0);
64 my $sectors = $off % 75;
65 if ($off >= 75) {
66 $seconds = $off / 75;
67 if ($seconds >= 60) {
68 $minutes = $seconds / 60;
69 $seconds = $seconds % 60;
70 }
71 }
72
73 printf($fh " TRACK %02d AUDIO\n", $track);
74 printf($fh " INDEX 01 %02d:%02d:%02d\n",
75 $minutes, $seconds, $sectors);
76 }
77
78 close($fh) or die("close(>cue): $!");
79
80 return $trackcount;
81 }
82
83 sub tags_file {
84 my $fn = shift;
85 my $trackcount = shift;
86 my $various = shift;
87 my $artist = shift;
88 my $album = shift;
89 my $release_dates = shift;
90 my $fh;
91 my $i;
92 my $track;
93 my $name;
94
95 open($fh, '>', $fn) or die("open('>$fn'): $!");
96 print($fh 'ARTIST=', (defined($artist) and $artist or ''), "\n");
97 print($fh 'ALBUM=', (defined($album) and $album or ''), "\n");
98
99 if (defined($release_dates) and %$release_dates) {
100 while (my ($country, $date) = each(%$release_dates)) {
101 print($fh "DATE[$country]=$date\n");
102 }
103 } else {
104 print($fh "DATE=\n");
105 }
106
107 for $i (1 .. $trackcount) {
108 $various and print($fh "ARTIST[$i]=\n");
109 if ($track = shift(@_)) {
110 $name = $track->get_name;
111 } else {
112 $name = '';
113 }
114 print($fh "TITLE[$i]=$name\n");
115 }
116
117 close($fh) or die("close(>$fn): $!");
118 }
119
120 sub tags {
121 my $device = shift;
122 my $trackcount = shift;
123 my $no_mb = shift;
124 my $mb;
125 my @results;
126 my $album;
127 my $i;
128 my $various;
129 my $seen_various;
130
131 tags_file('candidate-tags-0', $trackcount, 0);
132
133 defined($no_mb) and $no_mb and return;
134
135 if (defined($device)) {
136 $mb = new MusicBrainz::Client::Simple (device=>$device);
137 } else {
138 $mb = new MusicBrainz::Client::Simple;
139 }
140
141 @results = $mb->lookup_cd;
142 if (not $mb->success) {
143 die($mb->get_error);
144 }
145
146 for $album (@results) {
147 $i++;
148
149 if ($various = $album->has_various_artists) {
150 if (not $seen_various) {
151 $seen_various = 1;
152 tags_file('candidate-tags-0v', $trackcount, 1);
153 }
154 }
155
156 my %dates = $album->get_release_dates;
157 tags_file("candidate-tags-$i", $trackcount, $various,
158 $album->get_artist->get_name, $album->get_name,
159 \%dates, $album->get_tracks);
160 }
161 }
162
163 sub rip {
164 my $device = shift;
165 my $trackcount = shift;
166 my $single_file = shift;
167
168 $device ||= '/dev/cdrom';
169
170 exec('cdparanoia', '-d', $device,
171 (($single_file and ("1-$trackcount", 'wav')) or ('-B')));
172 # exec prints its own error message so just
173 die;
174 }
175
176 sub make_post_processor {
177 my $command = shift;
178
179 defined($command) or return;
180
181 sysopen(F, 'post-processor', O_CREAT | O_WRONLY, 0555)
182 or die("sysopen(post-processor, O_CREAT | O_WRONLY, 0555): $!");
183 print(F $command, ' "$@"', "\n");
184 close(F) or die("close(post-processor, O_CREAT | O_WRONLY, 0555): $!");
185 }
186
187 MAIN: {
188 my $no_mb;
189 my $post_processor;
190 my $single_file;
191 my $trackcount = 99;
192 my $help;
193 my $tempdir;
194
195 GetOptions(
196 'device|d=s' => \$CDDEV,
197 'no-musicbrainz|m' => \$no_mb,
198 'post-processor|p=s', \$post_processor,
199 'single-file|s' => \$single_file,
200 'tracks|t=i' => \$trackcount,
201 'help|h|?' => \$help,
202 ) or pod2usage();
203 $help and pod2usage(-exitstatus=>0, -verbose=>1);
204
205 # File::Temp::tempdir calls die on error.
206 $tempdir = File::Temp::tempdir('flac-archive.XXXXXXXXXX');
207 chdir($tempdir) or die("chdir($tempdir): $!");
208
209 make_post_processor($post_processor);
210 $trackcount = mkcue($CDDEV, $trackcount);
211 tags($CDDEV, $trackcount, $no_mb);
212 rip($CDDEV, $trackcount, $single_file);
213 }
214
215 \f
216 __END__
217
218 =head1 DESCRIPTION
219
220 B<fa-rip> creates a temporary directory for storage of its
221 intermediate files, uses MusicBrainz to create the "cue" file and
222 candidate tags files, and runs C<cdparanoia(1)> to rip the CD to the
223 "wav" file.
224
225 In order for this CD to be processed by B<fa-flacd>, you must create a
226 "tags" file. This is usually done by renaming one of the
227 candidate-tags files and deleting the others. Don't forget to fill in
228 the DATE tag in the selected candidate before renaming it. If
229 B<fa-rip> could not find any tag information from MusicBrainz, you'll
230 have to fill out the candidate-tags-0 template.
231
232 =head1 OPTIONS
233
234 =over 4
235
236 =item B<-d> [B<--device>] I<device>
237
238 Use I<device> as the CD-ROM device, instead of the default
239 "/dev/cdrom" or the environment variable CDDEV.
240
241 =item B<-p> [B<--post-processor>] I<post-processor>
242
243 Create a "post-processor" file in the temporary directory containing
244 the line 'I<post-processor> "$@"'. See B<fa-flacd>'s man page for
245 information about this hook.
246
247 =item B<-t> [B<--tracks>] I<track-count>
248
249 Archive only the first I<track-count> tracks. This is handy for
250 ignoring data tracks.
251
252 =back
253
254 =head1 ENVIRONMENT
255
256 =over 4
257
258 =item CDDEV
259
260 B<fa-rip> uses this to rip audio and save the cuesheet for a CD.
261 MusicBrainz::Client can usually figure this out automatically.
262
263 =back
264
265 =head1 AUTHORS
266
267 Written by Eric Gillespie <epg@pretzelnet.org>.
268
269 flac-archive is free software; you may redistribute it and/or modify
270 it under the same terms as Perl itself.
271
272 =cut
273
274 # Local variables:
275 # cperl-indent-level: 4
276 # perl-indent-level: 4
277 # indent-tabs-mode: nil
278 # End:
279
280 # vi: set tabstop=4 expandtab: