Bilibili-Evolved/min/danmaku-converter.min.js
2020-06-13 11:59:38 +08:00

1 line
7.9 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 i{constructor({content:t,time:e,type:i,fontSize:r,color:a}){this.content=t;this.time=e;this.startTime=parseFloat(e);this.type=parseInt(i);this.fontSize=parseFloat(r);this.color=parseInt(a)}}class r extends i{constructor({content:t,time:e,type:i,fontSize:r,color:a,timeStamp:n,pool:s,userHash:o,rowId:l}){super({content:t,time:e,type:i,fontSize:r,color:a});this.timeStamp=parseInt(n);this.pool=parseInt(s);this.userHash=o;this.rowId=parseInt(l);this.pDataArray=[e,i,r,a,n,s,o,l]}text(){const t=this.pDataArray.join(",");return`<d p="${t}">${this.content}</d>`}static parse(t){const e=t.getAttribute("p");const[i,a,n,s,o,l,c,h]=e.split(",");const u=t.innerHTML;return new r({content:u,time:i,type:a,fontSize:n,color:s,timeStamp:o,pool:l,userHash:c,rowId:h})}}class a{constructor(t){this.xml=t;const e=(new DOMParser).parseFromString(t,"application/xml").documentElement;this.danmakus=[...e.querySelectorAll("d[p]")].map(t=>r.parse(t))}}var n;(function(t){t[t["Normal"]=1]="Normal";t[t["Normal2"]=2]="Normal2";t[t["Normal3"]=3]="Normal3";t[t["Bottom"]=4]="Bottom";t[t["Top"]=5]="Top";t[t["Reversed"]=6]="Reversed";t[t["Special"]=7]="Special";t[t["Special2"]=8]="Special2"})(n||(n={}));class s extends i{constructor({content:t,time:e,type:i,fontSize:r,color:a,typeTag:n,colorTag:s,endTime:o}){super({content:t,time:e,type:i,fontSize:r,color:a});this.typeTag=n;this.colorTag=s;this.endTime=o}text(t){let e=t[this.fontSize];if(!e){e=t[25]}const i=e.match(/Style:(.*?),/)[1].trim();return`Dialogue: 0,${this.time},${this.endTime},${i},,0,0,0,,{${this.typeTag}${this.colorTag}}${this.content}`}}class o{constructor(t,e,i,r,a){this.danmakus=t;this.title=e;this.fontStyles=i;this.blockTypes=r;this.resolution=a}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)).filter(t=>t!=="").join("\n")}}class l{constructor(t,e,i,r){this.horizontalStack=[];this.horizontalTrack=[];this.verticalStack=[];this.verticalTrack=[];this.resolution=e;this.duration=i;this.canvas=document.createElement("canvas");this.context=this.canvas.getContext("2d");this.fontSizes={30:`64px ${t}`,25:`52px ${t}`,18:`36px ${t}`,45:`90px ${t}`};this.bottomMarginPercent=r;this.generateTracks()}generateTracks(){const t=52;this.danmakuHeight=t;this.trackHeight=l.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 i=e.width/2;return[i,this.danmakuHeight/2]}getTags(t,{targetTrack:e,initTrackNumber:i,nextTrackNumber:r,willOverlay:a,getTrackItem:n,getTag:s}){const[o,c]=this.getTextSize(t);const h=o*2;const u=this.duration(t)*h/(this.resolution.x+h)+l.nextDanmakuDelay;let m=i;let p=null;do{p=e.find(t=>a(t,m,h));m+=r}while(p&&m<=this.trackCount&&m>=0);if(m>this.trackCount||m<0){return`\\pos(0,-999)`}m-=r;e.push(n(m,h,u));return s({trackNumber:m,x:o,y:c})}getHorizontalTags(t){return this.getTags(t,{targetTrack:this.horizontalTrack,initTrackNumber:0,nextTrackNumber:1,willOverlay:(e,i,r)=>{if(e.trackNumber!==i){return false}if(e.width<r){return this.duration(t)*this.resolution.x/(this.resolution.x+r)<=e.end-t.startTime}else{return e.visible>t.startTime}},getTrackItem:(e,i,r)=>{return{width:i,start:t.startTime,visible:t.startTime+r,end:t.startTime+this.duration(t),trackNumber:e}},getTag:({trackNumber:e,x:i,y:r})=>{return`\\move(${this.resolution.x+i},${e*this.trackHeight+l.margin+r},${-i},${e*this.trackHeight+l.margin+r},0,${this.duration(t)*1e3})`}})}getVerticalTags(t){const e=l.danmakuType[t.type]==="top";return this.getTags(t,{targetTrack:this.verticalTrack,initTrackNumber:e?0:this.trackCount-1,nextTrackNumber:e?1:-1,willOverlay:(e,i)=>{if(e.trackNumber!==i){return false}return e.end>t.startTime},getTrackItem:e=>{return{start:t.startTime,end:t.startTime+this.duration(t),trackNumber:e}},getTag:({trackNumber:t,y:i})=>{if(e){return`\\pos(${this.resolution.x/2},${t*this.trackHeight+l.margin+i})`}else{return`\\pos(${this.resolution.x/2},${this.resolution.y-l.margin-i-(this.trackCount-1-t)*this.trackHeight})`}}})}push(t){let e="";let i=[];switch(l.danmakuType[t.type]){case"normal":case"reversed":{e=this.getHorizontalTags(t);i=this.horizontalStack;break}case"top":case"bottom":{e=this.getVerticalTags(t);i=this.verticalStack;break}case"special":default:{return{tags:`\\pos(0,-999)`}}}const r={tags:e};i.push(r);return r}}l.danmakuType={[n.Normal]:"normal",[n.Normal2]:"normal",[n.Normal3]:"normal",[n.Bottom]:"bottom",[n.Top]:"top",[n.Reversed]:"reversed",[n.Special]:"special",[n.Special2]:"special"};l.margin=4;l.nextDanmakuDelay=.05;class c{constructor({title:t,font:e,alpha:i,duration:r,blockTypes:a,blockFilter:n,resolution:s,bottomMarginPercent:o,bold:c}){this.title=t;this.font=e;this.alpha=Math.round(i*255).toString(16).toUpperCase().padStart(2,"0");this.duration=r;this.blockTypes=a;this.blockFilter=n||(()=>true);this.resolution=s;this.bold=c;this.danmakuStack=new l(e,s,r,o)}get fontStyles(){return{36:`Style: Larger,${this.font},72,&H${this.alpha}FFFFFF,&H${this.alpha}FFFFFF,&H${this.alpha}000000,&H${this.alpha}000000,${this.bold?"1":"0"},0,0,0,100,100,0,0,1,1.2,0,5,0,0,0,0`,30:`Style: Large,${this.font},64,&H${this.alpha}FFFFFF,&H${this.alpha}FFFFFF,&H${this.alpha}000000,&H${this.alpha}000000,${this.bold?"1":"0"},0,0,0,100,100,0,0,1,1.2,0,5,0,0,0,0`,25:`Style: Medium,${this.font},52,&H${this.alpha}FFFFFF,&H${this.alpha}FFFFFF,&H${this.alpha}000000,&H${this.alpha}000000,${this.bold?"1":"0"},0,0,0,100,100,0,0,1,1.2,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,${this.bold?"1":"0"},0,0,0,100,100,0,0,1,1.2,0,5,0,0,0,0`,45:`Style: ExtraLarge,${this.font},90,&H${this.alpha}FFFFFF,&H${this.alpha}FFFFFF,&H${this.alpha}000000,&H${this.alpha}000000,${this.bold?"1":"0"},0,0,0,100,100,0,0,1,1.2,0,5,0,0,0,0`}}convertToAssDocument(t){const e=new a(t);const i=[];for(const t of e.danmakus.sort((t,e)=>t.startTime-e.startTime)){if(this.blockTypes.indexOf(t.type)!==-1||this.blockTypes.indexOf("color")!==-1&&t.color!==c.white){continue}if(!this.blockFilter(t)){continue}const[e,r]=this.convertTime(t.startTime,this.duration(t));i.push(new s({content:this.convertText(t.content),time:e,endTime:r,type:t.type.valueOf().toString(),fontSize:t.fontSize.toString(),color:t.color.toString(),typeTag:this.convertType(t),colorTag:this.convertColor(t.color)}))}return new o(i,this.title,this.fontStyles,this.blockTypes,this.resolution)}convertText(t){const e={"{":"","}":"","&amp;":"&","&lt;":"<","&gt;":">","&quot;":'"',"&apos;":"'"};for(const[i,r]of Object.entries(e)){t=t.replace(new RegExp(i,"g"),r)}return t}convertType(t){return this.danmakuStack.push(t).tags}convertColor(t){if(t===c.white){return""}const e=t.toString(16);const i=e.substring(0,2);const r=e.substring(2,4);const a=e.substring(4,6);return`\\c&H${a}${r}${i}&`}convertTime(t,e){function i(t){const[e,i="00"]=String(t).split(".");return`${e.padStart(2,"0")}.${i.substr(0,2).padEnd(2,"0")}`}function r(t){let e=0;let r=0;while(t>=60){t-=60;r++}while(r>=60){r-=60;e++}return`${e}:${String(r).padStart(2,"0")}:${i(t)}`}return[r(t),r(t+e)]}}c.white=16777215;return{export:{AssDanmaku:s,AssDanmakuDocument:o,Danmaku:i,DanmakuConverter:c,DanmakuStack:l,DanmakuType:n,XmlDanmaku:r,XmlDanmakuDocument:a}}}})();