Skip to content

Instantly share code, notes, and snippets.

@leshniak
Created February 4, 2022 23:07
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 leshniak/7a9cd466b2add22324507ffbee49e4b2 to your computer and use it in GitHub Desktop.
Save leshniak/7a9cd466b2add22324507ffbee49e4b2 to your computer and use it in GitHub Desktop.
Adjusts SRT subtitles by a factor or moves them by a shift in milliseconds
#!/usr/bin/env -S awk -f
## Adjusts SRT subtitles by a factor or moves them by a shift in milliseconds
##
## Usage:
## ./adjust-srt.awk [-v SCALE=<factor>] [-v SHIFT=<ms>] subtitles.srt > new-subtitles.srt
##
## Useful scales: 25/23.97=1.04297, 23.97/25=0.9588, 25/29.97=0.834168, 29.97/25=1.1988
function convert(timestamp, hh, mm, ss, sss, adjusted){
hh = substr(timestamp, 0, 2) * 3600 * 1000;
mm = substr(timestamp, 4, 2) * 60 * 1000;
ss = substr(timestamp, 7, 2) * 1000;
sss = substr(timestamp, 10, 3);
adjusted = (hh + mm + ss + sss) * SCALE + SHIFT;
hh = int(adjusted / 3600 / 1000);
mm = int((adjusted - hh * 3600 * 1000) / 60 / 1000);
ss = int((adjusted - hh * 3600 * 1000 - mm * 60 * 1000) / 1000);
sss = int(adjusted - hh * 3600 * 1000 - mm * 60 * 1000 - ss * 1000);
return sprintf("%.2d:%.2d:%.2d,%.3d", hh, mm, ss, sss);
}
BEGIN {
FS = " --> ";
OFS = FS;
SHIFT = SHIFT == "" ? 0 : SHIFT
SCALE = SCALE == "" ? 1 : SCALE;
}
/^[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} --> [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}$/ {
print convert($1), convert($2);
next;
}
{ print $0; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment