Skip to content

Instantly share code, notes, and snippets.

@sbarski
Created May 8, 2019 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbarski/bbe778fda7dfae161dfacdf4992b8f50 to your computer and use it in GitHub Desktop.
Save sbarski/bbe778fda7dfae161dfacdf4992b8f50 to your computer and use it in GitHub Desktop.
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