UNPKG

parse-srt

Version:

Parse and convert SRT subtitles into JSON format.

11 lines (10 loc) 1.5 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.parseSRT=e()}(this,function(){"use strict";/** * @name parseSRT * @desc Parses and converts SRT subtitle data into JSON format. Adapted from the popcorn.js SRT parser plugin. * @see http://popcornjs.org/ * @author Luis Rodrigues (http://www.luisrodriguesweb.com) * @version 1.0.0-alpha * @license MIT */ function t(t){var e=t.split(":");try{var r=e[2].split(",");return 1===r.length&&(r=e[2].split(".")),3600*parseFloat(e[0],10)+60*parseFloat(e[1],10)+parseFloat(r[0],10)+parseFloat(r[1],10)/1e3}catch(t){return 0}}function e(t,e){for(var r=e;!t[r];)r++;return r}function r(t){for(var e=t.length-1;e>=0&&!t[e];)e--;return e}function n(){for(var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",i=[],o=n.split(/(?:\r\n|\r|\n)/gm),s=r(o)+1,a=0,p=void 0,l=void 0,u=void 0,f=0;f<s;f++){for(u={},l=[],f=e(o,f),u.id=parseInt(o[f++],10),p=o[f++].split(/[\t ]*-->[\t ]*/),u.start=t(p[0]),a=p[1].indexOf(" "),a!==-1&&(p[1]=p[1].substr(0,a)),u.end=t(p[1]);f<s&&o[f];)l.push(o[f++]);u.text=l.join("\\N").replace(/\{(\\[\w]+\(?([\w\d]+,?)+\)?)+\}/gi,""),u.text=u.text.replace(/</g,"&lt;").replace(/>/g,"&gt;"),u.text=u.text.replace(/&lt;(\/?(font|b|u|i|s))((\s+(\w|\w[\w\-]*\w)(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)(\/?)&gt;/gi,"<$1$3$7>"),u.text=u.text.replace(/\\N/gi,"<br />"),i.push(u)}return i}return n}); //# sourceMappingURL=parse-srt.min.js.map