| 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.} ); |
|---|
| | 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) ); |
|---|
| 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 | |
|---|
| | 80 | my %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 | |
|---|
| | 87 | my $format_re = qr/%(u|l|t|i)/; |
|---|
| | 88 | |
|---|
| | 89 | sub 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; |
|---|