So your uncle recorded for you this old swedish movie on his DVR, and gave you a copy of it. You transcode it to XVID then realize that, Dammit! you haven't got the subtitles and your swedish really is cranky, so you get the subtitles from some website but alas, they're for some DVD version and begins more quickly (because there aren't the ads), or else.

So you need to edit those thousands of time entries in the .srt file to correct them. There comes my script to the rescue...

Let's say you need to shift the subtitles 3 minutes 42 seconds sooner... Simply do :

subshift /data/subtitle.srt - 3:42 > /data/newsubtitle.srt

Now you may want to make it later by 1 hour, 5 minutes, 32 seconds and bananas :

subshift /data/subtitle.srt + 1:5:32,242 > /data/newsubtitle.srt
Well, that's all folks. Here it is :
#!/usr/bin/perl use strict; use warnings; ############################################# # init my ( $infile, $oper, $shiftime ) = @ARGV; usage() if not ( $infile and $oper and $shiftime); usage() if ( $oper ne "+" and $oper ne "-" ); ############################################# # main open my $fh, "<", $infile or die "error opening '$infile':$!"; $shiftime = time_to_sec($shiftime); while (<$fh>) { if ( m/^(\d{2}:\d{2}:\d{2},\d+)\s\-\-\>\s(\d{2}:\d{2}:\d{2},\d+)/ +) { my ($start, $end) = ( $1, $2); my $startsec = time_to_sec($start); my $endsec = time_to_sec($end); if ( $oper eq "+" ) { $startsec += $shiftime; $endsec += $shiftime; } else { # bug : should check that time stays positive $startsec -= $shiftime; $endsec -= $shiftime; } $start = sec_to_time($startsec); $end= sec_to_time($endsec); print "$start --> $end\r\n"; } else { print $_ ; } } close $fh; ############################################# # subs sub usage { print "usage : $0 <file> <+|-> <duration>\n\n"; print "duration is expressed in hours:minutes:secondes,millisecond +s.\nLeading zeros are optional.\n"; print "result output to stdout.\n"; exit 1; } sub time_to_sec { my $time = shift; my @elems = split(':', $time); my $seconds = pop @elems; my $minutes = pop @elems; my $hours = pop @elems; $seconds =~ s/,/./g; # force to numerical $hours += 0; $minutes += 0; $seconds += 0; # convert to seconds for easier manipulation my $time_sec = ( $hours * 3600 ) + ($minutes * 60 ) + $seconds ; return $time_sec; } sub sec_to_time { my $sectime = shift; my $hours = sprintf( "%02d", int ( $sectime / 3600 ) ); my $minutes = sprintf( "%02d", int ( ( $sectime - ( $hours * 3600 + ) ) / 60 ) ); my $seconds = sprintf( "%02.3f", ( ( $sectime - ( $hours * 3600 ) +) - ( $minutes * 60 )) ); $seconds =~ s/\./,/g; return "$hours:$minutes:$seconds"; }

Edit: applied some corrections as suggested by jwkrahn.


In reply to re-syncing these subtitles by wazoox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.