| 1 | | package Plagger::Plugin::Filter::FFmpeg; |
|---|
| 2 | | |
|---|
| 3 | | use strict; |
|---|
| 4 | | use base qw( Plagger::Plugin ); |
|---|
| 5 | | use Plagger::Util; |
|---|
| 6 | | use Encode; |
|---|
| 7 | | use File::Spec; |
|---|
| 8 | | use FFmpeg::Command; |
|---|
| 9 | | use File::stat; |
|---|
| 10 | | |
|---|
| 11 | | sub register { |
|---|
| 12 | | my($self, $context) = @_; |
|---|
| 13 | | $context->register_hook( |
|---|
| 14 | | $self, |
|---|
| 15 | | 'update.entry.fixup' => \&filter, |
|---|
| 16 | | ); |
|---|
| 17 | | } |
|---|
| 18 | | |
|---|
| 19 | | sub filter { |
|---|
| 20 | | my($self, $context, $args) = @_; |
|---|
| 21 | | |
|---|
| 22 | | my $entry = $args->{entry}; |
|---|
| 23 | | |
|---|
| 24 | | 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 | | |
|---|
| 28 | | my $encoding = $self->conf->{encoding} || 'cp932'; |
|---|
| 29 | | |
|---|
| 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.} ); |
|---|
| 38 | | return; |
|---|
| 39 | | } |
|---|
| 40 | | |
|---|
| 41 | | my $file; |
|---|
| 42 | | if ($self->conf->{filename}) { |
|---|
| 43 | | $file = Plagger::Util::filename_for($entry, $self->conf->{filename}); |
|---|
| 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 | | my $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 | | if ( $self->conf->{extra_options} ) { |
|---|
| 64 | | my %option_to_name = reverse %FFmpeg::Command::option; |
|---|
| 65 | | my @extra_options = split ' ', $self->conf->{extra_options}; |
|---|
| 66 | | for ( @extra_options ){ |
|---|
| 67 | | my $name = $option_to_name{$_}; |
|---|
| 68 | | delete $output_options->{$name} if defined $output_options->{$name} and $name; |
|---|
| 69 | | } |
|---|
| 70 | | $ff->output_options($output_options); |
|---|
| 71 | | $ff->options( @extra_options, @{ $ff->options } ); |
|---|
| 72 | | } |
|---|
| 73 | | else { |
|---|
| 74 | | $ff->output_options($output_options); |
|---|
| 75 | | } |
|---|
| 76 | | |
|---|
| 77 | | unless( -e $output_file ){ |
|---|
| 78 | | $context->log( info => 'Converting ' . $enclosure->filename . ' ...' ); |
|---|
| 79 | | my $result = $ff->exec(); |
|---|
| 80 | | unless ( $result ){ |
|---|
| 81 | | $context->log( error => $ff->errstr ); |
|---|
| 82 | | return; |
|---|
| 83 | | } |
|---|
| 84 | | } |
|---|
| 85 | | |
|---|
| 86 | | my $st = stat($output_file); |
|---|
| 87 | | $enclosure->length($st->size); |
|---|
| 88 | | $enclosure->local_path($output_file); |
|---|
| 89 | | $enclosure->filename("$file"); |
|---|
| 90 | | $enclosure->type( Plagger::Util::mime_type_of($file) ); |
|---|
| 91 | | } |
|---|
| 92 | | } |
|---|
| 93 | | |
|---|
| 94 | | 1; |
|---|
| 95 | | __END__ |
|---|
| 96 | | |
|---|
| 97 | | =head1 NAME |
|---|
| 98 | | |
|---|
| 99 | | Plagger::Plugin::Filter::FFmpeg - Convert enclosure file with ffmpeg command. |
|---|
| 100 | | |
|---|
| 101 | | =head1 SYNOPSIS |
|---|
| 102 | | |
|---|
| 103 | | # Convert video file into iPod playable format. |
|---|
| 104 | | - module: Filter::FFmpeg |
|---|
| 105 | | config: |
|---|
| 106 | | dir: /home/miya/mov |
|---|
| 107 | | |
|---|
| 108 | | # Convert video file into PSP playable format. |
|---|
| 109 | | - module: Filter::FFmpeg |
|---|
| 110 | | config: |
|---|
| 111 | | dir: /home/miya/mov |
|---|
| 112 | | device: psp |
|---|
| 113 | | |
|---|
| 114 | | # Convert video file into your favorite format. |
|---|
| 115 | | - module: Filter::FFmpeg |
|---|
| 116 | | config: |
|---|
| 117 | | dir: /home/miya/mov |
|---|
| 118 | | ext: m4v |
|---|
| 119 | | command: /usr/local/bin/ffmpeg |
|---|
| 120 | | encoding: cp932 |
|---|
| 121 | | filename: %t.m4v |
|---|
| 122 | | options: |
|---|
| 123 | | format: psp |
|---|
| 124 | | video_codec: h264 |
|---|
| 125 | | bitrate: 600 |
|---|
| 126 | | frame_size: 320x240 |
|---|
| 127 | | audio_codec: aac |
|---|
| 128 | | audio_sampling_rate: 48000 |
|---|
| 129 | | audio_bit_rate: 64 |
|---|
| 130 | | extra_options: -coder 0 -vlevel 13 -ac 2 |
|---|
| 131 | | |
|---|
| 132 | | =head1 DESCRIPTION |
|---|
| 133 | | |
|---|
| 134 | | This plugin converts enclosure into iPod playable format, PSP playable format and other formats you lie with ffmpeg command. |
|---|
| 135 | | |
|---|
| 136 | | =head1 CONFIG |
|---|
| 137 | | |
|---|
| 138 | | =head2 dir |
|---|
| 139 | | |
|---|
| 140 | | Specify the directory name that converted files put in. |
|---|
| 141 | | |
|---|
| 142 | | =head2 ext |
|---|
| 143 | | |
|---|
| 144 | | Specify a file extension of converted enclosure file. |
|---|
| 145 | | Default is 'm4v.' |
|---|
| 146 | | |
|---|
| 147 | | =head2 filename |
|---|
| 148 | | |
|---|
| 149 | | Set 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. |
|---|
| 150 | | |
|---|
| 151 | | If this option is set, ext option is ignored. |
|---|
| 152 | | |
|---|
| 153 | | =head2 command |
|---|
| 154 | | |
|---|
| 155 | | Specify ffmpeg command path. |
|---|
| 156 | | You need to set this parameter if ffmpeg command is not in PATH envoiroment variable. |
|---|
| 157 | | |
|---|
| 158 | | =head2 device |
|---|
| 159 | | |
|---|
| 160 | | For which device you'd like to convert enclosure files. |
|---|
| 161 | | You can chooe 'ipod' or 'psp.' Default is 'ipod.' |
|---|
| 162 | | |
|---|
| 163 | | =head2 encoding |
|---|
| 164 | | |
|---|
| 165 | | Character encoding of title, author and comment. Default value is cp932. |
|---|
| 166 | | |
|---|
| 167 | | =head2 options |
|---|
| 168 | | |
|---|
| 169 | | You can specify output video format as you like. |
|---|
| 170 | | |
|---|
| 171 | | Available options are: |
|---|
| 172 | | |
|---|
| 173 | | =over |
|---|
| 174 | | |
|---|
| 175 | | =item format |
|---|
| 176 | | |
|---|
| 177 | | Output video format. |
|---|
| 178 | | |
|---|
| 179 | | =item video_codec |
|---|
| 180 | | |
|---|
| 181 | | Output video codec. |
|---|
| 182 | | |
|---|
| 183 | | =item bitrate |
|---|
| 184 | | |
|---|
| 185 | | Output video bitrate. |
|---|
| 186 | | |
|---|
| 187 | | =item frame_size |
|---|
| 188 | | |
|---|
| 189 | | Output video screen size. |
|---|
| 190 | | |
|---|
| 191 | | =item audio_codec |
|---|
| 192 | | |
|---|
| 193 | | Output audio code. |
|---|
| 194 | | |
|---|
| 195 | | =item audio_sampling_rate |
|---|
| 196 | | |
|---|
| 197 | | Output audio sampling rate. |
|---|
| 198 | | |
|---|
| 199 | | =item audio_bit_rate |
|---|
| 200 | | |
|---|
| 201 | | Output audio bit rate. |
|---|
| 202 | | |
|---|
| 203 | | =back |
|---|
| 204 | | |
|---|
| 205 | | =head2 extra_options |
|---|
| 206 | | |
|---|
| 207 | | Extra raw options passed to ffmpeg command. |
|---|
| 208 | | |
|---|
| 209 | | =head1 AUTHOR |
|---|
| 210 | | |
|---|
| 211 | | Gosuke Miyashita |
|---|
| 212 | | |
|---|
| 213 | | =head1 SEE ALSO |
|---|
| 214 | | |
|---|
| 215 | | L<Plagger>, L<FFmpeg::Command> |
|---|
| 216 | | |
|---|
| 217 | | =cut |
|---|
| | 1 | This project moved to http://svn.coderepos.org/share/lang/perl/plagger/lib/Plagger/Plugin/Filter/FFmpeg.pm |
|---|