8 B<fa-rip> - rip a CD for B<fa-flacd>
12 B<fa-rip> [B<-d> I<device>] [B<-p> I<post-processor> [B<-t> I<track-count>]
23 use Fcntl
qw(O_CREAT O_WRONLY);
25 use Getopt
::Long
qw(:config gnu_getopt no_ignore_case);
26 use List
::Util
qw(min);
27 use POSIX
':sys_wait_h';
30 use MusicBrainz
::Client
;
31 use MusicBrainz
::Queries
qw(
35 MBE_TOCGetTrackSectorOffset
37 use MusicBrainz
::Client
::Simple
;
41 my $trackcount = shift;
43 my $mb = MusicBrainz
::Client-
>new;
44 defined($device) and $mb->set_device($device);
46 $mb->query(MBQ_GetCDTOC
) or die($mb->get_query_error);
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");
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);
59 for my $track ($first .. $trackcount) {
60 my $off = $mb->get_result_data1(MBE_TOCGetTrackSectorOffset
, $track+1);
63 my ($minutes,$seconds)=(0,0);
64 my $sectors = $off % 75;
68 $minutes = $seconds / 60;
69 $seconds = $seconds % 60;
73 printf($fh " TRACK %02d AUDIO\n", $track);
74 printf($fh " INDEX 01 %02d:%02d:%02d\n",
75 $minutes, $seconds, $sectors);
78 close($fh) or die("close(>cue): $!");
85 my $trackcount = shift;
89 my $release_dates = shift;
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");
99 if (defined($release_dates) and %$release_dates) {
100 while (my ($country, $date) = each(%$release_dates)) {
101 print($fh "DATE[$country]=$date\n");
104 print($fh "DATE=\n");
107 for $i (1 .. $trackcount) {
108 $various and print($fh "ARTIST[$i]=\n");
109 if ($track = shift(@_)) {
110 $name = $track->get_name;
114 print($fh "TITLE[$i]=$name\n");
117 close($fh) or die("close(>$fn): $!");
122 my $trackcount = shift;
131 tags_file
('candidate-tags-0', $trackcount, 0);
133 defined($no_mb) and $no_mb and return;
135 if (defined($device)) {
136 $mb = new MusicBrainz
::Client
::Simple
(device
=>$device);
138 $mb = new MusicBrainz
::Client
::Simple
;
141 @results = $mb->lookup_cd;
142 if (not $mb->success) {
146 for $album (@results) {
149 if ($various = $album->has_various_artists) {
150 if (not $seen_various) {
152 tags_file
('candidate-tags-0v', $trackcount, 1);
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);
165 my $trackcount = shift;
166 my $single_file = shift;
169 $device ||= '/dev/cdrom';
171 @output = ("1-$trackcount", 'wav');
176 exec('cdparanoia', '-d', $device, @output);
180 sub make_post_processor
{
183 defined($command) or return;
185 sysopen(F
, 'post-processor', O_CREAT
| O_WRONLY
, 0555)
186 or die("sysopen(post-processor, O_CREAT | O_WRONLY, 0555): $!");
187 print(F
$command, ' "$@"', "\n");
188 close(F
) or die("close(post-processor, O_CREAT | O_WRONLY, 0555): $!");
200 'device|d=s' => \
$CDDEV,
201 'no-musicbrainz|m' => \
$no_mb,
202 'post-processor|p=s', \
$post_processor,
203 'single-file|s' => \
$single_file,
204 'tracks|t=i' => \
$trackcount,
205 'help|h|?' => \
$help,
207 $help and pod2usage
(-exitstatus
=>0, -verbose
=>1);
209 # File::Temp::tempdir calls die on error.
210 $tempdir = File
::Temp
::tempdir
('flac-archive.XXXXXXXXXX');
211 chdir($tempdir) or die("chdir($tempdir): $!");
213 make_post_processor
($post_processor);
214 $trackcount = mkcue
($CDDEV, $trackcount);
215 tags
($CDDEV, $trackcount, $no_mb);
216 rip
($CDDEV, $trackcount, $single_file);
224 B<fa-rip> creates a temporary directory for storage of its
225 intermediate files, uses MusicBrainz to create the "cue" file and
226 candidate tags files, and runs C<cdparanoia(1)> to rip the CD to the
229 In order for this CD to be processed by B<fa-flacd>, you must create a
230 "tags" file. This is usually done by renaming one of the
231 candidate-tags files and deleting the others. Don't forget to fill in
232 the DATE tag in the selected candidate before renaming it. If
233 B<fa-rip> could not find any tag information from MusicBrainz, you'll
234 have to fill out the candidate-tags-0 template.
240 =item B<-d> [B<--device>] I<device>
242 Use I<device> as the CD-ROM device, instead of the default
243 "/dev/cdrom" or the environment variable CDDEV.
245 =item B<-p> [B<--post-processor>] I<post-processor>
247 Create a "post-processor" file in the temporary directory containing
248 the line 'I<post-processor> "$@"'. See B<fa-flacd>'s man page for
249 information about this hook.
251 =item B<-t> [B<--tracks>] I<track-count>
253 Archive only the first I<track-count> tracks. This is handy for
254 ignoring data tracks.
264 B<fa-rip> uses this to rip audio and save the cuesheet for a CD.
265 MusicBrainz::Client can usually figure this out automatically.
271 Written by Eric Gillespie <epg@pretzelnet.org>.
273 flac-archive is free software; you may redistribute it and/or modify
274 it under the same terms as Perl itself.
279 # cperl-indent-level: 4
280 # perl-indent-level: 4
281 # indent-tabs-mode: nil
284 # vi: set tabstop=4 expandtab: