blib/lib/Video/Generator.pm

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