UNPKG

@bililive-tools/huya-recorder

Version:
338 lines (337 loc) 12 kB
import path from "node:path"; import mitt from "mitt"; import { defaultFromJSON, defaultToJSON, genRecorderUUID, genRecordUUID, utils, createDownloader, } from "@bililive-tools/manager"; import { getInfo, getStream } from "./stream.js"; import { ensureFolderExist } from "./utils.js"; import HuYaDanMu from "huya-danma-listener"; function createRecorder(opts) { // 内部实现时,应该只有 proxy 包裹的那一层会使用这个 recorder 标识符,不应该有直接通过 // 此标志来操作这个对象的地方,不然会跳过 proxy 的拦截。 const recorder = { id: opts.id ?? genRecorderUUID(), extra: opts.extra ?? {}, // @ts-ignore ...mitt(), ...opts, cache: null, availableStreams: [], availableSources: [], qualityRetry: opts.qualityRetry ?? 0, state: "idle", api: opts.api ?? "auto", formatPriorities: opts.formatPriorities ?? ["flv", "hls"], getChannelURL() { return `https://www.huya.com/${this.channelId}`; }, checkLiveStatusAndRecord: utils.singleton(checkLiveStatusAndRecord), toJSON() { return defaultToJSON(provider, this); }, async getLiveInfo() { const channelId = this.channelId; const info = await getInfo(channelId); return { channelId, ...info, }; }, async getStream() { const res = await getStream({ channelId: this.channelId, quality: this.quality, streamPriorities: this.streamPriorities, sourcePriorities: this.sourcePriorities, }); return res.currentStream; }, }; const recorderWithSupportUpdatedEvent = new Proxy(recorder, { set(obj, prop, value) { Reflect.set(obj, prop, value); if (typeof prop === "string") { obj.emit("Updated", [prop]); } return true; }, }); return recorderWithSupportUpdatedEvent; } const ffmpegOutputOptions = []; const ffmpegInputOptions = ["-rw_timeout", "10000000", "-timeout", "10000000"]; const checkLiveStatusAndRecord = async function ({ getSavePath, banLiveId, isManualStart, }) { // 如果已经在录制中,只在需要检查标题关键词时才获取最新信息 if (this.recordHandle != null) { const shouldStop = await utils.checkTitleKeywordsWhileRecording(this, isManualStart, getInfo); if (shouldStop) { return null; } // 已经在录制中,直接返回 return this.recordHandle; } // 获取直播间信息 try { const liveInfo = await getInfo(this.channelId); this.liveInfo = liveInfo; this.state = "idle"; } catch (error) { this.state = "check-error"; throw error; } const { living, owner, title, liveStartTime, recordStartTime } = this.liveInfo; if (this.liveInfo.liveId === banLiveId) { this.tempStopIntervalCheck = true; } else { this.tempStopIntervalCheck = false; } if (this.tempStopIntervalCheck) return null; if (!living) return null; // 检查标题是否包含关键词 if (utils.checkTitleKeywordsBeforeRecord(title, this, isManualStart)) return null; const qualityRetryLeft = (await this.cache.get("qualityRetryLeft")) ?? this.qualityRetry; const strictQuality = utils.shouldUseStrictQuality(qualityRetryLeft, this.qualityRetry, isManualStart); let res; try { res = await getStream({ channelId: this.channelId, quality: this.quality, streamPriorities: this.streamPriorities, sourcePriorities: this.sourcePriorities, api: this.api, //"wup" strictQuality, formatPriorities: this.formatPriorities, }); } catch (err) { if (qualityRetryLeft > 0) await this.cache.set("qualityRetryLeft", qualityRetryLeft - 1); this.state = "check-error"; throw err; } this.state = "recording"; const { currentStream: stream, sources: availableSources, streams: availableStreams } = res; this.availableStreams = availableStreams.map((s) => s.desc); this.availableSources = availableSources.map((s) => s.name); this.usedStream = stream.name; this.usedSource = stream.source; let isEnded = false; const onEnd = (...args) => { if (isEnded) return; isEnded = true; this.emit("DebugLog", { type: "common", text: `record end, reason: ${JSON.stringify(args, (_, v) => (v instanceof Error ? v.stack : v))}`, }); const reason = args[0] instanceof Error ? args[0].message : String(args[0]); this.recordHandle?.stop(reason); }; // let ua = // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"; // if (res.api === "wup") { // ua = "HYSDK(Windows,30000002)_APP(pc_exe&7030003&official)_SDK(trans&2.29.0.5493)"; // } const downloader = createDownloader(this.recorderType, { url: stream.url, outputOptions: ffmpegOutputOptions, inputOptions: ffmpegInputOptions, segment: this.segment ?? 0, getSavePath: (opts) => getSavePath({ owner, title: opts.title ?? title, startTime: opts.startTime, liveStartTime, recordStartTime, }), disableDanma: this.disableProvideCommentsWhenRecording, videoFormat: this.videoFormat ?? "auto", debugLevel: this.debugLevel ?? "none", headers: { "User-Agent": stream.ua, }, }, onEnd, async () => { const info = await getInfo(this.channelId); return info; }); const savePath = getSavePath({ owner, title, startTime: Date.now(), liveStartTime, recordStartTime, }); try { ensureFolderExist(savePath); } catch (err) { this.state = "idle"; throw err; } const handleVideoCreated = async ({ filename, title, cover, rawFilename }) => { this.emit("videoFileCreated", { filename, cover, rawFilename }); if (title && this?.liveInfo) { this.liveInfo.title = title; } if (cover && this?.liveInfo) { this.liveInfo.cover = cover; } const extraDataController = downloader.getExtraDataController(); extraDataController?.setMeta({ room_id: this.channelId, platform: provider?.id, liveStartTimestamp: this?.liveInfo?.liveStartTime?.getTime(), // recordStopTimestamp: Date.now(), title: title, user_name: owner, }); }; downloader.on("videoFileCreated", handleVideoCreated); downloader.on("videoFileCompleted", ({ filename }) => { this.emit("videoFileCompleted", { filename }); }); downloader.on("DebugLog", (data) => { this.emit("DebugLog", data); }); downloader.on("progress", (progress) => { if (this.recordHandle) { this.recordHandle.progress = progress; } this.emit("progress", progress); }); let client = null; if (!this.disableProvideCommentsWhenRecording) { client = new HuYaDanMu({ roomid: this.channelId, uid: res.currentStream.uid, subChannelId: res.currentStream.subChannelId, channelId: res.currentStream.channelId, }); client.on("message", (msg) => { const extraDataController = downloader.getExtraDataController(); if (!extraDataController) return; switch (msg.type) { case "chat": { const comment = { type: "comment", timestamp: Date.now(), text: msg.content, color: msg.color, sender: { uid: msg.from.rid, name: msg.from.name, }, }; this.emit("Message", comment); extraDataController.addMessage(comment); break; } case "gift": { if (this.saveGiftDanma === false) return; // console.log('gift', msg) const gift = { type: "give_gift", timestamp: Date.now(), name: msg.name, count: msg.count, // 保留一位小数 price: Number((msg.price / msg.count).toFixed(2)), sender: { uid: msg.from.rid, name: msg.from.name, }, }; this.emit("Message", gift); extraDataController.addMessage(gift); break; } } }); client.on("error", (e) => { this.emit("DebugLog", { type: "common", text: String(e) }); }); client.on("retry", (e) => { this.emit("DebugLog", { type: "common", text: `${this?.liveInfo?.owner}:${this.channelId} huya danmu retry: ${e.count}/${e.max}`, }); }); client.start(); } const downloaderArgs = downloader.getArguments(); downloader.run(); const cut = utils.singleton(async () => { if (!this.recordHandle) return; downloader.cut(); }); const stop = utils.singleton(async (reason) => { if (!this.recordHandle) return; this.state = "stopping-record"; try { client?.stop(); await downloader.stop(); } catch (err) { this.emit("DebugLog", { type: "error", text: `stop record error: ${String(err)}`, }); } this.usedStream = undefined; this.usedSource = undefined; this.emit("RecordStop", { recordHandle: this.recordHandle, reason }); this.recordHandle = undefined; this.liveInfo = undefined; this.state = "idle"; this.cache.set("qualityRetryLeft", this.qualityRetry); }); this.recordHandle = { id: genRecordUUID(), stream: stream.name, source: stream.source, recorderType: downloader.type, url: stream.url, downloaderArgs, savePath: savePath, stop, cut, }; this.emit("RecordStart", this.recordHandle); return this.recordHandle; }; export const provider = { id: "HuYa", name: "虎牙", siteURL: "https://www.huya.com/", matchURL(channelURL) { return /https?:\/\/(?:.*?\.)?huya.com\//.test(channelURL); }, async resolveChannelInfoFromURL(channelURL) { if (!this.matchURL(channelURL)) return null; const id = path.basename(new URL(channelURL).pathname); const info = await getInfo(id); return { id: info.roomId.toString(), title: info.title, owner: info.owner, avatar: info.avatar, }; }, createRecorder(opts) { return createRecorder({ providerId: provider.id, ...opts }); }, fromJSON(recorder) { return defaultFromJSON(this, recorder); }, setFFMPEGOutputArgs(args) { ffmpegOutputOptions.splice(0, ffmpegOutputOptions.length, ...args); }, };