| 1 | package Plagger::Plugin::Filter::Audio; |
|---|
| 2 | use strict; |
|---|
| 3 | use base qw( Plagger::Plugin ); |
|---|
| 4 | |
|---|
| 5 | use Audio::ConvTools; |
|---|
| 6 | use File::Basename; |
|---|
| 7 | use File::stat; |
|---|
| 8 | use File::Copy; |
|---|
| 9 | use File::Spec; |
|---|
| 10 | |
|---|
| 11 | sub register { |
|---|
| 12 | my($self, $context) = @_; |
|---|
| 13 | $context->register_hook( |
|---|
| 14 | $self, |
|---|
| 15 | 'update.entry.fixup' => \&filter, |
|---|
| 16 | ); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | sub filter { |
|---|
| 20 | my($self, $context, $args) = @_; |
|---|
| 21 | |
|---|
| 22 | my $from = $self->conf->{from}; |
|---|
| 23 | my $to = $self->conf->{to}; |
|---|
| 24 | |
|---|
| 25 | my $e = $args->{entry}; |
|---|
| 26 | |
|---|
| 27 | { |
|---|
| 28 | no strict 'refs'; |
|---|
| 29 | *convert = *{ $from . '2' . $to }; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | my $local_path = eval { $e->enclosure->local_path }; |
|---|
| 33 | if($@){ |
|---|
| 34 | $context->log(error => q{Can't get local path.} ); |
|---|
| 35 | return; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | my $basename = basename($local_path, $from); |
|---|
| 39 | my $local_dir = dirname($local_path); |
|---|
| 40 | my $converted_file = File::Spec->catfile($self->conf->{dir}, $basename . $to); |
|---|
| 41 | |
|---|
| 42 | unless ( -e $converted_file ){ |
|---|
| 43 | my $status = convert($local_path); |
|---|
| 44 | unless ( $status ) { |
|---|
| 45 | $context->log(error => "Could not convert $local_path" ); |
|---|
| 46 | return; |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | my $converted_file_before_move = File::Spec->catfile( $local_dir, $basename . $to ); |
|---|
| 51 | if($self->conf->{normalize}){ |
|---|
| 52 | my $vol = `sox $converted_file_before_move -e stat -v 2<&1`; |
|---|
| 53 | chomp $vol; |
|---|
| 54 | system('sox', '-v', $vol, $converted_file_before_move, $converted_file); |
|---|
| 55 | } |
|---|
| 56 | else { |
|---|
| 57 | move( $converted_file_before_move, $converted_file ); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | my $st = stat($converted_file); |
|---|
| 61 | $e->enclosure->length($st->size); |
|---|
| 62 | $e->enclosure->local_path($converted_file); |
|---|
| 63 | $e->enclosure->filename($basename . $to); |
|---|
| 64 | $e->enclosure->type( Plagger::Util::mime_type_of($to) ); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | 1; |
|---|
| 68 | __END__ |
|---|
| 69 | |
|---|
| 70 | =head1 NAME |
|---|
| 71 | |
|---|
| 72 | Plagger::Plugin::Filter::Audio - Convert enclosure audio files with Audio::ConvTools. |
|---|
| 73 | |
|---|
| 74 | =head1 SYNOPSIS |
|---|
| 75 | |
|---|
| 76 | - module: Filter::Audio |
|---|
| 77 | config: |
|---|
| 78 | dir: /home/miya/audio |
|---|
| 79 | from: ogg |
|---|
| 80 | to: mp3 |
|---|
| 81 | normalize: 1 |
|---|
| 82 | |
|---|
| 83 | =head1 DESCRIPTION |
|---|
| 84 | |
|---|
| 85 | XXX Write the description for Filter::Audio |
|---|
| 86 | |
|---|
| 87 | =head1 CONFIG |
|---|
| 88 | |
|---|
| 89 | XXX Document configuration variables if any. |
|---|
| 90 | |
|---|
| 91 | =head1 AUTHOR |
|---|
| 92 | |
|---|
| 93 | Gosuke Miyashita |
|---|
| 94 | |
|---|
| 95 | =head1 SEE ALSO |
|---|
| 96 | |
|---|
| 97 | L<Plagger>, L<Audio::ConvTools> |
|---|
| 98 | |
|---|
| 99 | =cut |
|---|