From 37a6a6b45ba63098f5858e715c7182f76a675799 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Thu, 7 Jul 2016 16:48:28 +1000 Subject: [PATCH] * Subtitle::MSub : implement build() --- lib/Subtitle/MSub.pm | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/Subtitle/MSub.pm b/lib/Subtitle/MSub.pm index b382164..2e96b27 100644 --- a/lib/Subtitle/MSub.pm +++ b/lib/Subtitle/MSub.pm @@ -4,7 +4,7 @@ use strict; use warnings; use utf8; -use Subtitle::Utils qw(:string); +use Subtitle::Utils qw(:string round); use base 'Subtitle::BASE'; @@ -80,6 +80,22 @@ sub parse { return scalar @{ $self->{events} }; } +sub build { + my ($self) = @_; + my @lines; + + 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 $text = ($e->{text} =~ s{$self->{eol}}{|}gr); + push @lines, sprintf "{%d}{%d}%s", $show, $hide, $text; + } + push @lines, ""; + + return join($self->{eol} => @lines); +} + 1; =pod @@ -110,4 +126,10 @@ Recognized keys are: Parse subtitle from array of lines. Returns parsed events count on success, -1 on error +=head2 C + + my $text = $msub->build(); + +Builds parsed subtitle to string. + =cut