Browse Source

* Subtitle::SSA : update

master
Alex 'AdUser' Z 9 years ago
parent
commit
ab010ef8de
  1. 41
      lib/Subtitle/SSA.pm

41
lib/Subtitle/SSA.pm

@ -2,9 +2,10 @@ package Subtitle::SSA;
use strict; use strict;
use warnings; use warnings;
use feature qw(switch);
use utf8; use utf8;
use Subtitle::Utils qw(:string :timing);
use base 'Subtitle::BASE'; use base 'Subtitle::BASE';
use constant SSA_STYLE_FMT => [qw(Name Fontname Fontsize PrimaryColour SecondaryColour use constant SSA_STYLE_FMT => [qw(Name Fontname Fontsize PrimaryColour SecondaryColour
@ -107,7 +108,7 @@ sub parse_event {
} }
foreach my $key (qw(start end)) { foreach my $key (qw(start end)) {
next unless $event->{$key}; next unless $event->{$key};
$event->{$key} = $self->parse_timing($event->{$key}); $event->{$key} = parse_timing($event->{$key});
} }
$event->{marked} =~ s/^Marked=\s*//oi $event->{marked} =~ s/^Marked=\s*//oi
if ($self->{type} eq 'ssa'); if ($self->{type} eq 'ssa');
@ -125,27 +126,26 @@ sub parse {
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;
study $line; study $line;
# check section switch # check section switch
if ($line =~ m/^\s*\[(.+)\]\s*/o) { if ($line =~ m/^\s*\[(.+)\]\s*/o) {
my $name = $1; my $name = $1;
given ($name) { if ($name =~ m{script \s+ info}oix) {
when (m/script\s+info/oi) { $section = "header"; } $section = 'header';
when (m/events/oi) { $section = "events"; } } elsif ($name =~ m{events}oi) {
when (m/v4(\+)?\s+styles/oi) { $section = 'events';
$section = "styles"; } elsif ($name =~ m{v4(\+)? \s+ styles}oix) {
unless ($self->{type}) { $section = 'styles';
$self->{type} = ($1) ? 'ass' : 'ssa'; unless ($self->{type}) {
$self->log(warn => "Set type to $self->{type} because of $linenum (fallback mode)"); $self->{type} = ($1) ? 'ass' : 'ssa';
} $self->log(warn => "Set type to $self->{type} because of $linenum (fallback mode)");
}
default {
$self->log(debug => "Unknown section at line $linenum: $line");
} }
} else {
$self->log(debug => "Unknown section at line $linenum: $line");
} }
next; next;
} }
@ -209,7 +209,7 @@ sub build {
my $out = "[Script Info]\n"; my $out = "[Script Info]\n";
# headers # headers
foreach my $h (sort keys($self->{headers})) { foreach my $h (sort keys(%{ $self->{headers} })) {
$out .= sprintf "%s: %s\n", $h, $self->{headers}->{$h}; $out .= sprintf "%s: %s\n", $h, $self->{headers}->{$h};
} }
$out .= "\n"; $out .= "\n";
@ -236,10 +236,13 @@ sub build {
$out .= sprintf "Format: %s\n", join(', ' => @{ $self->SSA_EVENT_FMT }); $out .= sprintf "Format: %s\n", join(', ' => @{ $self->SSA_EVENT_FMT });
} }
@fields = @{ $self->{event_fmt} }; @fields = @{ $self->{event_fmt} };
my ($hrs, $min, $sec, $msec);
foreach my $e (@{ $self->{events} }) { foreach my $e (@{ $self->{events} }) {
my %event = %{$e}; my %event = %{$e};
$event{start} = $self->build_timing($event{start}); ($hrs, $min, $sec, $msec) = make_timing($e->{start});
$event{end} = $self->build_timing($event{end}); $event{start} = sprintf $self->{timing_fmt}, $hrs, $min, $sec, int($msec / 10);
($hrs, $min, $sec, $msec) = make_timing($e->{end});
$event{end} = sprintf $self->{timing_fmt}, $hrs, $min, $sec, int($msec / 10);
$event{marked} =~ s/^/Marked=/o if $self->{type} eq 'ssa'; $event{marked} =~ s/^/Marked=/o if $self->{type} eq 'ssa';
$out .= sprintf "%s: %s\n", ucfirst($e->{type}), join(',' => @event{@fields}); $out .= sprintf "%s: %s\n", ucfirst($e->{type}), join(',' => @event{@fields});
} }

Loading…
Cancel
Save