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

Revision 431, 7.6 kB (checked in by miya, 2 years ago)

FFmpeg-Command-0.08.patch from Will Hawes.

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