@dan-uni/dan-any
Version:
A danmaku transformer lib, supporting danmaku from different platforms.
256 lines (255 loc) • 9.03 kB
TypeScript
import 'reflect-metadata/lite';
import type { Options as AssGenOptions, CanvasCtx } from './ass-gen';
import type { CommandDm as DM_JSON_BiliCommandGrpc } from './proto/gen/bili/dm_pb';
import { UniDM } from './utils/dm-gen';
import * as UniDMTools from './utils/dm-gen';
import * as UniIDTools from './utils/id-gen';
import * as platform from './utils/platform';
interface DanUniConvertTip {
meassage: string;
version: string;
data?: string;
}
export interface DM_XML_Bili {
i: {
chatserver: string;
chatid: bigint;
mission: number;
maxlimit: number;
state: number;
real_name: number;
source: string;
d: {
'#text': string;
'@_p': string;
}[];
};
}
export interface DM_JSON_BiliUp {
/** 接口状态码,0 表示成功 */
code: number;
/** 文本形式的状态码,约定为字符串 "0" */
message: string;
/** TTL(time to live) 标识,本接口常量为 1 */
ttl: number;
data: {
/** 分页元信息 */
page: {
/** 当前页序号,从 1 开始 */
num: number;
/** 每页返回的弹幕条数 */
size: number;
/** 总页数 */
total: number;
};
result: {
/** 弹幕 ID,int64 */
id: bigint;
/** 弹幕 ID 字符串形式 */
id_str: string;
/** 弹幕类型:1 表示视频弹幕(当前接口恒为 1) */
type: number;
aid: bigint;
bvid: string;
oid: bigint;
mid: bigint;
/** 发送者 mid 的 CRC 哈希(正常接口里用的是这个,保护隐私) */
mid_hash: string;
/** 弹幕池 */
pool: number;
/** 属性位字符串,逗号分隔的数字列表,对应 attr 二进制位 */
attrs: string;
/** 弹幕出现时间,单位毫秒(注意,此处与protobuf接口保持一致,但xml中progress是秒) */
progress: number;
mode: number;
/** 弹幕内容, content */
msg: string;
state: number;
fontsize: number;
/** 弹幕颜色,需将16进制转化为普通弹幕的10进制,示例:"ffffff" */
color: string;
/** 发送时间戳,单位秒 */
ctime: number;
/** 发送者昵称 */
uname: string;
/** 发送者头像链接 */
uface: string;
/** 视频主标题 */
title: string;
self_seen: boolean;
/** 弹幕点赞数 */
like_count: number;
user_like: number;
/** 分 P 标题 */
p_title: string;
/** 视频封面链接 */
cover: string;
is_charge: boolean;
is_charge_plus: boolean;
following: boolean;
extra_cps: null;
}[];
};
}
export interface DM_JSON_Dplayer {
code: number;
/**
* progress,mode,color,midHash,content
*/
data: [number, number, number, string, string][];
}
export interface DM_JSON_Artplayer {
danmuku: {
text: string;
time?: number;
mode?: number;
color?: string;
border?: boolean;
style?: {};
}[];
}
export interface DM_JSON_DDPlay {
count: number | string;
comments: {
cid: bigint;
p: string;
m: string;
}[];
}
export type DM_format = 'danuni.json' | 'danuni.pb.bin' | 'bili.xml' | 'bili.pb.bin' | 'bili.cmd.pb.bin' | 'bili.up.json' | 'dplayer.json' | 'artplayer.json' | 'ddplay.json' | 'common.ass';
type shareItems = Partial<Pick<UniDMTools.UniDMObj, 'SOID' | 'senderID' | 'platform' | 'SOID' | 'pool' | 'mode' | 'color'>>;
type statItems = Partial<Pick<UniDMTools.UniDMObj, 'SOID' | 'mode' | 'fontsize' | 'color' | 'senderID' | 'content' | 'weight' | 'pool' | 'platform'>>;
type Stats<T extends keyof statItems> = Map<statItems[T], number>;
type UniPoolPipe = (that: UniPool) => Promise<UniPool>;
type UniPoolPipeSync = (that: UniPool) => UniPool;
export interface Options {
dedupe?: boolean;
/**
* @description
* 当为`false`时,关闭DMID生成; 当为正整数时,表示生成DMID的截取位数; 或可传入一个DMID生成器实例
*/
dmid?: boolean | number | UniIDTools.DMIDGenerator;
}
export declare class UniPool {
dans: UniDM[];
options: Options;
info: {
/**
* 是否从已被转换过的第三方格式弹幕再次转换而来
*/
fromConverted: boolean;
};
constructor(dans: UniDM[], options?: Options, info?: {
/**
* 是否从已被转换过的第三方格式弹幕再次转换而来
*/
fromConverted: boolean;
});
pipe(fn: UniPoolPipe): Promise<UniPool>;
pipeSync(fn: UniPoolPipeSync): UniPool;
/**
* @deprecated 使用 `getShared` 代替
*/
get shared(): shareItems;
getShared<K extends keyof shareItems>(key: K): shareItems[K];
getStat<K extends keyof statItems>(key: K): Stats<K>;
getMost<K extends keyof statItems>(key: K): {
val: Partial<Pick<UniDMTools.UniDMObj, "SOID" | "mode" | "fontsize" | "color" | "senderID" | "content" | "weight" | "pool" | "platform">>[K] | undefined;
count: number;
};
/**
* @deprecated 使用 `getMost` 代替
*/
get most(): {
mode: UniDMTools.Modes;
fontsize: number;
color: number;
senderID: string;
content: string;
weight: number;
pool: UniDMTools.Pools;
platform: string | undefined;
};
static create(options?: Options): UniPool;
/**
* 合并弹幕/弹幕库
*/
assign(dans: UniPool | UniDM | UniDM[]): UniPool;
/**
* 按共通属性拆分弹幕库
*/
split(key: keyof shareItems): UniPool[];
/**
* 基于DMID的基本去重功能,用于解决该class下dans为array而非Set的问题
*/
private dedupe;
/**
* 合并一定时间段内的重复弹幕,防止同屏出现过多
* @param lifetime 查重时间区段,单位秒 (默认为 0,表示不查重)
*/
merge(lifetime?: number): UniPool;
minify(): (Partial<UniDMTools.UniDMObj> & Pick<UniDMTools.UniDMObj, "SOID">)[];
static import(file: unknown, options?: Options,
/**
* 加载指定解析模块,为空则全选
*/
mod?: ('json' | 'str' | 'bin')[]): {
pool: UniPool;
fmt: DM_format;
};
convert2(format: DM_format, continue_on_error?: boolean): string | Uint8Array<ArrayBuffer> | UniDM[] | (DM_JSON_Dplayer & {
danuni?: DanUniConvertTip;
}) | (DM_JSON_Artplayer & {
danuni?: DanUniConvertTip;
}) | (DM_JSON_DDPlay & {
danuni?: DanUniConvertTip;
});
static fromPb(bin: Uint8Array | ArrayBuffer, options?: Options): UniPool;
/**
* 转为 protobuf 二进制
*/
toPb(): Uint8Array<ArrayBuffer>;
static fromBiliXML(xml: string, options?: Options): UniPool;
toBiliXML(options?: {
/**
* 当SOID非来源bili时,若此处指定则使用该值为cid,否则使用SOID
*/
cid?: bigint;
/**
* 当仅含有来自bili的弹幕时,启用将保持发送者标识不含`@`
* @description
* bili的弹幕含midHash(crc),不启用该处使用senderID填充,启用则去除`@bili`部分,提高兼容性
*/
avoidSenderIDWithAt?: boolean;
}): string;
static fromBiliGrpc(bin: Uint8Array | ArrayBuffer, options?: Options): UniPool;
/**
* @param bin 符合`DmWebViewReplySchema`(bili视频meta)的protobuf二进制
*/
static fromBiliCommandGrpc(bin: Uint8Array | ArrayBuffer, options?: Options): UniPool;
static fromBiliUp(json: DM_JSON_BiliUp, options?: Options): UniPool;
static fromDplayer(json: DM_JSON_Dplayer & {
danuni?: DanUniConvertTip;
}, playerID: string, domain?: string, options?: Options): UniPool;
toDplayer(): DM_JSON_Dplayer & {
danuni?: DanUniConvertTip;
};
static fromArtplayer(json: DM_JSON_Artplayer & {
danuni?: DanUniConvertTip;
}, playerID: string, domain?: string, options?: Options): UniPool;
toArtplayer(): DM_JSON_Artplayer & {
danuni?: DanUniConvertTip;
};
static fromDDPlay(json: DM_JSON_DDPlay & {
danuni?: DanUniConvertTip;
}, episodeId: string, options?: Options): UniPool;
toDDplay(): DM_JSON_DDPlay & {
danuni?: DanUniConvertTip;
};
static fromASS(ass: string, options?: Options): UniPool;
/**
* 转换为ASS字幕格式的弹幕,需播放器支持多行ASS渲染
*/
toASS(canvasCtx: CanvasCtx, options?: AssGenOptions): string;
}
export { platform, UniDM, UniDMTools, UniIDTools, type DM_JSON_BiliCommandGrpc, };