Last active
June 21, 2023 18:29
-
-
Save tayiorbeii/6baae9b8ed806a92aa451cefb9e0ac7e 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: shorten-deepgram-srt | |
let deepgramJson = await drop("Drop deepgram json file"); | |
let jsonInput = await readFile(deepgramJson[0].path, 'utf-8'); | |
let data = JSON.parse(jsonInput); | |
function convertTime(inputSeconds) { | |
const date = new Date(inputSeconds * 1000); | |
const hours = String(date.getUTCHours()).padStart(2, '0'); | |
const minutes = String(date.getUTCMinutes()).padStart(2, '0'); | |
const seconds = String(date.getUTCSeconds()).padStart(2, '0'); | |
const milliseconds = String(date.getUTCMilliseconds()).padStart(3, '0'); | |
return `${hours}:${minutes}:${seconds},${milliseconds}`; | |
} | |
let timeLimitInSeconds = 5.5; | |
let charLimit = 42; | |
let currentTimeInSeconds = 0; | |
let currentCharCount = 0; | |
let arrayByTimes = []; | |
let tempArray = []; | |
data.forEach((item, index) => { | |
let timeExceeded = (currentTimeInSeconds + (item.end - item.start)) >= timeLimitInSeconds; | |
let charCountExceeded = (currentCharCount + item.punctuated_word.length) > charLimit; | |
if (timeExceeded || charCountExceeded || index === data.length - 1) { | |
if(tempArray.length) { | |
arrayByTimes.push(tempArray); | |
tempArray = []; | |
currentTimeInSeconds = 0; | |
currentCharCount = 0; | |
} | |
} | |
if (!timeExceeded || !charCountExceeded) { | |
tempArray.push(item); | |
currentTimeInSeconds += (item.end - item.start); | |
currentCharCount += item.word.length; | |
} | |
// If the data is at the last item and the time or char count isn't exceeded, push the last item. | |
if (index === data.length - 1 && (!timeExceeded || !charCountExceeded)) { | |
arrayByTimes.push(tempArray); | |
} | |
}); | |
let srtEntries = arrayByTimes.map((timeBlock, index) => { | |
let startTime = convertTime(timeBlock[0].start); | |
let endTime = convertTime(timeBlock[timeBlock.length - 1].end); | |
let text = timeBlock.map(x => x.punctuated_word).join(' '); | |
let srtEntry = `${index + 1} | |
${startTime} --> ${endTime} | |
${text} | |
`; | |
return srtEntry; | |
}); | |
let output = srtEntries.join('\n\n'); | |
await writeFile(`${deepgramJson[0].path}-shorter-subs.srt`, output); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment