-
-
Save johnbahamon/337234b151c86951fc1827474c0ff9a6 to your computer and use it in GitHub Desktop.
converts an SRT file into JSON
This file contains hidden or 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
var fs = require('fs'); | |
fs.readFile('subtitle.srt', function(error, data) { | |
if(error) | |
throw error; | |
var text = data.toString(); | |
var lines = text.split('\n'); | |
var output = []; | |
var buffer = { | |
content: [] | |
}; | |
lines.forEach(function(line) { | |
if(!buffer.id) | |
buffer.id = line; | |
else if(!buffer.start) { | |
var range = line.split(' --> '); | |
buffer.start = range[0]; | |
buffer.end = range[1]; | |
} | |
else if(line !== '') | |
buffer.content.push(line); | |
else { | |
output.push(buffer); | |
buffer = { | |
content: [] | |
}; | |
} | |
}); | |
fs.writeFile('subtitle.json', JSON.stringify(output)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment