1 | package Ripper; |
---|
2 | |
---|
3 | use Moose; |
---|
4 | |
---|
5 | use Tracks; |
---|
6 | |
---|
7 | use File::Temp qw{tempdir}; |
---|
8 | use File::Spec::Functions qw{catfile}; |
---|
9 | use File::Copy; |
---|
10 | |
---|
11 | has device => ( is => 'rw' ); |
---|
12 | has tracks => ( |
---|
13 | is => 'rw', |
---|
14 | handles => [qw{read_disc has_tracks get_cuesheet}], |
---|
15 | ); |
---|
16 | |
---|
17 | sub rip_to_flac { |
---|
18 | my ($self, $archive_flac) = @_; |
---|
19 | |
---|
20 | $self->tracks(Tracks->new); |
---|
21 | $self->read_disc($self->device); |
---|
22 | |
---|
23 | die "No tracks found; is there a CD in the drive?\n" unless $self->has_tracks; |
---|
24 | |
---|
25 | my $tempdir = tempdir(CLEANUP => 1); |
---|
26 | |
---|
27 | my $wav_file = catfile($tempdir, 'cdda.wav'); |
---|
28 | my $flac_file = catfile($tempdir, 'cdda.flac'); |
---|
29 | my $cue_file = catfile($tempdir, 'cdda.cue'); |
---|
30 | |
---|
31 | # rip |
---|
32 | my $span = $self->_get_cdparanoia_span; |
---|
33 | system 'cdparanoia', '-d', $self->device, $span, $wav_file; |
---|
34 | die "\nRipping canceled\n" if ($? & 127); |
---|
35 | |
---|
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); |
---|
42 | |
---|
43 | # MusicBrainz discid metadata |
---|
44 | my $discid = $self->tracks->get_musicbrainz_discid; |
---|
45 | |
---|
46 | # copy to permanent location |
---|
47 | copy($flac_file, $archive_flac); |
---|
48 | system 'metaflac', '--set-tag', "MUSICBRAINZ_DISCID=$discid", $archive_flac; |
---|
49 | } |
---|
50 | |
---|
51 | sub _get_cdparanoia_span { |
---|
52 | my ($self) = @_; |
---|
53 | # use a msf start unless track 1 begins at sector |
---|
54 | return $self->tracks->tracks->[1]{sector} == 0 ? '1-' : '00:00.00-'; |
---|
55 | } |
---|
56 | |
---|
57 | # module return |
---|
58 | 1; |
---|