Skip to content

Instantly share code, notes, and snippets.

@lestrrat
Last active September 27, 2021 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lestrrat/a7ee9330a8bc2af0a49405959ddf1c05 to your computer and use it in GitHub Desktop.
Save lestrrat/a7ee9330a8bc2af0a49405959ddf1c05 to your computer and use it in GitHub Desktop.
"Ripple" shift the contents of an SBV file
#!perl
# perl ripple.pl $ref $delta file.sbv
use strict;
use POSIX qw(floor);
# $ref is the time code (in seconds) where the ripple should start
my $ref = shift @ARGV;
# $delta is the duration (in seconds, can be fractional) that data should be shifted by.
# Positive values shift the data towards the end of the file, negative values shift
# the data towards the beginning of the file
my $delta = shift @ARGV;
my $timecode_pattern = qr/
(\d):(\d\d):(\d\d)\.(\d\d\d)
,
(\d):(\d\d):(\d\d)\.(\d\d\d)
/x;
sub code2sec {
my ($h, $m, $s, $msec) = @_;
return $h * 3600 + $m * 60 + $s + ($msec/1000);
}
sub sec2code {
my ($sec) = @_;
my $h = floor($sec) / 3600;
my $m = floor($sec) / 60;
my $s = floor($sec) % 60;
return sprintf("%d:%02d:%02d.%03d",$h, $m, $s, ($sec - floor($sec))*1000)
}
my $in_text = 0;
while (<>) {
if ($in_text) {
if (/^\s*$/) {
print "\n";
$in_text = 0;
next;
}
print "$_";
next;
}
if (/$timecode_pattern/) {
my $start = code2sec($1, $2, $3, $4);
my $end = code2sec($5, $6, $7, $8);
if ($start >= $ref) {
$start += $delta;
$end += $delta;
print sec2code($start), ",", sec2code($end), "\n";
$in_text = 1;
next;
}
print $_;
next;
}
print $_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment