Created
June 14, 2021 23:57
-
-
Save sweeneyapps/c3587770e89b0949190d0208c3f579ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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