@bililive-tools/douyin-recorder
Version:
@bililive-tools douyin recorder implemention
62 lines (61 loc) • 1.93 kB
JavaScript
import { getRoomInfo } from "./douyin_api.js";
export async function getInfo(channelId) {
const info = await getRoomInfo(channelId);
return {
living: info.living,
owner: info.owner,
title: info.title,
roomId: info.roomId,
avatar: info.avatar,
cover: info.cover,
startTime: new Date(),
liveId: info.liveId,
};
}
export async function getStream(opts) {
const info = await getRoomInfo(opts.channelId, {
retryOnSpecialCode: true,
doubleScreen: opts.doubleScreen ?? true,
auth: opts.cookie,
});
if (!info.living) {
throw new Error("It must be called getStream when living");
}
// 抖音为自动cdn,所以指定选择第一个
const sources = info.sources[0];
const formatPriorities = opts.formatPriorities || ["flv", "hls"];
// 查找指定质量的流
let targetStream = sources.streams.find((s) => s.quality === opts.quality);
let qualityName = targetStream?.name ?? "未知";
if (!targetStream && opts.strictQuality) {
throw new Error("Can not get expect quality because of strictQuality");
}
// 如果找不到指定质量的流,按照流顺序选择第一个可用的流
if (!targetStream) {
targetStream = sources.streams.find((stream) => stream.flv || stream.hls);
if (targetStream) {
qualityName = targetStream.name;
}
}
if (!targetStream) {
throw new Error("未找到对应的流");
}
// 根据格式优先级选择 URL
let url;
for (const format of formatPriorities) {
url = targetStream[format];
if (url)
break;
}
if (!url) {
throw new Error("未找到对应的流");
}
return {
...info,
currentStream: {
name: qualityName,
source: "自动",
url: url,
},
};
}