Created
June 15, 2017 10:31
-
-
Save ChristiaanScheermeijer/703cbcccebc3c97e2211a9a52250d6a5 to your computer and use it in GitHub Desktop.
SRT to WebVTT in Javascript
This file contains 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
(function () { | |
window.srtToVtt = function (url, cb) { | |
var xhr = new XMLHttpRequest(); | |
xhr.onload = function () { | |
var vtt = 'WEBVTT FILE\r\n\r\n'; | |
var blob; | |
var url; | |
if (xhr.readyState !== xhr.DONE) { | |
return; | |
} | |
vtt += xhr.responseText; | |
// replace bracket tags with html tags | |
vtt = vtt.replace(/\{(\/?[biu])}/g, '<$1>'); | |
// vtt uses a dot between seconds and milliseconds | |
vtt = vtt.replace(/(\d\d:\d\d:\d\d),(\d\d\d)/g, '$1.$2'); | |
blob = new Blob([vtt], {type: 'text/vtt; charset=UTF-8'}); | |
url = URL.createObjectURL(blob); | |
cb(url); | |
}; | |
xhr.open('GET', url); | |
xhr.send(); | |
} | |
}()); | |
srtToVtt('captions.srt', function (url) { | |
// use url as text track | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment