Skip to content

Instantly share code, notes, and snippets.

@Windows81
Last active March 2, 2022 20:29
Show Gist options
  • Save Windows81/cd7b846562d75c79832f4c85082cc3ed to your computer and use it in GitHub Desktop.
Save Windows81/cd7b846562d75c79832f4c85082cc3ed to your computer and use it in GitHub Desktop.
SRT/VTT Subtitle Reverser
// SRT or VTT string is stored in variable 's'.
var time=27*60000+11463 // Time of video in milliseconds.
var mult=5444/5444 // Timestamp multiplier AFTER 'time' offset.
var index=0
var isVtt
function f(v,i,a){
if(v=='WEBVTT'){
isVtt=true
return''
}
var t=v.split('\n')
if(isVtt)t.unshift(null)
t[0]=++index
//console.log(t)
t[1]=t[1].split(' --> ').map(v=>{
if(v.length==9)v='00:'+v
var h=Number.parseInt(v.substring(0,2))
var m=Number.parseInt(v.substring(3,5))
var s=Number.parseInt(v.substring(6,8))
var l=Number.parseInt(v.substring(9))
var r=mult*(time-(1000*(60*(60*h+m)+s)+l))
var H=Math.floor(r/3600000)
var M=Math.floor(r/60000%60)
var S=Math.floor(r/1000%60)
var L=Math.floor(r%1000)
if(H<10)H='0'+H
if(M<10)M='0'+M
if(S<10)S='0'+S
if(L<10)L='00'+L
else if(L<100)L='0'+L
return ''+H+':'+M+':'+S+','+L
}).reverse().join(' --> ')
return t.join('\n')
}
var t=s.trim().split('\n\n')
var result=t.reverse().filter(s=>s.length).map(f).join('\n\n')
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment