<div dir="ltr">#!/usr/bin/perl<br><br>use strict;<br><br>use DateTime;<br><br>print 'Enter the subtitle file name: ';<br>my $file = <STDIN>;<br>chomp $file;<br><br>print "No file name given.\n\n" and exit if !$file;<br>

print "No file exists.\n\n" and exit if !-e $file;<br><br>print 'Enter the time diff to add in seconds: ';<br>my $delta = <STDIN>;<br>chomp $delta;<br><br>print "No time diff given.\n\n" and exit if !$delta;<br>

print "Not a valid time diff.\n\n" and exit if $delta !~ /^ \d+ $/x;<br><br>print 'Enter 1 to add or 2 to subtract: ';<br>my $option = <STDIN>;<br>chomp $option;<br><br>print "No option given.\n\n" and exit if !$option;<br>

print "Not a valid option.\n\n" and exit if $option != 1 && $option != 2;<br><br>my $op = ($option == 1) ? '+' : '-';<br><br>my @splits = split /\//, $file;<br>my ($file_name, $extension) = split /\./, $splits[-1];<br>

my $new_file = "${file_name}_new.$extension";<br><br>open my $in,  '<', $file     or die "Unable to open $file : $!";<br>open my $out, '>', $new_file or die "Unable to open $new_file : $!";<br>

<br>while (my $line = <$in>) {<br>    if ($line<br>        =~ / (\d{2}) : (\d{2}) : (\d{2}) , (\d{3}) \s --> \s (\d{2}) : (\d{2}) : (\d{2}) , (\d{3}) /x<br>        )<br>    {<br>        my ($start_hour, $start_minute, $start_second, $start_millisecond,<br>

            $end_hour, $end_minute, $end_second, $end_millisecond)<br>            = ($1, $2, $3, $4, $5, $6, $7, $8);<br><br>        my $start_time = DateTime->new(<br>            year       => 2013,<br>            hour       => $start_hour,<br>

            minute     => $start_minute,<br>            second     => $start_second,<br>            nanosecond => $start_millisecond<br>        );<br>        my $end_time = DateTime->new(<br>            year       => 2013,<br>

            hour       => $end_hour,<br>            minute     => $end_minute,<br>            second     => $end_second,<br>            nanosecond => $end_millisecond<br>        );<br><br>        my $start_time_new = $start_time->add(seconds => "$op$delta");<br>

        my $end_time_new   = $end_time->add(seconds => "$op$delta");<br><br>        print $out sprintf "%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n",<br>            $start_time_new->hour,   $start_time_new->minute,<br>

            $start_time_new->second, $start_time_new->nanosecond,<br>            $end_time_new->hour, $end_time_new->minute, $end_time_new->second,<br>            $end_time_new->nanosecond;<br>    } else {<br>

        print $out $line;<br>    }<br>}<br><br>close $in;<br>close $out;<br><br>print "New subtitle file $new_file created in the current directory.\n\n";<br><br></div>