Skip to content

Instantly share code, notes, and snippets.

@Dylancyclone
Created March 25, 2019 07:21
Show Gist options
  • Save Dylancyclone/5ae2c0569cf2649a5a4e799918135215 to your computer and use it in GitHub Desktop.
Save Dylancyclone/5ae2c0569cf2649a5a4e799918135215 to your computer and use it in GitHub Desktop.
.srt to .json
var regBlock = /^[0-9]+\n(.+\n)+/gm; //Use /^\n(.+\n)+/gm if there are no leading sequence numbers
var regText = /^[0-9]+\n(.+\n)/gm; //Use /^\n(.+\n)/gm if there are no leading sequence numbers
var regTime = /[0-9:]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} --> [0-9:]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}/gm;
var result;
final = [];
while((result = regBlock.exec(text)) !== null) {
while((resultTime = regTime.exec(result[0])) !== null) {
start = resultTime[0].split(" --> ")[0];
end = resultTime[0].split(" --> ")[1];
var temp = start.split(':');
var start = (+temp[0]) * 60 * 60 + (+temp[1]) * 60 + (+temp[2].replace(",", "."));
var temp = end.split(':');
var end = (+temp[0]) * 60 * 60 + (+temp[1]) * 60 + (+temp[2].replace(",", "."));
}
final.push(JSON.parse('{"start":'+start+',"end":'+end+',"text":"'+result[0].replace(regText, "").replace(/(?:\r\n|\r|\n)/g, "\\n").replace(/\"/g, "\\\"")+'"}'));
}
console.log(JSON.stringify(final));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment