]> diplodocus.org Git - flac-archive/blob - fa-rip
(get_tags): http://us.xiph.org/ogg/vorbis/doc/v-comment.html says the
[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 POSIX ':sys_wait_h';
27 use Pod::Usage;
28
29 use MusicBrainz::Client::Simple;
30
31 sub run_or_die {
32 my $command = shift;
33 my $status;
34
35 $status = system($command);
36
37 if (WIFEXITED($status)) {
38 if (($status = WEXITSTATUS($status)) != 0) {
39 die("$command exited with status $status");
40 }
41 } elsif (WIFSIGNALED($status)) {
42 die("$command killed with signal ", WTERMSIG($status));
43 } elsif (WIFSTOPPED($status)) {
44 die("$command stopped with signal ", WSTOPSIG($status));
45 } else {
46 die("Major horkage on system($command): \$? = $? \$! = $!");
47 }
48 }
49
50 sub mkcue {
51 my $device = shift;
52 my $trackcount = shift;
53 my @command;
54
55 push(@command, 'mkcue');
56
57 if (defined($trackcount)) {
58 push(@command, "-t $trackcount");
59 }
60
61 if (defined($device)) {
62 push(@command, $device);
63 }
64
65 push(@command, '> cue');
66 run_or_die(join(' ', @command));
67
68 if (not defined($trackcount)) {
69 open(F, 'cue') or die("open(cue): $!");
70 $trackcount = grep(/TRACK.*AUDIO/, <F>);
71 close(F);
72 }
73
74 return $trackcount;
75 }
76
77 sub tags_file {
78 my $fn = shift;
79 my $trackcount = shift;
80 my $various = shift;
81 my $artist = shift;
82 my $album = shift;
83 my $fh;
84 my $i;
85 my $track;
86 my $name;
87
88 open($fh, '>', $fn) or die("open('>$fn'): $!");
89 print($fh 'ARTIST=', (defined($artist) and $artist or ''), "\n");
90 print($fh 'ALBUM=', (defined($album) and $album or ''), "\n");
91 # MusicBrainz doesn't have dates yet; these are usually wrong anyway.
92 print($fh "DATE=\n");
93
94 for $i (1 .. $trackcount) {
95 $various and print($fh "ARTIST[$i]=\n");
96 if ($track = shift(@_)) {
97 $name = $track->get_name;
98 } else {
99 $name = '';
100 }
101 print($fh "TITLE[$i]=$name\n");
102 }
103
104 close($fh) or die("close(>$fn): $!");
105 }
106
107 sub tags {
108 my $device = shift;
109 my $trackcount = shift;
110 my $mb;
111 my @results;
112 my $album;
113 my $i;
114 my $various;
115 my $seen_various;
116
117 if (defined($device)) {
118 $mb = new MusicBrainz::Client::Simple (device=>$device);
119 } else {
120 $mb = new MusicBrainz::Client::Simple;
121 }
122
123 @results = $mb->lookup_cd;
124 if (not $mb->success) {
125 die($mb->get_error);
126 }
127
128 tags_file('candidate-tags-0', $trackcount, 0);
129
130 for $album (@results) {
131 $i++;
132
133 if ($various = $album->has_various_artists) {
134 if (not $seen_various) {
135 $seen_various = 1;
136 tags_file('candidate-tags-0v', $trackcount, 1);
137 }
138 }
139
140 tags_file("candidate-tags-$i", $trackcount, $various,
141 $album->get_artist->get_name, $album->get_name,
142 $album->get_tracks);
143 }
144 }
145
146 sub rip {
147 my $device = shift;
148 my $trackcount = shift;
149
150 $device ||= '/dev/cdrom';
151
152 exec('cdparanoia', '-d', $device, "1-$trackcount", 'wav');
153 # exec prints its own error message so just
154 die;
155 }
156
157 sub make_post_processor {
158 my $command = shift;
159
160 defined($command) or return;
161
162 sysopen(F, 'post-processor', O_CREAT | O_WRONLY, 0555)
163 or die("sysopen(post-processor, O_CREAT | O_WRONLY, 0555): $!");
164 print(F $command, ' "$@"', "\n");
165 close(F) or die("close(post-processor, O_CREAT | O_WRONLY, 0555): $!");
166 }
167
168 MAIN: {
169 my $post_processor;
170 my $trackcount;
171 my $help;
172 my $tempdir;
173
174 GetOptions(
175 'device|d=s' => \$CDDEV,
176 'post-processor|p=s', \$post_processor,
177 'tracks|t=i' => \$trackcount,
178 'help|h|?' => \$help,
179 ) or pod2usage();
180 $help and pod2usage(-exitstatus=>0, -verbose=>1);
181
182 # File::Temp::tempdir calls die on error.
183 $tempdir = File::Temp::tempdir('flac-archive.XXXXXXXXXX');
184 chdir($tempdir) or die("chdir($tempdir): $!");
185
186 make_post_processor($post_processor);
187 $trackcount = mkcue($CDDEV, $trackcount);
188 tags($CDDEV, $trackcount);
189 rip($CDDEV, $trackcount);
190 }
191
192 \f
193 __END__
194
195 =head1 DESCRIPTION
196
197 B<fa-rip> creates a temporary directory for storage of its
198 intermediate files, runs C<mkcue(1)> to create the "cue" file, uses
199 MusicBrainz to generate candidate tags files, and runs
200 C<cdparanoia(1)> to rip the CD to the "wav" file.
201
202 In order for this CD to be processed by B<fa-flacd>, you must create a
203 "tags" file. This is usually done by renaming one of the
204 candidate-tags files and deleting the others. Don't forget to fill in
205 the DATE tag in the selected candidate before renaming it. If
206 B<fa-rip> could not find any tag information from MusicBrainz, you'll
207 have to fill out the candidate-tags-0 template.
208
209 =head1 OPTIONS
210
211 =over 4
212
213 =item B<-d> [B<--device>] I<device>
214
215 Use I<device> as the CD-ROM device, instead of the default
216 "/dev/cdrom" or the environment variable CDDEV.
217
218 =item B<-p> [B<--post-processor>] I<post-processor>
219
220 Create a "post-processor" file in the temporary directory containing
221 the line 'I<post-processor> "$@"'. See B<fa-flacd>'s man page for
222 information about this hook.
223
224 =item B<-t> [B<--tracks>] I<track-count>
225
226 Archive only the first I<track-count> tracks. This is handy for
227 ignoring data tracks.
228
229 =back
230
231 =head1 ENVIRONMENT
232
233 =over 4
234
235 =item CDDEV
236
237 B<fa-rip> uses this to rip audio and save the cuesheet for a CD. It
238 makes some effort to check some common device names for FreeBSD,
239 Linux, and NetBSD by default.
240
241 =back
242
243 =head1 AUTHORS
244
245 Written by Eric Gillespie <epg@pretzelnet.org>.
246
247 flac-archive is free software; you may redistribute it and/or modify
248 it under the same terms as Perl itself.
249
250 =cut
251
252 # Local variables:
253 # cperl-indent-level: 4
254 # perl-indent-level: 4
255 # indent-tabs-mode: nil
256 # End:
257
258 # vi: set tabstop=4 expandtab: