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 => ( |
---|
12 | is => 'rw', |
---|
13 | isa => 'Str', |
---|
14 | ); |
---|
15 | has tracks => ( |
---|
16 | is => 'rw', |
---|
17 | isa => 'Tracks', |
---|
18 | handles => [qw{read_disc has_tracks}], |
---|
19 | ); |
---|
20 | |
---|
21 | sub rip_to_flac { |
---|
22 | my ($self, $archive_flac) = @_; |
---|
23 | |
---|
24 | $self->tracks(Tracks->new); |
---|
25 | $self->read_disc($self->device); |
---|
26 | |
---|
27 | die "No tracks found; is there a CD in the drive?\n" unless $self->has_tracks; |
---|
28 | |
---|
29 | my $tempdir = tempdir(CLEANUP => 1); |
---|
30 | |
---|
31 | my $wav_file = catfile($tempdir, 'cdda.wav'); |
---|
32 | my $flac_file = catfile($tempdir, 'cdda.flac'); |
---|
33 | my $cue_file = catfile($tempdir, 'cdda.cue'); |
---|
34 | |
---|
35 | # rip |
---|
36 | my $span = $self->_get_cdparanoia_span; |
---|
37 | system 'cdparanoia', '-d', $self->device, $span, $wav_file; |
---|
38 | die "\nRipping canceled\n" if ($? & 127); |
---|
39 | |
---|
40 | # encode + cuesheet |
---|
41 | open my $CUE, "> $cue_file"; |
---|
42 | print $CUE $self->tracks->get_cuesheet; |
---|
43 | close $CUE; |
---|
44 | system 'flac', '-o', $flac_file, '--cuesheet', $cue_file, $wav_file; |
---|
45 | die "\nFLAC encoding canceled\n" if ($? & 127); |
---|
46 | |
---|
47 | # MusicBrainz discid metadata |
---|
48 | my $discid = $self->tracks->discid; |
---|
49 | |
---|
50 | # copy to permanent location |
---|
51 | copy($flac_file, $archive_flac); |
---|
52 | system 'metaflac', '--set-tag', "MUSICBRAINZ_DISCID=$discid", $archive_flac; |
---|
53 | } |
---|
54 | |
---|
55 | sub _get_cdparanoia_span { |
---|
56 | my ($self) = @_; |
---|
57 | # use a msf start unless track 1 begins at sector |
---|
58 | return $self->tracks->tracks->[1]{sector} == 0 ? '1-' : '00:00.00-'; |
---|
59 | } |
---|
60 | |
---|
61 | # module return |
---|
62 | 1; |
---|