root/lang/perl/plagger/lib/Plagger/Plugin/Filter/Lame.pm

Revision 8756, 2.5 kB (checked in by otsune, 5 months ago)

plagger/lib/Plagger/Plugin/Filter/Lame.pm: salvage wildplugin (via http://d.hatena.ne.jp/hakobe932/20060708/1152364512 )

Line 
1package Plagger::Plugin::Filter::Lame;
2use strict;
3use base qw(Plagger::Plugin::Filter::Base);
4
5sub register {
6    my($self, $context) = @_;
7    $context->register_hook(
8        $self,
9        'update.entry.fixup' => \&filter,
10    );
11}
12
13sub init {
14    my $self = shift;
15    $self->SUPER::init(@_);
16
17    defined $self->conf->{dir} or Plagger->context->error("config 'dir' is not set.");
18    unless (-e $self->conf->{dir} && -d _) {
19        Plagger->context->log(warn => $self->conf->{dir} . " does not exist. Creating");
20        mkpath $self->conf->{dir};
21    }
22}
23
24sub filter {
25    my($self, $context, $args) = @_;
26
27    for my $enclosure ($args->{entry}->enclosures) {
28        my $feed_dir = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe);
29        unless (-e $feed_dir && -d _) {
30            $context->log(info => "mkdir $feed_dir");
31            mkdir $feed_dir, 0777;
32        }
33
34        my $srcpath = $enclosure->local_path;
35        $srcpath =~ m/(.*)\./;
36        my $dstpath = $1 || $srcpath;
37        $dstpath .= '.mp3';
38
39        if (-e $dstpath) {
40            my $length = -s _;
41            $enclosure->length($length);
42            $enclosure->type('audio/x-mp3');
43            $enclosure->local_path($dstpath);
44            $context->log(debug => $srcpath. "is already converted into $dstpath");
45            next;
46        }
47
48        unless (-e $srcpath) {
49            $context->log(info => "The local data has not stored yet.");
50            next;
51        }
52
53        $context->log(info => "Convert " . $srcpath . " to " . $dstpath);
54
55        my @options = split(/ /,$self->conf->{option}) or ('-V5', '--athaa-sensitivity', '1');
56        my @command = ('lame', @options, $srcpath, $dstpath);
57
58        system(@command);
59
60        if (!$? && -s $dstpath ) {
61            my $length = -s _;
62            $enclosure->length($length);
63            $enclosure->type('audio/x-mp3');
64            $enclosure->local_path($dstpath);
65            $context->log(info => "Converting to $dstpath is done [$length]");
66            if ($self->conf->{delsrc}) {
67                $context->log(info => "Deleting $srcpath");
68                unlink $srcpath or $self->log(error => "Cannot delete $srcpath. ");
69            }
70        }
71    }
72}
73
741;
75
76__END__
77
78=head1 NAME
79
80Plagger::Plugin::Filter::Lame - Convert local files to mp3  using lame
81
82=head1 SYNOPSIS
83
84  - module: Filter::Lame
85    config:
86      dir: /path/for/convert
87      option: -lame -options 
88      delsrc: 1
89
90
91=head1 DESCRIPTION
92
93This plugin converts fetched files to mp3 files using lame.
94
95=head1 AUTHOR
96
97Yohei Fushii
98
99=head1 SEE ALSO
100
101L<Plagger>
102
103=cut
Note: See TracBrowser for help on using the browser.