]> diplodocus.org Git - flac-archive/blob - fa-rip
Looks like MusicBrainz::Client exposed the TOC info i needed all
[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 $fh;
90 my $i;
91 my $track;
92 my $name;
93
94 open($fh, '>', $fn) or die("open('>$fn'): $!");
95 print($fh 'ARTIST=', (defined($artist) and $artist or ''), "\n");
96 print($fh 'ALBUM=', (defined($album) and $album or ''), "\n");
97 # MusicBrainz doesn't have dates yet; these are usually wrong anyway.
98 print($fh "DATE=\n");
99
100 for $i (1 .. $trackcount) {
101 $various and print($fh "ARTIST[$i]=\n");
102 if ($track = shift(@_)) {
103 $name = $track->get_name;
104 } else {
105 $name = '';
106 }
107 print($fh "TITLE[$i]=$name\n");
108 }
109
110 close($fh) or die("close(>$fn): $!");
111 }
112
113 sub tags {
114 my $device = shift;
115 my $trackcount = shift;
116 my $mb;
117 my @results;
118 my $album;
119 my $i;
120 my $various;
121 my $seen_various;
122
123 if (defined($device)) {
124 $mb = new MusicBrainz::Client::Simple (device=>$device);
125 } else {
126 $mb = new MusicBrainz::Client::Simple;
127 }
128
129 @results = $mb->lookup_cd;
130 if (not $mb->success) {
131 die($mb->get_error);
132 }
133
134 tags_file('candidate-tags-0', $trackcount, 0);
135
136 for $album (@results) {
137 $i++;
138
139 if ($various = $album->has_various_artists) {
140 if (not $seen_various) {
141 $seen_various = 1;
142 tags_file('candidate-tags-0v', $trackcount, 1);
143 }
144 }
145
146 tags_file("candidate-tags-$i", $trackcount, $various,
147 $album->get_artist->get_name, $album->get_name,
148 $album->get_tracks);
149 }
150 }
151
152 sub rip {
153 my $device = shift;
154 my $trackcount = shift;
155
156 $device ||= '/dev/cdrom';
157
158 exec('cdparanoia', '-d', $device, "1-$trackcount", 'wav');
159 # exec prints its own error message so just
160 die;
161 }
162
163 sub make_post_processor {
164 my $command = shift;
165
166 defined($command) or return;
167
168 sysopen(F, 'post-processor', O_CREAT | O_WRONLY, 0555)
169 or die("sysopen(post-processor, O_CREAT | O_WRONLY, 0555): $!");
170 print(F $command, ' "$@"', "\n");
171 close(F) or die("close(post-processor, O_CREAT | O_WRONLY, 0555): $!");
172 }
173
174 MAIN: {
175 my $post_processor;
176 my $trackcount = 99;
177 my $help;
178 my $tempdir;
179
180 GetOptions(
181 'device|d=s' => \$CDDEV,
182 'post-processor|p=s', \$post_processor,
183 'tracks|t=i' => \$trackcount,
184 'help|h|?' => \$help,
185 ) or pod2usage();
186 $help and pod2usage(-exitstatus=>0, -verbose=>1);
187
188 # File::Temp::tempdir calls die on error.
189 $tempdir = File::Temp::tempdir('flac-archive.XXXXXXXXXX');
190 chdir($tempdir) or die("chdir($tempdir): $!");
191
192 make_post_processor($post_processor);
193 $trackcount = mkcue($CDDEV, $trackcount);
194 tags($CDDEV, $trackcount);
195 rip($CDDEV, $trackcount);
196 }
197
198 \f
199 __END__
200
201 =head1 DESCRIPTION
202
203 B<fa-rip> creates a temporary directory for storage of its
204 intermediate files, uses MusicBrainz to create the "cue" file and
205 candidate tags files, and runs C<cdparanoia(1)> to rip the CD to the
206 "wav" file.
207
208 In order for this CD to be processed by B<fa-flacd>, you must create a
209 "tags" file. This is usually done by renaming one of the
210 candidate-tags files and deleting the others. Don't forget to fill in
211 the DATE tag in the selected candidate before renaming it. If
212 B<fa-rip> could not find any tag information from MusicBrainz, you'll
213 have to fill out the candidate-tags-0 template.
214
215 =head1 OPTIONS
216
217 =over 4
218
219 =item B<-d> [B<--device>] I<device>
220
221 Use I<device> as the CD-ROM device, instead of the default
222 "/dev/cdrom" or the environment variable CDDEV.
223
224 =item B<-p> [B<--post-processor>] I<post-processor>
225
226 Create a "post-processor" file in the temporary directory containing
227 the line 'I<post-processor> "$@"'. See B<fa-flacd>'s man page for
228 information about this hook.
229
230 =item B<-t> [B<--tracks>] I<track-count>
231
232 Archive only the first I<track-count> tracks. This is handy for
233 ignoring data tracks.
234
235 =back
236
237 =head1 ENVIRONMENT
238
239 =over 4
240
241 =item CDDEV
242
243 B<fa-rip> uses this to rip audio and save the cuesheet for a CD. It
244 makes some effort to check some common device names for FreeBSD,
245 Linux, and NetBSD by default.
246
247 =back
248
249 =head1 AUTHORS
250
251 Written by Eric Gillespie <epg@pretzelnet.org>.
252
253 flac-archive is free software; you may redistribute it and/or modify
254 it under the same terms as Perl itself.
255
256 =cut
257
258 # Local variables:
259 # cperl-indent-level: 4
260 # perl-indent-level: 4
261 # indent-tabs-mode: nil
262 # End:
263
264 # vi: set tabstop=4 expandtab: