Changeset 253

Show
Ignore:
Timestamp:
08/31/06 11:27:23 (2 years ago)
Author:
miya
Message:

Filter::FFmpeg: Support entries which have several enclosures.
Change rewrite_filename option to filename option folloing Pubish::Feed.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plagger/trunk/lib/Plagger/Plugin/Filter/FFmpeg.pm

    r252 r253  
    2020    my($self, $context, $args) = @_; 
    2121 
    22     my $e = $args->{entry}; 
     22    my $entry = $args->{entry}; 
    2323 
    2424    my $ext = $self->conf->{ext} || 'm4v'; 
     25    $context->log( warn => 'ext option is ignored because you set filename option' ) 
     26        if $self->conf->{ext} and $self->conf->{filename}; 
     27 
    2528    my $encoding = $self->conf->{encoding} || 'cp932'; 
    2629 
    27     my $file = eval { $e->enclosure->filename }; 
    28     if($@){ 
    29         $context->log(error => q{Can't get filename.} ); 
    30         return; 
    31     } 
    32  
    33     if ($self->conf->{rewrite_filename}) { 
    34         $file = $e->title; 
    35     } else { 
    36         $file =~ s/\.[^\.]*$//; 
    37     } 
    38  
    39     $file = $self->safe_filename($file); 
    40  
    41     my $ff = FFmpeg::Command->new($self->conf->{command}); 
    42     $ff->input_options({ file => encode($encoding, $e->enclosure->local_path) }); 
    43  
    44     my $output_file = File::Spec->catfile($self->conf->{dir}, "$file.$ext"); 
    45     $ff->output_options({ 
    46         file    => encode($encoding, $output_file), 
    47         device  => $self->conf->{device} || 'ipod', 
    48         title   => encode($encoding, $e->title), 
    49         author  => encode($encoding, $e->author), 
    50         comment => encode($encoding, $e->summary), 
    51         %{ $self->conf->{options} || {} }, 
    52     }); 
    53  
    54     unless( -e $output_file ){ 
    55         $context->log( info => 'Converting ' . $e->enclosure->filename . ' ...' ); 
    56         my $result = $ff->exec(); 
    57         unless ( $result ){ 
    58             $context->log( error => $ff->errstr ); 
     30    my $enclosures = $entry->enclosures; 
     31    $context->log( warn => 'You shuld not use filename option because the entry has several enclosures.' ) 
     32        if $#{$enclosures} > 1; 
     33 
     34    for my $enclosure ( @$enclosures ){ 
     35        eval { $enclosure->local_path }; 
     36        if($@){ 
     37            $context->log(error => q{Can't get local file.} ); 
    5938            return; 
    6039        } 
     40 
     41        my $file; 
     42        if ($self->conf->{filename}) { 
     43            $file = $self->gen_filename($entry); 
     44        } else { 
     45            $file = $enclosure->filename; 
     46            $file =~ s/\.[^\.]*$//; 
     47            $file .= ".$ext"; 
     48        } 
     49 
     50        my $ff = FFmpeg::Command->new($self->conf->{command}); 
     51        $ff->input_options({ file => encode($encoding, $enclosure->local_path) }); 
     52 
     53        my $output_file = File::Spec->catfile($self->conf->{dir}, "$file"); 
     54        $ff->output_options({ 
     55            file    => encode($encoding, $output_file), 
     56            device  => $self->conf->{device} || 'ipod', 
     57            title   => encode($encoding, $entry->title), 
     58            author  => encode($encoding, $entry->author), 
     59            comment => encode($encoding, $entry->summary), 
     60            %{ $self->conf->{options} || {} }, 
     61        }); 
     62 
     63        unless( -e $output_file ){ 
     64            $context->log( info => 'Converting ' . $enclosure->filename . ' ...' ); 
     65            my $result = $ff->exec(); 
     66            unless ( $result ){ 
     67                $context->log( error => $ff->errstr ); 
     68                return; 
     69            } 
     70        } 
     71 
     72        my $st = stat($output_file); 
     73        $enclosure->length($st->size); 
     74        $enclosure->local_path($output_file); 
     75        $enclosure->filename("$file"); 
     76        $enclosure->type( Plagger::Util::mime_type_of($file) ); 
    6177    } 
    62  
    63     my $st = stat($output_file); 
    64     $e->enclosure->length($st->size); 
    65     $e->enclosure->local_path($output_file); 
    66     $e->enclosure->filename("$file.$ext"); 
    67     $e->enclosure->type( Plagger::Util::mime_type_of($ext) ); 
     78
     79 
     80my %formats = ( 
     81    'u' => sub { my $s = $_[0]->url;  $s =~ s!^https?://!!; $s }, 
     82    'l' => sub { my $s = $_[0]->link; $s =~ s!^https?://!!; $s }, 
     83    't' => sub { $_[0]->title }, 
     84    'i' => sub { $_[0]->id }, 
     85); 
     86 
     87my $format_re = qr/%(u|l|t|i)/; 
     88 
     89sub gen_filename { 
     90    my($self, $feed) = @_; 
     91 
     92    my $file = $self->conf->{filename} || 
     93        '%i.' . ($self->conf->{format} eq 'RSS' ? 'rss' : 'atom'); 
     94    $file =~ s{$format_re}{ 
     95        $self->safe_filename($formats{$1}->($feed)) 
     96    }egx; 
     97    $file; 
    6898} 
    6999 
     
    102132      command: /usr/local/bin/ffmpeg 
    103133      encoding: cp932 
    104       rewrite_filename: 0 
     134      filename: %t.m4v 
    105135      options: 
    106136        format:              psp 
     
    127157Default is 'm4v.' 
    128158 
     159=head2 filename 
     160 
     161Set a filename of converted files.If this option is null, this plugin uses enclosure->filename and ext option to compose the filename of converted file. 
     162 
     163If this option is set, ext option is ignored. 
     164 
    129165=head2 command 
    130166 
     
    141177Character encoding of title, author and comment. Default value is cp932. 
    142178 
    143 =head2 rewrite_filename 
    144  
    145 Whether rewrite filename to title or not. Default value is 0 (not rewrite). 
    146  
    147  
    148179=head3 options 
    149180