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