@bililive-tools/douyin-recorder 1.3.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/README.md +2 -0
- package/lib/douyin_api.d.ts +5 -1
- package/lib/douyin_api.js +25 -6
- package/lib/index.js +8 -5
- package/lib/stream.d.ts +1 -0
- package/lib/stream.js +5 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -43,6 +43,8 @@ interface Options {
|
|
|
43
43
|
saveGiftDanma?: boolean; // 保存礼物弹幕
|
|
44
44
|
saveCover?: boolean; // 保存封面
|
|
45
45
|
videoFormat?: "auto"; // 视频格式: "auto", "ts", "mkv" ,auto模式下, 分段使用 "ts",不分段使用 "mp4"
|
|
46
|
+
useServerTimestamp?: boolean; // 控制弹幕是否使用服务端时间戳,默认为true
|
|
47
|
+
doubleScreen?: boolean; // 是否使用双屏直播流,开启后如果是双屏直播,那么就使用拼接的流,默认为true
|
|
46
48
|
auth?: string; // 传递cookie,用于录制会员视频
|
|
47
49
|
}
|
|
48
50
|
```
|
package/lib/douyin_api.d.ts
CHANGED
|
@@ -5,7 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export declare function resolveShortURL(shortURL: string): Promise<string>;
|
|
7
7
|
export declare const getCookie: () => Promise<string>;
|
|
8
|
-
export declare function getRoomInfo(webRoomId: string,
|
|
8
|
+
export declare function getRoomInfo(webRoomId: string, opts?: {
|
|
9
|
+
retryOnSpecialCode?: boolean;
|
|
10
|
+
auth?: string;
|
|
11
|
+
doubleScreen?: boolean;
|
|
12
|
+
}): Promise<{
|
|
9
13
|
living: boolean;
|
|
10
14
|
roomId: string;
|
|
11
15
|
owner: string;
|
package/lib/douyin_api.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
|
+
import { isEmpty } from "lodash-es";
|
|
2
3
|
import { assert } from "./utils.js";
|
|
3
4
|
const requester = axios.create({
|
|
4
5
|
timeout: 10e3,
|
|
@@ -76,10 +77,10 @@ export const getCookie = async () => {
|
|
|
76
77
|
};
|
|
77
78
|
return cookies;
|
|
78
79
|
};
|
|
79
|
-
export async function getRoomInfo(webRoomId,
|
|
80
|
+
export async function getRoomInfo(webRoomId, opts = {}) {
|
|
80
81
|
let cookies = undefined;
|
|
81
|
-
if (auth) {
|
|
82
|
-
cookies = auth;
|
|
82
|
+
if (opts.auth) {
|
|
83
|
+
cookies = opts.auth;
|
|
83
84
|
}
|
|
84
85
|
else {
|
|
85
86
|
// 抖音的 'webcast/room/web/enter' api 会需要 ttwid 的 cookie,这个 cookie 是由这个请求的响应头设置的,
|
|
@@ -110,7 +111,7 @@ export async function getRoomInfo(webRoomId, retryOnSpecialCode = true, auth) {
|
|
|
110
111
|
},
|
|
111
112
|
});
|
|
112
113
|
// 无 cookie 时 code 为 10037
|
|
113
|
-
if (res.data.status_code === 10037 && retryOnSpecialCode) {
|
|
114
|
+
if (res.data.status_code === 10037 && opts.retryOnSpecialCode) {
|
|
114
115
|
// resp 自动设置 cookie
|
|
115
116
|
// const cookieRes = await requester.get("https://live.douyin.com/favicon.ico");
|
|
116
117
|
// const cookies = cookieRes.headers["set-cookie"]
|
|
@@ -119,7 +120,10 @@ export async function getRoomInfo(webRoomId, retryOnSpecialCode = true, auth) {
|
|
|
119
120
|
// })
|
|
120
121
|
// .join("; ");
|
|
121
122
|
// console.log("cookies", cookies);
|
|
122
|
-
return getRoomInfo(webRoomId,
|
|
123
|
+
return getRoomInfo(webRoomId, {
|
|
124
|
+
retryOnSpecialCode: false,
|
|
125
|
+
doubleScreen: opts.doubleScreen,
|
|
126
|
+
});
|
|
123
127
|
}
|
|
124
128
|
assert(res.data.status_code === 0, `Unexpected resp, code ${res.data.status_code}, msg ${JSON.stringify(res.data.data)}, id ${webRoomId}`);
|
|
125
129
|
const data = res.data.data;
|
|
@@ -138,7 +142,22 @@ export async function getRoomInfo(webRoomId, retryOnSpecialCode = true, auth) {
|
|
|
138
142
|
liveId: room.id_str,
|
|
139
143
|
};
|
|
140
144
|
}
|
|
141
|
-
|
|
145
|
+
let qualities = [];
|
|
146
|
+
let stream_data = "";
|
|
147
|
+
if (opts.doubleScreen && !isEmpty(room.stream_url.pull_datas)) {
|
|
148
|
+
const pull_data = Object.values(room.stream_url.pull_datas)[0] ?? {
|
|
149
|
+
options: {
|
|
150
|
+
qualities: [],
|
|
151
|
+
},
|
|
152
|
+
stream_data: "",
|
|
153
|
+
};
|
|
154
|
+
qualities = pull_data.options.qualities;
|
|
155
|
+
stream_data = pull_data.stream_data;
|
|
156
|
+
}
|
|
157
|
+
if (!stream_data) {
|
|
158
|
+
qualities = room.stream_url.live_core_sdk_data.pull_data.options.qualities;
|
|
159
|
+
stream_data = room.stream_url.live_core_sdk_data.pull_data.stream_data;
|
|
160
|
+
}
|
|
142
161
|
const streamData = JSON.parse(stream_data).data;
|
|
143
162
|
const streams = qualities.map((info) => ({
|
|
144
163
|
desc: info.name,
|
package/lib/index.js
CHANGED
|
@@ -18,6 +18,7 @@ function createRecorder(opts) {
|
|
|
18
18
|
availableSources: [],
|
|
19
19
|
qualityMaxRetry: opts.qualityRetry ?? 0,
|
|
20
20
|
qualityRetry: opts.qualityRetry ?? 0,
|
|
21
|
+
useServerTimestamp: opts.useServerTimestamp ?? true,
|
|
21
22
|
state: "idle",
|
|
22
23
|
getChannelURL() {
|
|
23
24
|
return `https://live.douyin.com/${this.channelId}`;
|
|
@@ -109,6 +110,7 @@ const checkLiveStatusAndRecord = async function ({ getSavePath, banLiveId, isMan
|
|
|
109
110
|
strictQuality: strictQuality,
|
|
110
111
|
cookie: this.auth,
|
|
111
112
|
formatPriorities: this.formatPriorities,
|
|
113
|
+
doubleScreen: this.doubleScreen,
|
|
112
114
|
});
|
|
113
115
|
}
|
|
114
116
|
catch (err) {
|
|
@@ -178,7 +180,7 @@ const checkLiveStatusAndRecord = async function ({ getSavePath, banLiveId, isMan
|
|
|
178
180
|
room_id: this.channelId,
|
|
179
181
|
platform: provider?.id,
|
|
180
182
|
// liveStartTimestamp: liveInfo.startTime?.getTime(),
|
|
181
|
-
recordStopTimestamp: Date.now(),
|
|
183
|
+
// recordStopTimestamp: Date.now(),
|
|
182
184
|
title: title,
|
|
183
185
|
user_name: owner,
|
|
184
186
|
});
|
|
@@ -205,7 +207,7 @@ const checkLiveStatusAndRecord = async function ({ getSavePath, banLiveId, isMan
|
|
|
205
207
|
return;
|
|
206
208
|
const comment = {
|
|
207
209
|
type: "comment",
|
|
208
|
-
timestamp: Number(msg.eventTime) * 1000,
|
|
210
|
+
timestamp: this.useServerTimestamp ? Number(msg.eventTime) * 1000 : Date.now(),
|
|
209
211
|
text: msg.content,
|
|
210
212
|
color: "#ffffff",
|
|
211
213
|
sender: {
|
|
@@ -226,11 +228,12 @@ const checkLiveStatusAndRecord = async function ({ getSavePath, banLiveId, isMan
|
|
|
226
228
|
return;
|
|
227
229
|
if (this.saveGiftDanma === false)
|
|
228
230
|
return;
|
|
231
|
+
const serverTimestamp = Number(msg.common.createTime) > 9999999999
|
|
232
|
+
? Number(msg.common.createTime)
|
|
233
|
+
: Number(msg.common.createTime) * 1000;
|
|
229
234
|
const gift = {
|
|
230
235
|
type: "give_gift",
|
|
231
|
-
timestamp:
|
|
232
|
-
? Number(msg.common.createTime)
|
|
233
|
-
: Number(msg.common.createTime) * 1000,
|
|
236
|
+
timestamp: this.useServerTimestamp ? serverTimestamp : Date.now(),
|
|
234
237
|
name: msg.gift.name,
|
|
235
238
|
price: 1,
|
|
236
239
|
count: Number(msg.totalCount ?? 1),
|
package/lib/stream.d.ts
CHANGED
package/lib/stream.js
CHANGED
|
@@ -13,7 +13,11 @@ export async function getInfo(channelId) {
|
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
15
|
export async function getStream(opts) {
|
|
16
|
-
const info = await getRoomInfo(opts.channelId,
|
|
16
|
+
const info = await getRoomInfo(opts.channelId, {
|
|
17
|
+
retryOnSpecialCode: true,
|
|
18
|
+
doubleScreen: opts.doubleScreen ?? true,
|
|
19
|
+
auth: opts.cookie,
|
|
20
|
+
});
|
|
17
21
|
if (!info.living) {
|
|
18
22
|
throw new Error("It must be called getStream when living");
|
|
19
23
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bililive-tools/douyin-recorder",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "@bililive-tools douyin recorder implemention",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"axios": "^1.7.8",
|
|
38
38
|
"lodash-es": "^4.17.21",
|
|
39
39
|
"mitt": "^3.0.1",
|
|
40
|
-
"@bililive-tools/manager": "^1.
|
|
40
|
+
"@bililive-tools/manager": "^1.4.0",
|
|
41
41
|
"douyin-danma-listener": "0.2.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|