Skip to content

Instantly share code, notes, and snippets.

@sweeneyapps
Created June 14, 2021 23:57
Show Gist options
  • Save sweeneyapps/c3587770e89b0949190d0208c3f579ac to your computer and use it in GitHub Desktop.
Save sweeneyapps/c3587770e89b0949190d0208c3f579ac to your computer and use it in GitHub Desktop.
const timeCodeRegEx = /\1(\d\d):\2(\d\d),\3(\d+)\4([sm])/
const isTimeCode = (str) => timeCodeRegEx.test(str)
const convertTime = (str) => {
let arr = timeCodeRegEx.exec(str)
return {
min: Number(arr[1]),
sec: Number(arr[2]),
duration: Number(arr[3]),
time: arr[4]
}
}
const convert = (line) => {
if(isTimeCode(line)) {
const { min, sec, duration, time } = convertTime(line)
let addMin = sec + duration > 59 ? 1 : 0
let subSec = addMin === 1 ? 60 : 0
return {
startTime: {
min,
sec
},
endTime: {
min: min + (time === 'm' ? duration : 0) + addMin,
sec: sec + (time === 's' ? duration : 0) - subSec
}
}
} else {
return ''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment