|
|
@ -2,9 +2,10 @@ package Subtitle::MSub; |
|
|
|
|
|
|
|
|
|
|
|
use strict; |
|
|
|
use strict; |
|
|
|
use warnings; |
|
|
|
use warnings; |
|
|
|
use feature qw(switch); |
|
|
|
|
|
|
|
use utf8; |
|
|
|
use utf8; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Subtitle::Utils qw(:string); |
|
|
|
|
|
|
|
|
|
|
|
use base 'Subtitle::BASE'; |
|
|
|
use base 'Subtitle::BASE'; |
|
|
|
|
|
|
|
|
|
|
|
sub new { |
|
|
|
sub new { |
|
|
@ -30,11 +31,11 @@ sub parse { |
|
|
|
my $linenum = 0; |
|
|
|
my $linenum = 0; |
|
|
|
my $event; |
|
|
|
my $event; |
|
|
|
|
|
|
|
|
|
|
|
foreach my $line (@$lines) { |
|
|
|
foreach my $line (@{ $lines }) { |
|
|
|
$linenum++; |
|
|
|
$linenum++; |
|
|
|
$line = $self->chomp($line); |
|
|
|
chomp_all($line); |
|
|
|
$line = $self->trim($line); |
|
|
|
trim($line); |
|
|
|
next unless ($line); |
|
|
|
next unless $line; |
|
|
|
my ($start, $end, $rest) = ($line =~ m/{(\d+)}\s*{(\d+)}\s*(.+)/o); |
|
|
|
my ($start, $end, $rest) = ($line =~ m/{(\d+)}\s*{(\d+)}\s*(.+)/o); |
|
|
|
# expected: garbage |
|
|
|
# expected: garbage |
|
|
|
unless ($start and $end and $rest) { |
|
|
|
unless ($start and $end and $rest) { |
|
|
@ -42,7 +43,7 @@ sub parse { |
|
|
|
next; |
|
|
|
next; |
|
|
|
} |
|
|
|
} |
|
|
|
# expected: valid line |
|
|
|
# expected: valid line |
|
|
|
# special case - set fps |
|
|
|
# special case - '{1}{1}XX.YY' line sets fps |
|
|
|
if ($start == 1 and $end == 1) { |
|
|
|
if ($start == 1 and $end == 1) { |
|
|
|
my ($fps) = ($rest =~ m/(\d+(?:\.\d+))/o); |
|
|
|
my ($fps) = ($rest =~ m/(\d+(?:\.\d+))/o); |
|
|
|
unless ($fps) { |
|
|
|
unless ($fps) { |
|
|
@ -50,17 +51,18 @@ sub parse { |
|
|
|
next; |
|
|
|
next; |
|
|
|
} |
|
|
|
} |
|
|
|
if ($fps and $self->{fps}) { |
|
|
|
if ($fps and $self->{fps}) { |
|
|
|
$self->log(warn => "Found fps line at $linenum, but fps already set before"); |
|
|
|
$self->log(warn => "Found fps line at $linenum, but fps already set by user"); |
|
|
|
next; |
|
|
|
next; |
|
|
|
} |
|
|
|
} |
|
|
|
$self->log(debug => "Set fps to $fps and line $linenum"); |
|
|
|
$self->log(debug => "Set fps to $fps and line $linenum"); |
|
|
|
$self->{fps} = $fps; |
|
|
|
$self->{fps} = $fps; |
|
|
|
next; |
|
|
|
next; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
trim($rest); |
|
|
|
$rest =~ s/\|/$self->{eol}/og; |
|
|
|
$rest =~ s/\|/$self->{eol}/og; |
|
|
|
my $event = $self->new_event; |
|
|
|
my $event = $self->new_event; |
|
|
|
$event->{timing} = [$start, $end]; |
|
|
|
$event->{timing} = [$start, $end]; |
|
|
|
$event->{text} = $self->trim($rest); |
|
|
|
$event->{text} = $rest; |
|
|
|
push @{ $self->{events} }, $event; |
|
|
|
push @{ $self->{events} }, $event; |
|
|
|
undef $event; |
|
|
|
undef $event; |
|
|
|
} |
|
|
|
} |
|
|
@ -79,3 +81,33 @@ sub parse { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
1; |
|
|
|
1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=pod |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head1 NAME |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Subtitle::MSub -- micsrosub subtitle format |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head1 METHODS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head2 C<new> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $msub = Subtitle::MSub->new(%opts); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Creates new object, takes list of options as argument. |
|
|
|
|
|
|
|
Recognized keys are: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* debug -- write debug messages to log (default: 0) |
|
|
|
|
|
|
|
* eol -- replace "|" symbol within subtitle text with this chars (default: "\n") |
|
|
|
|
|
|
|
* fps -- sets FPS for subtitle (default: not set, try detect from file, then use default) |
|
|
|
|
|
|
|
* default_fps -- FPS for subtitle, if not found in file, and not set by user (default: 25.0) |
|
|
|
|
|
|
|
* convert_timing -- convert frame numbers to seconds (default: 1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=head2 C<parse> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $events_count = $msub->parse(\@lines); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parse subtitle from array of lines. |
|
|
|
|
|
|
|
Returns parsed events count on success, -1 on error |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=cut |
|
|
|