--- ./before/FFmpeg.pm 2007-10-02 19:34:17.000000000 +0900 +++ ./after/FFmpeg.pm 2007-10-02 19:33:04.000000000 +0900 @@ -7,6 +7,7 @@ use File::Spec; use FFmpeg::Command; use File::stat; +use FLV::Info; sub register { my($self, $context) = @_; @@ -40,40 +41,48 @@ my $file; if ($self->conf->{filename}) { - $file = Plagger::Util::filename_for($entry, $self->conf->{filename}); + $file = $self->gen_filename($entry); } else { $file = $enclosure->filename; $file =~ s/\.[^\.]*$//; $file .= ".$ext"; } + # アスペクト比によって出力サイズを変える + my $reader = FLV::Info->new(); + $reader->parse($enclosure->local_path); + + my %info = $reader->get_info; + + my $height = $info{video_height} || $info{meta_height}; + my $width = $info{video_width} || $info{meta_width}; + + $context->log(info => "Width: $width, Height: $height"); + my $size; + my $aspect; + if ($width/$height > 1.5) { + $size = "640x360"; + $aspect = '16:9'; + $context->log(info => 'Aspect 16:9'); + } + else { + $size = "640x480"; + $aspect = '4:3'; + $context->log(info => 'Aspect 4:3'); + } + my $ff = FFmpeg::Command->new($self->conf->{command}); $ff->input_options({ file => encode($encoding, $enclosure->local_path) }); my $output_file = File::Spec->catfile($self->conf->{dir}, "$file"); - my $output_options = { - file => encode($encoding, $output_file), - device => $self->conf->{device} || 'ipod', - title => encode($encoding, $entry->title), - author => encode($encoding, $entry->author), - comment => encode($encoding, $entry->summary), - %{ $self->conf->{options} || {} }, - }; - - if ( $self->conf->{extra_options} ) { - my %option_to_name = reverse %FFmpeg::Command::option; - my @extra_options = split ' ', $self->conf->{extra_options}; - for ( @extra_options ){ - my $name = $option_to_name{$_}; - delete $output_options->{$name} if defined $output_options->{$name} and $name; - } - $ff->output_options($output_options); - $ff->options( @extra_options, @{ $ff->options } ); - } - else { - $ff->output_options($output_options); - } + my $option_string = $self->conf->{option_string}; + $option_string =~ s//$size/xmsg; + $option_string =~ s//$aspect/xmsg; + $ff->options( + [ split /\s+/, $option_string ], + ); + $ff->output_file($output_file); unless( -e $output_file ){ $context->log( info => 'Converting ' . $enclosure->filename . ' ...' ); my $result = $ff->exec(); @@ -91,6 +100,33 @@ } } +my %formats = ( + 'u' => sub { my $s = $_[0]->url; $s =~ s!^https?://!!; $s }, + 'l' => sub { my $s = $_[0]->link; $s =~ s!^https?://!!; $s }, + 't' => sub { $_[0]->title }, + 'i' => sub { $_[0]->id }, +); + +my $format_re = qr/%(u|l|t|i)/; + +sub gen_filename { + my($self, $feed) = @_; + + my $file = $self->conf->{filename} || + '%i.' . ($self->conf->{format} eq 'RSS' ? 'rss' : 'atom'); + $file =~ s{$format_re}{ + $self->safe_filename($formats{$1}->($feed)) + }egx; + $file; +} + +sub safe_filename { + my($self, $path) = @_; + $path =~ s![^\w\s]+!_!g; + $path =~ s!\s+!_!g; + $path; +} + 1; __END__