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