blib/lib/Video/FrameGrab.pm

filter: Statementstmt 15.9% 36 / 226 Branchbran 4.1% 3 / 72 Conditioncond 3.7% 1 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 38.4% 10 / 26 Podpod 47.0% 8 / 17 totaltotal 15.1% 58 / 382 CC 55 SCAR 40.3
CC
55
Cov
12%
cropdetect_schilli
156.0
cropdetect_average
132.0
intensity_average
56.0
CRAP 56.3
↑ Summary
1 ###########################################
2 package Video::FrameGrab;
3 ###########################################
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Sysadm::Install qw(bin_find tap slurp blurt);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use DateTime::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use DateTime::Format::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 our $VERSION = "0.07";
17  
18 ###########################################
19 3 ▶sub new {
Subroutine new: called CC=5 SCAR=18.7
Pod undocumented
20 ###########################################
21 3 my($class, %options) = @_;
22  
23 3 my $self = {
24 mplayer => undef,
25 tmpdir => tempdir(CLEANUP => 1),
26 meta => undef,
27 video => undef,
28 aspects => ['16:9', '4:3'],
29 test_dont_snap => 0,
30 %options,
31 };
32  
33 3 ▶ if(! defined $self->{video}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"video"}) 0 3 3
34 0 LOGDIE "Parameter missing: video";
35 }
36  
37 3 ▶ if(! defined $self->{mplayer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"mplayer"}) 3 0 3
38 3 $self->{mplayer} = bin_find("mplayer"),
39 }
40  
41 3 ▶ if(!defined $self->{mplayer} or ! -x $self->{mplayer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"mplayer"} and -x $self->{"mplayer"}) 3 0 3
Condition33%1/3
defined $self->{"mplayer"} and -x $self->{"mplayer"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $self->{"mplayer"} and -x $self->{"mplayer"}
defined $self->{"mplayer"} -x $self->{"mplayer"}
Truth table33%1/3
A and B
A= defined $self->{"mplayer"}B= -x $self->{"mplayer"}
ABresult
0X0
100
111
42 3 LOGDIE "Fatal error: Can't find mplayer";
43 }
44  
45 0 bless $self, $class;
46 }
47  
48 ###########################################
49 0 ▶sub snap {
Subroutine snap: not called CC=1 SCAR=6.9
Pod documented
50 ###########################################
51 0 goto &frame_grab;
52 }
53  
54 ###########################################
55 0 ▶sub frame_grab {
Subroutine frame_grab: not called CC=5 SCAR=34.0
Pod undocumented
56 ###########################################
57 0 my($self, $time) = @_;
58  
59 0 ▶ if($self->{test_dont_snap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"test_dont_snap"}) 0 0 0
60 0 INFO "Test mode, no snap";
61 0 return $self->{jpeg_data};
62 }
63  
64 0 my $tmpdir = $self->{tmpdir};
65  
66 0 for (<$tmpdir/*>) {
67 0 unlink $_;
68 }
69  
70 my($stdout, $stderr, $rc) =
71 tap $self->{mplayer}, qw(-frames 1 -ss), $time,
72 "-vo", "jpeg:maxfiles=1:outdir=$self->{tmpdir}",
73 "-ao", "null",
74 0 $self->{video};
75  
76 0 ▶ if($rc != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != 0) 0 0 0
77 0 ERROR "$stderr";
78 0 return undef;
79 }
80  
81 0 my $frame = "$self->{tmpdir}/00000001.jpg";
82  
83 0 ▶ if(! -f $frame) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $frame) 0 0 0
84 0 ERROR "$stderr";
85 0 return undef;
86 }
87  
88 0 $self->{jpeg} = slurp("$self->{tmpdir}/00000001.jpg");
89 return $self->{jpeg}
90 0 }
91  
92 ###########################################
93 0 ▶sub cropdetect {
Subroutine cropdetect: not called CC=4 SCAR=30.0
Pod documented
94 ###########################################
95 0 my($self, $time, $opts) = @_;
96  
97 0 ▶ if(!defined $time) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 0 0 0
98 0 LOGDIE "Missing parameter: time";
99 }
100  
101 0 ▶ $opts = {} unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 0 0 0
102 0 ▶ $opts->{algorithm} = "schilli" unless exists $opts->{algorithm};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"algorithm"} 0 0 0
103  
104 0 my $algo = $opts->{algorithm};
105  
106 0 my $method = "cropdetect_$algo";
107  
108 0 return $self->$method( $time, $opts );
109 }
110  
111 ###########################################
112 0 ▶sub cropdetect_schilli {
Subroutine cropdetect_schilli: not called CC=12 SCAR=50.5
Pod undocumented
113 ###########################################
114 0 my($self, $time, $opts) = @_;
115  
116 0 ▶ $opts = {} unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 0 0 0
117  
118 $opts->{min_intensity_average} = 20 unless
119 0 ▶ exists $opts->{min_intensity_average};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"min_intensity_average"} 0 0 0
120  
121 $opts->{gaussian_blur_radius} = 3 unless
122 0 ▶ exists $opts->{gaussian_blur_radius};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"gaussian_blur_radius"} 0 0 0
123  
124 0 my $img;
125  
126 0 ▶ if(exists $opts->{image}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{'image'}) { } 0 0 0
127 0 $img = $opts->{image};
128 } else {
129 0 my $data = $self->snap( $time );
130 0 $img = Imager->new();
131 0 my $rc = $img->read( data => $data );
132 0 ▶ die $img->errstr() unless $rc;
Branch: 1 branch
BranchTrueFalseTotal
unless $rc 0 0 0
133 }
134  
135 $img->filter( type => "gaussian",
136 0 stddev => $opts->{gaussian_blur_radius} );
137  
138 0 my $width = $img->getwidth();
139 0 my $height = $img->getheight();
140  
141 0 my $borders = { left => 0, right => 0, lower => 0, upper => 0 };
142  
143 0 for my $traverse (
144 ["upper", 0, 0, 1, 0, 0, 1],
145 ["left", 0, 0, 0, 1, 1, 0],
146 ["right", $width-1, 0, 0, 1, -1, 0],
147 ["lower", 0, $height-1, 1, 0, 0, -1],
148 ) {
149  
150 0 my($trav_name, $x, $y, $dx, $dy, $mdx, $mdy) = @$traverse;
151 0 my $border_width = 0;
152  
153 0 ▶ while($x < $width and $x >= 0 and $y >= 0 and $y < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $width and $x >= 0 and $y >= 0 and $y < $height) 0 0 0
Condition0%0/9
$x < $width and $x >= 0 and $y >= 0 and $y < $height
!ll&&!rl&&r
000
$x < $width and $x >= 0 and $y >= 0
!ll&&!rl&&r
000
$x < $width and $x >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
$x < $width and $x >= 0 and $y >= 0 and $y < $height
$x < $width $x >= 0 $y >= 0 $y < $height
Truth table0%0/5
A and B and C and D
A= $x < $widthB= $x >= 0C= $y >= 0D= $y < $height
ABCDresult
0XXX0
10XX0
110X0
11100
11111
154 0 my $avg = $self->intensity_average(
155 $img, $width, $height, $x, $y, $dx, $dy );
156  
157 0 TRACE "Intensity[$trav_name,$x,$y]: $avg";
158  
159 0 ▶ if($avg < $opts->{min_intensity_average}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($avg < $opts->{"min_intensity_average"}) 0 0 0
160 0 $border_width++;
161 0 $x += $mdx;
162 0 $y += $mdy;
163 0 next;
164 }
165  
166 0 last;
167 }
168  
169 0 DEBUG "Border[$trav_name]: $border_width";
170 0 $borders->{$trav_name} = $border_width;
171 }
172  
173 0 my $cw = $width - $borders->{left} - $borders->{right};
174 0 my $ch = $height - $borders->{upper} - $borders->{lower};
175 0 my $cx = $borders->{left};
176 0 my $cy = $borders->{upper};
177  
178 0 DEBUG "Crop detect: $cw, $ch, $cx, $cy";
179 0 return ($cw, $ch, $cx, $cy);
180 }
181  
182 ###########################################
183 0 ▶sub intensity_average {
Subroutine intensity_average: not called CC=7 SCAR=40.3
Pod undocumented
184 ###########################################
185 0 my($self, $img, $width, $height, $x, $y, $dx, $dy) = @_;
186  
187 0 DEBUG "intensity_average: $width, $height, $x, $y, $dx, $dy";
188  
189 0 my $intensity = 0;
190 0 my $data_points = 0;
191  
192 0 ▶ while($x < $width and $x >= 0 and $y >= 0 and $y < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $width and $x >= 0 and $y >= 0 and $y < $height) 0 0 0
Condition0%0/9
$x < $width and $x >= 0 and $y >= 0 and $y < $height
!ll&&!rl&&r
000
$x < $width and $x >= 0 and $y >= 0
!ll&&!rl&&r
000
$x < $width and $x >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
$x < $width and $x >= 0 and $y >= 0 and $y < $height
$x < $width $x >= 0 $y >= 0 $y < $height
Truth table0%0/5
A and B and C and D
A= $x < $widthB= $x >= 0C= $y >= 0D= $y < $height
ABCDresult
0XXX0
10XX0
110X0
11100
11111
193 0 my $color = $img->getpixel( x => $x, y => $y );
194  
195 0 ▶ if(! defined $color) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $color) 0 0 0
196 0 LOGDIE "Failed to obtain pixel $x/$y";
197 }
198  
199 0 my @comps = $color->rgba();
200  
201 0 $intensity += ($comps[0] + $comps[1] + $comps[2]) / 3.0;
202 0 $data_points++;
203  
204 0 $x += $dx;
205 0 $y += $dy;
206 }
207  
208 0 ▶ return 0 if $data_points == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $data_points == 0 0 0 0
209  
210 0 return int(1.0*$intensity/$data_points);
211 }
212  
213 ###########################################
214 0 ▶sub cropdetect_mplayer {
Subroutine cropdetect_mplayer: not called CC=3 SCAR=24.8
Pod undocumented
215 ###########################################
216 0 my($self, $time) = @_;
217  
218 my($stdout, $stderr, $rc) =
219 tap $self->{mplayer}, qw(-vf cropdetect -ss), $time,
220 "-frames", 10,
221 "-vo", "null",
222 "-ao", "null",
223 0 $self->{video};
224  
225 0 ▶ if(defined $stdout and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stdout and $stdout =~ /-vf crop=(\d+):(\d+):(\d+):(\d+)/) 0 0 0
Condition0%0/3
defined $stdout and $stdout =~ /-vf crop=(\d+):(\d+):(\d+):(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $stdout and $stdout =~ /-vf crop=(\d+):(\d+):(\d+):(\d+)/
defined $stdout $stdout =~ /-vf crop=(\d+):(\d+):(\d+):(\d+)/
Truth table0%0/3
A and B
A= defined $stdoutB= $stdout =~ /-vf crop=(\d+):(\d+):(\d+):(\d+)/
ABresult
0X0
100
111
226 $stdout =~ /-vf crop=(\d+):(\d+):(\d+):(\d+)/) {
227 0 DEBUG "Suggested crop: $1, $2, $3, $4";
228 0 return ($1, $2, $3, $4);
229 }
230  
231 0 ERROR "$stderr";
232  
233 0 return undef;
234 }
235  
236 ###########################################
237 0 ▶sub cropdetect_average {
Subroutine cropdetect_average: not called CC=11 SCAR=48.8
Pod documented
238 ###########################################
239 0 my($self, $nof_probes, $opts) = @_;
240  
241 0 ▶ $opts = {} unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 0 0 0
242  
243 0 $self->result_clear();
244  
245 0 my @images = ();
246  
247 0 ▶ if(exists $opts->{images}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{'images'}) { } 0 0 0
248 0 for my $img (@{ $opts->{images} }) {
249 0 push @images, $img;
250 }
251 } else {
252 0 for my $probe (
253 $self->equidistant_snap_times( $nof_probes,
254 $opts ) ) {
255  
256 0 my $data = $self->snap( $probe );
257 0 my $img = Imager->new();
258 0 my $rc = $img->read( data => $data );
259 0 ▶ if(! $rc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc) 0 0 0
260 0 LOGWARN "Reading snapshop at time $probe failed ($!)";
261 0 next;
262 }
263 0 push @images, $img;
264 }
265 }
266  
267 # average all snapshots to obtain a single overlay image
268 0 my $overlay;
269  
270 0 my $i = 1;
271  
272 0 for my $img (@images) {
273 0 ▶ $img->filter(type=>"gaussian", stddev=>10)
Branch: 1 branch
BranchTrueFalseTotal
$img->filter("type", "gaussian", "stddev", 10) or die $overlay->errstr 0 0 0
274 or die $overlay->errstr;
275  
276 0 ▶ if(! defined $overlay) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $overlay) 0 0 0
277 0 $overlay = $img;
278 0 next;
279 }
280 0 $overlay->compose( src => $img, combine => 'add' );
281 0 ▶ $overlay->filter(type=>"postlevels", levels=>3) or
Branch: 1 branch
BranchTrueFalseTotal
$overlay->filter("type", "postlevels", "levels", 3) or die $overlay->errstr 0 0 0
282 die $overlay->errstr;
283  
284 0 ▶ if(get_logger()->is_trace()) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_logger()->is_trace) 0 0 0
285 0 $overlay->write(file => "i-$i.jpg");
286 }
287 0 $i++;
288 }
289  
290 0 my @params = $self->cropdetect( 0, { image => $overlay } );
291  
292 0 return @params;
293  
294 # my @params = $self->cropdetect( $probe, $opts );
295 # if(! defined $params[0] ) {
296 # ERROR "cropdetect returned an error";
297 # next;
298 # }
299 # DEBUG "Cropdetect at $probe yielded (@params)";
300 # $self->result_push( @params );
301 # }
302 #
303 # my @result = $self->result_majority_decision();
304 # DEBUG "Majority decision: (@result)";
305 # return @result;
306  
307 }
308  
309 ###########################################
310 0 ▶sub result_clear {
Subroutine result_clear: not called CC=1 SCAR=6.9
Pod undocumented
311 ###########################################
312 0 my($self) = @_;
313  
314 0 $self->{result} = [];
315 }
316  
317 ###########################################
318 0 ▶sub result_push {
Subroutine result_push: not called CC=2 SCAR=17.9
Pod undocumented
319 ###########################################
320 0 my($self, @result) = @_;
321  
322 0 for(0..$#result) {
323 0 $self->{result}->[$_]->{ $result[$_] }++;
324 }
325 }
326  
327 ###########################################
328 0 ▶sub result_majority_decision {
Subroutine result_majority_decision: not called CC=2 SCAR=17.9
Pod undocumented
329 ###########################################
330 0 my($self) = @_;
331  
332 0 my @result = ();
333  
334 0 for my $sample (@{ $self->{result} }) {
335 0 my($majority) = sort { $sample->{$b} <=> $sample->{$a} } keys %$sample;
336 0 push @result, $majority;
337 }
338  
339 0 return @result;
340 }
341  
342 ###########################################
343 0 ▶sub jpeg_data {
Subroutine jpeg_data: not called CC=1 SCAR=6.9
Pod undocumented
344 ###########################################
345 0 my($self) = @_;
346 0 return $self->{jpeg};
347 }
348  
349 ###########################################
350 0 ▶sub jpeg_save {
Subroutine jpeg_save: not called CC=1 SCAR=6.9
Pod documented
351 ###########################################
352 0 my($self, $file) = @_;
353  
354 0 blurt $self->{jpeg}, $file;
355 }
356  
357 ###########################################
358 0 ▶sub meta_data {
Subroutine meta_data: not called CC=3 SCAR=24.8
Pod documented
359 ###########################################
360 0 my($self) = @_;
361  
362 my($stdout, $stderr, $rc) =
363 tap $self->{mplayer},
364 qw(-vo null -ao null -frames 0 -identify),
365 0 $self->{video};
366  
367 0 ▶ if($rc != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != 0) 0 0 0
368 0 ERROR "$stderr";
369 0 return undef;
370 }
371  
372 0 $self->{meta} = {};
373  
374 0 ▶ while($stdout =~ /^ID_(.*?)=(.*)/mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout =~ /^ID_(.*?)=(.*)/gm) 0 0 0
375 0 $self->{meta}->{ lc($1) } = $2;
376 }
377  
378 0 return $self->{meta};
379 }
380  
381 ###########################################
382 0 ▶sub equidistant_snap_times {
Subroutine equidistant_snap_times: not called CC=6 SCAR=37.4
Pod documented
383 ###########################################
384 0 my($self, $nof_snaps, $opts) = @_;
385  
386 0 ▶ $opts = {} unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 0 0 0
387  
388 0 ▶ if(! defined $nof_snaps) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nof_snaps) 0 0 0
389 0 LOGDIE "Parameter missing: nof_snaps";
390 }
391  
392 0 my @stamps = ();
393  
394 0 ▶ if(!defined $self->{meta}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"meta"}) 0 0 0
395 0 $self->meta_data();
396 }
397  
398 0 my $length = $self->{meta}->{length};
399 0 ▶ $length = $opts->{movie_length} if defined $opts->{movie_length};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"movie_length"} 0 0 0
400  
401 0 my $interval = $length / ($nof_snaps + 1.0);
402 0 my $interval_seconds = int( $interval );
403  
404 0 my $dur = DateTime::Duration->new(seconds => $interval_seconds);
405 0 my $point = DateTime::Duration->new(seconds => 0);
406  
407 0 my $format = DateTime::Format::Duration->new(pattern => "%r");
408 0 $format->set_normalizing( "ISO" );
409  
410 0 for my $snap_no (1 .. $nof_snaps) {
411 0 $point->add_duration( $dur );
412  
413 0 my $stamp = $format->format_duration( $point );
414 0 push @stamps, $stamp;
415 }
416  
417 0 return @stamps;
418 }
419  
420 ###########################################
421 0 ▶sub dimensions {
Subroutine dimensions: not called CC=2 SCAR=17.9
Pod documented
422 ###########################################
423 0 my($self) = @_;
424  
425 0 ▶ if(defined $self->{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"width"}) 0 0 0
426 0 return ($self->{width}, $self->{height});
427 }
428  
429 0 my $time = "00:00:01";
430  
431 0 my $data = $self->frame_grab( $time );
432 0 my $img = Imager->new();
433 0 my $rc = $img->read( data => $data );
434  
435 0 my $width = $img->getwidth();
436 0 my $height = $img->getheight();
437  
438 0 $self->{width} = $width;
439 0 $self->{height} = $height;
440  
441 0 return($width, $height);
442 }
443  
444 ###########################################
445 0 ▶sub aspect_ratio_guess {
Subroutine aspect_ratio_guess: not called CC=5 SCAR=34.0
Pod documented
446 ###########################################
447 0 my($self, $formats) = @_;
448  
449 0 ▶ if(! defined $formats) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $formats) 0 0 0
450 0 $formats = $self->{aspects};
451 }
452  
453 0 my($width, $height) = $self->dimensions();
454  
455 0 ▶ if(!$width or !$height) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($width and $height) 0 0 0
Condition0%0/3
$width and $height
!ll&&!rl&&r
000
MC/DC0%0/2
$width and $height
$width $height
Truth table0%0/3
A and B
A= $widthB= $height
ABresult
0X0
100
111
456 0 ERROR "Can't get image dimensions data for width/height";
457 0 return undef;
458 }
459  
460 0 my %matches = ();
461  
462 0 for my $format (@$formats) {
463 0 my($fw, $fh) = split /:/, $format;
464 0 my $factor = 1.0*$width/$fw;
465 0 my $fhguess = 1.0*$height/$factor;
466  
467 0 my $deviate = abs($fh-$fhguess)/$fh*100.0;
468 0 INFO "$format deviates from $width:$height ",
469 sprintf("%.2f", $deviate), "%";
470
471 0 $matches{ $format } = $deviate;
472 }
473  
474 0 return (sort { $matches{$a} <=> $matches{$b} } keys %matches)[0];
475 }
476  
477 3 1;
478  
479 __END__
↑ Summary