Skip to content

Instantly share code, notes, and snippets.

@nicholastay
Last active April 2, 2018 06:22
Show Gist options
  • Save nicholastay/b355cc66c8920a45ff48080bd6b3f41f to your computer and use it in GitHub Desktop.
Save nicholastay/b355cc66c8920a45ff48080bd6b3f41f to your computer and use it in GitHub Desktop.
Quick js script to convert vtt format data to srt
function vtt2srt(raw) {
if (raw.substr(0,6) !== 'WEBVTT')
throw new TypeError('data does not look like webvtt');
var vtt = raw.replace('\r', '').trim();
var data = '1\n' + vtt.substr(vtt.indexOf('\n\n')+2); // get past the blank line and get the 1 started
var i = 1; // iterate the sub numbers
return data.replace(/\n\n/g, str => '\n\n' + (++i) + '\n'); // add the numbers at new lines
}
module.exports = vtt2srt;
// fs.writeFileSync('./out.en.srt', vtt2srt(fs.readFileSync('./lw.en.vtt', 'utf8')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment