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