blib/lib/Subtitles.pm

filter: Statementstmt 73.5% 348 / 473 Branchbran 51.0% 101 / 198 Conditioncond 34.7% 16 / 46 MC/DCmcdc 6.6% 2 / 30 Subroutinesub 76.0% 38 / 50 Podpod 100% 20 / 20 totaltotal 64.2% 525 / 817 CC 125 SCAR 32.1
CC
125
Cov
65%
parse_time
156.0
load
59.7
read
30.0
CRAP 24.8
↑ Summary
1 # $Id: Subtitles.pm,v 1.22 2012/02/14 13:21:48 dk Exp $
2 package Subtitles;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 require Exporter;
5 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK @codecs $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 @ISA = qw(Exporter);
7 1 @EXPORT = qw(codecs time2str);
8 1 @EXPORT_OK = qw(codecs time2hms time2shms hms2time time2str);
9 1 $VERSION = '1.04';
10
11 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 1 push @codecs, map { "Subtitles::Codec::$_" } qw( srt mdvd sub2 smi idx);
15  
16 #
17 # package-oriented API
18 #
19  
20 sub time2hms
21 8 ▶{
Subroutine time2hms: called CC=3 SCAR=11.3
Pod documented
22 8 ▶ shift if $#_ == 1; # package and object
Branch: 1 branch
BranchTrueFalseTotal
if $#_ == 1 0 8 8
23 8 my $time = $_[0];
24 8 ▶ $time = 0 if $time < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $time < 0 0 8 8
25 8 $time += .0005;
26 8 return int($time/3600),int(($time%3600)/60),int($time%60),int(($time-int($time))*1000),
27 }
28  
29 sub time2shms
30 0 ▶{
Subroutine time2shms: not called CC=3 SCAR=24.8
Pod documented
31 0 ▶ shift if $#_ == 1; # package and object
Branch: 1 branch
BranchTrueFalseTotal
if $#_ == 1 0 0 0
32 0 my $time = $_[0];
33 0 my $sign;
34 0 ▶ if ( $time < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time < 0) { } 0 0 0
35 0 $sign = -1;
36 0 $time = -$time;
37 } else {
38 0 $sign = 1;
39 }
40 0 $time += .0005;
41 0 return $sign,int($time/3600),int(($time%3600)/60),int($time%60),int(($time-int($time))*1000),
42 }
43  
44 sub hms2time
45 8 ▶{
Subroutine hms2time: called CC=2 SCAR=7.1
Pod documented
46 8 ▶ shift if $#_ == 4; # package and object
Branch: 1 branch
BranchTrueFalseTotal
if $#_ == 4 0 8 8
47 8 my ( $h, $m, $s, $ms) = @_;
48 8 return $h * 3600 + $m * 60 + $s + $ms / 1000;
49 }
50  
51  
52 sub time2str
53 0 ▶{
Subroutine time2str: not called CC=3 SCAR=24.8
Pod documented
54 0 ▶ shift if $#_ == 1; # package and object
Branch: 1 branch
BranchTrueFalseTotal
if $#_ == 1 0 0 0
55 0 my $time = $_[0];
56 0 my $is_minus = '';
57 0 ▶ $time = -$time, $is_minus = "-" if $time < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $time < 0 0 0 0
58 0 return sprintf ( "$is_minus%02d:%02d:%02d.%03d", time2hms($time));
59 }
60  
61 1 ▶sub codecs { @codecs }
Subroutine codecs: called CC=1 SCAR=0.0
Pod documented
62  
63 #
64 # object-oriented API
65 #
66  
67 sub new
68 5 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
69 5 my $class = shift;
70 5 return bless {
71 codec => undef,
72 @_,
73 text => [],
74 from => [],
75 to => [],
76 class => $class,
77 }, $class;
78 }
79  
80 sub load
81 4 ▶{
Subroutine load: called CC=17 SCAR=40.9
Pod documented
82 4 my ( $self, $fh, $codec) = @_;
83 4 $self-> clear;
84 4 local $/;
85 4 my $content = <$fh>;
86 4 ▶ if ( $content =~ s/^(\xff\xfe|\xfe\xff)//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ s/^(\xff\xfe|\xfe\xff)//) { } 0 4 4
elsif ($content =~ s/^\xef\xbb\xbf//) { } 0 4 4
87 # found a 16-bit bom
88 0 ▶ my $le = ( $1 eq "\xff\xfe" ) ? 'v*' : 'n*';
Branch: 1 branch
BranchTrueFalseTotal
$1 eq "\377\376" ? : 0 0 0
89 0 $content = join('', map { chr } unpack($le, $content));
90 } elsif ( $content =~ s/^\xef\xbb\xbf//) {
91 # found a utf-8 bom
92 0 Encode::_utf8_on($content);
93 }
94 4 my @content;
95 4 for ( split "\n", $content) {
96 55 s/[\s\n\r]+$//;
97 55 push @content, $_;
98 }
99 4 ▶ unless ( defined $codec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $codec) 4 0 4
100 4 for ( @content) {
101 55 my $line = $_;
102 55 for ( @codecs) {
103 275 ▶ next unless $_-> match( $line);
Branch: 1 branch
BranchTrueFalseTotal
unless $_->match($line) 267 8 275
104 8 $codec = $_;
105 }
106 }
107 }
108 4 ▶ unless ( defined $codec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $codec) 0 4 4
109 0 $@ = "No suitable codec is found";
110 0 return undef;
111 }
112 4 my $ret;
113 4 eval {
114 4 $ret = $codec-> read( $self, \@content);
115 };
116 4 ▶ return undef if $@ or !defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $ret 0 4 4
Condition33%1/3
$@ or not defined $ret
l!l&&r!l&&!r
004
MC/DC0%0/2
$@ or not defined $ret
$@ not defined $ret
Truth table33%1/3
A or not(B)
A= $@B= not defined $ret
ABresult
1X1
011
000
117 # validate
118 4 ▶ if ( @{$self->{from}} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"from"};} == 0) 0 4 4
119 0 $@ = "Empty subtitle";
120 0 return undef;
121 }
122 4 ▶ if ( @{$self->{from}} != @{$self->{to}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"from"};} != @{$self->{"to"};}) 0 4 4
123 0 ▶ if ( @{$self->{from}} == @{$self->{to}} + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'from'};} == @{$self->{'to'};} + 1) { } 0 0 0
124 0 push @{$self->{to}}, $self->{from}->[-1] + 2; # fix a dangling tail
125 } else {
126 0 my $a = @{$self->{from}};
127 0 my $b = @{$self->{to}};
128 0 $@ = "Number of 'from' ($a) and 'to' ($b) timeframe positions is different";
129 0 return undef;
130 }
131 }
132 4 ▶ if ( @{$self->{from}} != @{$self->{text}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"from"};} != @{$self->{"text"};}) 0 4 4
133 0 ▶ if ( @{$self->{from}} == @{$self->{text}} + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'from'};} == @{$self->{'text'};} + 1) { } 0 0 0
134 0 push @{$self->{text}}, ''; # fix a dangling tail
135 } else {
136 0 my $a = @{$self->{from}};
137 0 my $b = @{$self->{text}};
138 0 $@ = "Number of timeframes ($a) is different from the number of text lines ($b)";
139 0 return undef;
140 }
141 }
142 4 $self->{codec} = $codec;
143 4 return 1;
144 }
145  
146 sub codec
147 4 ▶{
Subroutine codec: called CC=6 SCAR=18.5
Pod documented
148 4 ▶ return $_[0]-> {codec} unless $#_;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ 0 4 4
149 4 my ( $self, $codec) = @_;
150 4 my %c = map { $_ => 1 } @codecs;
151 4 ▶ return unless exists $c{$codec};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $c{$codec} 0 4 4
152 4 ▶ return if defined $self->{codec} && $self->{codec} eq $codec;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"codec"} and $self->{"codec"} eq $codec 0 4 4
Condition66%2/3
defined $self->{"codec"} and $self->{"codec"} eq $codec
!ll&&!rl&&r
130
MC/DC0%0/2
defined $self->{"codec"} and $self->{"codec"} eq $codec
defined $self->{"codec"} $self->{"codec"} eq $codec
Truth table66%2/3
A and B
A= defined $self->{"codec"}B= $self->{"codec"} eq $codec
ABresult
0X0
100
111
153 4 ▶ $self->{codec}-> downgrade($self, $codec) if defined $self->{codec};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"codec"} 3 1 4
154 4 $self->{codec} = $codec;
155 }
156  
157 sub rate
158 0 ▶{
Subroutine rate: not called CC=4 SCAR=30.0
Pod documented
159 0 ▶ return $_[0]-> {rate} unless $#_;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ 0 0 0
160 0 ▶ return if defined $_[1] && $_[1] <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] and $_[1] <= 0 0 0 0
Condition0%0/3
defined $_[1] and $_[1] <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[1] and $_[1] <= 0
defined $_[1] $_[1] <= 0
Truth table0%0/3
A and B
A= defined $_[1]B= $_[1] <= 0
ABresult
0X0
100
111
161 0 $_[0]->{rate} = $_[1];
162 }
163  
164 # parses
165 # SS
166 # MM:SS
167 # HH:MM:SS
168 # HH:MM:SS,msec
169 # MM:SS,msec
170 # into time
171 sub parse_time
172 0 ▶{
Subroutine parse_time: not called CC=12 SCAR=50.5
Pod documented
173 0 my ( $self, $time) = @_;
174 0 my $sign = 1;
175 0 ▶ $sign = -1 if $time =~ s/^-//;
Branch: 1 branch
BranchTrueFalseTotal
if $time =~ s/^-// 0 0 0
176 0 ▶ if ( $time =~ m/^(?:(\d{1,2}):)?(?:(\d{1,2}):)?(\d{1,2})(?:[\,\.\:](\d{1,3}))?$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($time =~ /^(?:(\d{1,2}):)?(?:(\d{1,2}):)?(\d{1,2})(?:[\,\.\:](\d{1,3}))?$/) { } 0 0 0
elsif ($self and $self->{'codec'}) { } 0 0 0
177 0 my ( $h, $m, $s, $ms) = ( $1, $2, $3, $4);
178 0 ▶ ( $h, $m) = ( $m, $h) if defined $h && ! defined $m;
Branch: 1 branch
BranchTrueFalseTotal
if defined $h and not defined $m 0 0 0
Condition0%0/3
defined $h and not defined $m
!ll&&!rl&&r
000
MC/DC0%0/2
defined $h and not defined $m
defined $h not defined $m
Truth table0%0/3
A and not(B)
A= defined $hB= not defined $m
ABresult
0X0
100
111
179 0 ▶ $h = 0 unless defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 0 0 0
180 0 ▶ $m = 0 unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 0 0 0
181 0 ▶ $ms = '0' unless defined $ms;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ms 0 0 0
182 0 ▶ $ms .= '0' while length($ms) < 3;
Branch: 1 branch
BranchTrueFalseTotal
CORE::length $ms < 3 and do { $ms .= '0' } 0 0 0
183 0 return $sign * ( $h * 3600 + $m * 60 + $s + $ms / 1000);
184 ▶ } elsif ( $self && $self-> {codec}) {
Condition0%0/3
$self and $self->{"codec"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self and $self->{"codec"}
$self $self->{"codec"}
Truth table0%0/3
A and B
A= $selfB= $self->{"codec"}
ABresult
0X0
100
111
185 0 my $t = $self->{codec}->time($time);
186 0 ▶ return $sign * $t if defined $t;
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 0 0 0
187 }
188 0 undef;
189 }
190  
191 1 ▶sub shift { $_[0]-> transform( 1, $_[1]) }
Subroutine shift: called CC=1 SCAR=0.0
Pod documented
192 0 ▶sub scale { $_[0]-> transform( $_[1], 0) }
Subroutine scale: not called CC=1 SCAR=6.9
Pod documented
193  
194 2 ▶sub lines { scalar @{$_[0]->{text}} }
Subroutine lines: called CC=1 SCAR=0.0
Pod documented
195  
196 # applies linear (y = ax+b) transformation within a scope
197 sub transform
198 1 ▶{
Subroutine transform: called CC=8 SCAR=22.5
Pod documented
199 1 my ( $self, $a, $b, $qfrom, $qto) = @_;
200 1 ▶ return if $a == 1 && $b == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $a == 1 and $b == 0 0 1 1
Condition33%1/3
$a == 1 and $b == 0
!ll&&!rl&&r
010
MC/DC0%0/2
$a == 1 and $b == 0
$a == 1 $b == 0
Truth table33%1/3
A and B
A= $a == 1B= $b == 0
ABresult
0X0
100
111
201 1 ▶ $qfrom = 0 unless defined $qfrom;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qfrom 1 0 1
202 1 ▶ $qto = $self->{to}->[-1] unless defined $qto;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qto 1 0 1
203 1 my $i;
204 1 my $n = $self-> lines;
205 1 my $from = $self->{from};
206 1 my $to = $self->{to};
207 1 ▶ for ( $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 1 1 2
208 1 ▶ next if $$from[$i] > $qto || $$to[$i] < $qfrom;
Branch: 1 branch
BranchTrueFalseTotal
if $from->[$i] > $qto or $to->[$i] < $qfrom 0 1 1
Condition33%1/3
$from->[$i] > $qto or $to->[$i] < $qfrom
l!l&&r!l&&!r
001
MC/DC0%0/2
$from->[$i] > $qto or $to->[$i] < $qfrom
$from->[$i] > $qto $to->[$i] < $qfrom
Truth table33%1/3
A or B
A= $from->[$i] > $qtoB= $to->[$i] < $qfrom
ABresult
1X1
011
000
209 1 $$from[$i] = $a * $$from[$i] + $b;
210 1 $$to[$i] = $a * $$to[$i] + $b;
211 }
212 }
213  
214 sub dup
215 2 ▶{
Subroutine dup: called CC=2 SCAR=9.9
Pod documented
216 2 my ( $self, $clear) = @_;
217 2 ▶ if ( $clear) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clear) { } 2 0 2
218 return bless {
219 %$self,
220 text => [],
221 from => [],
222 to => [],
223 2 }, $self-> {class};
224 } else {
225 return bless {
226 %$self,
227 0 text => [ @{$self->{text}}],
228 0 from => [ @{$self->{from}}],
229 0 to => [ @{$self->{to}}],
230 0 }, $self-> {class};
231 }
232 }
233  
234 sub clear
235 4 ▶{
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
236 4 my $self = $_[0];
237 4 $self-> {text} = [];
238 4 $self-> {from} = [];
239 4 $self-> {to} = [];
240 }
241  
242 sub join
243 1 ▶{
Subroutine join: called CC=2 SCAR=6.9
Pod documented
244 1 my ( $self, $guest, $time_between) = @_;
245 1 ▶ $time_between = 2 unless defined $time_between;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time_between 0 1 1
246 1 my $delta = $time_between + $self-> length;
247 1 push @{$self->{text}}, @{$guest->{text}};
248 1 push @{$self->{from}}, map { $_ + $delta } @{$guest->{from}};
249 1 push @{$self->{to}}, map { $_ + $delta } @{$guest->{to}};
250 }
251  
252 sub split
253 1 ▶{
Subroutine split: called CC=7 SCAR=19.7
Pod documented
254 1 my ( $self, $where) = @_;
255  
256 1 my ( $s1, $s2) = ( $self-> dup(1), $self-> dup(1));
257  
258 1 my $i;
259 1 my $n = $self->lines;
260 1 my $t = $self->{to};
261 1 my ( $end, $begin);
262  
263 1 $end = $n - 1;
264 1 ▶ for ( $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 2 0 2
265 2 ▶ next if $$t[$i] <= $where;
Branch: 1 branch
BranchTrueFalseTotal
if $t->[$i] <= $where 1 1 2
266 1 $begin = $i;
267 1 $end = $i - 1;
268 1 last;
269 }
270  
271 1 ▶ if ( defined $end && $end >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end and $end >= 0) 1 0 1
Condition33%1/3
defined $end and $end >= 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $end and $end >= 0
defined $end $end >= 0
Truth table33%1/3
A and B
A= defined $endB= $end >= 0
ABresult
0X0
100
111
272 1 @{$s1->{text}} = @{$self->{text}}[0..$end];
273 1 @{$s1->{from}} = @{$self->{from}}[0..$end];
274 1 @{$s1->{to}} = @{$self->{to}}[0..$end];
275 }
276 1 ▶ if ( defined $begin && $begin < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $begin and $begin < $n) 1 0 1
Condition33%1/3
defined $begin and $begin < $n
!ll&&!rl&&r
001
MC/DC0%0/2
defined $begin and $begin < $n
defined $begin $begin < $n
Truth table33%1/3
A and B
A= defined $beginB= $begin < $n
ABresult
0X0
100
111
277 1 @{$s2->{text}} = @{$self->{text}}[$begin..$n-1];
278 1 @{$s2->{from}} = @{$self->{from}}[$begin..$n-1];
279 1 @{$s2->{to}} = @{$self->{to}}[$begin..$n-1];
280 1 $s2-> shift( -$where);
281 }
282 1 ($s1,$s2);
283 }
284  
285 sub length
286 9 ▶{
Subroutine length: called CC=2 SCAR=7.1
Pod documented
287 9 my $self = $_[0];
288 9 ▶ return @{$self->{to}} ? $self->{to}->[-1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'to'};} ? : 9 0 9
289 }
290  
291 sub save
292 4 ▶{
Subroutine save: called CC=7 SCAR=23.6
Pod documented
293 4 my ( $self, $fh) = @_;
294 4 my $content;
295 4 eval {
296 4 $content = $self-> {codec}-> write( $self);
297 4 ▶ die "no content" unless defined $content and @$content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and @$content 0 4 4
Condition33%1/3
defined $content and @$content
!ll&&!rl&&r
004
MC/DC0%0/2
defined $content and @$content
defined $content @$content
Truth table33%1/3
A and B
A= defined $contentB= @$content
ABresult
0X0
100
111
298  
299 4 $content = CORE::join("\n", @$content);
300 4 ▶ if ( Encode::is_utf8($content)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($content)) 0 4 4
301 # bomify
302 0 ▶ print $fh "\xef\xbb\xbf" or die "write error:$!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh "\357\273\277" or die "write error:$!" 0 0 0
303 0 binmode $fh, ':utf8';
304 }
305  
306 4 ▶ print $fh $content, "\n" or die "write error:$!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh $content, "\n" or die "write error:$!" 4 0 4
307 };
308  
309 4 ▶ return $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 4 4
310 }
311  
312 package Subtitles::Codec;
313 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
314  
315 sub match
316 0 ▶{
Subroutine match: not called CC=1 SCAR=6.9
317 0 my ( $self, $line) = @_;
318 0 undef;
319 }
320  
321 sub read
322 0 ▶{
Subroutine read: not called CC=1 SCAR=6.9
323 0 my ( $self, $sub, $content) = @_;
324 0 die "abstract method call";
325 }
326  
327 sub write
328 0 ▶{
Subroutine write: not called CC=1 SCAR=6.9
329 0 my ( $self, $sub) = @_;
330 0 die "abstract method call";
331 }
332  
333 0 ▶sub time { undef }
Subroutine time: not called CC=1 SCAR=6.9
334  
335 3 ▶sub downgrade {}
Subroutine downgrade: called CC=1 SCAR=0.0
336  
337 package Subtitles::Codec::srt;
338 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
339 1 @ISA=qw(Subtitles::Codec);
340  
341 sub match
342 55 ▶{
Subroutine match: called CC=1 SCAR=0.0
343 55 $_[1] =~ m/^(\d\d):(\d\d):(\d\d)[.,](\d\d\d)\s*-->\s*(\d\d):(\d\d):(\d\d)[.,](\d\d\d)/;
344 }
345  
346 sub read
347 1 ▶{
Subroutine read: called CC=10 SCAR=23.8
348 1 my ( $self, $sub, $content) = @_;
349  
350 1 my $stage = 0;
351 1 my $num = 1;
352 1 my $line = 0;
353 # 0:
354 # 1: 1
355 # 2: 00:00:04,073 --> 00:00:05,781
356 # 3: Subtitle
357  
358 1 for ( @$content) {
359 7 $line++;
360 7 ▶ if ( $stage == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($stage == 0) { } 2 5 7
elsif ($stage == 1) { } 2 3 5
elsif ($stage == 2) { } 2 1 3
361 2 ▶ next unless length;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 0 2 2
362 2 ▶ die "Invalid line numbering at line $line\n" unless m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\d+$/ 0 2 2
363 2 $num++;
364 2 $stage++;
365 } elsif ( $stage == 1) {
366 2 ▶ die "Invalid timing at line $line\n" unless
Branch: 1 branch
BranchTrueFalseTotal
unless /^(\d\d):(\d\d):(\d\d)[.,](\d\d\d)\s*-->\s*(\d\d):(\d\d):(\d\d)[.,](\d\d\d)/ 0 2 2
367 m/^(\d\d):(\d\d):(\d\d)[.,](\d\d\d)\s*-->\s*(\d\d):(\d\d):(\d\d)[.,](\d\d\d)/;
368 2 push @{$sub->{from}}, Subtitles::hms2time( $1, $2, $3, $4);
369 2 push @{$sub->{to}}, Subtitles::hms2time( $5, $6, $7, $8);
370 2 $stage++;
371 } elsif ( $stage == 2) {
372 2 ▶ if ( length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) { } 2 0 2
373 2 push @{$sub->{text}}, $_;
374 2 $stage++;
375 } else {
376 0 push @{$sub->{text}}, '';
377 0 $stage = 0;
378 }
379 } else {
380 1 ▶ if ( length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) { } 0 1 1
381 0 $sub->{text}->[-1] .= "\n$_";
382 } else {
383 1 $stage = 0;
384 }
385 }
386 }
387 1 1;
388 }
389  
390 sub write
391 1 ▶{
Subroutine write: called CC=2 SCAR=6.9
392 1 my ( $self, $sub) = @_;
393  
394 1 my $n = @{$sub->{text}};
395 1 my $i;
396 my @ret;
397 1 my $from = $sub->{from};
398 1 my $to = $sub->{to};
399 1 my $text = $sub->{text};
400 1 ▶ for ( $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 2 1 3
401 2 push @ret,
402 $i + 1,
403 sprintf ( "%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d",
404 Subtitles::time2hms($from->[$i]),
405 Subtitles::time2hms($to->[$i]),
406 ),
407 split ("\n", $text->[$i]),
408 ''
409 ;
410 }
411 1 \@ret;
412 }
413  
414 package Subtitles::Codec::mdvd;
415 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
416 1 @ISA=qw(Subtitles::Codec);
417  
418 sub match
419 55 ▶{
Subroutine match: called CC=1 SCAR=0.0
420 55 $_[1] =~ m/^[{\[]\d+[}\]][{\[]\d*[}\]]/;
421 }
422  
423 sub read
424 1 ▶{
Subroutine read: called CC=5 SCAR=16.6
425 1 my ( $self, $sub, $content) = @_;
426  
427 1 my $line = 0;
428 # {3724}{3774}Text
429  
430 1 ▶ my $fps = $sub->{rate} ? $sub->{rate} : 23.976;
Branch: 1 branch
BranchTrueFalseTotal
$sub->{'rate'} ? : 0 1 1
431 1 my $from = $sub->{from};
432 1 my $to = $sub->{to};
433 1 my $text = $sub->{text};
434  
435 1 for ( @$content) {
436 2 $line++;
437 2 ▶ unless ( m/^[{\[](\d+)[}\]][{\[](\d*)[}\]](.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^[{\[](\d+)[}\]][{\[](\d*)[}\]](.*)$/) 0 2 2
438 0 warn "Invalid input at line $line\n";
439 0 next;
440 }
441 2 push @$from, $1/$fps;
442 2 ▶ push @$to, length($2) ? ($2/$fps) : ($1+1)/$fps;
Branch: 1 branch
BranchTrueFalseTotal
length $2 ? : 2 0 2
443 2 my $t = $3;
444 2 $t=~ s/\|\s*/\n/g;
445 2 push @$text, $t;
446 }
447 1 1;
448 }
449  
450 sub write
451 1 ▶{
Subroutine write: called CC=3 SCAR=11.0
452 1 my ( $self, $sub) = @_;
453
454 1 ▶ my $fps = $sub->{rate} ? $sub->{rate} : 23.976;
Branch: 1 branch
BranchTrueFalseTotal
$sub->{'rate'} ? : 0 1 1
455  
456 1 my $n = @{$sub->{text}};
457 1 my $i;
458 my @ret;
459 1 my $from = $sub->{from};
460 1 my $to = $sub->{to};
461 1 my $text = $sub->{text};
462 1 ▶ for ( $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 2 1 3
463 2 my $t = $text->[$i];
464 2 $t =~ s/\n/\|/g;
465 2 push @ret,
466 sprintf ( "{%d}{%d}%s",
467 int( $from->[$i] * $fps + .5),
468 int( $to->[$i] * $fps + .5),
469 $t
470 );
471 }
472 1 \@ret;
473 }
474  
475 package Subtitles::Codec::sub2;
476 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
477 1 @ISA=qw(Subtitles::Codec);
478  
479 sub match
480 55 ▶{
Subroutine match: called CC=2 SCAR=6.9
481 55 ▶ $_[1] =~ m/^\[(SUBTITLE|COLF)\]/i or
Branch: 1 branch
BranchTrueFalseTotal
$_[1] =~ /^\[(SUBTITLE|COLF)\]/i or $_[1] =~ /^(\d\d):(\d\d):(\d\d)\.(\d\d),(\d\d):(\d\d):(\d\d)\.(\d\d)/ 1 54 55
482 $_[1] =~ m/^(\d\d):(\d\d):(\d\d)\.(\d\d),(\d\d):(\d\d):(\d\d)\.(\d\d)/;
483 }
484  
485 sub read
486 1 ▶{
Subroutine read: called CC=7 SCAR=19.5
487 1 my ( $self, $sub, $content) = @_;
488  
489 1 my $line = 0;
490 # [INFORMATION]
491 # [AUTHOR]
492 # [SOURCE]
493 # [PRG]
494 # [FILEPATH]
495 # [DELAY]
496 # [CD TRACK]
497 # [COMMENT]
498 # [END INFORMATION]
499 #
500 # [SUBTITLE]
501 # [COLF]&HFFFFFF,[STYLE]no,[SIZE]18,[FONT]Arial
502 # 00:04:10.26,00:04:13.57
503 # Welcome to Gattaca.
504  
505 1 my $from = $sub->{from};
506 1 my $to = $sub->{to};
507 1 my $text = $sub->{text};
508 1 my @header;
509  
510 1 my $read_header = 1;
511 1 my $state = 0;
512  
513 1 for ( @$content) {
514 17 $line++;
515 17 ▶ if ( $read_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_header) { } 13 4 17
516 13 ▶ if ( m/^(\d\d):(\d\d):(\d\d)\.(\d\d)\,(\d\d):(\d\d):(\d\d)\.(\d\d)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d\d):(\d\d):(\d\d)\.(\d\d)\,(\d\d):(\d\d):(\d\d)\.(\d\d)/) 1 12 13
517 1 $read_header = 0;
518 1 goto BODY;
519 }
520 12 push @header, $_;
521 } else {
522 BODY:
523 5 ▶ if ( $state == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 0) { } 3 2 5
524 3 ▶ next unless length;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 1 2 3
525 2 ▶ die "Invalid timing at line $line\n" unless
Branch: 1 branch
BranchTrueFalseTotal
unless /^(\d\d):(\d\d):(\d\d)\.(\d\d)\,(\d\d):(\d\d):(\d\d)\.(\d\d)/ 0 2 2
526 m/^(\d\d):(\d\d):(\d\d)\.(\d\d)\,(\d\d):(\d\d):(\d\d)\.(\d\d)/;
527 2 push @$from, Subtitles::hms2time( $1, $2, $3, $4 * 10);
528 2 push @$to, Subtitles::hms2time( $5, $6, $7, $8 * 10);
529 2 $state = 1;
530 } else {
531 2 s/\[br\]\s*/\n/g;
532 2 push @$text, $_;
533 2 $state = 0;
534 }
535 }
536 }
537  
538 1 $sub->{sub2}->{header} = \@header;
539 1 1;
540 }
541  
542 sub write
543 1 ▶{
Subroutine write: called CC=3 SCAR=11.0
544 1 my ( $self, $sub) = @_;
545  
546 1 my $n = @{$sub->{text}};
547 1 my $i;
548 my @ret;
549 1 ▶ if ( $sub->{sub2}->{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub->{'sub2'}{'header'}) { } 0 1 1
550 0 @ret = @{$sub->{sub2}->{header}};
551 } else {
552 1 @ret = split "\n", <<HEADER;
553 [INFORMATION]
554 [AUTHOR]
555 [SOURCE]
556 [PRG]
557 [FILEPATH]
558 [DELAY]
559 [CD TRACK]
560 [COMMENT]
561 [END INFORMATION]
562  
563 [SUBTITLE]
564 [STYLE]no,[SIZE]18
565 HEADER
566 }
567
568 1 my $from = $sub->{from};
569 1 my $to = $sub->{to};
570 1 my $text = $sub->{text};
571 1 ▶ for ( $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 2 1 3
572 2 my ($fh,$fm,$fs,$fms) = Subtitles::time2hms($from->[$i]);
573 2 my ($th,$tm,$ts,$tms) = Subtitles::time2hms($to->[$i]);
574 2 $fms = int ( $fms / 10);
575 2 $tms = int ( $tms / 10);
576 2 my $t = $text->[$i];
577 2 $t =~ s/\n/[br]/g;
578 2 push @ret,
579 sprintf ( "%02d:%02d:%02d.%02d,%02d:%02d:%02d.%02d",
580 $fh,$fm,$fs,$fms,
581 $th,$tm,$ts,$tms
582 ),
583 $t,
584 ''
585 ;
586 }
587 1 \@ret;
588 }
589  
590 package Subtitles::Codec::smi;
591 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
592 1 @ISA=qw(Subtitles::Codec);
593  
594 sub match
595 55 ▶{
Subroutine match: called CC=1 SCAR=0.0
596 55 $_[1] =~ m/^<SAMI>/i;
597 }
598  
599 sub read
600 1 ▶{
Subroutine read: called CC=19 SCAR=29.6
601 1 my ( $self, $sub, $content) = @_;
602  
603 # <SAMI>
604 # <HEAD>
605 # <STYLE TYPE="Text/css">
606 # <!--
607 # P {margin-left: 29pt; margin-right: 29pt; font-size: 24pt; text-align: center; font-family: Tahoma; font-weight: bold; color: #FFFFFF; background-color: #FFFFFF;}
608 # .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}
609 # -->
610 # </STYLE>
611 # </HEAD>
612 # <BODY>
613 # <SYNC START=2002>
614 # <P CLASS=SUBTTL>Juon - A curse born of a strong grudge held by someone<br>who died.
615 # <SYNC START=7707>
616 # <P CLASS=SUBTTL>&nbsp;
617 # <SYNC START=7741>
618 # </BODY>
619 # </SAMI>
620  
621 1 my $from = $sub->{from};
622 1 my $to = $sub->{to};
623 1 my $text = $sub->{text};
624 1 my (@header,@footer);
625  
626 1 my $read_header = 1;
627 1 my $read_footer = 0;
628  
629 1 my $body = '';
630  
631 # extract body to inspect closer
632 1 for ( @$content) {
633 29 ▶ if ( $read_header) {
Branch: 2 branches
BranchTrueFalseTotal
if ($read_header) { } 19 10 29
elsif ($read_footer) { } 1 9 10
634 19 ▶ if ( m/<BODY>/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/<BODY>/i) 1 18 19
635 1 $read_header = 0;
636 }
637 19 push @header, $_;
638 } elsif ( $read_footer) {
639 1 push @footer, $_;
640 } else {
641 9 ▶ if ( m/<\/BODY>/) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[</BODY>]) 1 8 9
642 1 push @footer, $_;
643 1 $read_footer = 1;
644 1 next;
645 }
646 8 $body .= $_;
647 }
648 }
649
650 # parse body
651 1 my $sync = 0;
652 1 my $line = '';
653 1 ▶ while ( $body =~ m/\G(?:(?:(\s*)<\s*([^\>]*)\s*>)|([^<>]*))/gcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\G(?:(?:(\s*)<\s*([^\>]*)\s*>)|([^<>]*))/cgs) 13 1 14
654 13 ▶ if ( defined $2 and length $2) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $2 and length $2) { } 8 5 13
elsif (defined $3 and length $3) { } 4 1 5
Condition66%2/3
defined $2 and length $2
!ll&&!rl&&r
508
MC/DC50%1/2
defined $2 and length $2
defined $2 length $2
Truth table66%2/3
A and B
A= defined $2B= length $2
ABresult
0X0
100
111
655 8 my $t = $1;
656 8 $_ = $2;
657 8 ▶ if ( m/^sync\s+start\s*=\s*(\d+)/i) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^sync\s+start\s*=\s*(\d+)/i) { } 4 4 8
elsif (/^p\s+class\s*\=\s*(\S+)/i) { } 4 0 4
elsif (/^\s*br\s*/i) { } 0 0 0
658 $sub->{smi}->{s1gap} = length $t
659 4 ▶ unless defined $sub->{smi}->{s1gap};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub->{"smi"}{"s1gap"} 1 3 4
660 4 my $s = $1;
661 4 ▶ die "Inconsistency near '$_' ( is less than previous sync $sync )\n"
Branch: 1 branch
BranchTrueFalseTotal
if $s < $sync 0 4 4
662 if $s < $sync;
663 4 ▶ if ( $line !~ /^[\n\s]*$/s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^[\n\s]*$/s) 3 1 4
664 3 $line =~ s/[\n\s]+$//s;
665 3 push @$from, $sync / 1000;
666 3 push @$to, $s / 1000;
667 3 push @$text, $line;
668 }
669 4 $sync = $s;
670 4 $line = '';
671 } elsif ( m/^p\s+class\s*\=\s*(\S+)/i) {
672 $sub->{smi}->{s2gap} = length $t
673 4 ▶ unless defined $sub->{smi}->{s2gap};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub->{"smi"}{"s2gap"} 1 3 4
674 $sub-> {smi}-> {class} = $1
675 4 ▶ unless defined $sub->{smi}->{class};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub->{"smi"}{"class"} 1 3 4
676 } elsif ( m/^\s*br\s*/i) {
677 0 $line .= "\n";
678 }
679 ▶ } elsif ( defined $3 and length $3) {
Condition66%2/3
defined $3 and length $3
!ll&&!rl&&r
014
MC/DC50%1/2
defined $3 and length $3
defined $3 length $3
Truth table66%2/3
A and B
A= defined $3B= length $3
ABresult
0X0
100
111
680 4 $_ = $3;
681 4 s/&nsbp;/ /g;
682 4 $line .= $_;
683 }
684 }
685  
686 1 $sub->{smi}->{header} = \@header;
687 1 $sub->{smi}->{footer} = \@footer;
688 1 return 1;
689 }
690  
691 sub write
692 1 ▶{
Subroutine write: called CC=9 SCAR=23.4
693 1 my ( $self, $sub) = @_;
694  
695 1 my $n = @{$sub->{text}};
696 1 my $i;
697 my @ret;
698 1 my $from = $sub->{from};
699 1 my $to = $sub->{to};
700 1 my $text = $sub->{text};
701  
702 1 ▶ my $smi_class = defined ($sub->{smi}->{class}) ? $sub->{smi}->{class} : 'SUBTTL';
Branch: 1 branch
BranchTrueFalseTotal
defined $sub->{'smi'}{'class'} ? : 0 1 1
703 1 ▶ if ( $sub->{smi}->{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub->{'smi'}{'header'}) { } 0 1 1
704 0 @ret = @{$sub->{smi}->{header}};
705 } else {
706 1 @ret = split "\n", <<HEADER;
707 <SAMI>
708 <HEAD>
709 <STYLE TYPE="Text/css">
710 <!--
711 P {
712 margin-left: 29pt;
713 margin-right: 29pt;
714 font-size: 24pt;
715 text-align: center;
716 font-family: Tahoma;
717 font-weight: bold;
718 color: #FFFFFF;
719 background-color: #FFFFFF;
720 }
721 .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}
722 -->
723 </STYLE>
724 </HEAD>
725 <BODY>
726 HEADER
727 }
728  
729 1 ▶ my $s1 = ' ' x ( $sub->{smi}->{s1gap} || 0);
Condition50%1/2
$sub->{'smi'}{'s1gap'} || 0
l!l
01
MC/DC0%0/1
$sub->{'smi'}{'s1gap'} || 0
$sub->{'smi'}{'s1gap'}
Truth table50%1/2
A
A= $sub->{'smi'}{'s1gap'}
Aresult
11
00
730 1 ▶ my $s2 = ' ' x ( $sub->{smi}->{s2gap} || 0);
Condition50%1/2
$sub->{'smi'}{'s2gap'} || 0
l!l
01
MC/DC0%0/1
$sub->{'smi'}{'s2gap'} || 0
$sub->{'smi'}{'s2gap'}
Truth table50%1/2
A
A= $sub->{'smi'}{'s2gap'}
Aresult
11
00
731 1 ▶ for ( $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 2 1 3
732 2 my $f = int($$from[$i] * 1000 + .5);
733 2 my $t = int($$to[$i] * 1000 + .5);
734 2 my $x = $$text[$i];
735 2 $x =~ s/\n/<br>/g;
736 2 push @ret,
737 "$s1<SYNC Start=$f>",
738 "$s2<P Class=$smi_class>$x";
739 2 ▶ push @ret,
Branch: 1 branch
BranchTrueFalseTotal
if $i == $n - 1 or int $$from[$i + 1] * 1000 + "0.5" != $t 2 0 2
Condition66%2/3
$i == $n - 1 or int $$from[$i + 1] * 1000 + "0.5" != $t
l!l&&r!l&&!r
110
MC/DC0%0/2
$i == $n - 1 or int $$from[$i + 1] * 1000 + "0.5" != $t
$i == $n - 1 int $$from[$i + 1] * 1000 + "0.5" != $t
Truth table66%2/3
A or B
A= $i == $n - 1B= int $$from[$i + 1] * 1000 + "0.5" != $t
ABresult
1X1
011
000
740 "$s1<SYNC Start=$t>",
741 "$s2<P Class=$smi_class>&nbsp;"
742 if $i == $n - 1 || int($$from[$i+1] * 1000 + .5) != $t;
743 ;
744 }
745 1 ▶ if ( $sub->{smi}->{footer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub->{'smi'}{'footer'}) { } 0 1 1
746 0 push @ret, @{$sub->{smi}->{footer}};
747 } else {
748 1 push @ret, split "\n", <<FOOTER;
749 </BODY>
750 </SAMI>
751 FOOTER
752 }
753 1 \@ret;
754 }
755  
756 sub downgrade
757 0 ▶{
Subroutine downgrade: not called CC=2 SCAR=17.9
758 0 for ( @{$_[1]->{text}}) {
759 0 s/<[^\>]*>//g;
760 0 s/{[^\}]*}//g;
761 }
762 }
763  
764 package Subtitles::Codec::idx;
765 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
766 1 @ISA=qw(Subtitles::Codec);
767  
768 sub match
769 55 ▶{
Subroutine match: called CC=1 SCAR=0.0
770 55 $_[1] =~ m/^\s*\#\s*VobSub index file/
771 }
772  
773 sub read
774 0 ▶{
Subroutine read: not called CC=5 SCAR=34.0
775 0 my ( $self, $sub, $content) = @_;
776  
777 0 my $line = 0;
778 # # VobSub index file, v7 (do not modify this line!)
779 # #
780 # # To repair desyncronization, you can insert gaps this way:
781 # # (it usually happens after vob id changes)
782 # #
783 # # delay: [sign]hh:mm:ss:ms
784 # #
785 # # Where:
786 # # [sign]: +, - (optional)
787 # # hh: hours (0 <= hh)
788 # # mm/ss: minutes/seconds (0 <= mm/ss <= 59)
789 # # ms: milliseconds (0 <= ms <= 999)
790 # #
791 # # Note: You can't position a sub before the previous with a negative value.
792 # #
793 # # You can also modify timestamps or delete a few subs you don't like.
794 # # Just make sure they stay in increasing order.
795 #
796 #
797 # # Settings
798 #
799 # # Original frame size
800 # size: 720x576
801 #
802 # # Origin, relative to the upper-left corner, can be overloaded by aligment
803 # org: 0, 0
804 #
805 # # Image scaling (hor,ver), origin is at the upper-left corner or at the alignment coord (x, y)
806 # scale: 100%, 100%
807 #
808 # # Alpha blending
809 # alpha: 100%
810 #
811 # # Smoothing for very blocky images (use OLD for no filtering)
812 # smooth: OFF
813 #
814 # # In millisecs
815 # fadein/out: 50, 50
816 #
817 # # Force subtitle placement relative to (org.x, org.y)
818 # align: OFF at LEFT TOP
819 #
820 # # For correcting non-progressive desync. (in millisecs or hh:mm:ss:ms)
821 # # Note: Not effective in DirectVobSub, use "delay: ... " instead.
822 # time offset: 0
823 #
824 # # ON: displays only forced subtitles, OFF: shows everything
825 # forced subs: OFF
826 #
827 # # The original palette of the DVD
828 # palette: 0000e1, e83f07, 000000, fdfdfd, 033a03, ea12eb, faff1a, 095d76, 7c7c7c, e0e0e0, 701f03, 077307, 00006c, cc0ae9, d2ab0f, 730972
829 #
830 # # Custom colors (transp idxs and the four colors)
831 # custom colors: OFF, tridx: 1000, colors: fdfdfd, 000000, e0e0e0, faff1a
832 #
833 # # Language index in use
834 # langidx: 0
835 #
836 # # Dansk
837 # id: da, index: 0
838 # # Decomment next line to activate alternative name in DirectVobSub / Windows Media Player 6.x
839 # # alt: Dansk
840 # # Vob/Cell ID: 3, 1 (PTS: 0)
841 # timestamp: 00:00:44:280, filepos: 000000000
842 # timestamp: 00:00:50:520, filepos: 000003000
843  
844 0 my $from = $sub->{from};
845 0 my $to = $sub->{to};
846 0 my $text = $sub->{text};
847 0 my @header;
848  
849 0 my $read_header = 1;
850 0 my $state = 0;
851  
852 0 my @comments;
853  
854 0 for ( @$content) {
855 0 ▶ if ( m/^\s*timestamp\:\s*(\d\d)\:(\d\d)\:(\d\d)\:(\d+).*?filepos\:\s*(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*timestamp\:\s*(\d\d)\:(\d\d)\:(\d\d)\:(\d+).*?filepos\:\s*(.*)$/) { } 0 0 0
856 0 push @$from, Subtitles::hms2time( $1, $2, $3, $4);
857 0 push @$text, $5;
858 } else {
859 0 push @comments, [ scalar @$from, $_ ];
860 }
861 0 $line++;
862 }
863  
864 0 ▶ for ( $line = 0; $line < @$from - 1; $line++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line < @$from - 1) 0 0 0
865 0 $$to[$line] = $$from[$line + 1] - 0.002;
866 }
867 0 ▶ push @$to, $$from[-1] + 2.0 if @$from;
Branch: 1 branch
BranchTrueFalseTotal
if @$from 0 0 0
868  
869 0 $sub->{idx}->{comments} = \@comments;
870  
871 0 1;
872 }
873  
874 sub write
875 0 ▶{
Subroutine write: not called CC=5 SCAR=34.0
876 0 my ( $self, $sub) = @_;
877  
878 die "The idx format subtitles cannot be created from the other formats\n"
879 0 ▶ unless $sub->{idx}->{comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $sub->{"idx"}{"comments"} 0 0 0
880  
881 0 my $from = $sub->{from};
882 0 my $to = $sub->{to};
883 0 my $text = $sub->{text};
884 0 my $c = $sub->{idx}->{comments};
885 0 my ( $i, $j);
886 0 my $n = @$text;
887 0 my @ret;
888 0 ▶ for ( $i = $j = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
889 0 ▶ push @ret, $$c[$j++][1] while $j < @$c and $$c[$j][0] <= $i;
Branch: 1 branch
BranchTrueFalseTotal
$j < @$c and $c->[$j][0] <= $i and do { push @ret, $$c[$j++][1] } 0 0 0
Condition0%0/3
$j < @$c and $c->[$j][0] <= $i
!ll&&!rl&&r
000
MC/DC0%0/2
$j < @$c and $c->[$j][0] <= $i
$j < @$c $c->[$j][0] <= $i
Truth table0%0/3
A and B
A= $j < @$cB= $c->[$j][0] <= $i
ABresult
0X0
100
111
890 0 push @ret, sprintf( "timestamp: %02d:%02d:%02d:%03d, filepos: %s",
891 Subtitles::time2hms($from->[$i]), $text->[$i]);
892 }
893 0 \@ret;
894 }
895  
896 1 1;
897  
898 =pod
899  
900 =head1 NAME
901  
902 Subtitles - handle video subtitles in various text formats
903  
904 =head1 DESCRIPTION
905  
906 Video files (avi mpeg etc) are sometimes accompanied with subtitles, which are
907 currently very popular as text files. C<Subtitles> provides means for simple
908 loading, re-timing, and storing these subtitle files. A command-line tool
909 F<subs> for the same purpose and using C<Subtitles> interface is included in
910 the distribution.
911  
912 The module supports C<srt>, C<sub>, C<smi>, and C<mdvd> subtitle formats.
913  
914 Time values are floats, in seconds with millisecond precision.
915  
916 =head1 SYNOPSIS
917  
918 use Subtitles;
919
920 my $sub = Subtitles->new();
921  
922 open F, 'Ichi The Killer.sub' or die "Cannot read:$!";
923 die "Cannot load:$@\n" unless $sub-> load(\*F);
924 close F;
925  
926 # back two minutes
927 $sub-> shift( $sub-> parse_time('-02:00'));
928  
929 # re-frame from 25 fps
930 $sub-> scale( 23.976 / 25 );
931  
932 # or both
933 $sub-> transform( -120, 0.96);
934 $sub-> transform( -120, 0.96, 0, $sub-> length - 60);
935  
936 # split in 2
937 my ( $part1, $part2) = $sub-> split( $self-> length / 2);
938  
939 # join back with 5-second gap
940 $part1-> join( $part2, 5);
941  
942 # save
943 open F, "> out.sub" or die "Cannot write:$!\n";
944 $part1-> save( \*F);
945 close F;
946  
947 # report
948 print "sub is ", time2str( $sub-> length);
949  
950 =head1 API
951  
952 =head2 Package methods
953  
954 =over
955  
956 =item codecs
957  
958 Returns array of installed codecs.
959  
960 =item hms2time HOURS, MINUTES, SECONDS, MILLISECONDS
961  
962 Combines four parameters into float time in seconds.
963  
964 =item time2hms TIME
965  
966 Splits time into four integers, - hours, minutes, seconds, and milliseconds.
967 If time is less than zero, zero times are returned.
968  
969 =item time2shms
970  
971 Splits time into five integers, - time sign, hours, minutes, seconds, and milliseconds.
972  
973 =item time2str TIME
974  
975 Converts time to a human-readable string.
976  
977 =back
978  
979 =head2 Object methods
980  
981 =over
982  
983 =item clear
984  
985 Removes all content
986  
987 =item codec [ STRING ]
988  
989 If STRING is not defined, returns currently associated codec.
990 Otherwise, sets the new codec in association. The STRING is
991 the codec's package name, such as C<Subtitles::Codec::srt>.
992  
993 =item dup [ CLEAR ]
994  
995 Duplicates object instance in deep-copy fashion. If CLEAR
996 flag is set, timeframes are not copied.
997  
998 =item join GUEST, GAP
999  
1000 Adds content of object GUEST at the end of the list of subtitles with GAP in seconds.
1001  
1002 =item length
1003  
1004 Returns length of subtitle span.
1005  
1006 =item load FH [ CODEC ]
1007  
1008 Reads subtitle content into object. If successful, returns 1;
1009 otherwise undef is returned and C<$@> contains the error.
1010  
1011 By default, tries to deduce which codec to use; to point the
1012 selection explicitly CODEC string is to be used.
1013  
1014 =item lines
1015  
1016 Returns number of subtitle cues.
1017  
1018 =item new
1019  
1020 Creates a new instance. To force a particular
1021 codec, supply C<codec> string here.
1022  
1023 =item parse_time STRING
1024  
1025 Parses STRING which is either a C<[[HH:]MM:]SS[,MSEC]> string
1026 or string in a format specific to a codec, for example, number
1027 of a frame.
1028  
1029 =item rate FPS
1030  
1031 Forces a particluar frame-per-second rate, if a codec
1032 can make use of it.
1033  
1034 =item save FH
1035  
1036 Writes content of instance into FH file handle,
1037 using the associated codec.
1038  
1039 =item scale A
1040  
1041 Changes time-scale. If A is 2, the subtitles
1042 go off 2 times slower, if 0.5 - two times faster, etc.
1043  
1044 =item shift B
1045  
1046 Shifts timings by B seconds. B can be negative.
1047  
1048 =item split TIME
1049  
1050 Splits the content of the instance between
1051 two newly created instances of the same class,
1052 by TIME, and returns these. The both resulting
1053 subtitles begin at time 0.
1054  
1055 =item transform A, B [FROM, TO]
1056  
1057 Applies linear transformation to the time-scale,
1058 such as C<u = At + B> where C<t> is the original
1059 time and C<u> is the result. If FROM and TO
1060 brackets are set, the changes are applied only
1061 to the lines in the timeframe between these.
1062  
1063 =back
1064  
1065 =head1 BUGS
1066  
1067 This is alpha code, more a proof-of-concept rather
1068 that anything else, so most surely bugs are lurking.
1069  
1070 Anyway: not all subtitle types are recognized.
1071 The modules doesn't handle multi-language subtitles.
1072  
1073 =head1 SEE ALSO
1074  
1075 L<subs> - command-line wrapper for this module
1076  
1077 =head1 THANKS
1078  
1079 L<http://dvd.box.sk/>, L<http://subs.2ya.com>.
1080  
1081 =head1 AUTHOR
1082  
1083 Dmitry Karasik, E<lt>dmitry@karasik.eu.orgE<gt>.
1084  
1085 =cut
↑ Summary