blib/lib/Media/Convert/Normalizer/Bs1770gain.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 78 CC 5 SCAR 20.4
CC
5
Cov
0%
run
20.0
_probe_bs1770gain_version
6.0
_probe_tempdir
2.0
CRAP 7.7
1 package Media::Convert::Normalizer::Bs1770gain;
2  
3 use Moose;
4 use File::Basename;
5 use File::Temp;
6 use Media::Convert::CodecMap qw/detect_to_write/;
7 use Media::Convert::Map;
8 use Media::Convert::Asset;
9 use Media::Convert::Pipe;
10  
11 extends 'Media::Convert::Normalizer';
12  
13 =head1 NAME
14  
15 Media::Convert::Normalizer::Bs1770gain - normalize the audio of a video asset using bs1770gain
16  
17 =head1 SYNOPSIS
18  
19 Media::Convert::Normalizer::Bs1770gain->new(input => Media::Convert::Asset->new(...), output => Media::Convert::Asset->new(...))->run();
20  
21 =head1 DESCRIPTION
22  
23 C<Media::Convert::Normalizer::Bs1770gain> is a class to normalize the
24 audio of a given Media::Convert::Asset asset using bs1770gain.
25  
26 =head1 ATTRIBUTES
27  
28 C<Media::Convert::Normalizer::Bs1770gain> supports all the attributes of
29 L<Media::Convert::Normalizer>, plus the following extra options:
30  
31 =head2 tempdir
32  
33 The directory in which to write temporary files.
34  
35 =cut
36  
37 has 'tempdir' => (
38 is => 'rw',
39 isa => 'Str',
40 lazy => 1,
41 builder => '_probe_tempdir',
42 );
43  
44 sub _probe_tempdir {
45 my $self = shift;
46  
47 return File::Temp::tempdir("normXXXXXX", CLEANUP => 1);
48 }
49  
50 has '_bs1770gain_version' => (
51 is => 'ro',
52 isa => 'Str',
53 lazy => 1,
54 builder => '_probe_bs1770gain_version',
55 );
56  
57 sub _probe_bs1770gain_version {
58 my $self = shift;
59  
60 open my $pipe, "-|", "bs1770gain", "--version";
61  
62 my $rv = "0.5";
63 while(<$pipe>) {
64 if(/^bs1770gain ([0-9]+\.[0-9]+\.)/i) {
65 $rv = $1;
66 last;
67 }
68 }
69 close $pipe;
70 return $rv;
71 }
72  
73 =head1 METHODS
74  
75 =head2 run
76  
77 Performs the normalization.
78  
79 =cut
80  
81 sub run {
82 my $self = shift;
83  
84 my $exten;
85  
86 $self->input->url =~ /(.*)\.[^.]+$/;
87 my $base = $1;
88 if(!defined($self->input->video_codec)) {
89 $exten = "flac";
90 } else {
91 $exten = "mkv";
92 }
93 my @command = ("bs1770gain", "-a", "-o", $self->tempdir);
94 if($self->_bs1770gain_version ne "0.5") {
95 $exten = "mkv";
96 push @command, "--suffix=mkv";
97 }
98 push @command, $self->input->url;
99 print "Running: '" . join("' '", @command) . "'\n";
100 system(@command);
101  
102 my $intermediate = $self->tempdir . "/" . basename($base) . ".$exten";
103 my $check = Media::Convert::Asset->new(url => $intermediate);
104  
105 if($check->audio_codec eq $self->input->audio_codec) {
106 Media::Convert::Pipe->new(inputs => [Media::Convert::Asset->new(url => $intermediate)], output => $self->output, vcopy => 1, acopy => 1)->run();
107 } else {
108 $self->output->audio_codec($self->input->audio_codec);
109 Media::Convert::Pipe->new(inputs => [Media::Convert::Asset->new(url => $intermediate)], output => $self->output, vcopy => 1, acopy => 0)->run();
110 }
111 }
112  
113 1;