blib/lib/Video/Generator.pm

filter: Statementstmt 76.7% 66 / 86 Branchbran 20.0% 4 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 100% 2 / 2 totaltotal 69.9% 86 / 123 SCAR 27.6
CC
11
Cov
66%
create
56.0
new
5.0
BEGIN
1.0
CRAP 15.7
↑ Summary
1 package Video::Generator;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use FFmpeg::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Path qw(rmtree);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use IO::CaptureOutput qw(capture_exec);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Image::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Video::Delay::Const;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Video::Pattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Constants.
19 3 Readonly::Scalar our $SPACE => q{ };
20  
21 3 our $VERSION = 0.09;
22  
23 # Constructor.
24 sub new {
25 9 ▶ my ($class, @params) = @_;
Subroutine new: called CC=5 SCAR=16.2
Pod documented
26  
27 # Create object.
28 9 my $self = bless {}, $class;
29  
30 # Delay generator.
31 9 $self->{'delay_generator'} = undef;
32  
33 # Duration.
34 9 $self->{'duration'} = 10000;
35  
36 # Frames per second.
37 9 $self->{'fps'} = 60;
38  
39 # FFmpeg pixel format.
40 9 $self->{'ffmpeg_pixel_format'} = undef;
41  
42 # FFmpeg video codec.
43 9 $self->{'ffmpeg_video_codec'} = undef;
44  
45 # Image generator.
46 9 $self->{'image_generator'} = undef;
47  
48 # Image type.
49 9 $self->{'image_type'} = 'bmp';
50  
51 # Temporary dir.
52 9 $self->{'temp_dir'} = undef;
53  
54 # Verbose mode.
55 9 $self->{'verbose'} = 0;
56  
57 # Video pattern generator.
58 9 $self->{'video_pattern'} = undef;
59  
60 # Sizes.
61 9 $self->{'height'} = 1080;
62 9 $self->{'width'} = 1920;
63  
64 # Process params.
65 9 set_params($self, @params);
66  
67 # Temporary directory.
68 7 ▶ if (! defined $self->{'temp_dir'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"temp_dir"}) 7 0 7
69 7 $self->{'temp_dir'} = tempdir();
70 }
71  
72 # Image generator.
73 7 ▶ if (! defined $self->{'image_generator'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"image_generator"}) 7 0 7
74 $self->{'image_generator'} = Image::Random->new(
75 'height' => $self->{'height'},
76 'type' => $self->{'image_type'},
77 7 'width' => $self->{'width'},
78 );
79 }
80  
81 # Delay generator.
82 6 ▶ if (! defined $self->{'delay_generator'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"delay_generator"}) 6 0 6
83 6 $self->{'delay_generator'} = Video::Delay::Const->new(
84 'const' => 1000,
85 );
86 }
87  
88 # Video pattern generator.
89 6 ▶ if (! defined $self->{'video_pattern'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"video_pattern"}) 6 0 6
90 $self->{'video_pattern'} = Video::Pattern->new(
91 'delay_generator' => $self->{'delay_generator'},
92 'duration' => $self->{'duration'},
93 'fps' => $self->{'fps'},
94 6 'image_generator' => $self->{'image_generator'},
95 );
96 }
97  
98 # Object.
99 1 return $self;
100 }
101  
102 # Create random video.
103 sub create {
104 0 ▶ my ($self, $out_path) = @_;
Subroutine create: not called CC=7 SCAR=40.3
Pod documented
105  
106 # Create images.
107 0 $self->{'video_pattern'}->create($self->{'temp_dir'});
108 0 ▶ if ($self->{'verbose'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
109 0 print "Video pattern generator created images for video in ".
110 "temporary directory.\n";
111 }
112  
113 # Create video.
114 0 my $ffmpeg = FFmpeg::Command->new;
115 my $images_path = catfile($self->{'temp_dir'},
116 0 '%03d.'.$self->{'image_type'});
117 my @command_options = ('-loglevel', 'error', '-r', $self->{'fps'},
118 '-i', $images_path,
119 $self->{'ffmpeg_video_codec'}
120 ? ('-c:v', $self->{'ffmpeg_video_codec'})
121 : (),
122 $self->{'ffmpeg_pixel_format'}
123 0 ▶ ? ('-pix_fmt', $self->{'ffmpeg_pixel_format'})
Branch: 2 branches
BranchTrueFalseTotal
$self->{'ffmpeg_video_codec'} ? : 0 0 0
$self->{'ffmpeg_pixel_format'} ? : 0 0 0
124 :(),
125 '-pix_fmt', 'yuv420p',
126 $out_path);
127 0 $ffmpeg->options(@command_options);
128 0 $ffmpeg->exec;
129 0 ▶ if ($ffmpeg->stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ffmpeg->stderr) 0 0 0
130 0 my @stderr = split m/\n/ms, $ffmpeg->stderr;
131 0 my $command = join $SPACE, @command_options;
132 err "Error with command 'ffmpeg $command'.",
133 0 map { ('STDERR', $_) } @stderr;
134 }
135 0 ▶ if ($self->{'verbose'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
136 0 print "Created video file.\n";
137 }
138  
139 # Remove temporary directory.
140 0 rmtree $self->{'temp_dir'};
141 0 ▶ if ($self->{'verbose'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
142 0 print "Removed temporary directory.\n";
143 }
144  
145 0 return;
146 }
147  
148 3 1;
149  
150 __END__
↑ Summary