Browse Source

* Subtitle::Format::MSub : use generic events

master
Alex 'AdUser' Z 6 years ago
parent
commit
a247f326fb
  1. 38
      lib/Subtitle/Format/MSub.pm
  2. 12
      t/format-msub.t

38
lib/Subtitle/Format/MSub.pm

@ -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>

12
t/format-msub.t

@ -19,11 +19,11 @@ EOF
my $cnt = $msub->from_string($sample); my $cnt = $msub->from_string($sample);
is($cnt, 2); is($cnt, 2);
my @events = @{ $msub->{events} }; my @events = @{ $msub->{events} };
is($events[0]->{timing}->[0], 2.0); is($events[0]->t_start, 2.0);
is($events[0]->{timing}->[1], 3.0); is($events[0]->t_end, 3.0);
is($events[0]->{text}, "Test event"); is($events[0]->text, "Test event");
is($events[1]->{timing}->[0], 3.0); is($events[1]->t_start, 3.0);
is($events[1]->{timing}->[1], 4.0); is($events[1]->t_end, 4.0);
is($events[1]->{text}, "Test event\nwith line wrap"); is($events[1]->text, "Test event\nwith line wrap");
exit 0; exit 0;

Loading…
Cancel
Save