GoSaveTool/Subtitles.pm

filter: Statementstmt 100% 93 / 93 Branchbran 83.3% 25 / 30 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 16 / 16 Podpod 100% 9 / 9 totaltotal 93.2% 151 / 162 CC 26 SCAR 14.8
CC
26
Cov
94%
repair
9.0
shift_cues
5.0
parse
5.0
CRAP 4.4
↑ Summary GoSaveTool/Text.pm »
1 package GoSaveTool::Subtitles;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use feature qw(unicode_strings);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.000';
11 1 our @EXPORT_OK = qw(to_ms from_ms parse is_vtt to_srt to_vtt convert shift_cues repair);
12  
13 1 my $STAMP = qr/(?:\d{1,2}:)?\d{1,2}:\d{1,2}[,.]\d{1,3}/;
14  
15 12 ▶sub to_ms {
Subroutine to_ms: called CC=3 SCAR=11.0
Pod documented
16 12 my ($stamp) = @_;
17 12 ▶ return undef unless $stamp =~ /(\d{1,2}:)?(\d{1,2}):(\d{1,2})[,.](\d{1,3})/;
Branch: 1 branch
BranchTrueFalseTotal
unless $stamp =~ /(\d{1,2}:)?(\d{1,2}):(\d{1,2})[,.](\d{1,3})/u 1 11 12
18 11 ▶ my $h = defined $1 ? substr($1, 0, -1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 7 4 11
19 11 my $frac = substr($4 . '00', 0, 3);
20 11 return (($h * 60 + $2) * 60 + $3) * 1000 + $frac;
21 }
22  
23 11 ▶sub from_ms {
Subroutine from_ms: called CC=3 SCAR=11.3
Pod documented
24 11 my ($ms, $sep) = @_;
25 11 ▶ $sep //= ',';
Condition50%1/2
$sep //= ","
l!l
110
MC/DC0%0/1
$sep //= ","
$sep
Truth table50%1/2
A
A= $sep
Aresult
11
00
26 # Half up, as JavaScript's Math.round
27 11 $ms = int($ms + 0.5);
28 11 ▶ $ms = 0 if $ms < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ms < 0 0 11 11
29 11 return sprintf '%02d:%02d:%02d%s%03d', int($ms / 3600000), int($ms / 60000) % 60, int($ms / 1000) % 60, $sep, $ms % 1000;
30 }
31  
32 4 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.1
Pod documented
33 4 my ($src) = @_;
34 4 (my $text = $src) =~ s/\A\x{FEFF}//;
35 # "\r\r\n" too: a file saved twice with Windows line endings
36 4 $text =~ s/\r*\n|\r/\n/g;
37 4 my @cues;
38 4 for my $block (split /\n{2,}/, $text) {
39 9 my @lines = split /\n/, $block, -1;
40 9 for my $i (0 .. $#lines) {
41 12 ▶ next unless $lines[$i] =~ /^\s*($STAMP)\s*-->\s*($STAMP)(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines[$i] =~ /^\s*($STAMP)\s*-->\s*($STAMP)(.*)$/u 7 5 12
42 5 my ($from, $to, $settings) = ($1, $2, $3);
43 5 my $body = join "\n", @lines[$i + 1 .. $#lines];
44 5 s/\A\s+|\s+\z//g for $body, $settings;
45 5 push @cues, { start => to_ms($from), end => to_ms($to), settings => $settings, text => $body };
46 5 last;
47 }
48 }
49 4 return \@cues;
50 }
51  
52 1 ▶sub is_vtt {
Subroutine is_vtt: called CC=2 SCAR=7.2
Pod documented
53 1 my ($src) = @_;
54 1 ▶ return $src =~ /\A\x{FEFF}?WEBVTT/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$src =~ /\A\x{FEFF}?WEBVTT/u ? : 1 0 1
55 }
56  
57 1 ▶sub to_srt {
Subroutine to_srt: called CC=1 SCAR=0.0
Pod documented
58 1 my ($cues) = @_;
59 1 my $i = 0;
60 1 return join("\n\n", map { ++$i . "\n" . from_ms($_->{start}, ',') . ' --> ' . from_ms($_->{end}, ',') . "\n" . $_->{text} } @$cues) . "\n";
61 }
62  
63 2 ▶sub to_vtt {
Subroutine to_vtt: called CC=3 SCAR=11.1
Pod documented
64 2 my ($cues) = @_;
65 return "WEBVTT\n\n" . join("\n\n", map {
66 2 from_ms($_->{start}, '.') . ' --> ' . from_ms($_->{end}, '.') . (length($_->{settings} // '') ? " $_->{settings}" : '') . "\n" . $_->{text}
67 4 ▶ } @$cues) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
length($_->{'settings'} // '') ? : 1 3 4
Condition50%1/2
$_->{"settings"} // ""
l!l
40
MC/DC0%0/1
$_->{"settings"} // ""
$_->{"settings"}
Truth table50%1/2
A
A= $_->{"settings"}
Aresult
11
00
68 }
69  
70 3 ▶sub convert {
Subroutine convert: called CC=3 SCAR=11.0
Pod documented
71 3 my ($src, $to) = @_;
72 3 ▶ return to_vtt(parse($src)) if $to eq 'vtt';
Branch: 1 branch
BranchTrueFalseTotal
if $to eq "vtt" 1 2 3
73 2 ▶ return to_srt(parse($src)) if $to eq 'srt';
Branch: 1 branch
BranchTrueFalseTotal
if $to eq "srt" 1 1 2
74 1 croak 'the target format must be "srt" or "vtt"';
75 }
76  
77 1 ▶sub shift_cues {
Subroutine shift_cues: called CC=5 SCAR=16.1
Pod documented
78 1 my ($cues, $ms) = @_;
79 1 my @out;
80 1 for my $c (@$cues) {
81 3 my $end = $c->{end} + $ms;
82 3 ▶ $end = 0 if $end < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $end < 0 1 2 3
83 3 ▶ next unless $end > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $end > 0 1 2 3
84 2 my $start = $c->{start} + $ms;
85 2 ▶ push @out, { %$c, start => ($start < 0 ? 0 : $start), end => $end };
Branch: 1 branch
BranchTrueFalseTotal
$start < 0 ? : 0 2 2
86 }
87 1 return \@out;
88 }
89  
90 1 ▶sub repair {
Subroutine repair: called CC=9 SCAR=22.0
Pod documented
91 1 my ($cues, $min_duration) = @_;
92 1 ▶ $min_duration //= 2000;
Condition50%1/2
$min_duration //= 2000
l!l
01
MC/DC0%0/1
$min_duration //= 2000
$min_duration
Truth table50%1/2
A
A= $min_duration
Aresult
11
00
93 1 my $fixed = grep { $_->{text} eq '' } @$cues;
94 1 my @kept = map { +{%$_} } grep { $_->{text} ne '' } @$cues;
95 # Perl's sort is stable (mergesort), so cues with the same start keep their order
96 1 ▶ use sort 'stable';
Subroutine BEGIN: called CC=1 SCAR=0.0
97 1 my @out = sort { $a->{start} <=> $b->{start} } @kept;
98 1 for my $i (0 .. $#out) {
99 1 ▶ if ($out[$i]{start} != $kept[$i]{start}) { $fixed++; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($out[$i]{"start"} != $kept[$i]{"start"}) 1 0 1
100 }
101 1 for my $i (0 .. $#out) {
102 3 my ($c, $next) = ($out[$i], $out[$i + 1]);
103 3 ▶ if (!($c->{end} > $c->{start})) { $c->{end} = $c->{start} + $min_duration; $fixed++ }
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->{"end"} > $c->{"start"}) 1 2 3
104 3 ▶ if ($next && $c->{end} > $next->{start}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next and $c->{"end"} > $next->{"start"}) 1 2 3
Condition100%3/3
$next and $c->{"end"} > $next->{"start"}
!ll&&!rl&&r
111
MC/DC100%2/2
$next and $c->{"end"} > $next->{"start"}
$next $c->{"end"} > $next->{"start"}
Truth table100%3/3
A and B
A= $nextB= $c->{"end"} > $next->{"start"}
ABresult
0X0
100
111
105 1 my $e = $next->{start} - 1;
106 1 ▶ $c->{end} = $e > $c->{start} + 1 ? $e : $c->{start} + 1;
Branch: 1 branch
BranchTrueFalseTotal
$e > $c->{'start'} + 1 ? : 1 0 1
107 1 $fixed++;
108 }
109 }
110 1 return (\@out, $fixed);
111 }
112  
113 1 1;
114  
115 __END__
↑ Summary GoSaveTool/Text.pm »