Created
May 8, 2019 23:48
-
-
Save sbarski/bbe778fda7dfae161dfacdf4992b8f50 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
const moment = require('moment'); | |
const momentDurationFormatSetup = require('moment-duration-format'); | |
function processItems(structure) { | |
const maxSentenceLength = 32; | |
const maxParagraphLength = 64; //we want to keep this to 64 or less (per line) | |
let srtTranscription = "WEBVTT\n\n"; | |
let numberOfChars = 0; | |
let sentence = ""; | |
let startTime = null; | |
let endTime = null; | |
let newLine = false; | |
for(let i = 0; i < structure.results.items.length; i++) { | |
let item = structure.results.items[i]; | |
let content = item.alternatives[0].content; | |
const newSentenceLength = (sentence + " " + content).length; | |
if (startTime === null) { | |
startTime = moment.duration(parseFloat(item.start_time), "seconds").format("hh:mm:ss.SSS", {trim: false}); | |
} | |
switch (item.type) { | |
case "pronunciation": { | |
if (newSentenceLength >= maxParagraphLength) { | |
i--; | |
break; | |
} | |
if (newSentenceLength >= maxSentenceLength && newLine === false) { | |
sentence += "\n"; | |
newLine = true; | |
} | |
sentence += " " + content; | |
endTime = moment.duration(parseFloat(item.end_time), "seconds").format("hh:mm:ss.SSS", {trim: false}); | |
break; | |
} | |
case "punctuation": { | |
sentence += content; | |
break; | |
} | |
} | |
if (newSentenceLength >= maxParagraphLength || (item.type === "punctuation" && content === '.')) { | |
//We are done. Start new block; | |
srtTranscription += (startTime + " --> " + endTime + "\n"); | |
srtTranscription += (sentence + "\n" + "\n"); | |
numberOfChars += sentence.length; | |
startTime = endTime = null; | |
sentence = ""; | |
newLine = false; | |
} | |
} | |
return { | |
transcription: srtTranscription, | |
numberOfChars: numberOfChars | |
}; | |
} | |
exports.handler = (structure) => { | |
console.log('Processing AWS Transcription JSON'); | |
const transcription = processItems(structure); | |
return transcription; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment