From a247f326fb8da1c8b85250aeb7bd33094d5a16a9 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Tue, 24 Jul 2018 03:08:54 +1000 Subject: [PATCH] * Subtitle::Format::MSub : use generic events --- lib/Subtitle/Format/MSub.pm | 38 +++++++++++-------------------------- t/format-msub.t | 12 ++++++------ 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/lib/Subtitle/Format/MSub.pm b/lib/Subtitle/Format/MSub.pm index 774117f..b20ace2 100644 --- a/lib/Subtitle/Format/MSub.pm +++ b/lib/Subtitle/Format/MSub.pm @@ -4,6 +4,7 @@ use strict; use warnings; use utf8; +use Subtitle::Event; use Subtitle::Utils qw(:string round); use base 'Subtitle::Format'; @@ -11,12 +12,9 @@ use base 'Subtitle::Format'; sub new { my ($class, %args) = @_; my $self = { - debug => 0, - eol => "\n", - fps => undef, - default_fps => 25.0, - convert_timing => 1, - %args, + debug => $args{debug} || 0, + fps => $args{fps} || 25.0, + eol => $args{eol} || "\n", events => [], log => [], }; @@ -24,7 +22,7 @@ sub new { return bless($self, $class); } -sub new_event { return +{ timing => undef, text => undef }; } +sub new_event { return Subtitle::Event->new; } sub parse { my ($self, $lines) = @_; @@ -50,10 +48,6 @@ sub parse { $self->log(error => "Expected fps at line $linenum, but found: $rest"); 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->{fps} = $fps; next; @@ -61,21 +55,12 @@ sub parse { trim($rest); $rest =~ s/\|/$self->{eol}/og; my $event = $self->new_event; - $event->{timing} = [$start, $end]; - $event->{text} = $rest; + $event->t_start($start / $self->{fps}); + $event->t_end ($end / $self->{fps}); + $event->text($rest); push @{ $self->{events} }, $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} }; } @@ -86,8 +71,8 @@ sub build { push @lines, sprintf "{1}{1}%.3f", $self->{fps}; foreach my $e (@{ $self->{events} }) { - my $show = round($e->{timing}->[0] * $self->{fps}, 0); - my $hide = round($e->{timing}->[1] * $self->{fps}, 0); + my $show = round($e->t_start * $self->{fps}, 0); + my $hide = round($e->t_end * $self->{fps}, 0); my $text = ($e->{text} =~ s{$self->{eol}}{|}gr); push @lines, sprintf "{%d}{%d}%s", $show, $hide, $text; } @@ -115,8 +100,7 @@ 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) +* fps -- sets FPS for subtitle (default: 25.0, try detect from file) * convert_timing -- convert frame numbers to seconds (default: 1) =head2 C diff --git a/t/format-msub.t b/t/format-msub.t index 4556d25..d597db6 100644 --- a/t/format-msub.t +++ b/t/format-msub.t @@ -19,11 +19,11 @@ EOF my $cnt = $msub->from_string($sample); is($cnt, 2); my @events = @{ $msub->{events} }; -is($events[0]->{timing}->[0], 2.0); -is($events[0]->{timing}->[1], 3.0); -is($events[0]->{text}, "Test event"); -is($events[1]->{timing}->[0], 3.0); -is($events[1]->{timing}->[1], 4.0); -is($events[1]->{text}, "Test event\nwith line wrap"); +is($events[0]->t_start, 2.0); +is($events[0]->t_end, 3.0); +is($events[0]->text, "Test event"); +is($events[1]->t_start, 3.0); +is($events[1]->t_end, 4.0); +is($events[1]->text, "Test event\nwith line wrap"); exit 0;