@cloud411716/fancy-webnovel 4.7.2 → 4.8.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 +1 -1
- package/docs/douyin-plugin.md +35 -15
- package/docs/material-library/README.md +5 -2
- package/docs/material-library/import.js +13 -3
- package/docs/material-library/prompt.md +7 -3
- package/docs/material-library/queries.surql +17 -9
- package/docs/material-library/sample.jsonl +1 -1
- package/docs/material-library/schema.surql +3 -1
- package/docs/workspace-mcp.md +8 -6
- package/lib/material-db.js +4 -4
- package/package.json +1 -1
- package/plugins/fancy-bootstrap/index.js +2 -2
- package/plugins/fancy-db/index.js +1 -1
- package/plugins/fancy-douyin/deploy.js +5 -4
- package/plugins/fancy-douyin/index.js +467 -31
- package/plugins/fancy-douyin/prompts/00-capability-prompt.md +22 -0
- package/plugins/fancy-douyin/prompts/01-analysis-prompt.md +44 -6
- package/plugins/fancy-douyin/prompts/02-video-analysis-prompt.md +95 -0
- package/plugins/fancy-douyin/scripts/crawl-video.cjs +420 -0
- package/plugins/fancy-douyin/scripts/scan-account.cjs +3 -2
- package/plugins/fancy-douyin/state.js +4 -3
- package/plugins/fancy-douyin/templates.js +114 -7
- package/plugins/fancy-material/exporter.js +3 -1
- package/plugins/fancy-material/inventory.js +3 -2
- package/plugins/fancy-material/scripts/query.js +11 -4
- package/plugins/fancy-material/templates.js +4 -3
- package/plugins/fancy-material/validate.js +11 -2
|
@@ -12,7 +12,43 @@
|
|
|
12
12
|
|
|
13
13
|
## 任务
|
|
14
14
|
|
|
15
|
-
分析截图中的内容,如果内容与小说网文创作的设定,素材,写作等无关的话则不进行分析;根据作品信息中的作品标题和截图中内容的排版布局(图片可能使用不同的排版布局对同类别内容进行归类)决定kind
|
|
15
|
+
分析截图中的内容,如果内容与小说网文创作的设定,素材,写作等无关的话则不进行分析;根据作品信息中的作品标题和截图中内容的排版布局(图片可能使用不同的排版布局对同类别内容进行归类)决定 kind、subkind、tags、genres 四个字段,并在 note 字段中补充一句
|
|
16
|
+
|
|
17
|
+
### 提取规则(必须遵守)
|
|
18
|
+
|
|
19
|
+
1. **先判断版式**:
|
|
20
|
+
- **清单 / 图鉴 / 设定集 / 列表**(颜色表、名字表、分类卡片、条目式设定):逐条提取,每条独立成一条素材。
|
|
21
|
+
- `name` 必须原样保留截图里的条目:颜色名就写“正红”“殷红”“绛红”,名字就写原名;**不要改写成“旧胭脂盒里剩下的一点红”这类意象句**。
|
|
22
|
+
- 不要合并、不要同义改写、不要只挑一部分。截图有多少条,就输出多少条。
|
|
23
|
+
- 截图里的分类标题必须进入 `tags`。例如【红/气场系】→ tags 至少包含 `红/气场系`、`红`、`气场系`;【粉/甜妹系】→ `粉/甜妹系`、`粉`、`甜妹系`。
|
|
24
|
+
- 作品标题里的主题(如“唇色设定”“瞳色设定”“发色”“印记”)必须进入 `tags`;每条至少包含主题 tag + 分类 tag。
|
|
25
|
+
- `kind` 一律用中文作品主题(如 `唇色`、`瞳色`、`特殊体质`、`发色`、`印记`);不要用 `color_word`、`person_appearance` 这类英文内部标识。即使整页都是纯颜色词,`kind` 也写中文主题(如 `唇色`),颜色本身放在 `name`。
|
|
26
|
+
- **长文 / 段落 / 剧情 / 场景描述**:抽象成可以独立取用的素材,不要整段抄写;机制、代价、限制、反制分开成条。
|
|
27
|
+
- **kind 与 subkind 的分工(清单类最重要)**:
|
|
28
|
+
- `kind` 是中文作品级大类,通常就是作品标题指向的主题:如“特殊体质”“唇色”“瞳色”“发色”“印记”。同一篇作品里的清单条目一般共用同一个 `kind`。
|
|
29
|
+
- `subkind` 是截图里的二级分类标题:如“病理变异”“赛博进化”“红/气场系”“粉/甜妹系”。条目出现在哪个二级标题下,就写哪个 `subkind`。
|
|
30
|
+
- 没有二级分类就省略 `subkind`;不要把 `kind` 重复写进 `subkind`,也不要把条目名当 `subkind`。
|
|
31
|
+
2. `tags` 3–8 个:除了主题和分类,还可以补充颜色家族、情绪、用途、适用对象等维度。
|
|
32
|
+
3. `genres` 1–5 个:根据条目气质和用途推断,不要全部写 `["通用"]`。
|
|
33
|
+
4. `note` 只补充一句使用场景、气质或来源,不要把 `name` 的内容再改写一遍。
|
|
34
|
+
5. 例子(来自“唇色设定”和“瞳色设定”截图):
|
|
35
|
+
- 【红/气场系】正红 → `{"kind":"唇色","subkind":"红/气场系","name":"正红","tags":["唇色","红/气场系","红","气场系","正红"],"genres":["古代","都市","言情"],"note":"适合正剧、强势角色或正式场合。"}`
|
|
36
|
+
- 【粉/甜妹系】蜜桃 → `{"kind":"唇色","subkind":"粉/甜妹系","name":"蜜桃","tags":["唇色","粉/甜妹系","粉","甜妹系","蜜桃"],"genres":["言情","校园","都市"],"note":"适合甜妹、日常、轻喜剧场景。"}`
|
|
37
|
+
- 【瞳色设定】猩红 → `{"kind":"瞳色","subkind":"赤/暖色系","name":"猩红","tags":["瞳色","赤/暖色系","红","暖色系","猩红"],"genres":["奇幻","悬疑","都市异能"],"note":"适合危险、异瞳或非人角色。"}`
|
|
38
|
+
6. **禁止**把清单条目改写成创作句;除非截图原文本身就是那句话。
|
|
39
|
+
7. **禁止**只提取 20 条就概括 100 条清单;清单类内容必须按条目数量逐条输出。
|
|
40
|
+
|
|
41
|
+
### 命名风格提炼与同风格扩写(每篇必做)
|
|
42
|
+
|
|
43
|
+
提取完全部原始条目后,先提炼本页作品的命名方式、特色和吸睛原因,再据此扩写 10 条新素材:
|
|
44
|
+
|
|
45
|
+
1. **先提炼,再动笔**:想清楚本页的命名方式(如“单字/双字颜色词”“器官 + 状态”“病理词 + 未来科技词”)、语言气质(古风、冷硬、甜妹、赛博)以及它为什么吸睛(反差、画面感、陌生化、可代入)。
|
|
46
|
+
2. **选一组 kind + subkind**:从本篇已提取的条目里选最有代表性的一组(通常条目最多或占据主版面的分类),扩写的 10 条必须全部使用这同一个 `kind` 和同一个 `subkind`;如果本篇没有二级分类,10 条保持同 `kind` 即可。
|
|
47
|
+
3. **保持风格一致**:新素材要沿用本页的长度、词性、意象密度、反差感和命名节奏;读起来必须像同一张清单里会出现的条目。
|
|
48
|
+
4. **必须是新条目**:不能与截图原始条目、也不能与数据库中已有素材同名或近义重复;颜色清单就继续给颜色词,设定清单就继续给同格式的设定名。
|
|
49
|
+
5. **同样写全字段**:每条都要有 `tags`(含作品主题和 subkind 分类)、`genres`、`note`;质量与原始条目一致。
|
|
50
|
+
6. **写在同一个文件**:10 条新素材接在原始条目后面,`id` 序号继续递增,仍用 `dy_<作品ID>_<序号>`(`kind` 是中文,不能直接放进 `id`)。
|
|
51
|
+
7. **数量**:原始条目全部保留,另加 10 条新素材;即使原始条目不足 10 条,也要补足这 10 条。若作品有多个 subkind,只选其中一组扩写 10 条,不要每个 subkind 各写 10 条。
|
|
16
52
|
|
|
17
53
|
## 输出文件
|
|
18
54
|
|
|
@@ -27,15 +63,17 @@
|
|
|
27
63
|
字段只能是:
|
|
28
64
|
|
|
29
65
|
```text
|
|
30
|
-
id / kind / name / tags / genres / note / source
|
|
66
|
+
id / kind / subkind / name / tags / genres / note / source
|
|
31
67
|
```
|
|
32
68
|
|
|
33
69
|
- `source` 固定写 `other`。
|
|
34
|
-
- `
|
|
35
|
-
- `
|
|
36
|
-
- `
|
|
70
|
+
- `name`:清单/图鉴类必须原样抄录条目(颜色名、名字、名词或短句),不要改写成意象句;长文类才抽象。
|
|
71
|
+
- `kind` 必须写中文作品主题(如 `唇色`、`瞳色`、`特殊体质`),由你根据标题和截图决定;不要写 `color_word`、`person_appearance` 这类英文内部标识。`source=other` 时 kind 不受内置列表限制,LLM 查询时直接用同一个中文词即可。
|
|
72
|
+
- `subkind` 可选,是 `kind` 下的中文二级分类,最多 40 字;截图有二级标题就原样写(如 `病理变异`、`赛博进化`、`红/气场系`),没有就省略。
|
|
73
|
+
- `tags` 3–8 个短标签;必须包含截图主题(如 `唇色`、`瞳色`)和分类标题(如 `红/气场系`、`粉/甜妹系`)。
|
|
74
|
+
- `genres` 1–5 个题材;如果素材同时适配多个题材/标签,把它们都写进去;不要全部写 `["通用"]`。
|
|
37
75
|
- `note` 可写一句补充,没有就写空字符串。
|
|
38
|
-
- `id`
|
|
76
|
+
- `id` 用小写字母、数字、下划线,格式建议:`dy_<作品ID>_<序号>`,全局唯一。
|
|
39
77
|
|
|
40
78
|
如果判断内容与网文创作无关:不要写任何素材行,只把下面的对象写进同一个文件,然后结束本轮(skip 标记不需要运行校验脚本):
|
|
41
79
|
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
你正在分析一篇抖音视频作品,把视频画面、字幕、口播内容拆成可以直接进入网文素材库的小积木。
|
|
2
|
+
|
|
3
|
+
## 作品信息
|
|
4
|
+
|
|
5
|
+
- 账号主页:{{ACCOUNT_URL}}
|
|
6
|
+
- 作品地址:{{VIDEO_URL}}
|
|
7
|
+
- 作品标题:{{VIDEO_TITLE}}
|
|
8
|
+
- 视频时长:{{VIDEO_DURATION}} 秒
|
|
9
|
+
- 前 5 秒已经通过相关性检查,确认与网文创作有关。
|
|
10
|
+
|
|
11
|
+
## 视频关键帧
|
|
12
|
+
|
|
13
|
+
以下是从视频中按时间抽取的关键帧(绝对路径):
|
|
14
|
+
|
|
15
|
+
{{FRAME_LIST}}
|
|
16
|
+
|
|
17
|
+
## 任务
|
|
18
|
+
|
|
19
|
+
结合视频标题、关键帧画面和字幕,提取可以进入网文素材库的设定、素材、灵感、剧情钩子或写作技巧。如果内容与网文创作无关,写 skip 标记并结束。
|
|
20
|
+
|
|
21
|
+
### 视频提取规则(必须遵守)
|
|
22
|
+
|
|
23
|
+
1. **按时间顺序看**:先看前 5 秒,再看后续关键帧;不要只看封面或第一帧。
|
|
24
|
+
2. **清单 / 图鉴 / 设定集**:逐条提取,每条独立成一条素材。
|
|
25
|
+
- `name` 必须原样保留视频里的条目:颜色名就写“正红”“殷红”“绛红”,设定名就写原名;**不要改写成意象句**。
|
|
26
|
+
- 不要合并、不要同义改写、不要只挑一部分。视频里有多少条,就输出多少条。
|
|
27
|
+
- 视频里的分类标题必须进入 `tags` 和 `subkind`。例如【红/气场系】→ tags 至少包含 `红/气场系`、`红`、`气场系`,subkind 写 `红/气场系`。
|
|
28
|
+
- 作品标题里的主题(如“唇色设定”“瞳色设定”“特殊体质”“发色”“印记”)必须进入 `tags`;每条至少包含主题 tag + 分类 tag。
|
|
29
|
+
3. **剧情 / 口播 / 场景演示**:抽象成可以独立取用的素材,不要整段抄写;人物、场景、冲突、规则、反转、文案钩子分开成条。字幕和贴纸里的关键信息也要提取。
|
|
30
|
+
4. `kind` 必须写中文作品主题(如 `唇色`、`瞳色`、`特殊体质`、`极端生存环境`),不要写 `color_word`、`person_appearance` 这类英文内部标识。`source=other` 时 kind 不受内置列表限制。
|
|
31
|
+
5. `subkind` 可选,是 `kind` 下的中文二级分类,最多 40 字;视频有二级标题就原样写(如 `病理变异`、`赛博进化`、`红/气场系`),没有就省略。
|
|
32
|
+
6. `tags` 3–8 个:包含视频主题、分类标题,再补充颜色家族、情绪、用途、适用对象等维度。
|
|
33
|
+
7. `genres` 1–5 个:根据内容气质和用途推断,不要全部写 `["通用"]`。
|
|
34
|
+
8. `note` 只补充一句使用场景、气质或来源,最多 300 字,不要把 `name` 再改写一遍。
|
|
35
|
+
9. **禁止**把清单条目改写成创作句;除非视频原文本身就是那句话。
|
|
36
|
+
10. **禁止**只提取一部分就概括整个视频;清单类内容必须按条目数量逐条输出。
|
|
37
|
+
|
|
38
|
+
### 命名风格提炼与同风格扩写(每个视频必做)
|
|
39
|
+
|
|
40
|
+
提取完全部原始条目后,先提炼这个视频的命名方式、特色和吸睛原因,再据此扩写 10 条新素材:
|
|
41
|
+
|
|
42
|
+
1. **先提炼,再动笔**:想清楚视频的命名方式(如“单字/双字颜色词”“器官 + 状态”“病理词 + 未来科技词”)、语言气质(古风、冷硬、甜妹、赛博)以及它为什么吸睛(反差、画面感、陌生化、可代入)。
|
|
43
|
+
2. **选一组 kind + subkind**:从本视频已提取的条目里选最有代表性的一组,扩写的 10 条必须全部使用这同一个 `kind` 和同一个 `subkind`;如果视频没有二级分类,10 条保持同 `kind` 即可。
|
|
44
|
+
3. **保持风格一致**:新素材要沿用视频里的长度、词性、意象密度、反差感和命名节奏;读起来必须像同一条视频里会出现的条目。
|
|
45
|
+
4. **必须是新条目**:不能与视频原始条目、也不能与数据库中已有素材同名或近义重复。
|
|
46
|
+
5. **同样写全字段**:每条都要有 `tags`(含视频主题和 subkind 分类)、`genres`、`note`;质量与原始条目一致。
|
|
47
|
+
6. **写在同一个文件**:10 条新素材接在原始条目后面,`id` 序号继续递增,仍用 `dy_<作品ID>_<序号>`。
|
|
48
|
+
7. **数量**:原始条目全部保留,另加 10 条新素材;即使原始条目不足 10 条,也要补足这 10 条。若视频有多个 subkind,只选其中一组扩写 10 条,不要每个 subkind 各写 10 条。
|
|
49
|
+
|
|
50
|
+
## 输出文件
|
|
51
|
+
|
|
52
|
+
把 JSONL 写到:
|
|
53
|
+
|
|
54
|
+
```text
|
|
55
|
+
{{TARGET_FILE}}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
一行一个 JSON 对象,不要 Markdown 代码围栏、标题、解释、前后缀。
|
|
59
|
+
|
|
60
|
+
字段只能是:
|
|
61
|
+
|
|
62
|
+
```text
|
|
63
|
+
id / kind / subkind / name / tags / genres / note / source
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
- `source` 固定写 `other`。
|
|
67
|
+
- `name`:清单/图鉴类必须原样抄录条目(颜色名、名字、名词或短句),不要改写成意象句;长文/口播类才抽象。
|
|
68
|
+
- `kind` 必须写中文作品主题,由你根据标题和视频内容决定;不要写英文内部标识。
|
|
69
|
+
- `subkind` 可选,是 `kind` 下的中文二级分类,最多 40 字。
|
|
70
|
+
- `tags` 3–8 个短标签;必须包含视频主题和分类标题。
|
|
71
|
+
- `genres` 1–5 个题材;如果素材同时适配多个题材/标签,把它们都写进去;不要全部写 `["通用"]`。
|
|
72
|
+
- `note` 可写一句补充,没有就写空字符串。
|
|
73
|
+
- `id` 用小写字母、数字、下划线,格式建议:`dy_<作品ID>_<序号>`,全局唯一。
|
|
74
|
+
|
|
75
|
+
如果判断内容与网文创作无关:不要写任何素材行,只把下面的对象写进同一个文件,然后结束本轮:
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{"skip": true, "reason": "与网文创作无关:<一句话原因>"}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
生成前可以查询已有素材,避免同名:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
node '{{QUERY_SCRIPT}}' --workspace '{{WORKSPACE}}' --kind <kind> --name '<候选名>' --json
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
写完运行格式校验:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
node '{{VALIDATE_SCRIPT}}' '{{TARGET_FILE}}'
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
校验失败就按错误修正,直到通过。
|
|
94
|
+
|
|
95
|
+
写完后只回复:`已写入 {{TARGET_FILE}}`。
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Open one Douyin /video/ work, check its duration, download it when possible,
|
|
5
|
+
* and extract keyframes for LLM analysis. stdout is one JSON object; Chrome
|
|
6
|
+
* stays open.
|
|
7
|
+
*
|
|
8
|
+
* Rules from the command layer:
|
|
9
|
+
* - videos longer than --max-seconds (default 60) are skipped before analysis;
|
|
10
|
+
* - the first 5 seconds are extracted separately for the relevance gate;
|
|
11
|
+
* - the full video gets a sparse storyboard (one frame every 5 seconds).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const fs = require('fs');
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const { spawnSync } = require('child_process');
|
|
17
|
+
const {
|
|
18
|
+
log, parseArgs, connect, installExternalProtocolGuard, restoreCookies, saveCookies,
|
|
19
|
+
safeGateResult, printJson, fail, ensureDir,
|
|
20
|
+
} = require('./lib.cjs');
|
|
21
|
+
|
|
22
|
+
const UA = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36';
|
|
23
|
+
const FIRST_SECONDS = 5;
|
|
24
|
+
const FRAME_STEP_SECONDS = 5;
|
|
25
|
+
|
|
26
|
+
function videoId(url) {
|
|
27
|
+
const match = String(url).match(/\/video\/(\d+)/);
|
|
28
|
+
if (match) return match[1];
|
|
29
|
+
return String(url).replace(/[?#].*$/, '').split('/').filter(Boolean).pop() || '';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function readMetadata(page) {
|
|
33
|
+
return await page.evaluate(() => {
|
|
34
|
+
const meta = (name) => {
|
|
35
|
+
const el = document.querySelector('meta[name="' + name + '"]');
|
|
36
|
+
return el && el.content ? el.content : '';
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
url: location.href,
|
|
40
|
+
title: document.title || '',
|
|
41
|
+
description: meta('description'),
|
|
42
|
+
body: (document.body && document.body.innerText ? document.body.innerText : '').slice(0, 6000),
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function absoluteVideoUrl(value) {
|
|
48
|
+
const text = String(value || '').trim();
|
|
49
|
+
if (!text) return '';
|
|
50
|
+
if (text.startsWith('//')) return 'https:' + text;
|
|
51
|
+
return text;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function waitForMainVideo(page) {
|
|
55
|
+
let fallback = null;
|
|
56
|
+
for (let round = 0; round < 12; round += 1) {
|
|
57
|
+
const info = await page.evaluate(() => {
|
|
58
|
+
const videos = [...document.querySelectorAll('video')].map((video) => {
|
|
59
|
+
const rect = video.getBoundingClientRect();
|
|
60
|
+
let src = video.currentSrc || video.src || '';
|
|
61
|
+
if (!src || src.startsWith('blob:')) {
|
|
62
|
+
const source = video.querySelector('source');
|
|
63
|
+
if (source) src = source.src || source.getAttribute('src') || src;
|
|
64
|
+
}
|
|
65
|
+
if (String(src).startsWith('//')) src = 'https:' + src;
|
|
66
|
+
return {
|
|
67
|
+
src,
|
|
68
|
+
duration: Number.isFinite(video.duration) && video.duration > 0 ? video.duration : null,
|
|
69
|
+
readyState: video.readyState,
|
|
70
|
+
width: rect.width,
|
|
71
|
+
height: rect.height,
|
|
72
|
+
mediatype: video.getAttribute('mediatype'),
|
|
73
|
+
index: video.getAttribute('data-index'),
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
const isHttp = (value) => /^https?:\/\//.test(String(value || ''));
|
|
77
|
+
const main = videos.find((video) => video.mediatype === 'video' && isHttp(video.src))
|
|
78
|
+
|| videos.filter((video) => video.width > 0 && video.height > 0)
|
|
79
|
+
.sort((a, b) => (b.width * b.height) - (a.width * a.height))[0]
|
|
80
|
+
|| videos.find((video) => isHttp(video.src))
|
|
81
|
+
|| videos[0]
|
|
82
|
+
|| null;
|
|
83
|
+
return { main, videos };
|
|
84
|
+
});
|
|
85
|
+
if (info.main && info.main.src) {
|
|
86
|
+
fallback = info;
|
|
87
|
+
// 时长通常在页面加载后 3–6 秒才可读。先等它出现,才能在下载前
|
|
88
|
+
// 判断「超过 1 分钟直接跳过」,避免白下载长视频。
|
|
89
|
+
if (info.main.duration) return info;
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
await page.evaluate(() => {
|
|
93
|
+
const video = document.querySelector('video[mediatype="video"]') || document.querySelector('video');
|
|
94
|
+
if (!video) return;
|
|
95
|
+
video.muted = true;
|
|
96
|
+
const played = video.play();
|
|
97
|
+
if (played && typeof played.catch === 'function') played.catch(() => {});
|
|
98
|
+
});
|
|
99
|
+
} catch { /* page may be navigating */ }
|
|
100
|
+
await page.waitForTimeout(900);
|
|
101
|
+
}
|
|
102
|
+
return fallback;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function downloadVideo(context, src, file) {
|
|
106
|
+
const response = await context.request.get(src, {
|
|
107
|
+
headers: {
|
|
108
|
+
Referer: 'https://www.douyin.com/',
|
|
109
|
+
'User-Agent': UA,
|
|
110
|
+
},
|
|
111
|
+
timeout: 120000,
|
|
112
|
+
failOnStatusCode: false,
|
|
113
|
+
});
|
|
114
|
+
if (!response.ok()) throw new Error('下载视频失败:HTTP ' + response.status());
|
|
115
|
+
const body = await response.body();
|
|
116
|
+
if (!body || !body.length) throw new Error('下载视频失败:响应为空');
|
|
117
|
+
fs.writeFileSync(file, body);
|
|
118
|
+
return body.length;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function ffprobeDuration(file) {
|
|
122
|
+
const result = spawnSync('ffprobe', [
|
|
123
|
+
'-v', 'error',
|
|
124
|
+
'-show_entries', 'format=duration',
|
|
125
|
+
'-of', 'default=nw=1:nk=1',
|
|
126
|
+
file,
|
|
127
|
+
], { encoding: 'utf8', timeout: 30000 });
|
|
128
|
+
if (result.status !== 0) return null;
|
|
129
|
+
const value = Number(String(result.stdout || '').trim());
|
|
130
|
+
return Number.isFinite(value) && value > 0 ? value : null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function runFfmpeg(args) {
|
|
134
|
+
const result = spawnSync('ffmpeg', args, { encoding: 'utf8', timeout: 180000 });
|
|
135
|
+
if (result.status !== 0) {
|
|
136
|
+
throw new Error((result.stderr || result.stdout || 'ffmpeg 执行失败').trim().slice(0, 500));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function jpegFiles(dir, pattern) {
|
|
141
|
+
try {
|
|
142
|
+
return fs.readdirSync(dir).filter((name) => pattern.test(name)).sort();
|
|
143
|
+
} catch {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function extractFramesWithFfmpeg(videoFile, outDir) {
|
|
149
|
+
const framesDir = path.join(outDir, 'frames');
|
|
150
|
+
ensureDir(framesDir);
|
|
151
|
+
runFfmpeg([
|
|
152
|
+
'-y', '-hide_banner', '-loglevel', 'error',
|
|
153
|
+
'-i', videoFile,
|
|
154
|
+
'-t', String(FIRST_SECONDS),
|
|
155
|
+
'-vf', 'fps=1',
|
|
156
|
+
'-q:v', '2',
|
|
157
|
+
path.join(framesDir, 'first_%02d.jpg'),
|
|
158
|
+
]);
|
|
159
|
+
runFfmpeg([
|
|
160
|
+
'-y', '-hide_banner', '-loglevel', 'error',
|
|
161
|
+
'-i', videoFile,
|
|
162
|
+
'-vf', 'fps=1/' + String(FRAME_STEP_SECONDS),
|
|
163
|
+
'-q:v', '2',
|
|
164
|
+
path.join(framesDir, 'all_%02d.jpg'),
|
|
165
|
+
]);
|
|
166
|
+
|
|
167
|
+
const first5s = jpegFiles(framesDir, /^first_\d+\.jpg$/).map((name, index) => ({
|
|
168
|
+
index: index + 1,
|
|
169
|
+
time: index,
|
|
170
|
+
file: path.join(framesDir, name),
|
|
171
|
+
}));
|
|
172
|
+
const all = jpegFiles(framesDir, /^all_\d+\.jpg$/).map((name, index) => ({
|
|
173
|
+
index: index + 1,
|
|
174
|
+
time: index * FRAME_STEP_SECONDS,
|
|
175
|
+
file: path.join(framesDir, name),
|
|
176
|
+
}));
|
|
177
|
+
const later = all.filter((frame) => frame.time >= FIRST_SECONDS);
|
|
178
|
+
return {
|
|
179
|
+
first5s: first5s.length ? first5s : all.slice(0, 1),
|
|
180
|
+
frames: [...first5s, ...later],
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function captureFramesFromPage(page, outDir, duration) {
|
|
185
|
+
const framesDir = path.join(outDir, 'frames');
|
|
186
|
+
ensureDir(framesDir);
|
|
187
|
+
const selector = 'video[mediatype="video"]';
|
|
188
|
+
let handle = await page.$(selector);
|
|
189
|
+
if (!handle) handle = await page.$('video');
|
|
190
|
+
if (!handle) throw new Error('没有找到视频元素,无法截取关键帧');
|
|
191
|
+
|
|
192
|
+
const times = [];
|
|
193
|
+
const limit = Number.isFinite(duration) && duration > 0 ? duration : FIRST_SECONDS;
|
|
194
|
+
for (let time = 0; time < Math.min(FIRST_SECONDS, limit); time += 1) times.push(time);
|
|
195
|
+
for (let time = FIRST_SECONDS; time < limit; time += FRAME_STEP_SECONDS) times.push(time);
|
|
196
|
+
|
|
197
|
+
const captured = [];
|
|
198
|
+
for (let index = 0; index < times.length; index += 1) {
|
|
199
|
+
const time = times[index];
|
|
200
|
+
try {
|
|
201
|
+
await page.evaluate(async ({ selector: sel, time: target }) => {
|
|
202
|
+
const video = document.querySelector(sel) || document.querySelector('video');
|
|
203
|
+
if (!video) return;
|
|
204
|
+
video.muted = true;
|
|
205
|
+
video.pause();
|
|
206
|
+
const safe = Math.max(0, Math.min(target, Math.max(0, (video.duration || target) - 0.05)));
|
|
207
|
+
if (Math.abs(video.currentTime - safe) > 0.15) {
|
|
208
|
+
await new Promise((resolve) => {
|
|
209
|
+
let settled = false;
|
|
210
|
+
const done = () => {
|
|
211
|
+
if (settled) return;
|
|
212
|
+
settled = true;
|
|
213
|
+
resolve();
|
|
214
|
+
};
|
|
215
|
+
video.addEventListener('seeked', done, { once: true });
|
|
216
|
+
video.currentTime = safe;
|
|
217
|
+
setTimeout(done, 3000);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}, { selector, time });
|
|
221
|
+
await page.waitForTimeout(250);
|
|
222
|
+
const file = path.join(framesDir, 'page_' + String(index + 1).padStart(2, '0') + '.jpg');
|
|
223
|
+
await handle.screenshot({ path: file, type: 'jpeg', quality: 90 });
|
|
224
|
+
captured.push({ index: index + 1, time, file });
|
|
225
|
+
} catch (err) {
|
|
226
|
+
log('[video] frame capture failed at ' + time + 's: ' + (err && err.message ? err.message : String(err)));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const first5s = captured.filter((frame) => frame.time < FIRST_SECONDS);
|
|
230
|
+
return {
|
|
231
|
+
first5s: first5s.length ? first5s : captured.slice(0, 1),
|
|
232
|
+
frames: captured,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function main() {
|
|
237
|
+
const args = parseArgs(process.argv.slice(2));
|
|
238
|
+
const workspace = path.resolve(String(args.workspace || process.cwd()));
|
|
239
|
+
const videoUrl = String(args.video || '');
|
|
240
|
+
const cdp = String(args.cdp || 'http://127.0.0.1:9222');
|
|
241
|
+
const outDir = path.resolve(String(args.out || path.join(workspace, '.fancy', 'douyin', 'output', 'video')));
|
|
242
|
+
const loginState = path.resolve(String(args['login-state'] || path.join(workspace, '.fancy', 'douyin', 'login-state.json')));
|
|
243
|
+
const maxSeconds = Number(args['max-seconds'] || 60);
|
|
244
|
+
if (!/\/video\//.test(videoUrl)) throw new Error('不是抖音视频作品地址:' + videoUrl);
|
|
245
|
+
ensureDir(outDir);
|
|
246
|
+
|
|
247
|
+
const { context } = await connect(cdp);
|
|
248
|
+
let page = null;
|
|
249
|
+
let keepPage = false;
|
|
250
|
+
try {
|
|
251
|
+
await restoreCookies(context, loginState);
|
|
252
|
+
page = await context.newPage();
|
|
253
|
+
await installExternalProtocolGuard(page);
|
|
254
|
+
await page.goto(videoUrl, { waitUntil: 'domcontentloaded', timeout: 60000 });
|
|
255
|
+
await page.waitForTimeout(3500);
|
|
256
|
+
|
|
257
|
+
const firstGate = await safeGateResult(page, outDir);
|
|
258
|
+
if (firstGate) {
|
|
259
|
+
keepPage = true;
|
|
260
|
+
await saveCookies(context, loginState, page.url());
|
|
261
|
+
printJson(firstGate);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const meta = await readMetadata(page);
|
|
266
|
+
const found = await waitForMainVideo(page);
|
|
267
|
+
const main = found && found.main ? found.main : null;
|
|
268
|
+
if (!main || !main.src) throw new Error('没有找到视频播放地址');
|
|
269
|
+
|
|
270
|
+
let duration = main.duration;
|
|
271
|
+
let videoFile = '';
|
|
272
|
+
let downloadError = '';
|
|
273
|
+
const directUrl = absoluteVideoUrl(main.src);
|
|
274
|
+
if (!directUrl || directUrl.startsWith('blob:')) {
|
|
275
|
+
downloadError = '视频地址不是可直接下载的 http(s) 地址';
|
|
276
|
+
log('[video] ' + downloadError + ',将使用播放器截图');
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (Number.isFinite(Number(duration)) && Number(duration) > maxSeconds) {
|
|
280
|
+
const video = {
|
|
281
|
+
id: videoId(videoUrl),
|
|
282
|
+
url: videoUrl,
|
|
283
|
+
final_url: meta.url,
|
|
284
|
+
type: 'video',
|
|
285
|
+
title: meta.title,
|
|
286
|
+
description: meta.description,
|
|
287
|
+
body: meta.body,
|
|
288
|
+
duration: Number(duration),
|
|
289
|
+
max_seconds: maxSeconds,
|
|
290
|
+
frames: [],
|
|
291
|
+
first5s: [],
|
|
292
|
+
video_file: '',
|
|
293
|
+
crawled_at: new Date().toISOString(),
|
|
294
|
+
dir: outDir,
|
|
295
|
+
};
|
|
296
|
+
const videoFileJson = path.join(outDir, 'video.json');
|
|
297
|
+
fs.writeFileSync(videoFileJson, JSON.stringify(video, null, 2) + '\n', 'utf8');
|
|
298
|
+
printJson({
|
|
299
|
+
ok: true,
|
|
300
|
+
login_required: false,
|
|
301
|
+
skip: true,
|
|
302
|
+
reason: '视频时长 ' + Number(duration).toFixed(1) + ' 秒,超过 ' + maxSeconds + ' 秒',
|
|
303
|
+
video,
|
|
304
|
+
video_file: videoFileJson,
|
|
305
|
+
});
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (directUrl && !directUrl.startsWith('blob:')) {
|
|
310
|
+
try {
|
|
311
|
+
videoFile = path.join(outDir, 'video.mp4');
|
|
312
|
+
await downloadVideo(context, directUrl, videoFile);
|
|
313
|
+
const probed = ffprobeDuration(videoFile);
|
|
314
|
+
if (probed) duration = probed;
|
|
315
|
+
} catch (err) {
|
|
316
|
+
downloadError = err && err.message ? err.message : String(err);
|
|
317
|
+
log('[video] download failed: ' + downloadError);
|
|
318
|
+
videoFile = '';
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (!Number.isFinite(Number(duration)) || Number(duration) <= 0) {
|
|
323
|
+
throw new Error('无法判断视频时长' + (downloadError ? '(' + downloadError + ')' : ''));
|
|
324
|
+
}
|
|
325
|
+
duration = Number(duration);
|
|
326
|
+
|
|
327
|
+
if (duration > maxSeconds) {
|
|
328
|
+
const video = {
|
|
329
|
+
id: videoId(videoUrl),
|
|
330
|
+
url: videoUrl,
|
|
331
|
+
final_url: meta.url,
|
|
332
|
+
type: 'video',
|
|
333
|
+
title: meta.title,
|
|
334
|
+
description: meta.description,
|
|
335
|
+
body: meta.body,
|
|
336
|
+
duration,
|
|
337
|
+
max_seconds: maxSeconds,
|
|
338
|
+
frames: [],
|
|
339
|
+
first5s: [],
|
|
340
|
+
video_file: videoFile,
|
|
341
|
+
crawled_at: new Date().toISOString(),
|
|
342
|
+
dir: outDir,
|
|
343
|
+
};
|
|
344
|
+
const videoFileJson = path.join(outDir, 'video.json');
|
|
345
|
+
fs.writeFileSync(videoFileJson, JSON.stringify(video, null, 2) + '\n', 'utf8');
|
|
346
|
+
printJson({
|
|
347
|
+
ok: true,
|
|
348
|
+
login_required: false,
|
|
349
|
+
skip: true,
|
|
350
|
+
reason: '视频时长 ' + duration.toFixed(1) + ' 秒,超过 ' + maxSeconds + ' 秒',
|
|
351
|
+
video,
|
|
352
|
+
video_file: videoFileJson,
|
|
353
|
+
});
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
let frames = [];
|
|
358
|
+
let first5s = [];
|
|
359
|
+
if (videoFile && fs.existsSync(videoFile)) {
|
|
360
|
+
try {
|
|
361
|
+
const extracted = extractFramesWithFfmpeg(videoFile, outDir);
|
|
362
|
+
frames = extracted.frames;
|
|
363
|
+
first5s = extracted.first5s;
|
|
364
|
+
} catch (err) {
|
|
365
|
+
log('[video] ffmpeg frame extraction failed: ' + (err && err.message ? err.message : String(err)));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (!frames.length) {
|
|
369
|
+
const captured = await captureFramesFromPage(page, outDir, duration);
|
|
370
|
+
frames = captured.frames;
|
|
371
|
+
first5s = captured.first5s;
|
|
372
|
+
}
|
|
373
|
+
if (!frames.length) throw new Error('没有抽取到视频关键帧');
|
|
374
|
+
|
|
375
|
+
const afterGate = await safeGateResult(page, outDir);
|
|
376
|
+
if (afterGate) {
|
|
377
|
+
keepPage = true;
|
|
378
|
+
await saveCookies(context, loginState, page.url());
|
|
379
|
+
printJson(afterGate);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
await saveCookies(context, loginState, page.url());
|
|
384
|
+
const video = {
|
|
385
|
+
id: videoId(videoUrl),
|
|
386
|
+
url: videoUrl,
|
|
387
|
+
final_url: meta.url,
|
|
388
|
+
type: 'video',
|
|
389
|
+
title: meta.title,
|
|
390
|
+
description: meta.description,
|
|
391
|
+
body: meta.body,
|
|
392
|
+
duration,
|
|
393
|
+
max_seconds: maxSeconds,
|
|
394
|
+
width: main.width || 0,
|
|
395
|
+
height: main.height || 0,
|
|
396
|
+
frames,
|
|
397
|
+
first5s,
|
|
398
|
+
video_file: videoFile,
|
|
399
|
+
download_error: downloadError,
|
|
400
|
+
crawled_at: new Date().toISOString(),
|
|
401
|
+
dir: outDir,
|
|
402
|
+
};
|
|
403
|
+
const videoFileJson = path.join(outDir, 'video.json');
|
|
404
|
+
fs.writeFileSync(videoFileJson, JSON.stringify(video, null, 2) + '\n', 'utf8');
|
|
405
|
+
printJson({
|
|
406
|
+
ok: true,
|
|
407
|
+
login_required: false,
|
|
408
|
+
video,
|
|
409
|
+
video_file: videoFileJson,
|
|
410
|
+
});
|
|
411
|
+
} finally {
|
|
412
|
+
if (page && !keepPage) {
|
|
413
|
+
try { await page.close(); } catch { /* ignore */ }
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
main()
|
|
419
|
+
.then(() => process.exit(0))
|
|
420
|
+
.catch((err) => { fail(err); process.exit(0); });
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Scan one Douyin account homepage and return its /note/ image-text works
|
|
4
|
+
* Scan one Douyin account homepage and return its /note/ image-text works
|
|
5
|
+
* and /video/ video works.
|
|
5
6
|
*
|
|
6
7
|
* stdout is one JSON object. Progress goes to stderr. The script never closes
|
|
7
8
|
* Chrome; it closes only its own page, so login state stays in the profile.
|
|
@@ -163,7 +164,7 @@ async function main() {
|
|
|
163
164
|
account,
|
|
164
165
|
title: await page.title(),
|
|
165
166
|
notes,
|
|
166
|
-
videos
|
|
167
|
+
videos,
|
|
167
168
|
cards_file: cardsFile,
|
|
168
169
|
});
|
|
169
170
|
} finally {
|
|
@@ -5,9 +5,10 @@
|
|
|
5
5
|
* live under <workspace>/.fancy/douyin. The deployed crawler scripts live under
|
|
6
6
|
* <workspace>/.fancy-tools/fancy-douyin.
|
|
7
7
|
*
|
|
8
|
-
* The crawl history is keyed by the note id parsed from /note/<id
|
|
9
|
-
* written only after its
|
|
10
|
-
* material library,
|
|
8
|
+
* The crawl history is keyed by the note/video id parsed from /note/<id> or
|
|
9
|
+
* /video/<id>. A work is written only after its analysis has been imported into
|
|
10
|
+
* the material library, or after it was confirmed irrelevant/long and skipped,
|
|
11
|
+
* so failed works are retried on the next run.
|
|
11
12
|
*/
|
|
12
13
|
|
|
13
14
|
import fs from 'fs';
|