|
|
@ -4,6 +4,7 @@ use strict; |
|
|
|
use warnings; |
|
|
|
use warnings; |
|
|
|
use utf8; |
|
|
|
use utf8; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Subtitle::Event; |
|
|
|
use Subtitle::Utils qw(:string round); |
|
|
|
use Subtitle::Utils qw(:string round); |
|
|
|
|
|
|
|
|
|
|
|
use base 'Subtitle::Format'; |
|
|
|
use base 'Subtitle::Format'; |
|
|
@ -11,12 +12,9 @@ use base 'Subtitle::Format'; |
|
|
|
sub new { |
|
|
|
sub new { |
|
|
|
my ($class, %args) = @_; |
|
|
|
my ($class, %args) = @_; |
|
|
|
my $self = { |
|
|
|
my $self = { |
|
|
|
debug => 0, |
|
|
|
debug => $args{debug} || 0, |
|
|
|
eol => "\n", |
|
|
|
fps => $args{fps} || 25.0, |
|
|
|
fps => undef, |
|
|
|
eol => $args{eol} || "\n", |
|
|
|
default_fps => 25.0, |
|
|
|
|
|
|
|
convert_timing => 1, |
|
|
|
|
|
|
|
%args, |
|
|
|
|
|
|
|
events => [], |
|
|
|
events => [], |
|
|
|
log => [], |
|
|
|
log => [], |
|
|
|
}; |
|
|
|
}; |
|
|
@ -24,7 +22,7 @@ sub new { |
|
|
|
return bless($self, $class); |
|
|
|
return bless($self, $class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub new_event { return +{ timing => undef, text => undef }; } |
|
|
|
sub new_event { return Subtitle::Event->new; } |
|
|
|
|
|
|
|
|
|
|
|
sub parse { |
|
|
|
sub parse { |
|
|
|
my ($self, $lines) = @_; |
|
|
|
my ($self, $lines) = @_; |
|
|
@ -50,10 +48,6 @@ sub parse { |
|
|
|
$self->log(error => "Expected fps at line $linenum, but found: $rest"); |
|
|
|
$self->log(error => "Expected fps at line $linenum, but found: $rest"); |
|
|
|
next; |
|
|
|
next; |
|
|
|
} |
|
|
|
} |
|
|
|
if ($fps and $self->{fps}) { |
|
|
|
|
|
|
|
$self->log(warn => "Found fps line at $linenum, but fps already set by user"); |
|
|
|
|
|
|
|
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; |
|
|
@ -61,21 +55,12 @@ sub parse { |
|
|
|
trim($rest); |
|
|
|
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->t_start($start / $self->{fps}); |
|
|
|
$event->{text} = $rest; |
|
|
|
$event->t_end ($end / $self->{fps}); |
|
|
|
|
|
|
|
$event->text($rest); |
|
|
|
push @{ $self->{events} }, $event; |
|
|
|
push @{ $self->{events} }, $event; |
|
|
|
undef $event; |
|
|
|
undef $event; |
|
|
|
} |
|
|
|
} |
|
|
|
# set fps if none and recalc timing |
|
|
|
|
|
|
|
$self->{fps} //= $self->{default_fps}; |
|
|
|
|
|
|
|
return scalar @{ $self->{events} } |
|
|
|
|
|
|
|
unless $self->{convert_timing}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$self->log(debug => "Converting frame numbers to time with fps $self->{fps}"); |
|
|
|
|
|
|
|
foreach my $event (@{ $self->{events} }) { |
|
|
|
|
|
|
|
$event->{timing}->[0] /= $self->{fps}; |
|
|
|
|
|
|
|
$event->{timing}->[1] /= $self->{fps}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return scalar @{ $self->{events} }; |
|
|
|
return scalar @{ $self->{events} }; |
|
|
|
} |
|
|
|
} |
|
|
@ -86,8 +71,8 @@ sub build { |
|
|
|
|
|
|
|
|
|
|
|
push @lines, sprintf "{1}{1}%.3f", $self->{fps}; |
|
|
|
push @lines, sprintf "{1}{1}%.3f", $self->{fps}; |
|
|
|
foreach my $e (@{ $self->{events} }) { |
|
|
|
foreach my $e (@{ $self->{events} }) { |
|
|
|
my $show = round($e->{timing}->[0] * $self->{fps}, 0); |
|
|
|
my $show = round($e->t_start * $self->{fps}, 0); |
|
|
|
my $hide = round($e->{timing}->[1] * $self->{fps}, 0); |
|
|
|
my $hide = round($e->t_end * $self->{fps}, 0); |
|
|
|
my $text = ($e->{text} =~ s{$self->{eol}}{|}gr); |
|
|
|
my $text = ($e->{text} =~ s{$self->{eol}}{|}gr); |
|
|
|
push @lines, sprintf "{%d}{%d}%s", $show, $hide, $text; |
|
|
|
push @lines, sprintf "{%d}{%d}%s", $show, $hide, $text; |
|
|
|
} |
|
|
|
} |
|
|
@ -115,8 +100,7 @@ Recognized keys are: |
|
|
|
|
|
|
|
|
|
|
|
* debug -- write debug messages to log (default: 0) |
|
|
|
* debug -- write debug messages to log (default: 0) |
|
|
|
* eol -- replace "|" symbol within subtitle text with this chars (default: "\n") |
|
|
|
* 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) |
|
|
|
* fps -- sets FPS for subtitle (default: 25.0, try detect from file) |
|
|
|
* 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) |
|
|
|
* convert_timing -- convert frame numbers to seconds (default: 1) |
|
|
|
|
|
|
|
|
|
|
|
=head2 C<parse> |
|
|
|
=head2 C<parse> |
|
|
|