source: flacrip/trunk/cd2flac @ 47

Last change on this file since 47 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
RevLine 
[1]1#!/usr/bin/perl -w
2use strict;
3
[22]4use FindBin qw{$RealBin};
5use lib "$RealBin/lib";
[1]6
[23]7use Ripper;
[1]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;
[38]13use Audio::FLAC::Header;
[42]14use MusicBrainz;
[1]15
16GetOptions(
[38]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,
[42]21    'properties'  => \my $PROPERTIES,
[1]22);
23
24# output file
[23]25die "Usage: $0 -o <output.flac> [-D <device>]\n" unless $OUTPUT_NAME;
[1]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
[41]41my $tags = $BARCODE ? { BARCODE => $BARCODE } : {};
[23]42my $ripper = Ripper->new({ device => $CD_DEVICE });
[42]43my $flac_disc = $ripper->rip_to_flac($archive_flac, $tags);
[1]44
[42]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
[1]59print "Rip saved as $archive_flac\n";
60system 'eject', $CD_DEVICE;
Note: See TracBrowser for help on using the repository browser.