source: flacrip/trunk/lib/Ripper.pm @ 46

Last change on this file since 46 was 41, checked in by peter, 10 years ago

Use Audio::FLAC::Header to write the tags to the FLAC file instead of making
external calls to metaflac. In addition, rip_to_flac now returns a
DiscFlacFile object.

  • Property svn:executable set to *
File size: 1.7 KB
Line 
1package Ripper;
2
3use Moose;
4
5use Tracks;
6use DiscFlacFile;
7
8use File::Temp qw{tempdir};
9use File::Spec::Functions qw{catfile};
10use File::Copy;
11
12has device => (
13    is  => 'rw',
14    isa => 'Str',
15);
16has tracks => ( 
17    is      => 'rw',
18    isa     => 'Tracks',
19    handles => [qw{read_disc has_tracks}],
20);
21
22sub 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
64sub _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
711;
Note: See TracBrowser for help on using the repository browser.