| 1 |
package FFmpeg::Command; |
|---|
| 2 |
|
|---|
| 3 |
use warnings; |
|---|
| 4 |
use strict; |
|---|
| 5 |
our $VERSION = '0.01'; |
|---|
| 6 |
|
|---|
| 7 |
use base qw/Class::Accessor::Fast/; |
|---|
| 8 |
__PACKAGE__->mk_accessors( qw(input_file output_file ffmpeg options) ); |
|---|
| 9 |
|
|---|
| 10 |
my %option = ( |
|---|
| 11 |
format => '-f', |
|---|
| 12 |
video_codec => '-vcodec', |
|---|
| 13 |
bitrate => '-b', |
|---|
| 14 |
size => '-s', |
|---|
| 15 |
audio_codec => '-acodec', |
|---|
| 16 |
audio_sampling_rate => '-ar', |
|---|
| 17 |
audio_bit_rate => '-ab', |
|---|
| 18 |
); |
|---|
| 19 |
|
|---|
| 20 |
sub new { |
|---|
| 21 |
my $class = shift; |
|---|
| 22 |
my $self = { |
|---|
| 23 |
ffmpeg => shift || 'ffmpeg', |
|---|
| 24 |
options => [], |
|---|
| 25 |
}; |
|---|
| 26 |
bless $self, $class; |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
sub input_options { |
|---|
| 30 |
my ( $self, $args ) = @_; |
|---|
| 31 |
$self->input_file($args->{file}); |
|---|
| 32 |
return; |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
sub output_options { |
|---|
| 36 |
my ( $self, $args ) = @_; |
|---|
| 37 |
$self->output_file(delete $args->{file}); |
|---|
| 38 |
|
|---|
| 39 |
for ( keys %$args ){ |
|---|
| 40 |
push @{ $self->options }, $option{$_}, $args->{$_}; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
return; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
sub execute { |
|---|
| 47 |
my $self = shift; |
|---|
| 48 |
system $self->ffmpeg, '-i', $self->input_file, @{ $self->options }, $self->output_file; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
*exec = \&execute; |
|---|
| 52 |
|
|---|
| 53 |
1; |
|---|
| 54 |
__END__ |
|---|
| 55 |
|
|---|
| 56 |
=head1 NAME |
|---|
| 57 |
|
|---|
| 58 |
FFmpeg::Command - A wrapper class for ffmpeg command line utility. |
|---|
| 59 |
|
|---|
| 60 |
=head1 DESCRIPTION |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
=head1 SYNOPSIS |
|---|
| 65 |
|
|---|
| 66 |
use FFmpeg::Command; |
|---|
| 67 |
|
|---|
| 68 |
my $ffmpeg = FFmpeg::Command->new('/usr/local/bin/ffmpeg'); |
|---|
| 69 |
|
|---|
| 70 |
# Converting a video file into another video format. |
|---|
| 71 |
$ffmpeg->input_options({ |
|---|
| 72 |
file => $input_file, |
|---|
| 73 |
}); |
|---|
| 74 |
|
|---|
| 75 |
$ffmpeg->output_options({ |
|---|
| 76 |
file => $output_file, |
|---|
| 77 |
format => 'mp4', |
|---|
| 78 |
video_codec => 'h264', |
|---|
| 79 |
bitrate => '640', |
|---|
| 80 |
size => '320x240', |
|---|
| 81 |
audio_codec => 'aac', |
|---|
| 82 |
audio_sampling_rate => '44100', |
|---|
| 83 |
audio_bit_rate => '128', |
|---|
| 84 |
}); |
|---|
| 85 |
|
|---|
| 86 |
$ffmpeg->exec(); |
|---|
| 87 |
|
|---|
| 88 |
# Executing ffmpeg with any options you like. |
|---|
| 89 |
# This sample code takes a screnn shot. |
|---|
| 90 |
$ffmpeg->input_file($input_file); |
|---|
| 91 |
$ffmpeg->output_file($output_file); |
|---|
| 92 |
|
|---|
| 93 |
$ffmpeg->options( |
|---|
| 94 |
'-y', |
|---|
| 95 |
'-f' => 'image2', |
|---|
| 96 |
'-pix_fmt' => 'jpg', |
|---|
| 97 |
'-vframes' => 1, |
|---|
| 98 |
'-ss' => 30, |
|---|
| 99 |
'-s' => '320x240', |
|---|
| 100 |
'-an', |
|---|
| 101 |
); |
|---|
| 102 |
|
|---|
| 103 |
$ffmeg->exec(); |
|---|
| 104 |
|
|---|
| 105 |
=head1 METHODS |
|---|
| 106 |
|
|---|
| 107 |
=head2 new |
|---|
| 108 |
|
|---|
| 109 |
=head2 input_file |
|---|
| 110 |
|
|---|
| 111 |
=head2 output_file |
|---|
| 112 |
|
|---|
| 113 |
=head2 options |
|---|
| 114 |
|
|---|
| 115 |
=head2 input_options |
|---|
| 116 |
|
|---|
| 117 |
=head2 output_options |
|---|
| 118 |
|
|---|
| 119 |
=head2 execute |
|---|
| 120 |
|
|---|
| 121 |
=head2 exec |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
=head1 AUTHOR |
|---|
| 125 |
|
|---|
| 126 |
Gosuke Miyashita, C<< <gosukenator at gmail.com> >> |
|---|
| 127 |
|
|---|
| 128 |
=head1 BUGS |
|---|
| 129 |
|
|---|
| 130 |
Please report any bugs or feature requests to |
|---|
| 131 |
C<bug-ffmpeg-command at rt.cpan.org>, or through the web interface at |
|---|
| 132 |
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FFmpeg-Command>. |
|---|
| 133 |
I will be notified, and then you'll automatically be notified of progress on |
|---|
| 134 |
your bug as I make changes. |
|---|
| 135 |
|
|---|
| 136 |
=head1 SUPPORT |
|---|
| 137 |
|
|---|
| 138 |
You can find documentation for this module with the perldoc command. |
|---|
| 139 |
|
|---|
| 140 |
perldoc FFmpeg::Command |
|---|
| 141 |
|
|---|
| 142 |
You can also look for information at: |
|---|
| 143 |
|
|---|
| 144 |
=over 4 |
|---|
| 145 |
|
|---|
| 146 |
=item * AnnoCPAN: Annotated CPAN documentation |
|---|
| 147 |
|
|---|
| 148 |
L<http://annocpan.org/dist/FFmpeg-Command> |
|---|
| 149 |
|
|---|
| 150 |
=item * CPAN Ratings |
|---|
| 151 |
|
|---|
| 152 |
L<http://cpanratings.perl.org/d/FFmpeg-Command> |
|---|
| 153 |
|
|---|
| 154 |
=item * RT: CPAN's request tracker |
|---|
| 155 |
|
|---|
| 156 |
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FFmpeg-Command> |
|---|
| 157 |
|
|---|
| 158 |
=item * Search CPAN |
|---|
| 159 |
|
|---|
| 160 |
L<http://search.cpan.org/dist/FFmpeg-Command> |
|---|
| 161 |
|
|---|
| 162 |
=back |
|---|
| 163 |
|
|---|
| 164 |
=head1 ACKNOWLEDGEMENTS |
|---|
| 165 |
|
|---|
| 166 |
=head1 COPYRIGHT & LICENSE |
|---|
| 167 |
|
|---|
| 168 |
Copyright 2006 Gosuke Miyashita, all rights reserved. |
|---|
| 169 |
|
|---|
| 170 |
This program is free software; you can redistribute it and/or modify it |
|---|
| 171 |
under the same terms as Perl itself. |
|---|
| 172 |
|
|---|
| 173 |
=cut |
|---|
| 174 |
|
|---|