Skip to content

Instantly share code, notes, and snippets.

@mateogianolio
Last active September 21, 2021 14:51
Show Gist options
  • Save mateogianolio/18a0db136d403b380049 to your computer and use it in GitHub Desktop.
Save mateogianolio/18a0db136d403b380049 to your computer and use it in GitHub Desktop.
converts an SRT file into JSON
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));
});
@corentinmace
Copy link

I want to convert srt to json but I got this error :
throw new ERR_INVALID_OPT_VALUE_ENCODING(encoding);

@lxtadhamnawito
Copy link

I'd like to convert an srt to json file, how does this work?

use this https://www.npmjs.com/package/srt-to-json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment