@dan-uni/dan-any 0.1.0 → 0.2.2

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.
Files changed (54) hide show
  1. package/README.md +10 -1
  2. package/dist/index.js +5963 -1
  3. package/dist/index.umd.min.js +19958 -1
  4. package/dist/index.umd.min.js.LICENSE.txt +17 -0
  5. package/dist/src/ass-gen/__tests__/generate.test.d.ts +1 -0
  6. package/dist/src/ass-gen/ass/create.d.ts +4 -0
  7. package/dist/src/ass-gen/ass/dialogue.d.ts +17 -0
  8. package/dist/src/ass-gen/ass/event.d.ts +3 -0
  9. package/dist/src/ass-gen/ass/info.d.ts +8 -0
  10. package/dist/src/ass-gen/ass/raw.d.ts +10 -0
  11. package/dist/src/ass-gen/ass/style.d.ts +3 -0
  12. package/dist/src/ass-gen/config.d.ts +3 -0
  13. package/dist/src/ass-gen/index.d.ts +26 -0
  14. package/dist/src/ass-gen/types.d.ts +47 -0
  15. package/dist/src/ass-gen/util/color.d.ts +18 -0
  16. package/dist/src/ass-gen/util/danconvert.d.ts +4 -0
  17. package/dist/src/ass-gen/util/index.d.ts +4 -0
  18. package/dist/src/ass-gen/util/lang.d.ts +3 -0
  19. package/dist/src/ass-gen/util/layout.d.ts +4 -0
  20. package/dist/{index.d.ts → src/index.d.ts} +15 -1
  21. package/dist/src/index.test.d.ts +1 -0
  22. package/dist/{utils → src/utils}/dm-gen.d.ts +13 -5
  23. package/dist/src/utils/dm-gen.test.d.ts +1 -0
  24. package/package.json +6 -5
  25. package/rslib.config.ts +81 -0
  26. package/src/ass-gen/__tests__/898651903.xml +1619 -0
  27. package/src/ass-gen/__tests__/898651903.xml.ass +1516 -0
  28. package/src/ass-gen/__tests__/canvas.test.ts +11 -0
  29. package/src/ass-gen/__tests__/generate.test.ts +20 -0
  30. package/src/ass-gen/ass/create.ts +26 -0
  31. package/src/ass-gen/ass/dialogue.ts +91 -0
  32. package/src/ass-gen/ass/event.ts +58 -0
  33. package/src/ass-gen/ass/info.ts +28 -0
  34. package/src/ass-gen/ass/raw.ts +69 -0
  35. package/src/ass-gen/ass/style.ts +67 -0
  36. package/src/ass-gen/config.ts +45 -0
  37. package/src/ass-gen/index.ts +52 -0
  38. package/src/ass-gen/types.ts +52 -0
  39. package/src/ass-gen/util/color.ts +55 -0
  40. package/src/ass-gen/util/danconvert.ts +36 -0
  41. package/src/ass-gen/util/index.ts +10 -0
  42. package/src/ass-gen/util/lang.ts +35 -0
  43. package/src/ass-gen/util/layout.ts +238 -0
  44. package/src/index.test.ts +9 -0
  45. package/src/index.ts +114 -0
  46. package/src/utils/dm-gen.test.ts +66 -0
  47. package/src/utils/dm-gen.ts +91 -11
  48. package/tsconfig.json +7 -7
  49. package/dist/index.min.js +0 -1
  50. package/rollup.config.mjs +0 -51
  51. /package/dist/{index.test.d.ts → src/ass-gen/__tests__/canvas.test.d.ts} +0 -0
  52. /package/dist/{proto → src/proto}/gen/bili/dm_pb.d.ts +0 -0
  53. /package/dist/{proto → src/proto}/gen/danuni_pb.d.ts +0 -0
  54. /package/dist/{utils → src/utils}/id-gen.d.ts +0 -0
@@ -0,0 +1,17 @@
1
+ /*!
2
+ * The buffer module from node.js, for the browser.
3
+ *
4
+ * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
5
+ * @license MIT
6
+ */
7
+
8
+ /*!
9
+ * The buffer module from node.js, for the browser.
10
+ *
11
+ * @author Feross Aboukhadijeh <https://feross.org>
12
+ * @license MIT
13
+ */
14
+
15
+ /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
16
+
17
+ /*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { UniPool } from '../..';
2
+ import type { Context, SubtitleStyle } from '../types';
3
+ declare const _default: (list: UniPool, rawList: UniPool, config: SubtitleStyle, context?: Context) => string;
4
+ export default _default;
@@ -0,0 +1,17 @@
1
+ import type { RGB } from '../types';
2
+ import { DanmakuType } from '../types';
3
+ declare const _default: (danmaku: {
4
+ type: (typeof DanmakuType)[keyof typeof DanmakuType];
5
+ color: RGB;
6
+ fontSizeType: number;
7
+ content: string;
8
+ time: number;
9
+ start: number;
10
+ end: number;
11
+ top: number;
12
+ left: number;
13
+ }, config: {
14
+ scrollTime: number;
15
+ fixTime: number;
16
+ }) => string;
17
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import type { Danmaku, SubtitleStyle } from '../types';
2
+ declare const _default: (list: Danmaku[], config: SubtitleStyle) => string;
3
+ export default _default;
@@ -0,0 +1,8 @@
1
+ import type { Context } from '../types';
2
+ type ExtraInfo = Context;
3
+ type Resolution = {
4
+ playResX: number;
5
+ playResY: number;
6
+ };
7
+ declare const _default: ({ playResX, playResY }: Resolution, { filename, title }: ExtraInfo) => string;
8
+ export default _default;
@@ -0,0 +1,10 @@
1
+ import type { Context, Danmaku, SubtitleStyle } from '../types';
2
+ type compressType = 'brotli' | 'gzip';
3
+ type baseType = 'base64' | 'base18384';
4
+ declare const _default: (list: Danmaku[], config: SubtitleStyle, context: Context, compressType?: compressType, baseType?: baseType) => string;
5
+ export default _default;
6
+ export declare function deRaw(ass: string): {
7
+ list: Danmaku[];
8
+ config: SubtitleStyle;
9
+ context: Context;
10
+ } | undefined;
@@ -0,0 +1,3 @@
1
+ import type { SubtitleStyle } from '../types';
2
+ declare const _default: ({ fontName, fontSize, color: configColor, outlineColor, backColor, bold, outline, shadow, opacity, }: SubtitleStyle) => string;
3
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import type { SubtitleStyle } from './types';
2
+ declare const _default: (overrides?: {}) => SubtitleStyle;
3
+ export default _default;
@@ -0,0 +1,26 @@
1
+ import type { SubtitleStyle } from './types';
2
+ import { UniPool } from '..';
3
+ export type Options = {
4
+ filename?: string;
5
+ title?: string;
6
+ substyle?: Partial<SubtitleStyle>;
7
+ };
8
+ /**
9
+ * 使用bilibili弹幕(XMl)生成ASS字幕文件
10
+ * @param {string} danmaku XML弹幕文件内容
11
+ * @param {Options} options 杂项
12
+ * @returns {string} 返回ASS字幕文件内容
13
+ * @description 杂项相关
14
+ `filename`: 还原文件为XML时使用的默认文件名
15
+ `title`: ASS [Script Info] Title 项的值,显示于播放器字幕选择
16
+ `substyle`: ASS字幕样式
17
+ * @example ```ts
18
+ import fs from 'fs'
19
+ const filename = 'example.xml'
20
+ const xmlText = fs.readFileSync(filename, 'utf-8')
21
+ const assText = generateASS(xmlText, { filename, title: 'Quick Example' })
22
+ fs.writeFileSync(`${filename}.ass`, assText, 'utf-8')
23
+ ```
24
+ */
25
+ export declare function generateASS(danmaku: UniPool, options: Options): string;
26
+ export declare function parseAssRawField(ass: string): UniPool;
@@ -0,0 +1,47 @@
1
+ import type { UniDM } from '../utils/dm-gen';
2
+ export interface Context {
3
+ filename: string;
4
+ title: string;
5
+ }
6
+ export interface SubtitleStyle {
7
+ fontSize: number[];
8
+ fontName: string;
9
+ color: string;
10
+ outlineColor?: string;
11
+ backColor?: string;
12
+ outline: number;
13
+ shadow: number;
14
+ bold: boolean;
15
+ padding: number[];
16
+ playResX: number;
17
+ playResY: number;
18
+ scrollTime: number;
19
+ fixTime: number;
20
+ opacity: number;
21
+ bottomSpace: number;
22
+ includeRaw: boolean;
23
+ mergeIn: number;
24
+ }
25
+ export type RGB = {
26
+ r: number;
27
+ g: number;
28
+ b: number;
29
+ };
30
+ export declare const DanmakuType: {
31
+ SCROLL: number;
32
+ BOTTOM: number;
33
+ TOP: number;
34
+ };
35
+ export declare const FontSize: {
36
+ SMALL: number;
37
+ NORMAL: number;
38
+ LARGE: number;
39
+ };
40
+ export type Danmaku = {
41
+ time: number;
42
+ type: number;
43
+ fontSizeType: number;
44
+ color: RGB;
45
+ content: string;
46
+ extra: UniDM;
47
+ };
@@ -0,0 +1,18 @@
1
+ import type { RGB } from '../types';
2
+ export declare const hexColorToRGB: (hex: string) => {
3
+ r: number;
4
+ g: number;
5
+ b: number;
6
+ };
7
+ export declare const decimalColorToRGB: (decimal: number) => {
8
+ r: number;
9
+ g: number;
10
+ b: number;
11
+ };
12
+ export declare const formatColor: ({ r, g, b }: RGB, opacity?: number) => string;
13
+ export declare const getDecoratingColor: (color: RGB) => {
14
+ r: number;
15
+ g: number;
16
+ b: number;
17
+ };
18
+ export declare const isWhite: (color: RGB) => boolean;
@@ -0,0 +1,4 @@
1
+ import type { Danmaku } from '../types';
2
+ import { UniPool } from '../..';
3
+ export declare function UniPool2DanmakuLists(UP: UniPool): Danmaku[];
4
+ export declare function DanmakuList2UniPool(d: Danmaku[]): UniPool;
@@ -0,0 +1,4 @@
1
+ export { decimalColorToRGB, formatColor, getDecoratingColor, hexColorToRGB, isWhite, } from './color';
2
+ export { layoutDanmaku } from './layout';
3
+ export { arrayOfLength, assign, uniqueArray } from './lang';
4
+ export { DanmakuList2UniPool, UniPool2DanmakuLists } from './danconvert';
@@ -0,0 +1,3 @@
1
+ export declare const assign: <T extends object>(source: T, ...targets: Partial<T>[]) => T;
2
+ export declare const arrayOfLength: <T>(length: number, defaultValue: T) => T[];
3
+ export declare const uniqueArray: <T>(array: T[]) => T[];
@@ -0,0 +1,4 @@
1
+ import type { UniPool } from '../..';
2
+ import type { SubtitleStyle } from '../types';
3
+ export declare const measureTextWidth: (fontName: string, fontSize: number, bold: boolean, text: string) => number;
4
+ export declare const layoutDanmaku: (inputList: UniPool, config: SubtitleStyle) => UniPool;
@@ -1,3 +1,4 @@
1
+ import type { Options as AssGenOptions } from './ass-gen';
1
2
  import type { CommandDm as DM_JSON_BiliCommandGrpc } from './proto/gen/bili/dm_pb';
2
3
  import type { platfrom } from './utils/id-gen';
3
4
  import { UniDM } from './utils/dm-gen';
@@ -41,15 +42,26 @@ export interface DM_JSON_DDPlay {
41
42
  m: string;
42
43
  }[];
43
44
  }
44
- export type DM_format = 'danuni.json' | 'danuni.bin' | 'danuni.pb.zst' | 'bili.xml' | 'bili.bin' | 'bili.cmd.bin' | 'dplayer.json' | 'artplayer.json' | 'ddplay.json';
45
+ export type DM_format = 'danuni.json' | 'danuni.bin' | 'danuni.pb.zst' | 'bili.xml' | 'bili.bin' | 'bili.cmd.bin' | 'dplayer.json' | 'artplayer.json' | 'ddplay.json' | 'common.ass';
45
46
  type shareItems = Partial<Pick<UniDMTools.UniDMObj, 'FCID' | 'senderID' | 'platform' | 'SPMO' | 'pool' | 'mode'>>;
46
47
  export declare class UniPool {
47
48
  dans: UniDM[];
48
49
  readonly shared: shareItems;
49
50
  constructor(dans: UniDM[]);
50
51
  static create(): UniPool;
52
+ /**
53
+ * 合并弹幕/弹幕库
54
+ */
51
55
  assign(dans: UniPool | UniDM | UniDM[]): UniPool;
56
+ /**
57
+ * 按共通属性拆分弹幕库
58
+ */
52
59
  split(key: keyof shareItems): UniPool[];
60
+ /**
61
+ * 合并一定时间段内的重复弹幕,防止同屏出现过多
62
+ * @param lifetime 查重时间区段,单位秒 (默认为0,表示不查重)
63
+ */
64
+ merge(lifetime?: number): UniPool;
53
65
  minify(): (Partial<UniDMTools.UniDMObj> & Pick<UniDMTools.UniDMObj, "FCID">)[];
54
66
  convert2(format: DM_format): string | Uint8Array<ArrayBufferLike> | DM_JSON_Dplayer | DM_JSON_DDPlay | DM_JSON_Artplayer[];
55
67
  static fromPb(bin: Uint8Array | ArrayBuffer): UniPool;
@@ -71,5 +83,7 @@ export declare class UniPool {
71
83
  toArtplayer(): DM_JSON_Artplayer[];
72
84
  static fromDDPlay(json: DM_JSON_DDPlay, episodeId: string, domain?: string): UniPool;
73
85
  toDDplay(): DM_JSON_DDPlay;
86
+ static fromASS(ass: string): UniPool;
87
+ toASS(options?: AssGenOptions): string;
74
88
  }
75
89
  export { UniDM, UniDMTools, UniIDTools, type DM_JSON_BiliCommandGrpc, type platfrom, };
@@ -0,0 +1 @@
1
+ export {};
@@ -91,10 +91,17 @@ interface ExtraBili {
91
91
  command?: DMBiliCommand;
92
92
  }
93
93
  export interface ExtraDanUni {
94
- chapter?: {
95
- duration: number;
96
- type: ExtraDanUniChapterType;
97
- };
94
+ chapter?: ExtraDanUniChapter;
95
+ merge?: ExtraDanUniMerge;
96
+ }
97
+ export interface ExtraDanUniChapter {
98
+ duration: number;
99
+ type: ExtraDanUniChapterType;
100
+ }
101
+ export interface ExtraDanUniMerge {
102
+ duration: number;
103
+ count: number;
104
+ senders: string[];
98
105
  }
99
106
  export declare enum ExtraDanUniChapterType {
100
107
  Chapter = "ch",//其它片段(用于标记章节)
@@ -318,6 +325,7 @@ export declare class UniDM {
318
325
  * @description 同一FCID下唯一
319
326
  */
320
327
  toDMID(): string;
328
+ isSameAs(dan: UniDM, _check2?: boolean): boolean;
321
329
  minify(): Partial<UniDMObj> & Pick<UniDMObj, "FCID">;
322
330
  downgradeAdvcancedDan(): this | undefined;
323
331
  /**
@@ -325,7 +333,7 @@ export declare class UniDM {
325
333
  * @param oriCtime
326
334
  * @param tsUnit 当`oriCtime`为数字类型表时间戳时的单位;
327
335
  * 为 毫秒(ms)/秒(s)
328
- * @returns {Date}
336
+ * @returns {Date} Date格式时间
329
337
  */
330
338
  static transCtime(oriCtime: ctime, tsUnit?: 'ms' | 's'): Date;
331
339
  static transMode(oriMode: number, fmt: 'bili' | 'dplayer' | 'artplayer' | 'ddplay'): Modes;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dan-uni/dan-any",
3
- "version": "0.1.0",
3
+ "version": "0.2.2",
4
4
  "description": "A danmaku transformer lib, supporting danmaku from different platforms.",
5
5
  "keywords": [
6
6
  "bangumi",
@@ -26,12 +26,13 @@
26
26
  "coverage": "vitest run --coverage",
27
27
  "bench": "vitest bench",
28
28
  "lint": "eslint",
29
- "build": "pnpm buf && rollup -c",
29
+ "build": "pnpm buf && rslib build",
30
30
  "build:types": "tsc",
31
- "dev": "pnpm build && node bundle.js",
32
31
  "buf": "buf generate"
33
32
  },
34
33
  "dependencies": {
34
+ "base16384": "^1.0.0",
35
+ "canvas": "^3.1.0",
35
36
  "fast-xml-parser": "^4.5.1",
36
37
  "jssha": "^3.3.1"
37
38
  },
@@ -39,7 +40,7 @@
39
40
  "@bufbuild/buf": "^1.50.0",
40
41
  "@bufbuild/protobuf": "^2.2.3",
41
42
  "@bufbuild/protoc-gen-es": "^2.2.3",
42
- "protobufjs": "^7.4.0",
43
- "tslib": "^2.8.1"
43
+ "@types/pako": "^2.0.3",
44
+ "protobufjs": "^7.4.0"
44
45
  }
45
46
  }
@@ -0,0 +1,81 @@
1
+ import { pluginNodePolyfill } from '@rsbuild/plugin-node-polyfill'
2
+ import { defineConfig } from '@rslib/core'
3
+
4
+ import pkg from './package.json'
5
+
6
+ export default defineConfig({
7
+ lib: [
8
+ {
9
+ format: 'esm',
10
+ output: {
11
+ filename: { js: 'index.js' },
12
+ target: 'node',
13
+ },
14
+ dts: true,
15
+ },
16
+ {
17
+ format: 'umd',
18
+ output: {
19
+ filename: { js: 'index.umd.min.js' },
20
+ target: 'web',
21
+ },
22
+ dts: true,
23
+ umdName: pkg.name,
24
+ plugins: [pluginNodePolyfill()],
25
+ },
26
+ ],
27
+ })
28
+
29
+ // import path from 'node:path'
30
+ // import type { Configuration } from '@rspack/cli'
31
+ // import type { SwcLoaderOptions } from '@rspack/core'
32
+
33
+ // import { RsdoctorRspackPlugin } from '@rsdoctor/rspack-plugin'
34
+
35
+ // const config: Configuration = {
36
+ // entry: './src/index.ts',
37
+ // output: { path: path.resolve(__dirname, 'dist'), clean: true },
38
+ // resolve: {
39
+ // extensions: ['.ts', '.js'],
40
+ // },
41
+ // experiments: {
42
+ // futureDefaults: true,
43
+ // },
44
+ // plugins: [
45
+ // // 仅在 RSDOCTOR 为 true 时注册插件,因为插件会增加构建耗时
46
+ // process.env.RSDOCTOR &&
47
+ // new RsdoctorRspackPlugin({
48
+ // // 插件选项
49
+ // }),
50
+ // ].filter(Boolean),
51
+ // module: {
52
+ // rules: [
53
+ // {
54
+ // test: /\.(ts|js)$/,
55
+ // exclude: [/node_modules/],
56
+ // loader: 'builtin:swc-loader',
57
+ // options: {
58
+ // jsc: {
59
+ // parser: {
60
+ // syntax: 'typescript',
61
+ // },
62
+ // },
63
+ // env: {
64
+ // mode: 'usage',
65
+ // coreJs: '3',
66
+ // targets: [
67
+ // 'chrome >= 87',
68
+ // 'edge >= 88',
69
+ // 'firefox >= 78',
70
+ // 'safari >= 14',
71
+ // ],
72
+ // },
73
+ // isModule: 'unknown',
74
+ // } satisfies SwcLoaderOptions,
75
+ // type: 'javascript/auto',
76
+ // },
77
+ // ],
78
+ // },
79
+ // }
80
+
81
+ // export default config