| 1 | #!/usr/bin/perl -w |
|---|
| 2 | use strict; |
|---|
| 3 | |
|---|
| 4 | use FindBin qw{$RealBin}; |
|---|
| 5 | use lib "$RealBin/lib"; |
|---|
| 6 | |
|---|
| 7 | use Tracks; |
|---|
| 8 | |
|---|
| 9 | use File::Temp qw{tempdir}; |
|---|
| 10 | use File::Spec::Functions qw{catfile splitpath}; |
|---|
| 11 | use File::Copy; |
|---|
| 12 | use File::Path qw{mkpath}; |
|---|
| 13 | use Getopt::Long qw{:config no_ignore_case no_auto_abbrev}; |
|---|
| 14 | use Cwd; |
|---|
| 15 | |
|---|
| 16 | GetOptions( |
|---|
| 17 | 'device|D=s' => \my $CD_DEVICE, |
|---|
| 18 | 'output|o=s' => \my $OUTPUT_NAME, |
|---|
| 19 | 'force|f' => \my $FORCE, |
|---|
| 20 | ); |
|---|
| 21 | |
|---|
| 22 | die "Usage: $0 -o <output.flac> [-D <device>]\n" unless $OUTPUT_NAME; |
|---|
| 23 | |
|---|
| 24 | # output file |
|---|
| 25 | my (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; |
|---|
| 30 | mkpath($out_dir) unless -e $out_dir; |
|---|
| 31 | my $archive_flac = catfile($out_dir, $out_file); |
|---|
| 32 | |
|---|
| 33 | # check for file exist; default to not overwrite |
|---|
| 34 | die "$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'; |
|---|
| 39 | my $tracks = Tracks->new; |
|---|
| 40 | $tracks->read_disc($CD_DEVICE); |
|---|
| 41 | |
|---|
| 42 | die "No tracks found; is there a CD in the drive?\n" unless @{ $tracks->get_tracks }; |
|---|
| 43 | |
|---|
| 44 | my $tempdir = tempdir(CLEANUP => 1); |
|---|
| 45 | |
|---|
| 46 | my $wav_file = catfile($tempdir, 'cdda.wav'); |
|---|
| 47 | my $flac_file = catfile($tempdir, 'cdda.flac'); |
|---|
| 48 | my $cue_file = catfile($tempdir, 'cdda.cue'); |
|---|
| 49 | |
|---|
| 50 | # rip |
|---|
| 51 | my $span = $tracks->get_cdparanoia_span; |
|---|
| 52 | system 'cdparanoia', '-d', $CD_DEVICE, $span, $wav_file; |
|---|
| 53 | die "\nRipping canceled\n" if ($? & 127); |
|---|
| 54 | |
|---|
| 55 | # encode + cuesheet |
|---|
| 56 | open my $CUE, "> $cue_file"; |
|---|
| 57 | print $CUE $tracks->get_cuesheet; |
|---|
| 58 | close $CUE; |
|---|
| 59 | system 'flac', '-o', $flac_file, '--cuesheet', $cue_file, $wav_file; |
|---|
| 60 | die "\nFLAC encoding canceled\n" if ($? & 127); |
|---|
| 61 | |
|---|
| 62 | # MusicBrainz discid metadata |
|---|
| 63 | my $discid = $tracks->get_mbz_discid; |
|---|
| 64 | |
|---|
| 65 | # copy to permanent location |
|---|
| 66 | copy($flac_file, $archive_flac); |
|---|
| 67 | system 'metaflac', '--set-tag', "MUSICBRAINZ_DISCID=$discid", $archive_flac; |
|---|
| 68 | print "Rip saved as $archive_flac\n"; |
|---|
| 69 | system 'eject', $CD_DEVICE; |
|---|