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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.