diff --git a/bin/ssa-retime b/bin/ssa-retime index 38f4b76..e705dce 100755 --- a/bin/ssa-retime +++ b/bin/ssa-retime @@ -8,6 +8,7 @@ use utf8; use Getopt::Long; use Subtitle::Format::SSA; use Subtitle::TimePoint; +use Subtitle::Utils ':timing'; my @points; @@ -141,12 +142,45 @@ if ($mode eq 'framerate') { } elsif ($mode eq 'drift') { if ($points[0]->time >= 0.2) { # add starting point - my $p = Subtitle::TimePoint->new; - $p->parse('0/0'); + my $p = Subtitle::TimePoint->new(time => 0.0, shift => 0.0); unshift @points, $p; } - # add endpoint - ...; + # add endpoint, if needed + my $maxtime = 0.0; + foreach my $e (@{ $ssa->events }) { + next unless $e->t_end > $maxtime; + $maxtime = $e->t_end; + } + if ($maxtime > $points[-1]->time) { + 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; + } + 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); + shift @points; + } + my ($a, $b) = @points; + my $pct = ($e->t_start - $a->time) / ($b->time - $a->time); + my $abs = ($a->shift > $b->shift) + ? ($a->shift - $b->shift) + : ($b->shift - $a->shift); + my $mod = ($a->shift > $b->shift) + ? ($a->shift - $abs * $pct) + : ($a->shift + $abs * $pct); + $e->t_start($e->t_start + $mod); + $e->t_end ($e->t_end + $mod); + printf "- %d:%02d:%02d.%03d ~ %.3f\n", make_timing($e->t_start), $mod; + } } if ($opts{outfile}) {