UNPKG

subtitle

Version:

Stream-based library for parsing and manipulating subtitles

3 lines (2 loc) 5.44 kB
"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var e=require("stream"),s=t(require("multipipe")),r=t(require("split2")),n=t(require("strip-bom"));function i(){return(i=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var s=arguments[e];for(var r in s)Object.prototype.hasOwnProperty.call(s,r)&&(t[r]=s[r])}return t}).apply(this,arguments)}var a=function(t,e){return void 0===e&&(e=2),t.toString().padStart(e,"0")},o=function(t){return new e.Duplex(i({objectMode:!0,autoDestroy:!1,read:function(){}},t))};function u(t,e){void 0===e&&(e={format:"SRT"});var s=new Date(0,0,0,0,0,0,t),r=s.getHours(),n=s.getMinutes(),i=s.getSeconds(),o=Math.floor(t-(36e5*r+6e4*n+1e3*i));return a(r)+":"+a(n)+":"+a(i)+("WebVTT"===e.format?".":",")+a(o,3)}var h=function(t){var s=0;return new e.Transform({objectMode:!0,autoDestroy:!1,transform:function(e,r,n){n(null,t(e,s++))}})},f=function(){function t(t){this.push=t.push,this.state={expect:"header",row:0,hasContentStarted:!1,isWebVTT:!1,node:{},buffer:[]}}var e=t.prototype;return e.isIndex=function(t){return/^\d+$/.test(t.trim())},e.isTimestamp=function(t){return d.test(t)},e.isVttComment=function(t){return/^NOTE/.test(t)},e.getError=function(t,e,s){return new Error("expected "+t+" at row "+(e+1)+', but received: "'+s+'"')},e.parseLine=function(t){var e=0===this.state.row?n(t):t;if(!this.state.hasContentStarted){if(!e.trim())return;this.state.hasContentStarted=!0}({header:this.parseHeader,id:this.parseId,timestamp:this.parseTimestamp,text:this.parseText,vtt_comment:this.parseVttComment})[this.state.expect].call(this,e),this.state.row++},e.flush=function(){this.state.buffer.length>0&&this.pushNode()},e.parseHeader=function(t){if(!this.state.isWebVTT){if(this.state.isWebVTT=/^WEBVTT/.test(t),!this.state.isWebVTT)return void this.parseId(t);this.state.node.type="header"}this.state.buffer.push(t),t||(this.state.expect="id")},e.parseId=function(t){this.state.expect="timestamp","header"===this.state.node.type&&this.pushNode(),this.isIndex(t)||(this.state.isWebVTT&&this.isVttComment(t)?this.state.expect="vtt_comment":this.parseTimestamp(t))},e.parseVttComment=function(t){this.state.expect="vtt_comment",""===t.trim()&&(this.state.expect="id")},e.parseTimestamp=function(t){if(!this.isTimestamp(t))throw this.getError("timestamp",this.state.row,t);this.state.node={type:"cue",data:i({},c(t),{text:""})},this.state.expect="text"},e.parseText=function(t){if(0!==this.state.buffer.length){if(this.isTimestamp(t))return this.isIndex(this.state.buffer[this.state.buffer.length-1])&&this.state.buffer.pop(),this.pushNode(),void this.parseTimestamp(t);if(this.isVttComment(t))return this.pushNode(),void this.parseVttComment(t);this.state.buffer.push(t)}else this.state.buffer.push(t)},e.pushNode=function(){if("cue"===this.state.node.type){for(;["","\n"].includes(this.state.buffer[this.state.buffer.length-1]);)this.state.buffer.pop();for(;["","\n"].includes(this.state.buffer[0]);)this.state.buffer.shift();this.state.node.data.text=this.state.buffer.join("\n")}"header"===this.state.node.type&&(this.state.node.data=this.state.buffer.join("\n").trim()),this.push(this.state.node),this.state.node={},this.state.buffer=[]},t}();function p(t){var e=t.match(/^(?:(\d{1,}):)?(\d{1,2}):(\d{1,2})[,.](\d{1,3})$/);if(!e)throw new Error('Invalid SRT or VTT time format: "'+t+'"');return(e[1]?36e5*parseInt(e[1],10):0)+6e4*parseInt(e[2],10)+1e3*parseInt(e[3],10)+parseInt(e[4],10)}var d=/^((?:\d{1,}:)?\d{1,2}:\d{1,2}[,.]\d{1,3}) --> ((?:\d{1,}:)?\d{1,2}:\d{1,2}[,.]\d{1,3})(?: (.*))?$/;function c(t){var e=d.exec(t);if(!e)throw new Error("Invalid timestamp format");var s={start:p(e[1]),end:p(e[2])};return e[3]&&(s.settings=e[3]),s}var m=function(){function t(t){this.options=t,this.hasReceivedHeader=!1,this.isVTT="WebVTT"===t.format,this.index=1}var e=t.prototype;return e.format=function(t){var e="";return"header"===t.type&&this.isVTT&&(this.hasReceivedHeader=!0,e+=t.data+"\n\n"),"cue"===t.type&&(!this.hasReceivedHeader&&this.isVTT&&(this.hasReceivedHeader=!0,e+="WEBVTT\n\n"),e+=this.formatCue(t.data,this.index++,this.options)),e},e.formatCue=function(t,e,s){return[(e>1?"\n":"")+e,u(t.start,s)+" --\x3e "+u(t.end,s)+("WebVTT"===s.format&&t.settings?" "+t.settings:""),t.text,""].join("\n")},t}();exports.RE_TIMESTAMP=d,exports.filter=function(t){return new e.Transform({objectMode:!0,autoDestroy:!1,transform:function(e,s,r){t(e)?r(null,e):r()}})},exports.formatTimestamp=u,exports.map=h,exports.parse=function(){var t=new f({push:function(t){return i.push(t)}}),e=o({write:function(e,s,r){try{t.parseLine(e.toString())}catch(t){return r(t)}r()}}),n=r();n.on("finish",(function(){t.flush(),e.push(null)}));var i=s(n,e,{objectMode:!0});return i},exports.parseSync=function(t){var e=[],s=new f({push:function(t){return e.push(t)}});return t.replace(/\r\n/g,"\n").split("\n").forEach((function(t){return s.parseLine(t)})),s.flush(),e},exports.parseTimestamp=p,exports.parseTimestamps=c,exports.resync=function(t){return h((function(e){return"cue"===e.type?i({},e,{data:i({},e.data,{start:e.data.start+t,end:e.data.end+t})}):e}))},exports.stringify=function(t){var e=new m(t);return h((function(t){return e.format(t)}))},exports.stringifySync=function(t,e){var s=new m(e);return t.reduce((function(t,e){return t+s.format(e)}),"")}; //# sourceMappingURL=subtitle.cjs.production.min.js.map