source: flacrip/trunk/cd2flac @ 22

Last change on this file since 22 was 22, checked in by peter, 10 years ago
  • split the Tracks module out into its own *.pm file
  • cd2flac dies unless there is an output file argument given
  • Property svn:executable set to *
File size: 1.9 KB
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use FindBin qw{$RealBin};
5use lib "$RealBin/lib";
6
7use Tracks;
8
9use File::Temp qw{tempdir};
10use File::Spec::Functions qw{catfile splitpath};
11use File::Copy;
12use File::Path qw{mkpath};
13use Getopt::Long qw{:config no_ignore_case no_auto_abbrev};
14use Cwd;
15
16GetOptions(
17    'device|D=s' => \my $CD_DEVICE,
18    'output|o=s' => \my $OUTPUT_NAME,
19    'force|f'    => \my $FORCE,
20);
21
22die "Usage: $0 -o <output.flac> [-D <device>]\n" unless $OUTPUT_NAME;
23
24# output file
25my (undef, $out_dir, $out_file) = splitpath($OUTPUT_NAME);
26# automatically add ".flac"
27$out_file .= '.flac' unless $out_file =~ /\.flac$/;
28# default to current directory
29$out_dir ||= getcwd;
30mkpath($out_dir) unless -e $out_dir;
31my $archive_flac = catfile($out_dir, $out_file);
32
33# check for file exist; default to not overwrite
34die "$archive_flac exists\nwill not overwrite (use --force to override this)\n" 
35    if -e $archive_flac && !$FORCE;
36
37# get the CD info
38$CD_DEVICE ||= '/dev/cdrom';
39my $tracks = Tracks->new;
40$tracks->read_disc($CD_DEVICE);
41
42die "No tracks found; is there a CD in the drive?\n" unless @{ $tracks->get_tracks };
43
44my $tempdir = tempdir(CLEANUP => 1);
45
46my $wav_file  = catfile($tempdir, 'cdda.wav');
47my $flac_file = catfile($tempdir, 'cdda.flac');
48my $cue_file  = catfile($tempdir, 'cdda.cue');
49
50# rip
51my $span = $tracks->get_cdparanoia_span;
52system 'cdparanoia', '-d', $CD_DEVICE, $span, $wav_file;
53die "\nRipping canceled\n" if ($? & 127);
54
55# encode + cuesheet
56open my $CUE, "> $cue_file";
57print $CUE $tracks->get_cuesheet;
58close $CUE;
59system 'flac', '-o', $flac_file, '--cuesheet', $cue_file, $wav_file;
60die "\nFLAC encoding canceled\n" if ($? & 127);
61
62# MusicBrainz discid metadata
63my $discid = $tracks->get_mbz_discid;
64
65# copy to permanent location
66copy($flac_file, $archive_flac);
67system 'metaflac', '--set-tag', "MUSICBRAINZ_DISCID=$discid", $archive_flac;
68print "Rip saved as $archive_flac\n";
69system 'eject', $CD_DEVICE;
Note: See TracBrowser for help on using the repository browser.