source: flacrip/trunk/cd2flac @ 44

Last change on this file since 44 was 42, checked in by peter, 10 years ago

issue #5: if the --properties flag is given to cd2flac, it will attempt to
automatically fetch the MusicBrainz metadata for the ripped CD and write it to
a properties file with the same name as the FLAC file, but with the
".properties" extension

  • Property svn:executable set to *
File size: 1.7 KB
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use FindBin qw{$RealBin};
5use lib "$RealBin/lib";
6
7use Ripper;
8
9use File::Spec::Functions qw{catfile splitpath};
10use File::Path qw{mkpath};
11use Getopt::Long qw{:config no_ignore_case no_auto_abbrev};
12use Cwd;
13use Audio::FLAC::Header;
14use MusicBrainz;
15
16GetOptions(
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
25die "Usage: $0 -o <output.flac> [-D <device>]\n" unless $OUTPUT_NAME;
26my (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;
31mkpath($out_dir) unless -e $out_dir;
32my $archive_flac = catfile($out_dir, $out_file);
33
34# check for file exist; default to not overwrite
35die "$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
41my $tags = $BARCODE ? { BARCODE => $BARCODE } : {};
42my $ripper = Ripper->new({ device => $CD_DEVICE });
43my $flac_disc = $ripper->rip_to_flac($archive_flac, $tags);
44
45if ($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
59print "Rip saved as $archive_flac\n";
60system 'eject', $CD_DEVICE;
Note: See TracBrowser for help on using the repository browser.