0

maybe somebody can help me. In bash, I don't know how to do that. I need to do a bash-script. At stdin I have .srt file of subtitles in this format:

num
HH:MM:SS,SSS --> HH:MM:SS,SSS
text line 1
text line 2
...

HH:MM:SS,SSS start and finish of title for text.

Script must shift seconds. (it can be + or -)

Example:

$cat bmt.srt
5
00:01:02,323 --> 00:01:05,572
Hello, my frieds!
6
....

$./shifter.sh +3<mbt.srt
5
00:01:05,323 --> 00:01:08,572
Hello, my frieds!
6

I think, I need to grab all HH:MM:SS and convert them to seconds firstly. If somebody can do this without sed, I will applaude. Thank you!

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
Georgy
  • 1

1 Answers1

2

Your shifter.sh:

#!/bin/sh
export DELTA=$1
perl -pe '
    BEGIN {
        sub to_secs {
            my ($h,$m,$s) = split(/:/, shift);
            $h*3600 + $m*60 + $s;
        }
        sub to_str {
            my $secs = shift;
            my $s = $secs % 60;
            my $h = int($secs / 3600);
            my $m = int(($secs - $h*3600) / 60);
            sprintf("%d:%02d:%02d", $h, $m, $s);
        }
    }
    s/(\d+:\d\d:\d\d)/ to_str(to_secs($1) + $ENV{DELTA}) /ge;
'
glenn jackman
  • 238,783
  • 38
  • 220
  • 352