Changeset 23 in flacrip


Ignore:
Timestamp:
10/08/14 21:24:21 (10 years ago)
Author:
peter
Message:

moved the code to do the ripping and converting to FLAC from cd2flac into a separate Ripper module

Location:
trunk
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/cd2flac

    r22 r23  
    55use lib "$RealBin/lib"; 
    66 
    7 use Tracks; 
     7use Ripper; 
    88 
    9 use File::Temp qw{tempdir}; 
    109use File::Spec::Functions qw{catfile splitpath}; 
    11 use File::Copy; 
    1210use File::Path qw{mkpath}; 
    1311use Getopt::Long qw{:config no_ignore_case no_auto_abbrev}; 
     
    2018); 
    2119 
     20# output file 
    2221die "Usage: $0 -o <output.flac> [-D <device>]\n" unless $OUTPUT_NAME; 
    23  
    24 # output file 
    2522my (undef, $out_dir, $out_file) = splitpath($OUTPUT_NAME); 
    2623# automatically add ".flac" 
     
    3734# get the CD info 
    3835$CD_DEVICE ||= '/dev/cdrom'; 
    39 my $tracks = Tracks->new; 
    40 $tracks->read_disc($CD_DEVICE); 
    4136 
    42 die "No tracks found; is there a CD in the drive?\n" unless @{ $tracks->get_tracks }; 
     37my $ripper = Ripper->new({ device => $CD_DEVICE }); 
     38$ripper->rip_to_flac($archive_flac); 
    4339 
    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; 
    6840print "Rip saved as $archive_flac\n"; 
    6941system 'eject', $CD_DEVICE; 
  • trunk/lib/Ripper.pm

    r22 r23  
    1 #!/usr/bin/perl -w 
    2 use strict; 
     1package Ripper; 
    32 
    4 use FindBin qw{$RealBin}; 
    5 use lib "$RealBin/lib"; 
     3use Moose; 
    64 
    75use Tracks; 
    86 
    97use File::Temp qw{tempdir}; 
    10 use File::Spec::Functions qw{catfile splitpath}; 
     8use File::Spec::Functions qw{catfile}; 
    119use File::Copy; 
    12 use File::Path qw{mkpath}; 
    13 use Getopt::Long qw{:config no_ignore_case no_auto_abbrev}; 
    14 use Cwd; 
    1510 
    16 GetOptions( 
    17     'device|D=s' => \my $CD_DEVICE, 
    18     'output|o=s' => \my $OUTPUT_NAME, 
    19     'force|f'    => \my $FORCE, 
     11has device => ( is => 'rw' ); 
     12has tracks => (  
     13    is => 'rw', 
     14    handles => [qw{read_disc get_tracks get_cuesheet}], 
    2015); 
    2116 
    22 die "Usage: $0 -o <output.flac> [-D <device>]\n" unless $OUTPUT_NAME; 
     17sub rip_to_flac { 
     18    my ($self, $archive_flac) = @_; 
    2319 
    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); 
    3222 
    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 }; 
    3624 
    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); 
    4126 
    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'); 
    4330 
    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); 
    4535 
    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); 
    4942 
    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; 
    5445 
    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} 
    6150 
    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 
     521; 
Note: See TracChangeset for help on using the changeset viewer.