#!/usr/bin/perl # Copyright (C) 2011 Trizen . # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # #------------------------------------------------------- # Appname: srt-delay # Version: 0.0.3 # Created on: 26 December 2011 # Latest edit on 02 January 2011 # Website: http://trizen.googlecode.com #------------------------------------------------------- use strict; sub usage { print <<"USAGE"; usage: $0 [options] [seconds] [file.srt] Options: -b, --backup : backup original to .bak Examples: $0 -b 1.439 file.srt $0 -b 0.321 file.srt $0 -b -3.14 file.srt USAGE exit 0; } sub time2sec { my @out; foreach my $time (@_) { my ($hours, $min, $sec, $milisec) = split(/[:,]/, $time, 4); push @out, $hours * 3600 + $min * 60 + $sec + $milisec / 1000; } return @out; } sub sec2time { my @out; foreach my $sec (map { sprintf '%.3f', $_ } @_) { push @out, sprintf('%02d:%02d:%02d,%03d', ($sec / 3600 % 24, $sec / 60 % 60, $sec % 60, substr($sec, index($sec, '.') + 1))); } return @out; } sub _looks_like_number { return 1 if $_[0] =~ /^[+-]?[0-9]+$/; return 1 if $_[0] =~ /^[+-]?[0-9]+\.[0-9]+$/; return 1 if $_[0] =~ /^[+-]?(?=[0-9]|\.[0-9])[0-9]*(?:\.[0-9]*)?(?:[Ee][+-]?[0-9]+)?$/; } my $addition = (grep { _looks_like_number($_) } @ARGV)[0] // usage(); my $backup = do { (grep { $_ ~~ ['-b', '--backup'] } @ARGV) ? 1 : 0; }; foreach my $file (grep { -f $_ } @ARGV) { my @output; open my $fh, '<', $file or die "Unable to open for read ${file}: $!\n"; while (defined(my $line = <$fh>)) { if ($line =~ /^\d+:\d+:\d+(?:,\d+)?\s*-->\s*\d+:\d+:\d+(?:,\d+)?(\s*)\z/) { push @output, join( ' --> ', sec2time( map ({ my $sec = $_ + $addition; $sec >= 0 ? $sec : !warn "[!] Time cannot be lower than zero at line $.\n"; } time2sec(split(/\s*-->\s*/, $line, 2))) )) . $1; } else { push @output, $line } } close $fh; rename $file, "$file.bak" if $backup; open $fh, '>', $file or die "Unable to open for write ${file}: $!\n"; print {$fh} @output; close $fh; }