- Timestamp:
- 10/08/14 21:24:21 (10 years ago)
- Location:
- trunk
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/cd2flac
r22 r23 5 5 use lib "$RealBin/lib"; 6 6 7 use Tracks;7 use Ripper; 8 8 9 use File::Temp qw{tempdir};10 9 use File::Spec::Functions qw{catfile splitpath}; 11 use File::Copy;12 10 use File::Path qw{mkpath}; 13 11 use Getopt::Long qw{:config no_ignore_case no_auto_abbrev}; … … 20 18 ); 21 19 20 # output file 22 21 die "Usage: $0 -o <output.flac> [-D <device>]\n" unless $OUTPUT_NAME; 23 24 # output file25 22 my (undef, $out_dir, $out_file) = splitpath($OUTPUT_NAME); 26 23 # automatically add ".flac" … … 37 34 # get the CD info 38 35 $CD_DEVICE ||= '/dev/cdrom'; 39 my $tracks = Tracks->new;40 $tracks->read_disc($CD_DEVICE);41 36 42 die "No tracks found; is there a CD in the drive?\n" unless @{ $tracks->get_tracks }; 37 my $ripper = Ripper->new({ device => $CD_DEVICE }); 38 $ripper->rip_to_flac($archive_flac); 43 39 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 # rip51 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 + cuesheet56 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 metadata63 my $discid = $tracks->get_mbz_discid;64 65 # copy to permanent location66 copy($flac_file, $archive_flac);67 system 'metaflac', '--set-tag', "MUSICBRAINZ_DISCID=$discid", $archive_flac;68 40 print "Rip saved as $archive_flac\n"; 69 41 system 'eject', $CD_DEVICE; -
trunk/lib/Ripper.pm
r22 r23 1 #!/usr/bin/perl -w 2 use strict; 1 package Ripper; 3 2 4 use FindBin qw{$RealBin}; 5 use lib "$RealBin/lib"; 3 use Moose; 6 4 7 5 use Tracks; 8 6 9 7 use File::Temp qw{tempdir}; 10 use File::Spec::Functions qw{catfile splitpath};8 use File::Spec::Functions qw{catfile}; 11 9 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 10 16 GetOptions( 17 'device|D=s' => \my $CD_DEVICE, 18 'output|o=s' => \my $OUTPUT_NAME,19 'force|f' => \my $FORCE,11 has device => ( is => 'rw' ); 12 has tracks => ( 13 is => 'rw', 14 handles => [qw{read_disc get_tracks get_cuesheet}], 20 15 ); 21 16 22 die "Usage: $0 -o <output.flac> [-D <device>]\n" unless $OUTPUT_NAME; 17 sub rip_to_flac { 18 my ($self, $archive_flac) = @_; 23 19 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); 20 $self->tracks(Tracks->new); 21 $self->read_disc($self->device); 32 22 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; 23 die "No tracks found; is there a CD in the drive?\n" unless @{ $self->get_tracks }; 36 24 37 # get the CD info 38 $CD_DEVICE ||= '/dev/cdrom'; 39 my $tracks = Tracks->new; 40 $tracks->read_disc($CD_DEVICE); 25 my $tempdir = tempdir(CLEANUP => 1); 41 26 42 die "No tracks found; is there a CD in the drive?\n" unless @{ $tracks->get_tracks }; 27 my $wav_file = catfile($tempdir, 'cdda.wav'); 28 my $flac_file = catfile($tempdir, 'cdda.flac'); 29 my $cue_file = catfile($tempdir, 'cdda.cue'); 43 30 44 my $tempdir = tempdir(CLEANUP => 1); 31 # rip 32 my $span = $self->tracks->get_cdparanoia_span; 33 system 'cdparanoia', '-d', $self->device, $span, $wav_file; 34 die "\nRipping canceled\n" if ($? & 127); 45 35 46 my $wav_file = catfile($tempdir, 'cdda.wav'); 47 my $flac_file = catfile($tempdir, 'cdda.flac'); 48 my $cue_file = catfile($tempdir, 'cdda.cue'); 36 # encode + cuesheet 37 open my $CUE, "> $cue_file"; 38 print $CUE $self->get_cuesheet; 39 close $CUE; 40 system 'flac', '-o', $flac_file, '--cuesheet', $cue_file, $wav_file; 41 die "\nFLAC encoding canceled\n" if ($? & 127); 49 42 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); 43 # MusicBrainz discid metadata 44 my $discid = $self->tracks->get_mbz_discid; 54 45 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); 46 # copy to permanent location 47 copy($flac_file, $archive_flac); 48 system 'metaflac', '--set-tag', "MUSICBRAINZ_DISCID=$discid", $archive_flac; 49 } 61 50 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; 51 # module return 52 1;
Note: See TracChangeset
for help on using the changeset viewer.