| 1 | package FFmpeg::Command; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 8 | our $VERSION = '0.19'; | |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use base qw( Class::Accessor::Fast Class::ErrorHandler ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 8 | __PACKAGE__->mk_accessors(qw/ | |||||||||||||||||||||||||||||||
| 9 | input_file | ||||||||||||||||||||||||||||||||
| 10 | output_file | ||||||||||||||||||||||||||||||||
| 11 | ffmpeg | ||||||||||||||||||||||||||||||||
| 12 | options | ||||||||||||||||||||||||||||||||
| 13 | global_options | ||||||||||||||||||||||||||||||||
| 14 | infile_options | ||||||||||||||||||||||||||||||||
| 15 | outfile_options | ||||||||||||||||||||||||||||||||
| 16 | timeout | ||||||||||||||||||||||||||||||||
| 17 | stdin | ||||||||||||||||||||||||||||||||
| 18 | stdout | ||||||||||||||||||||||||||||||||
| 19 | stderr | ||||||||||||||||||||||||||||||||
| 20 | command | ||||||||||||||||||||||||||||||||
| 21 | /); | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 8 | ▶ | use IPC::Run qw( start ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 8 | ▶ | use Carp qw( carp ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 8 | our %option = ( | |||||||||||||||||||||||||||||||
| 27 | format => '-f', | ||||||||||||||||||||||||||||||||
| 28 | video_codec => '-vcodec', | ||||||||||||||||||||||||||||||||
| 29 | bitrate => '-b', | ||||||||||||||||||||||||||||||||
| 30 | frame_rate => '-r', | ||||||||||||||||||||||||||||||||
| 31 | frame_size => '-s', | ||||||||||||||||||||||||||||||||
| 32 | audio_codec => '-acodec', | ||||||||||||||||||||||||||||||||
| 33 | audio_sampling_rate => '-ar', | ||||||||||||||||||||||||||||||||
| 34 | audio_bit_rate => '-ab', | ||||||||||||||||||||||||||||||||
| 35 | size => '-s', | ||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 8 | our %metadata = ( | |||||||||||||||||||||||||||||||
| 39 | title => 'title=', | ||||||||||||||||||||||||||||||||
| 40 | author => 'author=', | ||||||||||||||||||||||||||||||||
| 41 | comment => 'comment=', | ||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=21.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | 7 | my $class = shift; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 7 | my $command; | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 7 | ▶ | for my $cmd ( shift || 'ffmpeg', 'avconv' ) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'ffmpeg'
MC/DC0%0/1
shift() || 'ffmpeg'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 50 | 14 | system("$cmd -version > /dev/null 2>&1"); | |||||||||||||||||||||||||||||||
| 51 | 14 | my $ret = $? >> 8; | |||||||||||||||||||||||||||||||
| 52 | 14 | ▶ | if ( $ret == 0 || $ret == 1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ret == 0 or $ret == 1
MC/DC0%0/2
$ret == 0 or $ret == 1
$ret == 0
$ret == 1
Truth table33%1/3
A or B
A= $ret == 0B= $ret == 1
| |||||||||||||||||||||||||||||||||
| 53 | 0 | $command = $cmd; | |||||||||||||||||||||||||||||||
| 54 | 0 | last; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | 7 | ▶ | unless ( $command ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 7 | carp "Can't find ffmpeg/avconv command."; | |||||||||||||||||||||||||||||||
| 59 | 7 | exit 0; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 0 | my $self = { | |||||||||||||||||||||||||||||||
| 63 | ffmpeg => $command, | ||||||||||||||||||||||||||||||||
| 64 | options => [], | ||||||||||||||||||||||||||||||||
| 65 | global_options => [], | ||||||||||||||||||||||||||||||||
| 66 | infile_options => [], | ||||||||||||||||||||||||||||||||
| 67 | outfile_options => [], | ||||||||||||||||||||||||||||||||
| 68 | input_file => [], | ||||||||||||||||||||||||||||||||
| 69 | output_file => '', | ||||||||||||||||||||||||||||||||
| 70 | timeout => 0, | ||||||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub input_options { | ||||||||||||||||||||||||||||||
|
Subroutine
input_options: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 77 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $self->input_file($args->{file} || $args->{'files'}); | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$args->{'file'} || $args->{'files'}
MC/DC0%0/2
$args->{'file'} || $args->{'files'}
$args->{'file'}
$args->{'files'}
Truth table0%0/3
A || B
A= $args->{'file'}B= $args->{'files'}
| |||||||||||||||||||||||||||||||||
| 79 | 0 | return; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub output_options { | ||||||||||||||||||||||||||||||
|
Subroutine
output_options: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 83 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||||||
| 84 | 0 | $self->output_file(delete $args->{file}); | |||||||||||||||||||||||||||||||
| 85 | 0 | my $device = delete $args->{device}; | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 0 | my %device_option = ( | |||||||||||||||||||||||||||||||
| 88 | ipod => { | ||||||||||||||||||||||||||||||||
| 89 | format => 'mp4', | ||||||||||||||||||||||||||||||||
| 90 | video_codec => 'mpeg4', | ||||||||||||||||||||||||||||||||
| 91 | bitrate => 600, | ||||||||||||||||||||||||||||||||
| 92 | frame_size => '320x240', | ||||||||||||||||||||||||||||||||
| 93 | audio_codec => 'libfaac', | ||||||||||||||||||||||||||||||||
| 94 | audio_sampling_rate => 48000, | ||||||||||||||||||||||||||||||||
| 95 | audio_bit_rate => 64, | ||||||||||||||||||||||||||||||||
| 96 | }, | ||||||||||||||||||||||||||||||||
| 97 | psp => { | ||||||||||||||||||||||||||||||||
| 98 | format => 'psp', | ||||||||||||||||||||||||||||||||
| 99 | video_codec => 'mpeg4', | ||||||||||||||||||||||||||||||||
| 100 | bitrate => 600, | ||||||||||||||||||||||||||||||||
| 101 | frame_size => '320x240', | ||||||||||||||||||||||||||||||||
| 102 | audio_codec => 'libfaac', | ||||||||||||||||||||||||||||||||
| 103 | audio_sampling_rate => 48000, | ||||||||||||||||||||||||||||||||
| 104 | audio_bit_rate => 64, | ||||||||||||||||||||||||||||||||
| 105 | }, | ||||||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 0 | my %output_option = ( | |||||||||||||||||||||||||||||||
| 109 | %$args, | ||||||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | # if a device name was supplied, add its output options | ||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if( $device ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | %output_option = ( | ||||||||||||||||||||||||||||||||
| 115 | %output_option, | ||||||||||||||||||||||||||||||||
| 116 | 0 | %{ $device_option{$device} }, | |||||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 0 | for ( keys %output_option ){ | |||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if( defined $option{$_} and defined $output_option{$_} ){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $FFmpeg::Command::option{$_} and defined $output_option{$_}
MC/DC0%0/2
defined $FFmpeg::Command::option{$_} and defined $output_option{$_}
defined $FFmpeg::Command::option{$_}
defined $output_option{$_}
Truth table0%0/3
A and B
A= defined $FFmpeg::Command::option{$_}B= defined $output_option{$_}
| |||||||||||||||||||||||||||||||||
| 122 | 0 | push @{ $self->outfile_options }, $option{$_}, $output_option{$_}; | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | ▶ | elsif( defined $metadata{$_} and defined $output_option{$_} ){ | |||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $FFmpeg::Command::metadata{$_} and defined $output_option{$_}
MC/DC0%0/2
defined $FFmpeg::Command::metadata{$_} and defined $output_option{$_}
defined $FFmpeg::Command::metadata{$_}
defined $output_option{$_}
Truth table0%0/3
A and B
A= defined $FFmpeg::Command::metadata{$_}B= defined $output_option{$_}
| |||||||||||||||||||||||||||||||||
| 125 | 0 | push @{ $self->outfile_options }, '-metadata', $metadata{$_} . $output_option{$_}; | |||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||||||
| 128 | 0 | carp "$_ is not defined and ignored."; | |||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 0 | return; | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub execute { | ||||||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 136 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my @opts = map { $self->{$_} ? $self->{$_} : \$self->{$_} } qw/stdin stdout stderr/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | push @opts, IPC::Run::timeout($self->timeout) if $self->timeout; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 0 | my $cmd = $self->_compose_command; | |||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | # store the command line so we can debug it | ||||||||||||||||||||||||||||||||
| 144 | 0 | $self->command( join( ' ', @$cmd ) ); | |||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 0 | my $h = eval { | |||||||||||||||||||||||||||||||
| 147 | 0 | start( $cmd, @opts ) | |||||||||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | if( $@ ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | 0 | $self->error($@); | |||||||||||||||||||||||||||||||
| 152 | 0 | return; | |||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | finish $h or do { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 156 | 0 | $self->error($self->stderr); | |||||||||||||||||||||||||||||||
| 157 | 0 | return; | |||||||||||||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 8 | *exec = \&execute; | |||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub _compose_command { | ||||||||||||||||||||||||||||||
|
Subroutine
_compose_command: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 167 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 0 | my $files = $self->input_file; | |||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | $files = [ $files ] unless ref $files eq 'ARRAY'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | my $cmd = [ | ||||||||||||||||||||||||||||||||
| 173 | $self->ffmpeg, | ||||||||||||||||||||||||||||||||
| 174 | '-y', | ||||||||||||||||||||||||||||||||
| 175 | 0 | @{ $self->global_options }, | |||||||||||||||||||||||||||||||
| 176 | 0 | map ( { @{ $self->infile_options }, ( '-i', $_ ) } @$files ), | |||||||||||||||||||||||||||||||
| 177 | 0 | @{ $self->options }, | |||||||||||||||||||||||||||||||
| 178 | 0 | @{ $self->outfile_options }, | |||||||||||||||||||||||||||||||
| 179 | ]; | ||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | # add output file only if we have one | ||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | push @$cmd, $self->output_file | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | if $self->output_file; | ||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | 0 | return $cmd; | |||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | __END__ | ||||||||||||||||||||||||||||||||