--- FFmpeg.pm	2007-10-22 00:16:25.000000000 +0900
+++ FFmpeg.pm.1	2007-10-21 10:49:09.000000000 +0900
@@ -7,6 +7,8 @@
 use File::Spec;
 use FFmpeg::Command;
 use File::stat;
+use FLV::Info;
+use FLV::Util;
 
 sub register {
     my($self, $context) = @_;
@@ -40,40 +42,69 @@
 
         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 = "480x272";
+            $aspect = '16:9';
+            $context->log(info => 'Aspect 16:9');
+        }
+        else {
+            $size = "432x320";
+            $aspect = '4:3';
+            $context->log(info => 'Aspect 4:3');
+        }
+
+        if ($width == 320 && $height == 240) {
+            $size = "320x240";
+        }
+
+        # 音声のサンプリング周波数/チャンネルの設定
+        my $audioRate = $info{audio_rate};
+        my $audioCh = $info{audio_type};
+        $context->log(info => "Sample rate: $audioRate, Channel: $audioCh");
+
+        my $ar = "44100";
+        my $ac = "2";
+
+        if ($audioRate == $AUDIO_RATES{'22050 Hz'}) {
+            $ar = "22050";
+        }
+        
+        if ($audioCh != $AUDIO_TYPES{stereo}) {
+            $ac = "1";
+        }
+
         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>/$size/xmsg;
+           $option_string =~ s/<aspect>/$aspect/xmsg;
+           $option_string =~ s/<ar>/$ar/xmsg;
+           $option_string =~ s/<ac>/$ac/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 +122,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__
 
@@ -127,7 +185,6 @@
         audio_codec:         aac
         audio_sampling_rate: 48000
         audio_bit_rate:      64
-      extra_options: -coder 0 -vlevel 13 -ac 2
 
 =head1 DESCRIPTION
 
@@ -164,7 +221,7 @@
 
 Character encoding of title, author and comment. Default value is cp932.
 
-=head2 options
+=head3 options
 
 You can specify output video format as you like.
 
@@ -202,10 +259,6 @@
 
 =back
 
-=head2 extra_options
-
-Extra raw options passed to ffmpeg command.
-
 =head1 AUTHOR
 
 Gosuke Miyashita
