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

Last change on this file since 41 was 41, checked in by peter, 9 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
RevLine 
[23]1package Ripper;
[1]2
[23]3use Moose;
[1]4
[22]5use Tracks;
[41]6use DiscFlacFile;
[1]7
8use File::Temp qw{tempdir};
[23]9use File::Spec::Functions qw{catfile};
[1]10use File::Copy;
11
[29]12has device => (
13    is  => 'rw',
14    isa => 'Str',
15);
[23]16has tracks => ( 
[29]17    is      => 'rw',
18    isa     => 'Tracks',
19    handles => [qw{read_disc has_tracks}],
[1]20);
21
[23]22sub 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]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
[23]70# module return
711;
Note: See TracBrowser for help on using the repository browser.