@bililive-tools/manager 1.4.0 → 1.5.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/manager.d.ts CHANGED
@@ -13,6 +13,7 @@ export interface RecorderProvider<E extends AnyObject> {
13
13
  title: string;
14
14
  owner: string;
15
15
  uid?: number;
16
+ avatar?: string;
16
17
  } | null>;
17
18
  createRecorder: (this: RecorderProvider<E>, opts: Omit<RecorderCreateOpts<E>, "providerId">) => Recorder<E>;
18
19
  fromJSON: <T extends SerializedRecorder<E>>(this: RecorderProvider<E>, json: T) => Recorder<E>;
package/lib/manager.js CHANGED
@@ -4,7 +4,7 @@ import ejs from "ejs";
4
4
  import { omit, range } from "lodash-es";
5
5
  import { parseArgsStringToArgv } from "string-argv";
6
6
  import { getBiliStatusInfoByRoomIds } from "./api.js";
7
- import { formatDate, removeSystemReservedChars, formatTemplate, replaceExtName, downloadImage, } from "./utils.js";
7
+ import { formatDate, removeSystemReservedChars, formatTemplate, replaceExtName, downloadImage, isBetweenTimeRange, } from "./utils.js";
8
8
  import { StreamManager } from "./streamManager.js";
9
9
  const configurableProps = [
10
10
  "savePathRule",
@@ -39,7 +39,9 @@ export function createRecorderManager(opts) {
39
39
  const maxThreadCount = 3;
40
40
  // 这里暂时不打算用 state == recording 来过滤,provider 必须内部自己处理录制过程中的 check,
41
41
  // 这样可以防止一些意外调用 checkLiveStatusAndRecord 时出现重复录制。
42
- let needCheckRecorders = recorders.filter((r) => !r.disableAutoCheck);
42
+ let needCheckRecorders = recorders
43
+ .filter((r) => !r.disableAutoCheck)
44
+ .filter((r) => isBetweenTimeRange(r.handleTime));
43
45
  let threads = [];
44
46
  if (manager.biliBatchQuery) {
45
47
  const biliNeedCheckRecorders = needCheckRecorders
package/lib/recorder.d.ts CHANGED
@@ -47,6 +47,8 @@ export interface RecorderCreateOpts<E extends AnyObject = UnknownObject> {
47
47
  formatriorities?: Array<"flv" | "hls">;
48
48
  /** 只录制音频 */
49
49
  onlyAudio?: boolean;
50
+ /** 监控时间段 */
51
+ handleTime?: [string | null, string | null];
50
52
  /** 控制弹幕是否使用服务端时间戳 */
51
53
  useServerTimestamp?: boolean;
52
54
  extra?: Partial<E>;
package/lib/utils.d.ts CHANGED
@@ -61,6 +61,7 @@ export declare function sortByKeyOrder<T, K extends keyof T>(objects: T[], order
61
61
  * @returns Promise
62
62
  */
63
63
  export declare function retry<T>(fn: () => Promise<T>, retries?: number, delay?: number): Promise<T>;
64
+ export declare const isBetweenTimeRange: (range: undefined | [] | [string | null, string | null]) => boolean;
64
65
  declare const _default: {
65
66
  replaceExtName: typeof replaceExtName;
66
67
  singleton: typeof singleton;
@@ -79,5 +80,6 @@ declare const _default: {
79
80
  uuid: () => `${string}-${string}-${string}-${string}-${string}`;
80
81
  sortByKeyOrder: typeof sortByKeyOrder;
81
82
  retry: typeof retry;
83
+ isBetweenTimeRange: (range: undefined | [] | [string | null, string | null]) => boolean;
82
84
  };
83
85
  export default _default;
package/lib/utils.js CHANGED
@@ -127,7 +127,8 @@ export function isFfmpegStartSegment(line) {
127
127
  return line.includes("Opening ") && line.includes("for writing");
128
128
  }
129
129
  export function isFfmpegStart(line) {
130
- return line.includes("frame=") && line.includes("fps=");
130
+ return ((line.includes("frame=") && line.includes("fps=")) ||
131
+ (line.includes("speed=") && line.includes("time=")));
131
132
  }
132
133
  export const formatTemplate = function template(string, ...args) {
133
134
  const nargs = /\{([0-9a-zA-Z_]+)\}/g;
@@ -159,6 +160,10 @@ export function createInvalidStreamChecker(count = 15) {
159
160
  let prevFrame = 0;
160
161
  let frameUnchangedCount = 0;
161
162
  return (ffmpegLogLine) => {
163
+ // B站某些cdn在直播结束后仍会返回一些数据 https://github.com/renmu123/biliLive-tools/issues/123
164
+ if (ffmpegLogLine.includes("New subtitle stream with index")) {
165
+ return true;
166
+ }
162
167
  const streamInfo = ffmpegLogLine.match(/frame=\s*(\d+) fps=.*? q=.*? size=.*? time=.*? bitrate=.*? speed=.*?/);
163
168
  if (streamInfo != null) {
164
169
  const [, frameText] = streamInfo;
@@ -257,6 +262,49 @@ export async function retry(fn, retries = 3, delay = 1000) {
257
262
  return retry(fn, retries - 1, delay);
258
263
  }
259
264
  }
265
+ export const isBetweenTimeRange = (range) => {
266
+ if (!range)
267
+ return true;
268
+ if (range.length !== 2)
269
+ return true;
270
+ if (range[0] === null || range[1] === null)
271
+ return true;
272
+ try {
273
+ const status = isBetweenTime(new Date(), range);
274
+ return status;
275
+ }
276
+ catch (error) {
277
+ return true;
278
+ }
279
+ };
280
+ /**
281
+ * 当前时间是否在两个时间'HH:mm:ss'之间,如果是["22:00:00","05:00:00"],当前时间是凌晨3点,返回true
282
+ * @param {string} currentTime 当前时间
283
+ * @param {string[]} timeRange 时间范围
284
+ */
285
+ function isBetweenTime(currentTime, timeRange) {
286
+ const [startTime, endTime] = timeRange;
287
+ if (!startTime || !endTime)
288
+ return true;
289
+ const [startHour, startMinute, startSecond] = startTime.split(":").map(Number);
290
+ const [endHour, endMinute, endSecond] = endTime.split(":").map(Number);
291
+ const [currentHour, currentMinute, currentSecond] = [
292
+ currentTime.getHours(),
293
+ currentTime.getMinutes(),
294
+ currentTime.getSeconds(),
295
+ ];
296
+ const start = startHour * 3600 + startMinute * 60 + startSecond;
297
+ let end = endHour * 3600 + endMinute * 60 + endSecond;
298
+ let current = currentHour * 3600 + currentMinute * 60 + currentSecond;
299
+ // 如果结束时间小于开始时间,说明跨越了午夜
300
+ if (end < start) {
301
+ end += 24 * 3600; // 将结束时间加上24小时
302
+ if (current < start) {
303
+ current += 24 * 3600; // 如果当前时间小于开始时间,也加上24小时
304
+ }
305
+ }
306
+ return start <= current && current <= end;
307
+ }
260
308
  export default {
261
309
  replaceExtName,
262
310
  singleton,
@@ -275,4 +323,5 @@ export default {
275
323
  uuid,
276
324
  sortByKeyOrder,
277
325
  retry,
326
+ isBetweenTimeRange,
278
327
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bililive-tools/manager",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Batch scheduling recorders",
5
5
  "main": "./lib/index.js",
6
6
  "type": "module",