Created
August 14, 2023 23:01
-
-
Save tayiorbeii/bdfd8e9dbba4c7ef72d6056e17d42fd3 to your computer and use it in GitHub Desktop.
This file contains 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
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