Skip to content

Instantly share code, notes, and snippets.

@tayiorbeii
Created September 6, 2023 14:55
Show Gist options
  • Save tayiorbeii/4b29d1192f954f2a334127fb966ff06e to your computer and use it in GitHub Desktop.
Save tayiorbeii/4b29d1192f954f2a334127fb966ff06e to your computer and use it in GitHub Desktop.
import "@johnlindquist/kit"
// Name: srt to txt
// Author: Taylor Bell
// Description: Breaks an srt file into readable text blocks
let srtParser2 = await npm('srt-parser-2')
debugger;
let parser = new srtParser2()
let srtFiles = await drop()
function formatTimeString(str) {
let [h, m, s] = str.split(':')
if (h == '00') {
return `${m}:${s}`
}
return `${h}:${m}:${s}`
}
for (let f of srtFiles) {
let text = await readFile(`${f.path}`, 'utf-8')
let result = parser.fromSrt(text)
let withTimes = result.map(line => {return {...line, totalSeconds: line.endSeconds - line.startSeconds}})
let timeLimitInSeconds = 20
let currentTimeInSeconds = 0
let transcribedSentencesCount = 0
let arrayByTimes = []
let tempArray = []
withTimes.forEach((x, i) => {
if (currentTimeInSeconds + x.totalSeconds >= timeLimitInSeconds) {
arrayByTimes.push(tempArray)
tempArray = []
currentTimeInSeconds = 0
transcribedSentencesCount = 0
}
if (currentTimeInSeconds === 0) {
tempArray.push(`[${formatTimeString(x.startTime.split(',')[0])}] ${x.text}`)
} else {
tempArray.push(x.text)
}
currentTimeInSeconds += x.totalSeconds
transcribedSentencesCount++
})
arrayByTimes.push(tempArray)
let transcript = arrayByTimes.map(x => x.join(' ')).flat().join('\n\n')
await writeFile(`${f.path}-transcript.txt`, transcript)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment