@bililive-tools/manager 1.9.0 → 1.11.0

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.
package/lib/utils.js CHANGED
@@ -327,6 +327,32 @@ function isBetweenTime(currentTime, timeRange) {
327
327
  return start <= current && current <= end;
328
328
  }
329
329
  export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
330
+ /**
331
+ * 判断是否应该使用严格画质模式
332
+ * @param qualityRetryLeft 剩余的画质重试次数
333
+ * @param qualityRetry 初始画质重试次数配置
334
+ * @param isManualStart 是否手动启动
335
+ * @returns 是否使用严格画质模式
336
+ */
337
+ export function shouldUseStrictQuality(qualityRetryLeft, qualityRetry, isManualStart) {
338
+ // 手动启动时不使用严格模式
339
+ if (isManualStart) {
340
+ return false;
341
+ }
342
+ // 如果配置为0,不使用严格模式
343
+ if (qualityRetry === 0) {
344
+ return false;
345
+ }
346
+ // 如果还有重试次数,使用严格模式
347
+ if (qualityRetryLeft > 0) {
348
+ return true;
349
+ }
350
+ // 如果配置为负数(无限重试),使用严格模式
351
+ if (qualityRetry < 0) {
352
+ return true;
353
+ }
354
+ return false;
355
+ }
330
356
  /**
331
357
  * 检查标题是否包含黑名单关键词
332
358
  */
@@ -343,6 +369,106 @@ function hasBlockedTitleKeywords(title, titleKeywords) {
343
369
  function shouldCheckTitleKeywords(isManualStart, titleKeywords) {
344
370
  return (!isManualStart && !!titleKeywords && typeof titleKeywords === "string" && !!titleKeywords.trim());
345
371
  }
372
+ /**
373
+ * 逆向格式化"xxxB", "xxxKB", "xxxMB", "xxxGB"为字节数,如果值为空返回0,如果为数字则直接返回数字,如果带单位则转换为字节数
374
+ * @param sizeStr 大小字符串
375
+ * @returns 字节数
376
+ */
377
+ export function parseSizeToBytes(sizeStr) {
378
+ if (!sizeStr) {
379
+ return 0;
380
+ }
381
+ // 字符类型的数字
382
+ if (!isNaN(Number(sizeStr))) {
383
+ return Number(sizeStr);
384
+ }
385
+ const sizePattern = /^(\d+(?:\.\d+)?)(B|KB|MB|GB)$/i;
386
+ const match = sizeStr.toUpperCase().trim().match(sizePattern);
387
+ if (match) {
388
+ const size = parseFloat(match[1]);
389
+ const unit = match[2];
390
+ switch (unit) {
391
+ case "B":
392
+ return String(size);
393
+ case "KB":
394
+ return String(size * 1024);
395
+ case "MB":
396
+ return String(size * 1024 * 1024);
397
+ case "GB":
398
+ return String(size * 1024 * 1024 * 1024);
399
+ default:
400
+ return 0;
401
+ }
402
+ }
403
+ else {
404
+ return 0;
405
+ }
406
+ }
407
+ export const byte2MB = (bytes) => {
408
+ return bytes / (1024 * 1024);
409
+ };
410
+ /*
411
+ * 检查录制中的标题关键词
412
+ * @param recorder 录制器实例
413
+ * @param isManualStart 是否手动启动
414
+ * @param getInfo 获取直播间信息的函数
415
+ * @returns 如果标题包含关键词返回 true(需要停止),否则返回 false
416
+ */
417
+ export async function checkTitleKeywordsWhileRecording(recorder, isManualStart, getInfo) {
418
+ // 只有当设置了标题关键词时,并且不是手动启动的录制,才获取最新的直播间信息
419
+ if (!shouldCheckTitleKeywords(isManualStart, recorder.titleKeywords)) {
420
+ return false;
421
+ }
422
+ const now = Date.now();
423
+ // 每5分钟检查一次标题变化
424
+ const titleCheckInterval = 5 * 60 * 1000; // 5分钟
425
+ // 获取上次检查时间
426
+ const lastCheckTime = await recorder.cache.get("lastTitleCheckTime");
427
+ // 如果距离上次检查时间不足指定间隔,则跳过检查
428
+ if (lastCheckTime && now - lastCheckTime < titleCheckInterval) {
429
+ return false;
430
+ }
431
+ // 更新检查时间
432
+ await recorder.cache.set("lastTitleCheckTime", now);
433
+ // 获取直播间信息
434
+ const liveInfo = await getInfo(recorder.channelId);
435
+ const { title } = liveInfo;
436
+ // 检查标题是否包含关键词
437
+ if (hasBlockedTitleKeywords(title, recorder.titleKeywords)) {
438
+ recorder.state = "title-blocked";
439
+ recorder.emit("DebugLog", {
440
+ type: "common",
441
+ text: `检测到标题包含关键词,停止录制:直播间标题 "${title}" 包含关键词 "${recorder.titleKeywords}"`,
442
+ });
443
+ // 停止录制
444
+ await recorder?.recordHandle?.stop("直播间标题包含关键词");
445
+ return true;
446
+ }
447
+ return false;
448
+ }
449
+ /**
450
+ * 检查开始录制前的标题关键词
451
+ * @param title 直播间标题
452
+ * @param recorder 录制器实例
453
+ * @param isManualStart 是否手动启动
454
+ * @returns 如果标题包含关键词返回 true(不应录制),否则返回 false
455
+ */
456
+ export function checkTitleKeywordsBeforeRecord(title, recorder, isManualStart) {
457
+ // 检查标题是否包含关键词,如果包含则不自动录制
458
+ // 手动开始录制时不检查标题关键词
459
+ if (!shouldCheckTitleKeywords(isManualStart, recorder.titleKeywords)) {
460
+ return false;
461
+ }
462
+ if (hasBlockedTitleKeywords(title, recorder.titleKeywords)) {
463
+ recorder.state = "title-blocked";
464
+ recorder.emit("DebugLog", {
465
+ type: "common",
466
+ text: `跳过录制:直播间标题 "${title}" 包含关键词 "${recorder.titleKeywords}"`,
467
+ });
468
+ return true;
469
+ }
470
+ return false;
471
+ }
346
472
  export default {
347
473
  replaceExtName,
348
474
  singleton,
@@ -364,5 +490,8 @@ export default {
364
490
  isBetweenTimeRange,
365
491
  hasBlockedTitleKeywords,
366
492
  shouldCheckTitleKeywords,
493
+ shouldUseStrictQuality,
367
494
  sleep,
495
+ checkTitleKeywordsWhileRecording,
496
+ checkTitleKeywordsBeforeRecord,
368
497
  };
@@ -5,7 +5,7 @@ export interface XmlStreamData {
5
5
  recordStartTimestamp: number;
6
6
  recordStopTimestamp?: number;
7
7
  liveStartTimestamp?: number;
8
- ffmpegArgs?: string[];
8
+ downloaderArgs?: string[];
9
9
  platform?: string;
10
10
  user_name?: string;
11
11
  room_id?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bililive-tools/manager",
3
- "version": "1.9.0",
3
+ "version": "1.11.0",
4
4
  "description": "Batch scheduling recorders",
5
5
  "main": "./lib/index.js",
6
6
  "type": "module",
@@ -1,48 +0,0 @@
1
- import { FFMPEGRecorder } from "./FFMPEGRecorder.js";
2
- import { MesioRecorder } from "./mesioRecorder.js";
3
- import { BililiveRecorder } from "./BililiveRecorder.js";
4
- export { FFMPEGRecorder } from "./FFMPEGRecorder.js";
5
- export { MesioRecorder } from "./mesioRecorder.js";
6
- export { BililiveRecorder } from "./BililiveRecorder.js";
7
- import type { IRecorder, FFMPEGRecorderOptions, MesioRecorderOptions, BililiveRecorderOptions } from "./IRecorder.js";
8
- /**
9
- * 录制器类型
10
- */
11
- export type RecorderType = "ffmpeg" | "mesio" | "bililive";
12
- export type FormatName = "flv" | "ts" | "fmp4";
13
- /**
14
- * 根据录制器类型获取对应的配置选项类型
15
- */
16
- export type RecorderOptions<T extends RecorderType> = T extends "ffmpeg" ? FFMPEGRecorderOptions : T extends "mesio" ? MesioRecorderOptions : BililiveRecorderOptions;
17
- /**
18
- * 根据录制器类型获取对应的录制器实例类型
19
- */
20
- export type RecorderInstance<T extends RecorderType> = T extends "ffmpeg" ? FFMPEGRecorder : T extends "mesio" ? MesioRecorder : BililiveRecorder;
21
- type RecorderOpts = FFMPEGRecorderOptions | MesioRecorderOptions | BililiveRecorderOptions;
22
- /**
23
- * 创建录制器的工厂函数
24
- */
25
- export declare function createRecorder<T extends RecorderType>(type: T, opts: RecorderOptions<T> & {
26
- onlyAudio?: boolean;
27
- }, onEnd: (...args: unknown[]) => void, onUpdateLiveInfo: () => Promise<{
28
- title?: string;
29
- cover?: string;
30
- }>): IRecorder;
31
- /**
32
- * 选择录制器
33
- */
34
- export declare function selectRecorder(preferredRecorder: "auto" | RecorderType | undefined): RecorderType;
35
- /**
36
- * 判断原始录制流格式,flv, ts, m4s
37
- */
38
- export declare function getSourceFormatName(streamUrl: string, formatName: FormatName | undefined): FormatName;
39
- type PickPartial<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> & Partial<Pick<T, K>>;
40
- /**
41
- * 创建录制器的工厂函数
42
- */
43
- export declare function createBaseRecorder(type: "auto" | RecorderType | undefined, opts: PickPartial<RecorderOpts, "formatName"> & {
44
- onlyAudio?: boolean;
45
- }, onEnd: (...args: unknown[]) => void, onUpdateLiveInfo: () => Promise<{
46
- title?: string;
47
- cover?: string;
48
- }>): IRecorder;