root/library/perl/trunk/FFmpeg-Command/lib/FFmpeg/Command.pm

Revision 438, 7.9 kB (checked in by miya, 9 months ago)

patch from hsbt

Line 
1 package FFmpeg::Command;
2
3 use warnings;
4 use strict;
5 our $VERSION = '0.11';
6
7 use base qw( Class::Accessor::Fast Class::ErrorHandler );
8 __PACKAGE__->mk_accessors( qw( input_file output_file ffmpeg options timeout stdout stderr command ) );
9
10 use IPC::Run qw( start );
11 use Carp qw( carp );
12
13 our %option = (
14     format              => '-f',
15     video_codec         => '-vcodec',
16     bitrate             => '-b',
17     frame_rate          => '-r',
18     frame_size          => '-s',
19     audio_codec         => '-acodec',
20     audio_sampling_rate => '-ar',
21     audio_bit_rate      => '-ab',
22     size                => '-s',
23 );
24
25 our %metadata = (
26     title               => 'title=',
27     author              => 'author=',
28     comment             => 'comment=',
29 );
30
31 sub new {
32     my $class = shift;
33     my $self = {
34         ffmpeg      => shift || 'ffmpeg',
35         options     => [],
36         input_file  => '',
37         output_file => '',
38         timeout     => 0,
39     };
40
41     if ( system("$self->{ffmpeg} -version >& /dev/null") != 0 ) {
42         carp "Can't find ffmpeg command.";
43         exit 0;
44     }
45
46     bless $self, $class;
47 }
48
49 sub input_options {
50     my ( $self, $args ) = @_;
51     $self->input_file($args->{file});
52     return;
53 }
54
55 sub output_options {
56     my ( $self, $args ) = @_;
57     $self->output_file(delete $args->{file});
58     my $device = delete $args->{device};
59
60     my %device_option = (
61         ipod => {
62             format              => 'mp4',
63             video_codec         => 'mpeg4',
64             bitrate             => 600,
65             frame_size          => '320x240',
66             audio_codec         => 'libfaac',
67             audio_sampling_rate => 48000,
68             audio_bit_rate      => 64,
69         },
70         psp => {
71             format              => 'psp',
72             video_codec         => 'mpeg4',
73             bitrate             => 600,
74             frame_size          => '320x240',
75             audio_codec         => 'libfaac',
76             audio_sampling_rate => 48000,
77             audio_bit_rate      => 64,
78         },
79     );
80
81     my %output_option = (
82         %$args,
83     );
84
85     # if a device name was supplied, add its output options
86     if( $device ) {
87         %output_option = (
88             %output_option,
89             %{ $device_option{$device} },
90         );
91     }
92
93     for ( keys %output_option ){
94         if( defined $option{$_} and defined $output_option{$_} ){
95             push @{ $self->options }, $option{$_}, $output_option{$_};
96         }
97         elsif( defined $metadata{$_} and defined $output_option{$_} ){
98             push @{ $self->options }, '-metadata', $metadata{$_} . $output_option{$_};
99         }
100         else {
101             carp "$_ is not defined and ignored.";
102         }
103     }
104
105     return;
106 }
107
108 sub execute {
109     my $self = shift;
110
111     my @opts = ( \$self->{stdin}, \$self->{stdout}, \$self->{stderr} );
112     push @opts, IPC::Run::timeout($self->timeout) if $self->timeout;
113
114     my $cmd = [
115         $self->ffmpeg,
116         '-y',
117         '-i', $self->input_file,
118         @{ $self->options },
119     ];
120
121     # add output file only if we have one
122     push @$cmd, $self->output_file
123         if $self->output_file;
124
125     # store the command line so we can debug it
126     $self->command( join( ' ', @$cmd ) );
127
128     my $h = eval {
129         start( $cmd, @opts )
130     };
131
132     if( $@ ){
133         $self->error($@);
134         return;
135     }
136     else {
137         finish $h or do {
138             $self->error($self->stderr);
139             return;
140         };
141     }
142
143     return 1;
144 }
145
146 *exec = \&execute;
147
148 __END__
149
150 =head1 NAME
151
152 FFmpeg::Command - A wrapper class for ffmpeg command line utility.
153
154 =head1 DESCRIPTION
155
156 A simple interface for using ffmpeg command line utility.
157
158 =head1 SYNOPSIS
159
160     use FFmpeg::Command;
161
162     my $ffmpeg = FFmpeg::Command->new('/usr/local/bin/ffmpeg');
163
164     $ffmpeg->input_options({
165         file => $input_file,
166     });
167
168     # Set timeout
169     $ffmpeg->timeout(300);
170
171     # Convert a video file into iPod playable format.
172     $ffmpeg->output_options({
173         file   => $output_file,
174         device => 'ipod',
175     });
176
177     my $result = $ffmpeg->exec();
178
179     croak $ffmpeg->errstr unless $result;
180
181     # This is same as above.
182     $ffmpeg->output_options({
183         file                => $output_file,
184         format              => 'mp4',
185         video_codec         => 'mpeg4',
186         bitrate             => 600,
187         frame_size          => '320x240',
188         audio_codec         => 'libaac',
189         audio_sampling_rate => 48000,
190         audio_bit_rate      => 64,
191     });
192
193     $ffmpeg->exec();
194
195
196     # Convert a video file into PSP playable format.
197     $ffmpeg->output_options({
198         file  => $output_file,
199         device => 'psp',
200     });
201
202     $ffmpeg->exec();
203
204     # This is same as above.
205     $ffmpeg->output_options({
206         file                => $output_file,
207         format              => 'psp',
208         video_codec         => 'mpeg4',
209         bitrate             => 600,
210         frame_size          => '320x240',
211         audio_codec         => 'libaac',
212         audio_sampling_rate => 48000,
213         audio_bit_rate      => 64,
214     });
215
216     $ffmpeg->exec();
217
218     # Execute ffmpeg with any options you like.
219     # This sample code takes a screnn shot.
220     $ffmpeg->input_file($input_file);
221     $ffmpeg->output_file($output_file);
222
223     $ffmpeg->options(
224         '-y',
225         '-f'       => 'image2',
226         '-pix_fmt' => 'jpg',
227         '-vframes' => 1,
228         '-ss'      => 30,
229         '-s'       => '320x240',
230         '-an',
231     );
232
233     $ffmpeg->exec();
234
235
236 =head1 METHODS
237
238 =head2 new('/usr/bin/ffmpeg')
239
240 Contructs FFmpeg::Command object.It takes a path of ffmpeg command.
241 You can omit this argument and this module searches ffmpeg command within PATH environment variable.
242
243 =head2 timeout()
244
245 Set command timeout.Default is 0.
246
247 =head2 input_options({ %options })
248
249 Specify input file name and input options.(Now no options are available.)
250
251 =over
252
253 =item file
254
255 a file name of input file.
256
257 =back
258
259 =head2 output_options({ %options })
260
261 Specify output file name and output options.
262
263 Avaiable options are:
264
265 =over
266
267 =item file
268
269 a file name of output file.
270
271 =item format
272
273 Output video format.
274
275 =item video_codec
276
277 Output video codec.
278
279 =item bitrate
280
281 Output video bitrate.
282
283 =item frame_size
284
285 Output video screen size.
286
287 =item audio_codec
288
289 Output audio code.
290
291 =item audio_sampling_rate
292
293 Output audio sampling rate.
294
295 =item audio_bit_rate
296
297 Output audio bit rate.
298
299 =item title
300
301 Set the title.
302
303 =item author
304
305 Set the author.
306
307 =item comment
308
309 Set the comment.
310
311 =back
312
313 =head2 input_file('/path/to/inpuf_file')
314
315 Specify input file name using with options() method.
316
317 =head2 output_file('/path/to/output_file')
318
319 Specify output file name using with options() method.
320
321 =head2 options( @options )
322
323 Specify ffmpeg command options directly.
324
325 =head2 execute()
326
327 Executes ffmpeg comman with specified options.
328
329 =head2 exec()
330
331 An alias of execute()
332
333 =head2 stdout()
334
335 Get ffmpeg command output to stdout.
336
337 =head2 stderr()
338
339 Get ffmpeg command output to stderr.
340
341 =head1 AUTHOR
342
343 Gosuke Miyashita, C<< <gosukenator at gmail.com> >>
344
345 =head1 BUGS
346
347 Please report any bugs or feature requests to
348 C<bug-ffmpeg-command at rt.cpan.org>, or through the web interface at
349 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FFmpeg-Command>.
350 I will be notified, and then you'll automatically be notified of progress on
351 your bug as I make changes.
352
353 =head1 SUPPORT
354
355 You can find documentation for this module with the perldoc command.
356
357     perldoc FFmpeg::Command
358
359 You can also look for information at:
360
361 =over 4
362
363 =item * AnnoCPAN: Annotated CPAN documentation
364
365 L<http://annocpan.org/dist/FFmpeg-Command>
366
367 =item * CPAN Ratings
368
369 L<http://cpanratings.perl.org/d/FFmpeg-Command>
370
371 =item * RT: CPAN's request tracker
372
373 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FFmpeg-Command>
374
375 =item * Search CPAN
376
377 L<http://search.cpan.org/dist/FFmpeg-Command>
378
379 =back
380
381 =head1 ACKNOWLEDGEMENTS
382
383 =head1 COPYRIGHT & LICENSE
384
385 Copyright 2006 Gosuke Miyashita, all rights reserved.
386
387 This program is free software; you can redistribute it and/or modify it
388 under the same terms as Perl itself.
389
390 =cut
391
Note: See TracBrowser for help on using the browser.