#!/bin/bash Usage="shift timestamps and adjust time between (change speed), print result to stdout Usage: $(basename "$0") filename timefactor timedivisor timeoffset filename: plaintext file containing timestamps in the format \"(H)H:mm:ss.SS\" e.g. ASS/SSA, SRT or chapter files timefactor / timedivisor: factor to multiply to each timestamp (as an integer fraction) timeoffset: absolute amount of time to shift all timestamps (in 10s of ms) Examples: $(basename "$0") input.ssa 24024 24000 0 stretch time between timestamps by 24024/24000 i.e. adjust the speed of subtitles from 24 to 23.976 fps $(basename "$0") input.ssa 1 1 57 > output.ssa add a flat delay of 570 ms to all timestamps, don't change speed and save the result in output.ssa $(basename "$0") input.ssa 24000 24024 -100 adjust the speed of subtitles from 23.976 to 24 fps and shift all timestamps 1 second towards the start " if ! [[ $# -eq 4 && "$2$3$4" =~ ^[-0-9]+$ ]] then echo "$Usage" >&2 exit 1 fi Sourcefile=$1 Timefactor=$2 Timedivisor=$3 Timeoffset=$4 if [ -f "$Sourcefile" ] then while read -r Oldline do Newline=$Oldline while read Oldstamp do IFS=’:.,’ read -ra Oldarray <<< "${Oldstamp}" let "Oldtimesum = 10#${Oldarray[0]}*360000 + 10#${Oldarray[1]}*6000 + 10#${Oldarray[2]}*100 + 10#${Oldarray[3]}" let "Newtimesum = $Oldtimesum*$Timefactor/$Timedivisor +$Timeoffset" let "Newarray[0] = $Newtimesum/360000" let "Newarray[1] = ($Newtimesum%360000)/6000" let "Newarray[2] = (($Newtimesum%360000)%6000)/100" let "Newarray[3] = (($Newtimesum%360000)%6000)%100" HH=$( printf "%0${#Oldarray[0]}d" "${Newarray[0]}" ) MM=$( printf "%02d" "${Newarray[1]}" ) SS=$( printf "%02d" "${Newarray[2]}" ) CS=$( printf "%02d" "${Newarray[3]}" ) Newline=${Newline/$Oldstamp/$HH:$MM:$SS.$CS} done <<< $(echo $Oldline | grep -o -E "[0-9]+:[0-9]{2}:[0-9]{2}[.,][0-9]{2}" | sort -r) echo "$Newline" done < "$Sourcefile" else echo "File not found: $Sourcefile" fi