Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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":



  • 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 or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2021-06-21 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (97 votes). Check out past polls.

    Notices?