diff --git a/bin/ssa-retime b/bin/ssa-retime index e705dce..ed9cccc 100755 --- a/bin/ssa-retime +++ b/bin/ssa-retime @@ -55,6 +55,7 @@ sub add_point { my $err = $p->parse($val); die "$err\n" if $err; push @points, $p; + @points = sort { $a->time <=> $b->time } @points; } ########### init ################### @@ -100,7 +101,7 @@ if ($mode eq 'framerate') { $opts{infile} or die "No input file\n"; -my $ssa = Subtitle::Format::SSA->new(debug => !!$opts{loglevel}); +my $ssa = Subtitle::Format::SSA->new(debug => ($opts{loglevel} >= 2 ? 1 : 0)); unless ($ssa->from_file($opts{infile})) { foreach my $line (@{ $ssa->{log} }) { print $line, "\n"; @@ -108,10 +109,6 @@ unless ($ssa->from_file($opts{infile})) { exit 1; } -if (@points) { - @points = sort { $a->time <=> $b->time } @points; -} - if ($mode eq 'framerate') { my $mod = $opts{inrate} / $opts{outrate}; foreach my $e (@{ $ssa->events }) { @@ -125,8 +122,8 @@ if ($mode eq 'framerate') { next if $p->time > $e->t_start; $e->t_start($e->t_start + $p->shift); $e->t_end ($e->t_end + $p->shift); - } # foreach - } # while + } # foreach @events + } # while @points } elsif ($opts{amode} eq 'rst') { while (my $p = shift @points) { # use time of next point as upper limit for current @@ -136,8 +133,8 @@ if ($mode eq 'framerate') { next if $end and $end <= $e->t_start; # too late $e->t_start($e->t_start + $p->shift); $e->t_end ($e->t_end + $p->shift); - } # foreach - } # while + } # foreach @events + } # while @points } } elsif ($mode eq 'drift') { if ($points[0]->time >= 0.2) { @@ -155,18 +152,22 @@ if ($mode eq 'framerate') { my $p = Subtitle::TimePoint->new(time => $maxtime, shift => 0.0); push @points, $p; } - # dump points list - print "Final points list:\n"; - foreach my $p (@points) { - printf "- %d:%02d:%02d.%03d ~ %.3f\n", make_timing($p->time), $p->shift; + if ($opts{loglevel} >= 1) { + # dump points list + print "Final points list:\n"; + foreach my $p (@points) { + printf "- %d:%02d:%02d.%03d ~ %.3f\n", make_timing($p->time), $p->shift; + } + print "-" x 30, "\n"; } - print "-" x 30, "\n"; # real retime $ssa->sort_events; foreach my $e (@{ $ssa->events }) { while ($points[1]->time <= $e->t_start) { - printf "move timepoints: %d:%02d:%02d.%03d (endpoint) <= %d:%02d:%02d.%03d (event)\n", - make_timing($points[1]->time), make_timing($e->t_start); + if ($opts{loglevel} >= 2) { + printf "move timepoints: %d:%02d:%02d.%03d (endpoint) <= %d:%02d:%02d.%03d (event)\n", + make_timing($points[1]->time), make_timing($e->t_start); + } shift @points; } my ($a, $b) = @points; @@ -179,6 +180,7 @@ if ($mode eq 'framerate') { : ($a->shift + $abs * $pct); $e->t_start($e->t_start + $mod); $e->t_end ($e->t_end + $mod); + next unless $opts{loglevel} >= 2; printf "- %d:%02d:%02d.%03d ~ %.3f\n", make_timing($e->t_start), $mod; } }