1 | #!/usr/bin/perl -w |
---|
2 | use strict; |
---|
3 | |
---|
4 | use FindBin qw{$RealBin}; |
---|
5 | use lib "$RealBin/lib"; |
---|
6 | |
---|
7 | use Ripper; |
---|
8 | |
---|
9 | use File::Spec::Functions qw{catfile splitpath}; |
---|
10 | use File::Path qw{mkpath}; |
---|
11 | use Getopt::Long qw{:config no_ignore_case no_auto_abbrev}; |
---|
12 | use Cwd; |
---|
13 | use Audio::FLAC::Header; |
---|
14 | use MusicBrainz; |
---|
15 | |
---|
16 | GetOptions( |
---|
17 | 'device|D=s' => \my $CD_DEVICE, |
---|
18 | 'output|o=s' => \my $OUTPUT_NAME, |
---|
19 | 'force|f' => \my $FORCE, |
---|
20 | 'barcode|b=s' => \my $BARCODE, |
---|
21 | 'properties' => \my $PROPERTIES, |
---|
22 | ); |
---|
23 | |
---|
24 | # output file |
---|
25 | die "Usage: $0 -o <output.flac> [-D <device>]\n" unless $OUTPUT_NAME; |
---|
26 | my (undef, $out_dir, $out_file) = splitpath($OUTPUT_NAME); |
---|
27 | # automatically add ".flac" |
---|
28 | $out_file .= '.flac' unless $out_file =~ /\.flac$/; |
---|
29 | # default to current directory |
---|
30 | $out_dir ||= getcwd; |
---|
31 | mkpath($out_dir) unless -e $out_dir; |
---|
32 | my $archive_flac = catfile($out_dir, $out_file); |
---|
33 | |
---|
34 | # check for file exist; default to not overwrite |
---|
35 | die "$archive_flac exists\nwill not overwrite (use --force to override this)\n" |
---|
36 | if -e $archive_flac && !$FORCE; |
---|
37 | |
---|
38 | # get the CD info |
---|
39 | $CD_DEVICE ||= '/dev/cdrom'; |
---|
40 | |
---|
41 | my $tags = $BARCODE ? { BARCODE => $BARCODE } : {}; |
---|
42 | my $ripper = Ripper->new({ device => $CD_DEVICE }); |
---|
43 | my $flac_disc = $ripper->rip_to_flac($archive_flac, $tags); |
---|
44 | |
---|
45 | if ($PROPERTIES) { |
---|
46 | my $info = get_musicbrainz_info({ |
---|
47 | discid => $flac_disc->discid, |
---|
48 | barcode => $flac_disc->barcode, |
---|
49 | }); |
---|
50 | if ($info) { |
---|
51 | (my $properties_file = $archive_flac) =~ s/\.flac$/.properties/; |
---|
52 | open my $fh, '>', $properties_file or die "Cannot write $properties_file\n"; |
---|
53 | print $fh "$_=$$info{$_}\n" foreach sort keys %{ $info }; |
---|
54 | close $fh; |
---|
55 | print "Properties written as $properties_file\n"; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | print "Rip saved as $archive_flac\n"; |
---|
60 | system 'eject', $CD_DEVICE; |
---|