Bilibili-Evolved/min/danmaku-converter.min.js
2019-01-17 17:21:24 +08:00

1 line
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(()=>{return(t,e)=>{class s{constructor(t,e,s,i,n){this.content=t;this.time=e;this.type=parseInt(s);this.fontSize=parseFloat(i);this.color=parseInt(n)}}class i extends s{constructor({content:t,time:e,type:s,fontSize:i,color:n,timeStamp:o,pool:r,userHash:a,rowId:h}){super(t,e,s,i,n);this.timeStamp=parseInt(o);this.pool=parseInt(r);this.userHash=a;this.rowId=parseInt(h);this.time=parseFloat(this.time);this.pDataArray=[e,s,i,n,o,r,a,h]}text(){const t=this.pDataArray.join(",");return`<d p="${t}">${this.content}</d>`}static parse(t){const e=t.getAttribute("p");const[s,n,o,r,a,h,l,c]=e.split(",");const u=t.innerHTML;return new i({content:u,time:s,type:n,fontSize:o,color:r,timeStamp:a,pool:h,userHash:l,rowId:c})}}class n{constructor(t){this.xml=t;const e=(new DOMParser).parseFromString(t,"application/xml").documentElement;this.danmakus=[...e.querySelectorAll("d[p]")].map(t=>i.parse(t))}}class o extends s{constructor({content:t,time:e,type:s,fontSize:i,color:n,typeTag:o,colorTag:r,endTime:a}){super(t,e,s,i,n);this.typeTag=o;this.colorTag=r;this.endTime=a}text(t){const e=t[this.fontSize].match(/Style:(.*?),/)[1].trim();return`Dialogue: 0,${this.time},${this.endTime},${e},,0,0,0,,{${this.typeTag}${this.colorTag}}${this.content}`}}class r{constructor({danmakus:t,title:e,fontStyles:s,blockTypes:i,resolution:n}){this.danmakus=t;this.title=e;this.fontStyles=s;this.blockTypes=i;this.resolution=n}generateAss(){const t=`\n[Script Info]\n; Script generated by Bilibili Evolved Danmaku Converter\n; https://github.com/the1812/Bilibili-Evolved/\nTitle: ${this.title}\nScriptType: v4.00+\nPlayResX: ${this.resolution.x}\nPlayResY: ${this.resolution.y}\nTimer: 10.0000\nWrapStyle: 2\nScaledBorderAndShadow: no\n\n[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n${Object.values(this.fontStyles).join("\n")}\n\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n `.trim();return t+"\n"+this.danmakus.map(t=>t.text(this.fontStyles,this.blockTypes)).filter(t=>t!=="").join("\n")}}class a{constructor(t,e,s,i){this.horizontalDanmakus=[];this.horizontalTrack=[];this.verticalDanmakus=[];this.verticalTrack=[];this.resolution=e;this.duration=s;this.canvas=document.createElement("canvas");this.context=this.canvas.getContext("2d");this.fontSizes={25:`52px ${t}`,18:`36px ${t}`};this.danmakuType={1:"normal",2:"normal",3:"normal",4:"bottom",5:"top",6:"reversed",7:"special",8:"special"};this.bottomMarginPercent=i;this.margin=4;this.nextDanmakuDelay=.05;this.generateTracks()}generateTracks(){const t=52;this.danmakuHeight=t;this.trackHeight=this.margin*2+t;this.trackCount=parseInt(fixed(this.resolution.y*(1-this.bottomMarginPercent)/this.trackHeight,0))}getTextSize(t){this.context.font=this.fontSizes[t.fontSize];const e=this.context.measureText(t.content);const s=e.width/2;return[s,this.danmakuHeight/2]}getHorizonalTags(t){const[e,s]=this.getTextSize(t);const i=e*2;const n=this.duration(t)*i/(this.resolution.x+i)+this.nextDanmakuDelay;let o=0;let r=null;const a=e=>{if(e.track!==o){return false}if(e.width<i){return this.duration(t)*this.resolution.x/(this.resolution.x+i)<=e.end-t.time}else{return e.visible>t.time}};do{r=this.horizontalTrack.find(a);o++}while(r&&o<=this.trackCount);if(o>this.trackCount){return`\\pos(0,-999)`}o--;this.horizontalTrack.push({width:i,start:t.time,visible:t.time+n,end:t.time+this.duration(t),track:o});return`\\move(${this.resolution.x+e},${o*this.trackHeight+this.margin+s},${-e},${o*this.trackHeight+this.margin+s},0,${this.duration(t)*1e3})`}getVerticalTags(t){const[,e]=this.getTextSize(t);const s=this.danmakuType[t.type]==="top";let i=null;let n=s?0:this.trackCount-1;const o=s?1:-1;const r=e=>{if(e.track!==n){return false}return e.end>t.time};do{i=this.verticalTrack.find(r);n+=o}while(i&&n<=this.trackCount&&n>=0);if(n>this.trackCount||n<0){return`\\pos(0,-999)`}n-=o;this.verticalTrack.push({start:t.time,end:t.time+this.duration(t),track:n});if(s){return`\\pos(${this.resolution.x/2},${n*this.trackHeight+this.margin+e})`}else{return`\\pos(${this.resolution.x/2},${this.resolution.y-this.margin-e-(this.trackCount-1-n)*this.trackHeight})`}}push(t){let e=null;let s=null;switch(this.danmakuType[t.type]){case"normal":case"reversed":{e=this.getHorizonalTags(t);s=this.horizontalDanmakus;break}case"top":case"bottom":{e=this.getVerticalTags(t);s=this.verticalDanmakus;break}case"special":default:{return{tags:`\\pos(0,-999)`}}}const i={tags:e};s.push(i);return i}}class h{constructor({title:t,font:e,alpha:s,duration:i,blockTypes:n,resolution:o,bottomMarginPercent:r}){this.title=t;this.font=e;this.alpha=Math.round(s*100);this.duration=i;this.blockTypes=n;this.resolution=o;this.danmakuStack=new a(e,o,i,r)}get fontStyles(){return{25:`Style: Medium,${this.font},52,&H${this.alpha}FFFFFF,&H${this.alpha}FFFFFF,&H${this.alpha}000000,&H${this.alpha}000000,0,0,0,0,100,100,0,0,1,1,0,5,0,0,0,0`,18:`Style: Small,${this.font},36,&H${this.alpha}FFFFFF,&H${this.alpha}FFFFFF,&H${this.alpha}000000,&H${this.alpha}000000,0,0,0,0,100,100,0,0,1,1,0,5,0,0,0,0`}}convertToAssDocument(t){const e=new n(t);const s=[];for(const t of e.danmakus.sort((t,e)=>t.time-e.time)){if(this.blockTypes.indexOf(t.type)!==-1){continue}const[e,i]=this.convertTime(t.time,this.duration(t));s.push(new o({content:this.convertText(t.content),time:e,endTime:i,type:t.type,fontSize:t.fontSize,color:t.color,typeTag:this.convertType(t),colorTag:this.convertColor(t.color)}))}return new r({danmakus:s,title:this.title,blockTypes:this.blockTypes,fontStyles:this.fontStyles,resolution:this.resolution})}convertText(t){const e={"{":"","}":"","&amp;":"&","&lt;":"<","&gt;":">","&quot;":'"',"&apos;":"'"};for(const[s,i]of Object.entries(e)){t=t.replace(new RegExp(s,"g"),i)}return t}convertType(t){return this.danmakuStack.push(t).tags}convertColor(t){const e=16777215;if(t===e){return""}const s=t.toString(16);const i=s.substring(0,2);const n=s.substring(2,4);const o=s.substring(4,6);return`\\c&H${o}${n}${i}&`}convertTime(t,e){function s(t){const[e,s="00"]=String(t).split(".");return`${e.padStart(2,"0")}.${s.substr(0,2).padEnd(2,"0")}`}function i(t){let e=0;let i=0;while(t>=60){t-=60;i++}while(i>=60){i-=60;e++}return`${e}:${String(i).padStart(2,"0")}:${s(t)}`}return[i(t),i(t+e)]}}return{export:{AssDanmaku:o,AssDanmakuDocument:r,Danmaku:s,DanmakuConverter:h,DanmakuStack:a,XmlDanmaku:i,XmlDanmakuDocument:n}}}})();