@aim-packages/subtitle 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +11 -11
- package/dist/index.d.ts +145 -2
- package/dist/index.es.js +880 -156
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var me=Object.defineProperty;var je=(t,e,r)=>e in t?me(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var y=(t,e,r)=>je(t,typeof e!="symbol"?e+"":e,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function D(t,e=2){return t.toString().padStart(e,"0")}function S(t){const e=Math.floor(t/3600),r=Math.floor(t%3600/60),n=t%3600%60,a=Math.floor(n%1*1e3),o=D(e),i=D(r),f=D(Math.floor(n)),s=D(a,3);return`${o}:${i}:${f}.${s}`}function $(t){if(!t)return 0;const e=t.split(":");try{let r=e[2].split(",");return r.length===1&&(r=e[2].split(".")),parseFloat(e[0])*3600+parseFloat(e[1])*60+parseFloat(r[0])+parseFloat("0."+r[1])}catch{return 0}}function Kt(t){return/[\u4E00-\u9FFF\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF]/.test(t)}const Se=Object.freeze(Object.defineProperty({__proto__:null,containsCJKCharacters:Kt,convertToSeconds:$,formatTime:S,padNumber:D},Symbol.toStringTag,{value:"Module"}));/**
|
|
2
2
|
* @name ass2vtt
|
|
3
3
|
* @desc Converts ASS subtitle data into VTT format.
|
|
4
4
|
* @author YuQian2015 (https://github.com/YuQian2015)
|
|
5
5
|
* @license MIT
|
|
6
|
-
*/function
|
|
6
|
+
*/function $e(t){const e=new RegExp("Dialogue:\\s\\d,(\\d+:\\d\\d:\\d\\d.\\d\\d),(\\d+:\\d\\d:\\d\\d.\\d\\d),([^,]*),([^,]*),(?:[^,]*,){4}([\\s\\S]*)$","i");function r(n=""){return n.split(/[:.]/).map((a,o,i)=>{if(o===i.length-1){if(a.length===1)return"."+a+"00";if(a.length===2)return"."+a+"0"}else if(a.length===1)return(o===0?"0":":0")+a;return o===0?a:o===i.length-1?"."+a:":"+a}).join("")}return`WEBVTT
|
|
7
7
|
|
|
8
|
-
`+
|
|
9
|
-
`)}:null}).filter(
|
|
10
|
-
`+
|
|
8
|
+
`+t.split(/\r?\n/).map(n=>{const a=n.match(e);return a?{start:r(a[1].trim()),end:r(a[2].trim()),text:a[5].replace(/{[\s\S]*?}/g,"").replace(/(\\N)/g,"<br />").trim().split(/\r?\n/).map(o=>o.trim()).join(`
|
|
9
|
+
`)}:null}).filter(n=>n).map((n,a)=>n?n.start+" --> "+n.end+`
|
|
10
|
+
`+n.text:"").filter(n=>n.trim()).join(`
|
|
11
11
|
|
|
12
12
|
`)}/**
|
|
13
13
|
* @name parseSRT
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
* @author Newtopia Dev (http://www.newtopia.com)
|
|
18
18
|
* @version 1.0.1
|
|
19
19
|
* @license MIT
|
|
20
|
-
*/function
|
|
21
|
-
`:
|
|
22
|
-
`).split(/(?:\r\n|\r|\n)/gm),
|
|
23
|
-
`:" ").replace(/\{(\\[\w]+\(?([\w\d]+,?)+\)?)+\}/gi,""),
|
|
24
|
-
`);
|
|
25
|
-
${l}`,a===d.length-1&&c.push(v(i))):(i.part=l,c.push(v(i)),i.part="")))}),i=[];const h=/(<([0-9:.>]+)>)/ig;c.forEach(l=>{const _=l.part.split().map(function(p){return p.replace(h,function(u){return u.split("").reduce(function(T,D){return`==${u.replace("<","").replace(">","")}`},0)})})[0].replace(t,"").split(" "),x=[];_.forEach(function(p){if(p.indexOf("==")>-1){const u=p.split("=="),T=u[0];if(u[1],T==""||T=="##")return;x.push({word:L(p.split("==")[0]),time:k(p.split("==")[1])})}else x.push({word:L(p),time:void 0})}),l.words=x,l.part=l.part.replace(t,"")}),r(c)})}function k(s){const e=s.split(":"),t=e[e.length-3]?+e[e.length-3]:0,r=e[e.length-2]?+e[e.length-2]:0,n=+e[e.length-1];return t*3600+r*60+n}function L(s){return s.replace(/[^0-9a-z'-]/gi,"").toLowerCase()}function v(s){if(s==null||typeof s!="object")return s;const e=s.constructor();for(const t in s)s.hasOwnProperty(t)&&(e[t]=s[t]);return e}async function O(s){return E(s,{keepMultiLine:!0,propName:{}}).filter(r=>r.text).map(r=>({st:m(r.start),et:m(r.end),text:r.text}))}async function S(s){return(await N(s)).filter(r=>r.part).map(r=>({st:m(r.start),et:m(r.end),text:r.part}))}async function P(s){return S(F(s))}const A=Object.freeze(Object.defineProperty({__proto__:null,assToAimSegments:P,srtToAimSegments:O,vttToAimSegments:S},Symbol.toStringTag,{value:"Module"}));class z{constructor(e){f(this,"chains",{});f(this,"delimit","__replace__");f(this,"onFilter");f(this,"checked",!1);f(this,"checkedText","");f(this,"checkedLevel");f(this,"filteredText","");this.onFilter=e}add(e,t){typeof e!="string"&&(e=new TextDecoder().decode(e));const r=e;if(!r)return;let n=this.chains;for(let i=0;i<r.length;i++){if(r[i]in n)n=n[r[i]];else{if(!(n instanceof Object))break;let c,o;for(let d=i;d<r.length;d++)n[r[d]]={},[c,o]=[n,r[d]],n=n[r[d]];c&&o&&(c[o]={[this.delimit]:t||""});break}i===r.length-1&&(n[this.delimit]=t||"")}}parse(e){e.forEach(t=>{this.add(...t)})}reParse(e){this.chains={},e.forEach(t=>{this.add(t[0],t[1])})}feed(e){if(!this.checked&&!this.chains[e]){this.filteredText+=e,this.onFilter&&this.onFilter(e);return}if(!this.checked&&this.chains[e]){this.checked=!0,this.checkedLevel=this.chains[e],this.checkedText+=e;return}if(this.checked&&this.checkedLevel[e]){this.checkedLevel=this.checkedLevel[e],this.checkedText+=e;return}if(this.checked&&!this.checkedLevel[e]){const t=typeof this.checkedLevel[this.delimit]=="string"?this.checkedLevel[this.delimit]:this.checkedText;t&&t.length&&t.split("").forEach(r=>{this.filteredText+=r,this.onFilter&&this.onFilter(r)}),this.checked=!1,this.checkedText="",this.checkedLevel=void 0,this.chains[e]?(this.checked=!0,this.checkedLevel=this.chains[e],this.checkedText=e):(this.onFilter&&this.onFilter(e),this.filteredText+=e)}}feedAll(e){return e.length?(e.split("").forEach(r=>{this.feed(r)}),this.end()):""}end(){if(this.checked&&this.checkedText){const t=typeof this.checkedLevel[this.delimit]=="string"?this.checkedLevel[this.delimit]:this.checkedText;t&&t.length&&t.split("").forEach(r=>{this.onFilter&&this.onFilter(r),this.filteredText+=r}),this.checkedText="",this.checkedLevel=void 0,this.checked=!1}const e=this.filteredText;return this.filteredText="",e}}const I=Object.freeze(Object.defineProperty({__proto__:null,StreamFilter:z},Symbol.toStringTag,{value:"Module"}));exports.filter=I;exports.parser=A;exports.utils=w;
|
|
20
|
+
*/function xe(t,e){let r=e;for(;!t[r];)r++;return r}function Oe(t){let e=t.length-1;for(;e>=0&&!t[e];)e--;return e}function we(t="",e={propName:{}}){const r=[],n=t.split(/(?:\r\n|\r|\n)/gm).filter(u=>u.length>0),a=n.map((u,l)=>n[l+2]&&/\d{2}:\d{2}:\d{2}[,\.]\d{2,3}\s*-->\s*\d{2}:\d{2}:\d{2}[,\.]\d{2,3}/.test(n[l+2])?u+`\r
|
|
21
|
+
`:u).join(`
|
|
22
|
+
`).split(/(?:\r\n|\r|\n)/gm),o=Oe(a)+1;let i=0,f,s,c;for(let u=0;u<o;u++){for(c={},s=[],u=xe(a,u),c.id=parseInt(a[u++],10),f=a[u++].split(/[\t ]*-->[\t ]*/),c[e.propName.start||"start"]=e.timeInText?f[0]:$(f[0]),i=f[1].indexOf(" "),i!==-1&&(f[1]=f[1].substr(0,i)),c[e.propName.end||"end"]=e.timeInText?f[1]:$(f[1]);u<o&&a[u];)s.push(a[u++]);const l=e.propName.text||"text";c[l]=s.join(e.keepMultiLine?`
|
|
23
|
+
`:" ").replace(/\{(\\[\w]+\(?([\w\d]+,?)+\)?)+\}/gi,""),c[l]=c[l].replace(/</g,"<").replace(/>/g,">"),c[l]=c[l].replace(/<(\/?(font|b|u|i|s))((\s+(\w|\w[\w\-]*\w)(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)(\/?)>/gi,"<$1$3$7>"),e.ignoreLineBreaks?c[l]=c[l].replace(/\\N/gi,""):c[l]=c[l].replace(/\\N/gi,"<br />"),r.push(c)}return r}function Ae(t,e){const r=/<\/?[^>]+(>|$)/g;return new Promise((n,a)=>{let o={};const i=[];let f=!1;const s=t.split(`
|
|
24
|
+
`);s.forEach((u,l)=>{u.replace(r,"")===" "||u.replace(r,"")===""||(u.indexOf("-->")!==-1?(f=!0,typeof o.start=="number"&&o.start>=0&&i.push(et(o)),o={start:tt(u.split("-->")[0].trimRight().split(" ").pop()),end:tt(u.split("-->")[1].trimLeft().split(" ").shift()),part:""}):f&&(i.length!==0?(i[i.length-1].part.replace(r,""),u.replace(r,""),o.part.length===0?o.part=u:o.part=`${o.part}
|
|
25
|
+
${u}`,l===s.length-1&&i.push(et(o))):(o.part=u,i.push(et(o)),o.part="")))}),o=[];const c=/(<([0-9:.>]+)>)/ig;i.forEach(u=>{const j=u.part.split().map(function(d){return d.replace(c,function(g){return g.split("").reduce(function(O,bt){return`==${g.replace("<","").replace(">","")}`},0)})})[0].replace(r,"").split(" "),b=[];j.forEach(function(d){if(d.indexOf("==")>-1){const g=d.split("=="),O=g[0];if(g[1],O==""||O=="##")return;b.push({word:jt(d.split("==")[0]),time:tt(d.split("==")[1])})}else b.push({word:jt(d),time:void 0})}),u.words=b,u.part=u.part.replace(r,"")}),n(i)})}function tt(t){const e=t.split(":"),r=e[e.length-3]?+e[e.length-3]:0,n=e[e.length-2]?+e[e.length-2]:0,a=+e[e.length-1];return r*3600+n*60+a}function jt(t){return t.replace(/[^0-9a-z'-]/gi,"").toLowerCase()}function et(t){if(t==null||typeof t!="object")return t;const e=t.constructor();for(const r in t)t.hasOwnProperty(r)&&(e[r]=t[r]);return e}const rt={padding:[.4,0],maxGap:10,minGap:2,minStart:6};function Pe(t){let e,r,n,a,o,i,f,s,c;const u=!!(t&&t.vad&&t.vad.length>0),l="[--end--]",m="[--Single--]",x="[--Full--]",j=/\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\]/;let b=!1,d=!1,g="";return O(),{feed:yt,reset:O,end:bt};function O(){e=!0,r="",n=!1,a="",o=!1,i=void 0,f=0,s=!1,c=0}function bt(){if(s){console.log("whisper parser is already end");return}yt(l)}function ve(K){b||(g+=K),!b&&!d&&Kt(g)&&(d=!0),!b&&g.length>50&&(b=!0)}function yt(K){var vt,Tt,_t;ve(K);const V=[];r+=K,e&&((vt=t==null?void 0:t.onStart)==null||vt.call(t,{type:"event",event:"start",data:[]})),e&&Ee(r)&&(r=r.slice(Vt.length)),e=!1;const Te=r.length;for(;f<Te;){const W=r[f];if(++f,o&&i&&W!=="["&&(i.text+=W),W==="["&&(n&&a.length&&i&&(i.text+=a),a="",n=!0,o=!1),n){if(a+=W,a===x&&(a=""),a===m&&(a="",u&&c++),a===l){i&&V.push(i),s=!0;break}if(j.test(a)){const C=j.exec(a),z=C==null?void 0:C[1],Q=C==null?void 0:C[2];if(i&&V.push(i),z&&Q){if(u&&(t!=null&&t.vad)&&t.vad.length>0){let q=0;q=t.vad[c].start-rt.padding[0]<0?0:t.vad[c].start-rt.padding[0];const _e=z==="00:00:00.000";let mt=S($(z)+q);c>0&&_e&&(mt=S($(z)+q+rt.padding[0])),i={st:mt,et:S($(Q)+q),text:"",children:[]}}else i={st:z,et:Q,text:"",children:[]};o=!0,n=!1,a=""}}}}V.length&&((Tt=t==null?void 0:t.onParse)==null||Tt.call(t,{type:"event",event:"message",data:V})),s&&((_t=t==null?void 0:t.onEnd)==null||_t.call(t,{type:"event",event:"end",data:r}),O())}}const Vt=[239,187,191];function Ee(t){return Vt.every((e,r)=>t.charCodeAt(r)===e)}async function Ce(t){return we(t,{keepMultiLine:!0,propName:{}}).filter(n=>n.text).map(n=>({st:S(n.start),et:S(n.end),text:n.text}))}async function Wt(t){return(await Ae(t)).filter(n=>n.part).map(n=>({st:S(n.start),et:S(n.end),text:n.part}))}async function Fe(t){return Wt($e(t))}const Ie=Object.freeze(Object.defineProperty({__proto__:null,assToAimSegments:Fe,createWhisperStreamParser:Pe,srtToAimSegments:Ce,vttToAimSegments:Wt},Symbol.toStringTag,{value:"Module"}));class Le{constructor(e){y(this,"chains",{});y(this,"delimit","__replace__");y(this,"onFilter");y(this,"checked",!1);y(this,"checkedText","");y(this,"checkedLevel");y(this,"filteredText","");this.onFilter=e}add(e,r){typeof e!="string"&&(e=new TextDecoder().decode(e));const n=e;if(!n)return;let a=this.chains;for(let o=0;o<n.length;o++){if(n[o]in a)a=a[n[o]];else{if(!(a instanceof Object))break;let i,f;for(let s=o;s<n.length;s++)a[n[s]]={},[i,f]=[a,n[s]],a=a[n[s]];i&&f&&(i[f]={[this.delimit]:r||""});break}o===n.length-1&&(a[this.delimit]=r||"")}}parse(e){e.forEach(r=>{this.add(...r)})}reParse(e){this.chains={},e.forEach(r=>{this.add(r[0],r[1])})}feed(e){if(!this.checked&&!this.chains[e]){this.filteredText+=e,this.onFilter&&this.onFilter(e);return}if(!this.checked&&this.chains[e]){this.checked=!0,this.checkedLevel=this.chains[e],this.checkedText+=e;return}if(this.checked&&this.checkedLevel[e]){this.checkedLevel=this.checkedLevel[e],this.checkedText+=e;return}if(this.checked&&!this.checkedLevel[e]){const r=typeof this.checkedLevel[this.delimit]=="string"?this.checkedLevel[this.delimit]:this.checkedText;r&&r.length&&r.split("").forEach(n=>{this.filteredText+=n,this.onFilter&&this.onFilter(n)}),this.checked=!1,this.checkedText="",this.checkedLevel=void 0,this.chains[e]?(this.checked=!0,this.checkedLevel=this.chains[e],this.checkedText=e):(this.onFilter&&this.onFilter(e),this.filteredText+=e)}}feedAll(e){return e.length?(e.split("").forEach(n=>{this.feed(n)}),this.end()):""}end(){if(this.checked&&this.checkedText){const r=typeof this.checkedLevel[this.delimit]=="string"?this.checkedLevel[this.delimit]:this.checkedText;r&&r.length&&r.split("").forEach(n=>{this.onFilter&&this.onFilter(n),this.filteredText+=n}),this.checkedText="",this.checkedLevel=void 0,this.checked=!1}const e=this.filteredText;return this.filteredText="",e}}const Me=Object.freeze(Object.defineProperty({__proto__:null,StreamFilter:Le},Symbol.toStringTag,{value:"Module"}));var qt=typeof global=="object"&&global&&global.Object===Object&&global,ze=typeof self=="object"&&self&&self.Object===Object&&self,v=qt||ze||Function("return this")(),F=v.Symbol,Jt=Object.prototype,Ne=Jt.hasOwnProperty,De=Jt.toString,N=F?F.toStringTag:void 0;function ke(t){var e=Ne.call(t,N),r=t[N];try{t[N]=void 0;var n=!0}catch{}var a=De.call(t);return n&&(e?t[N]=r:delete t[N]),a}var Be=Object.prototype,Ge=Be.toString;function Ue(t){return Ge.call(t)}var Re="[object Null]",He="[object Undefined]",St=F?F.toStringTag:void 0;function U(t){return t==null?t===void 0?He:Re:St&&St in Object(t)?ke(t):Ue(t)}function R(t){return t!=null&&typeof t=="object"}var ut=Array.isArray;function H(t){var e=typeof t;return t!=null&&(e=="object"||e=="function")}var Ke="[object AsyncFunction]",Ve="[object Function]",We="[object GeneratorFunction]",qe="[object Proxy]";function Yt(t){if(!H(t))return!1;var e=U(t);return e==Ve||e==We||e==Ke||e==qe}var nt=v["__core-js_shared__"],$t=function(){var t=/[^.]+$/.exec(nt&&nt.keys&&nt.keys.IE_PROTO||"");return t?"Symbol(src)_1."+t:""}();function Je(t){return!!$t&&$t in t}var Ye=Function.prototype,Xe=Ye.toString;function P(t){if(t!=null){try{return Xe.call(t)}catch{}try{return t+""}catch{}}return""}var Ze=/[\\^$.*+?()[\]{}|]/g,Qe=/^\[object .+?Constructor\]$/,tr=Function.prototype,er=Object.prototype,rr=tr.toString,nr=er.hasOwnProperty,ar=RegExp("^"+rr.call(nr).replace(Ze,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function ir(t){if(!H(t)||Je(t))return!1;var e=Yt(t)?ar:Qe;return e.test(P(t))}function sr(t,e){return t==null?void 0:t[e]}function E(t,e){var r=sr(t,e);return ir(r)?r:void 0}var it=E(v,"WeakMap"),xt=Object.create,or=function(){function t(){}return function(e){if(!H(e))return{};if(xt)return xt(e);t.prototype=e;var r=new t;return t.prototype=void 0,r}}();function cr(t,e){var r=-1,n=t.length;for(e||(e=Array(n));++r<n;)e[r]=t[r];return e}var Ot=function(){try{var t=E(Object,"defineProperty");return t({},"",{}),t}catch{}}();function ur(t,e){for(var r=-1,n=t==null?0:t.length;++r<n&&e(t[r],r,t)!==!1;);return t}var fr=9007199254740991,lr=/^(?:0|[1-9]\d*)$/;function hr(t,e){var r=typeof t;return e=e??fr,!!e&&(r=="number"||r!="symbol"&&lr.test(t))&&t>-1&&t%1==0&&t<e}function Xt(t,e,r){e=="__proto__"&&Ot?Ot(t,e,{configurable:!0,enumerable:!0,value:r,writable:!0}):t[e]=r}function Zt(t,e){return t===e||t!==t&&e!==e}var pr=Object.prototype,dr=pr.hasOwnProperty;function Qt(t,e,r){var n=t[e];(!(dr.call(t,e)&&Zt(n,r))||r===void 0&&!(e in t))&&Xt(t,e,r)}function Y(t,e,r,n){var a=!r;r||(r={});for(var o=-1,i=e.length;++o<i;){var f=e[o],s=void 0;s===void 0&&(s=t[f]),a?Xt(r,f,s):Qt(r,f,s)}return r}var gr=9007199254740991;function te(t){return typeof t=="number"&&t>-1&&t%1==0&&t<=gr}function ee(t){return t!=null&&te(t.length)&&!Yt(t)}var br=Object.prototype;function ft(t){var e=t&&t.constructor,r=typeof e=="function"&&e.prototype||br;return t===r}function yr(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n}var vr="[object Arguments]";function wt(t){return R(t)&&U(t)==vr}var re=Object.prototype,Tr=re.hasOwnProperty,_r=re.propertyIsEnumerable,mr=wt(function(){return arguments}())?wt:function(t){return R(t)&&Tr.call(t,"callee")&&!_r.call(t,"callee")};function jr(){return!1}var ne=typeof exports=="object"&&exports&&!exports.nodeType&&exports,At=ne&&typeof module=="object"&&module&&!module.nodeType&&module,Sr=At&&At.exports===ne,Pt=Sr?v.Buffer:void 0,$r=Pt?Pt.isBuffer:void 0,ae=$r||jr,xr="[object Arguments]",Or="[object Array]",wr="[object Boolean]",Ar="[object Date]",Pr="[object Error]",Er="[object Function]",Cr="[object Map]",Fr="[object Number]",Ir="[object Object]",Lr="[object RegExp]",Mr="[object Set]",zr="[object String]",Nr="[object WeakMap]",Dr="[object ArrayBuffer]",kr="[object DataView]",Br="[object Float32Array]",Gr="[object Float64Array]",Ur="[object Int8Array]",Rr="[object Int16Array]",Hr="[object Int32Array]",Kr="[object Uint8Array]",Vr="[object Uint8ClampedArray]",Wr="[object Uint16Array]",qr="[object Uint32Array]",p={};p[Br]=p[Gr]=p[Ur]=p[Rr]=p[Hr]=p[Kr]=p[Vr]=p[Wr]=p[qr]=!0;p[xr]=p[Or]=p[Dr]=p[wr]=p[kr]=p[Ar]=p[Pr]=p[Er]=p[Cr]=p[Fr]=p[Ir]=p[Lr]=p[Mr]=p[zr]=p[Nr]=!1;function Jr(t){return R(t)&&te(t.length)&&!!p[U(t)]}function lt(t){return function(e){return t(e)}}var ie=typeof exports=="object"&&exports&&!exports.nodeType&&exports,k=ie&&typeof module=="object"&&module&&!module.nodeType&&module,Yr=k&&k.exports===ie,at=Yr&&qt.process,I=function(){try{var t=k&&k.require&&k.require("util").types;return t||at&&at.binding&&at.binding("util")}catch{}}(),Et=I&&I.isTypedArray,Xr=Et?lt(Et):Jr,Zr=Object.prototype,Qr=Zr.hasOwnProperty;function se(t,e){var r=ut(t),n=!r&&mr(t),a=!r&&!n&&ae(t),o=!r&&!n&&!a&&Xr(t),i=r||n||a||o,f=i?yr(t.length,String):[],s=f.length;for(var c in t)(e||Qr.call(t,c))&&!(i&&(c=="length"||a&&(c=="offset"||c=="parent")||o&&(c=="buffer"||c=="byteLength"||c=="byteOffset")||hr(c,s)))&&f.push(c);return f}function oe(t,e){return function(r){return t(e(r))}}var tn=oe(Object.keys,Object),en=Object.prototype,rn=en.hasOwnProperty;function nn(t){if(!ft(t))return tn(t);var e=[];for(var r in Object(t))rn.call(t,r)&&r!="constructor"&&e.push(r);return e}function ht(t){return ee(t)?se(t):nn(t)}function an(t){var e=[];if(t!=null)for(var r in Object(t))e.push(r);return e}var sn=Object.prototype,on=sn.hasOwnProperty;function cn(t){if(!H(t))return an(t);var e=ft(t),r=[];for(var n in t)n=="constructor"&&(e||!on.call(t,n))||r.push(n);return r}function pt(t){return ee(t)?se(t,!0):cn(t)}var B=E(Object,"create");function un(){this.__data__=B?B(null):{},this.size=0}function fn(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e}var ln="__lodash_hash_undefined__",hn=Object.prototype,pn=hn.hasOwnProperty;function dn(t){var e=this.__data__;if(B){var r=e[t];return r===ln?void 0:r}return pn.call(e,t)?e[t]:void 0}var gn=Object.prototype,bn=gn.hasOwnProperty;function yn(t){var e=this.__data__;return B?e[t]!==void 0:bn.call(e,t)}var vn="__lodash_hash_undefined__";function Tn(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=B&&e===void 0?vn:e,this}function A(t){var e=-1,r=t==null?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}A.prototype.clear=un;A.prototype.delete=fn;A.prototype.get=dn;A.prototype.has=yn;A.prototype.set=Tn;function _n(){this.__data__=[],this.size=0}function X(t,e){for(var r=t.length;r--;)if(Zt(t[r][0],e))return r;return-1}var mn=Array.prototype,jn=mn.splice;function Sn(t){var e=this.__data__,r=X(e,t);if(r<0)return!1;var n=e.length-1;return r==n?e.pop():jn.call(e,r,1),--this.size,!0}function $n(t){var e=this.__data__,r=X(e,t);return r<0?void 0:e[r][1]}function xn(t){return X(this.__data__,t)>-1}function On(t,e){var r=this.__data__,n=X(r,t);return n<0?(++this.size,r.push([t,e])):r[n][1]=e,this}function _(t){var e=-1,r=t==null?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}_.prototype.clear=_n;_.prototype.delete=Sn;_.prototype.get=$n;_.prototype.has=xn;_.prototype.set=On;var G=E(v,"Map");function wn(){this.size=0,this.__data__={hash:new A,map:new(G||_),string:new A}}function An(t){var e=typeof t;return e=="string"||e=="number"||e=="symbol"||e=="boolean"?t!=="__proto__":t===null}function Z(t,e){var r=t.__data__;return An(e)?r[typeof e=="string"?"string":"hash"]:r.map}function Pn(t){var e=Z(this,t).delete(t);return this.size-=e?1:0,e}function En(t){return Z(this,t).get(t)}function Cn(t){return Z(this,t).has(t)}function Fn(t,e){var r=Z(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this}function L(t){var e=-1,r=t==null?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}L.prototype.clear=wn;L.prototype.delete=Pn;L.prototype.get=En;L.prototype.has=Cn;L.prototype.set=Fn;function ce(t,e){for(var r=-1,n=e.length,a=t.length;++r<n;)t[a+r]=e[r];return t}var ue=oe(Object.getPrototypeOf,Object);function In(){this.__data__=new _,this.size=0}function Ln(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r}function Mn(t){return this.__data__.get(t)}function zn(t){return this.__data__.has(t)}var Nn=200;function Dn(t,e){var r=this.__data__;if(r instanceof _){var n=r.__data__;if(!G||n.length<Nn-1)return n.push([t,e]),this.size=++r.size,this;r=this.__data__=new L(n)}return r.set(t,e),this.size=r.size,this}function M(t){var e=this.__data__=new _(t);this.size=e.size}M.prototype.clear=In;M.prototype.delete=Ln;M.prototype.get=Mn;M.prototype.has=zn;M.prototype.set=Dn;function kn(t,e){return t&&Y(e,ht(e),t)}function Bn(t,e){return t&&Y(e,pt(e),t)}var fe=typeof exports=="object"&&exports&&!exports.nodeType&&exports,Ct=fe&&typeof module=="object"&&module&&!module.nodeType&&module,Gn=Ct&&Ct.exports===fe,Ft=Gn?v.Buffer:void 0,It=Ft?Ft.allocUnsafe:void 0;function Un(t,e){if(e)return t.slice();var r=t.length,n=It?It(r):new t.constructor(r);return t.copy(n),n}function Rn(t,e){for(var r=-1,n=t==null?0:t.length,a=0,o=[];++r<n;){var i=t[r];e(i,r,t)&&(o[a++]=i)}return o}function le(){return[]}var Hn=Object.prototype,Kn=Hn.propertyIsEnumerable,Lt=Object.getOwnPropertySymbols,dt=Lt?function(t){return t==null?[]:(t=Object(t),Rn(Lt(t),function(e){return Kn.call(t,e)}))}:le;function Vn(t,e){return Y(t,dt(t),e)}var Wn=Object.getOwnPropertySymbols,he=Wn?function(t){for(var e=[];t;)ce(e,dt(t)),t=ue(t);return e}:le;function qn(t,e){return Y(t,he(t),e)}function pe(t,e,r){var n=e(t);return ut(t)?n:ce(n,r(t))}function Jn(t){return pe(t,ht,dt)}function Yn(t){return pe(t,pt,he)}var st=E(v,"DataView"),ot=E(v,"Promise"),ct=E(v,"Set"),Mt="[object Map]",Xn="[object Object]",zt="[object Promise]",Nt="[object Set]",Dt="[object WeakMap]",kt="[object DataView]",Zn=P(st),Qn=P(G),ta=P(ot),ea=P(ct),ra=P(it),T=U;(st&&T(new st(new ArrayBuffer(1)))!=kt||G&&T(new G)!=Mt||ot&&T(ot.resolve())!=zt||ct&&T(new ct)!=Nt||it&&T(new it)!=Dt)&&(T=function(t){var e=U(t),r=e==Xn?t.constructor:void 0,n=r?P(r):"";if(n)switch(n){case Zn:return kt;case Qn:return Mt;case ta:return zt;case ea:return Nt;case ra:return Dt}return e});var na=Object.prototype,aa=na.hasOwnProperty;function ia(t){var e=t.length,r=new t.constructor(e);return e&&typeof t[0]=="string"&&aa.call(t,"index")&&(r.index=t.index,r.input=t.input),r}var Bt=v.Uint8Array;function gt(t){var e=new t.constructor(t.byteLength);return new Bt(e).set(new Bt(t)),e}function sa(t,e){var r=e?gt(t.buffer):t.buffer;return new t.constructor(r,t.byteOffset,t.byteLength)}var oa=/\w*$/;function ca(t){var e=new t.constructor(t.source,oa.exec(t));return e.lastIndex=t.lastIndex,e}var Gt=F?F.prototype:void 0,Ut=Gt?Gt.valueOf:void 0;function ua(t){return Ut?Object(Ut.call(t)):{}}function fa(t,e){var r=e?gt(t.buffer):t.buffer;return new t.constructor(r,t.byteOffset,t.length)}var la="[object Boolean]",ha="[object Date]",pa="[object Map]",da="[object Number]",ga="[object RegExp]",ba="[object Set]",ya="[object String]",va="[object Symbol]",Ta="[object ArrayBuffer]",_a="[object DataView]",ma="[object Float32Array]",ja="[object Float64Array]",Sa="[object Int8Array]",$a="[object Int16Array]",xa="[object Int32Array]",Oa="[object Uint8Array]",wa="[object Uint8ClampedArray]",Aa="[object Uint16Array]",Pa="[object Uint32Array]";function Ea(t,e,r){var n=t.constructor;switch(e){case Ta:return gt(t);case la:case ha:return new n(+t);case _a:return sa(t,r);case ma:case ja:case Sa:case $a:case xa:case Oa:case wa:case Aa:case Pa:return fa(t,r);case pa:return new n;case da:case ya:return new n(t);case ga:return ca(t);case ba:return new n;case va:return ua(t)}}function Ca(t){return typeof t.constructor=="function"&&!ft(t)?or(ue(t)):{}}var Fa="[object Map]";function Ia(t){return R(t)&&T(t)==Fa}var Rt=I&&I.isMap,La=Rt?lt(Rt):Ia,Ma="[object Set]";function za(t){return R(t)&&T(t)==Ma}var Ht=I&&I.isSet,Na=Ht?lt(Ht):za,Da=1,ka=2,Ba=4,de="[object Arguments]",Ga="[object Array]",Ua="[object Boolean]",Ra="[object Date]",Ha="[object Error]",ge="[object Function]",Ka="[object GeneratorFunction]",Va="[object Map]",Wa="[object Number]",be="[object Object]",qa="[object RegExp]",Ja="[object Set]",Ya="[object String]",Xa="[object Symbol]",Za="[object WeakMap]",Qa="[object ArrayBuffer]",ti="[object DataView]",ei="[object Float32Array]",ri="[object Float64Array]",ni="[object Int8Array]",ai="[object Int16Array]",ii="[object Int32Array]",si="[object Uint8Array]",oi="[object Uint8ClampedArray]",ci="[object Uint16Array]",ui="[object Uint32Array]",h={};h[de]=h[Ga]=h[Qa]=h[ti]=h[Ua]=h[Ra]=h[ei]=h[ri]=h[ni]=h[ai]=h[ii]=h[Va]=h[Wa]=h[be]=h[qa]=h[Ja]=h[Ya]=h[Xa]=h[si]=h[oi]=h[ci]=h[ui]=!0;h[Ha]=h[ge]=h[Za]=!1;function J(t,e,r,n,a,o){var i,f=e&Da,s=e&ka,c=e&Ba;if(i!==void 0)return i;if(!H(t))return t;var u=ut(t);if(u){if(i=ia(t),!f)return cr(t,i)}else{var l=T(t),m=l==ge||l==Ka;if(ae(t))return Un(t,f);if(l==be||l==de||m&&!a){if(i=s||m?{}:Ca(t),!f)return s?qn(t,Bn(i,t)):Vn(t,kn(i,t))}else{if(!h[l])return a?t:{};i=Ea(t,l,f)}}o||(o=new M);var x=o.get(t);if(x)return x;o.set(t,i),Na(t)?t.forEach(function(d){i.add(J(d,e,r,d,t,o))}):La(t)&&t.forEach(function(d,g){i.set(g,J(d,e,r,g,t,o))});var j=c?s?Yn:Jn:s?pt:ht,b=u?void 0:j(t);return ur(b||t,function(d,g){b&&(g=d,d=t[g]),Qt(i,g,J(d,e,r,g,t,o))}),i}var fi=1,li=4;function hi(t){return J(t,fi|li)}class ye{constructor(e){y(this,"threshold",2);y(this,"hit",0);y(this,"prevSegment");y(this,"hitSegment");y(this,"hitSegmentList",[]);y(this,"options");this.options=e||{}}push(e){var r,n;this.hitSegment&&this.hitSegment.text!==e.text&&(this.hitSegmentList.push(this.hitSegment),this.reset()),this.prevSegment&&e.text===this.prevSegment.text?(this.hit++,this.hit===1&&(this.hitSegment=hi(this.prevSegment)),this.threshold<=this.hit&&this.hitSegment&&(this.hitSegment.et=e.et,this.hitSegment.hit=this.hit,(n=(r=this.options).onHit)==null||n.call(r,this.hitSegmentList))):this.hit=0,this.prevSegment=e}end(){var e,r;this.hitSegment&&(this.hitSegmentList.push(this.hitSegment),(r=(e=this.options).onHit)==null||r.call(e,this.hitSegmentList)),this.reset()}reset(){this.hit=0,this.prevSegment=void 0,this.hitSegment=void 0}}const w={punc:/[.,?!\],。!?…]{1}/,em:/[.?!\]。!?…]{1}/,space:/^\s+|\s+$|\s{2,}/g,ep:/[.,?!,。!?…]{1}\s*$/};function pi(t,e){let r=[];const n=new ye({onHit:s=>{r=s}}),a=s=>s.trim().length===0,o=s=>{const c=$(s.st),u=$(s.et);return c>=u},i={emt:[],ep:[],zf:[],punc:[],em:[],space:[]},f=[];return t.forEach((s,c)=>{const u=w.punc.test(s.text),l=w.em.test(s.text),m=w.space.test(s.text),x=a(s.text),j=o(s),b=w.ep.test(s.text);n.push(s),s.punc=u?1:0,u&&i.punc.push(c),s.em=l?1:0,l&&i.em.push(c),s.space=m?1:0,m&&(e!=null&&e.space?s.text=s.text.replace(w.space," ").trim():i.space.push(c)),s.emt=a(s.text)?1:0,x&&(e!=null&&e.emt||i.emt.push(c)),s.zf=o(s)?1:0,j&&(e!=null&&e.zf||i.zf.push(c)),s.ep=b?1:0,b&&(e!=null&&e.ep?s.text=s.text.replace(w.ep,""):i.ep.push(c)),!(e!=null&&e.zf&&s.zf)&&(e!=null&&e.emt&&s.emt||f.push(s))}),n.end(),{result:i,repeat:r,segments:f}}const di=Object.freeze(Object.defineProperty({__proto__:null,RepeatCheck:ye,subtitleOptimization:pi,subtitleOptimizationRegExp:w},Symbol.toStringTag,{value:"Module"}));exports.filter=Me;exports.parser=Ie;exports.tools=di;exports.utils=Se;
|
package/dist/index.d.ts
CHANGED
|
@@ -66,6 +66,15 @@ declare interface AimSegments {
|
|
|
66
66
|
|
|
67
67
|
declare function assToAimSegments(text: string): Promise<AimSegments[]>;
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* 匹配中文、日文、韩文字符的正则表达式
|
|
71
|
+
*
|
|
72
|
+
* @export
|
|
73
|
+
* @param {string} str
|
|
74
|
+
* @return {*} {boolean}
|
|
75
|
+
*/
|
|
76
|
+
declare function containsCJKCharacters(str: string): boolean;
|
|
77
|
+
|
|
69
78
|
/**
|
|
70
79
|
* Convert a HH:MM:SS,MMM or HH:MM:SS.MMM time format into seconds.
|
|
71
80
|
*
|
|
@@ -75,6 +84,19 @@ declare function assToAimSegments(text: string): Promise<AimSegments[]>;
|
|
|
75
84
|
*/
|
|
76
85
|
declare function convertToSeconds(time?: string): number;
|
|
77
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Creates a new Parser parser.
|
|
89
|
+
*
|
|
90
|
+
* @param onParse - Callback to invoke when a new event is parsed, or a new reconnection interval
|
|
91
|
+
* has been sent from the server
|
|
92
|
+
*
|
|
93
|
+
* @returns A new Parser parser, with `parse`, `end` and `reset` methods.
|
|
94
|
+
* @public
|
|
95
|
+
*/
|
|
96
|
+
declare function create(options?: ParserOptions<AimSegments[], AimSegments[], string> & {
|
|
97
|
+
vad?: VADTimeItem[];
|
|
98
|
+
}): Parser<string>;
|
|
99
|
+
|
|
78
100
|
export declare namespace filter {
|
|
79
101
|
export {
|
|
80
102
|
StreamFilter
|
|
@@ -90,19 +112,105 @@ export declare namespace filter {
|
|
|
90
112
|
*/
|
|
91
113
|
declare function formatTime(seconds: number): string;
|
|
92
114
|
|
|
115
|
+
declare interface OptimizationOptions {
|
|
116
|
+
emt: boolean;
|
|
117
|
+
ep: boolean;
|
|
118
|
+
zf: boolean;
|
|
119
|
+
punc: boolean;
|
|
120
|
+
em: boolean;
|
|
121
|
+
space: boolean;
|
|
122
|
+
}
|
|
123
|
+
|
|
93
124
|
/**
|
|
94
125
|
* 为数字前方自动补0到特定的位数,默认两位
|
|
95
126
|
*/
|
|
96
127
|
declare function padNumber(num: number, length?: number): string;
|
|
97
128
|
|
|
129
|
+
/**
|
|
130
|
+
* Callback passed as the `onParse` callback to a parser
|
|
131
|
+
*
|
|
132
|
+
* @public
|
|
133
|
+
*/
|
|
134
|
+
declare type ParseCallback<T> = (event: ParsedEvent<T>) => void;
|
|
135
|
+
|
|
136
|
+
declare interface ParsedEvent<T> {
|
|
137
|
+
type: "event";
|
|
138
|
+
event: "start" | "message" | "end";
|
|
139
|
+
data?: T;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Parser parser instance.
|
|
144
|
+
*
|
|
145
|
+
* Needs to be reset between reconnections/when switching data source, using the `reset()` method.
|
|
146
|
+
*
|
|
147
|
+
* @public
|
|
148
|
+
*/
|
|
149
|
+
declare interface Parser<T = string> {
|
|
150
|
+
/**
|
|
151
|
+
* Feeds the parser another chunk. The method _does not_ return a parsed message.
|
|
152
|
+
* Instead, if the chunk was a complete message (or completed a previously incomplete message),
|
|
153
|
+
* it will invoke the `onParse` callback used to create the parsers.
|
|
154
|
+
*
|
|
155
|
+
* @param chunk - The chunk to parse. Can be a partial, eg in the case of streaming messages.
|
|
156
|
+
* @public
|
|
157
|
+
*/
|
|
158
|
+
feed(chunk: T): void;
|
|
159
|
+
/**
|
|
160
|
+
* Resets the parser state. This is required when you have a new stream of messages -
|
|
161
|
+
* for instance in the case of a client being disconnected and reconnecting.
|
|
162
|
+
*
|
|
163
|
+
* @public
|
|
164
|
+
*/
|
|
165
|
+
reset(): void;
|
|
166
|
+
end(): void;
|
|
167
|
+
}
|
|
168
|
+
|
|
98
169
|
export declare namespace parser {
|
|
99
170
|
export {
|
|
100
171
|
srtToAimSegments,
|
|
101
172
|
vttToAimSegments,
|
|
102
|
-
assToAimSegments
|
|
173
|
+
assToAimSegments,
|
|
174
|
+
create as createWhisperStreamParser
|
|
103
175
|
}
|
|
104
176
|
}
|
|
105
177
|
|
|
178
|
+
/**
|
|
179
|
+
* `ParserOptions` 是一个泛型类型,用于配置解析器的回调函数。
|
|
180
|
+
*
|
|
181
|
+
* @template S - 解析开始时的状态类型
|
|
182
|
+
* @template P - 解析过程中使用的状态类型
|
|
183
|
+
* @template E - 解析结束时的状态类型
|
|
184
|
+
*
|
|
185
|
+
* @property {ParseCallback<S>} [onStart] - 解析开始时的回调函数
|
|
186
|
+
* @property {ParseCallback<P>} [onParse] - 解析过程中调用的回调函数
|
|
187
|
+
* @property {ParseCallback<P>} [onProgress] - 解析进度更新时的回调函数
|
|
188
|
+
* @property {ParseCallback<E>} [onEnd] - 解析结束时的回调函数
|
|
189
|
+
*/
|
|
190
|
+
declare type ParserOptions<S, P, E> = {
|
|
191
|
+
onStart?: ParseCallback<S>;
|
|
192
|
+
onParse?: ParseCallback<P>;
|
|
193
|
+
onProgress?: ParseCallback<P>;
|
|
194
|
+
onEnd?: ParseCallback<E>;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
declare class RepeatCheck {
|
|
198
|
+
threshold: number;
|
|
199
|
+
hit: number;
|
|
200
|
+
prevSegment?: AimSegments;
|
|
201
|
+
hitSegment?: AimSegments;
|
|
202
|
+
hitSegmentList: AimSegments[];
|
|
203
|
+
options: RepeatCheckOption;
|
|
204
|
+
constructor(options?: RepeatCheckOption);
|
|
205
|
+
push(segment: AimSegments): void;
|
|
206
|
+
end(): void;
|
|
207
|
+
reset(): void;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
declare interface RepeatCheckOption {
|
|
211
|
+
onHit?: (segment: AimSegments[]) => void;
|
|
212
|
+
}
|
|
213
|
+
|
|
106
214
|
declare function srtToAimSegments(text: string): Promise<AimSegments[]>;
|
|
107
215
|
|
|
108
216
|
declare class StreamFilter {
|
|
@@ -122,14 +230,49 @@ declare class StreamFilter {
|
|
|
122
230
|
end(): string;
|
|
123
231
|
}
|
|
124
232
|
|
|
233
|
+
declare function subtitleOptimization(segments: AimSegments[], options?: OptimizationOptions): {
|
|
234
|
+
result: {
|
|
235
|
+
emt: number[];
|
|
236
|
+
ep: number[];
|
|
237
|
+
zf: number[];
|
|
238
|
+
punc: number[];
|
|
239
|
+
em: number[];
|
|
240
|
+
space: number[];
|
|
241
|
+
};
|
|
242
|
+
repeat: AimSegments[];
|
|
243
|
+
segments: AimSegments[];
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
declare const subtitleOptimizationRegExp: {
|
|
247
|
+
punc: RegExp;
|
|
248
|
+
em: RegExp;
|
|
249
|
+
space: RegExp;
|
|
250
|
+
ep: RegExp;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
export declare namespace tools {
|
|
254
|
+
export {
|
|
255
|
+
subtitleOptimization,
|
|
256
|
+
RepeatCheckOption,
|
|
257
|
+
RepeatCheck,
|
|
258
|
+
subtitleOptimizationRegExp
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
125
262
|
export declare namespace utils {
|
|
126
263
|
export {
|
|
127
264
|
padNumber,
|
|
128
265
|
formatTime,
|
|
129
|
-
convertToSeconds
|
|
266
|
+
convertToSeconds,
|
|
267
|
+
containsCJKCharacters
|
|
130
268
|
}
|
|
131
269
|
}
|
|
132
270
|
|
|
271
|
+
declare type VADTimeItem = {
|
|
272
|
+
start: number;
|
|
273
|
+
end: number;
|
|
274
|
+
};
|
|
275
|
+
|
|
133
276
|
declare function vttToAimSegments(text: string): Promise<AimSegments[]>;
|
|
134
277
|
|
|
135
278
|
export { }
|