Bilibili-Evolved/min/danmaku-converter.min.js

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