[23] | 1 | package Ripper; |
---|
[1] | 2 | |
---|
[23] | 3 | use Moose; |
---|
[1] | 4 | |
---|
[22] | 5 | use Tracks; |
---|
[41] | 6 | use DiscFlacFile; |
---|
[1] | 7 | |
---|
| 8 | use File::Temp qw{tempdir}; |
---|
[23] | 9 | use File::Spec::Functions qw{catfile}; |
---|
[1] | 10 | use File::Copy; |
---|
| 11 | |
---|
[29] | 12 | has device => ( |
---|
| 13 | is => 'rw', |
---|
| 14 | isa => 'Str', |
---|
| 15 | ); |
---|
[23] | 16 | has tracks => ( |
---|
[29] | 17 | is => 'rw', |
---|
| 18 | isa => 'Tracks', |
---|
| 19 | handles => [qw{read_disc has_tracks}], |
---|
[1] | 20 | ); |
---|
| 21 | |
---|
[23] | 22 | sub rip_to_flac { |
---|
[41] | 23 | my ($self, $archive_flac, $tags) = @_; |
---|
[22] | 24 | |
---|
[23] | 25 | $self->tracks(Tracks->new); |
---|
| 26 | $self->read_disc($self->device); |
---|
[1] | 27 | |
---|
[24] | 28 | die "No tracks found; is there a CD in the drive?\n" unless $self->has_tracks; |
---|
[1] | 29 | |
---|
[23] | 30 | my $tempdir = tempdir(CLEANUP => 1); |
---|
[1] | 31 | |
---|
[23] | 32 | my $wav_file = catfile($tempdir, 'cdda.wav'); |
---|
| 33 | my $flac_file = catfile($tempdir, 'cdda.flac'); |
---|
| 34 | my $cue_file = catfile($tempdir, 'cdda.cue'); |
---|
[1] | 35 | |
---|
[23] | 36 | # rip |
---|
[26] | 37 | my $span = $self->_get_cdparanoia_span; |
---|
[23] | 38 | system 'cdparanoia', '-d', $self->device, $span, $wav_file; |
---|
| 39 | die "\nRipping canceled\n" if ($? & 127); |
---|
[1] | 40 | |
---|
[23] | 41 | # encode + cuesheet |
---|
| 42 | open my $CUE, "> $cue_file"; |
---|
[29] | 43 | print $CUE $self->tracks->get_cuesheet; |
---|
[23] | 44 | close $CUE; |
---|
| 45 | system 'flac', '-o', $flac_file, '--cuesheet', $cue_file, $wav_file; |
---|
| 46 | die "\nFLAC encoding canceled\n" if ($? & 127); |
---|
[1] | 47 | |
---|
[23] | 48 | # MusicBrainz discid metadata |
---|
[28] | 49 | my $discid = $self->tracks->discid; |
---|
[1] | 50 | |
---|
[23] | 51 | # copy to permanent location |
---|
| 52 | copy($flac_file, $archive_flac); |
---|
[41] | 53 | |
---|
| 54 | # tag the archive flac |
---|
| 55 | $tags ||= {}; |
---|
| 56 | $tags->{MUSICBRAINZ_DISCID} = $discid; |
---|
| 57 | my $flac_disc = DiscFlacFile->new({ file => $archive_flac }); |
---|
| 58 | $flac_disc->flac->{tags}{$_} = $tags->{$_} foreach keys %{$tags}; |
---|
| 59 | $flac_disc->flac->write; |
---|
| 60 | |
---|
| 61 | return $flac_disc; |
---|
[23] | 62 | } |
---|
[1] | 63 | |
---|
[26] | 64 | sub _get_cdparanoia_span { |
---|
| 65 | my ($self) = @_; |
---|
| 66 | # use a msf start unless track 1 begins at sector |
---|
| 67 | return $self->tracks->tracks->[1]{sector} == 0 ? '1-' : '00:00.00-'; |
---|
| 68 | } |
---|
| 69 | |
---|
[23] | 70 | # module return |
---|
| 71 | 1; |
---|