@cloud411716/fancy-webnovel 4.19.0 → 4.20.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/docs/douyin-plugin.md +5 -4
- package/package.json +1 -1
- package/plugins/fancy-douyin/index.js +1 -1
- package/plugins/fancy-douyin/prompts/02-video-analysis-prompt.md +2 -2
- package/plugins/fancy-douyin/scripts/crawl-note.cjs +10 -9
- package/plugins/fancy-douyin/scripts/crawl-video.cjs +11 -7
- package/plugins/fancy-douyin/scripts/video-frames.cjs +255 -77
package/docs/douyin-plugin.md
CHANGED
|
@@ -40,7 +40,7 @@ LLM 能力自检(图片 / 视频)
|
|
|
40
40
|
→ 文章:提取标题、发布日期、阅读时长和正文
|
|
41
41
|
→ 图文:逐页判断类型
|
|
42
42
|
→ 图片页:下载轮播原图并转成 JPG(避免滚动时截到半张图)
|
|
43
|
-
→
|
|
43
|
+
→ 视频页:下载该页短视频,密集采样后按稳定画面段抽帧,拼成一张关键帧图
|
|
44
44
|
→ 视频:先判断时长
|
|
45
45
|
→ 超过 1 分钟:直接跳过并记录
|
|
46
46
|
→ 不超过 1 分钟:抽取前 5 秒关键帧,先判断是否与网文创作有关
|
|
@@ -199,11 +199,12 @@ id / kind / subkind / name / tags / genres / note / source
|
|
|
199
199
|
- 只采集个人研究/已授权账号,不要批量抓取或二次分发受版权保护的图片、视频和文案。
|
|
200
200
|
- 图文截图不是页面视口截图,而是下载每页轮播原图后再转成 JPEG,所以不会因为
|
|
201
201
|
滚动、懒加载或页面动画截到残缺图片。
|
|
202
|
-
-
|
|
203
|
-
|
|
202
|
+
- 图文作品的轮播可以混合图片页和视频页。视频页会下载该页短视频,15 秒以内每 0.2 秒采样、15–60 秒每 0.5 秒采样,
|
|
203
|
+
再按稳定画面段选帧并拼成一张关键帧图交给 LLM;这样快速切换的画面和动态字幕/标语不会被封面漏掉。
|
|
204
204
|
- 独立视频作品会先尝试下载原片并用 ffmpeg 抽帧;下载失败时退化为播放器元素截图。
|
|
205
205
|
视频时长超过 1 分钟直接跳过;前 5 秒与网文创作无关也跳过。
|
|
206
|
-
-
|
|
206
|
+
- 场景切换点本身常是过渡虚影。插件会在密集采样后用 ffmpeg blurdetect 给每帧打分,在每个稳定画面段的清晰中段选一帧,避免把文字尚未完全出现的模糊帧交给 LLM。
|
|
207
|
+
- 帧选择版本是 `frame_version: 3`;旧版本缓存的截图/关键帧会自动重新采集。
|
|
207
208
|
- 子 Agent 或 DSH 在分析途中中断时,作品任务保持 pending;下次运行会复用已采集文件重新分析。旧版本留下的 aborted/failed 任务会在启动采集时自动恢复为 pending。
|
|
208
209
|
- 抖音 DOM/类名会变,`scan-account.cjs`、`crawl-note.cjs` 和 `crawl-video.cjs`
|
|
209
210
|
的选择器可能需要维护。
|
package/package.json
CHANGED
|
@@ -46,7 +46,7 @@ import {
|
|
|
46
46
|
|
|
47
47
|
export const inject = ['commands', 'userQuestions'];
|
|
48
48
|
|
|
49
|
-
const FRAME_SELECTION_VERSION =
|
|
49
|
+
const FRAME_SELECTION_VERSION = 3;
|
|
50
50
|
const ANALYSIS_TIMEOUT_MS = 20 * 60 * 1000;
|
|
51
51
|
const ANALYSIS_IDLE_GRACE_MS = 60 * 1000;
|
|
52
52
|
const CAPABILITY_TIMEOUT_MS = 5 * 60 * 1000;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
你正在分析一篇抖音视频作品,把视频画面、字幕和屏幕文字拆成可以直接进入网文素材库的小积木。
|
|
2
2
|
|
|
3
3
|
## 作品信息
|
|
4
4
|
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
|
|
20
20
|
- 关键帧按时间顺序看,先看前 5 秒,再看后续。
|
|
21
21
|
- 同一内容跨帧只输出一次,不要每帧重复生成。
|
|
22
|
-
-
|
|
22
|
+
- 字幕、贴纸、屏幕文字都要读;画面里没有的信息不要编造。
|
|
23
23
|
- 片头、片尾、广告、水印、按钮、点赞信息跳过。
|
|
24
24
|
- 如果关键帧是拼图,按从左到右、从上到下读取。
|
|
25
25
|
- 画面切换很快时,只提取能确认的内容,不要猜。
|
|
@@ -15,12 +15,10 @@ const {
|
|
|
15
15
|
safeGateResult, printJson, fail, ensureDir,
|
|
16
16
|
} = require('./lib.cjs');
|
|
17
17
|
const {
|
|
18
|
-
probeDuration, detectSceneTimes, fixedShortTimes,
|
|
19
|
-
extractSharpFramesAt,
|
|
18
|
+
probeDuration, detectSceneTimes, fixedShortTimes, extractDenseFramesAt,
|
|
20
19
|
} = require('./video-frames.cjs');
|
|
21
20
|
|
|
22
21
|
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 VIDEO_FRAME_TIMES = 4;
|
|
24
22
|
|
|
25
23
|
function noteId(url) {
|
|
26
24
|
const match = String(url).match(/\/note\/(\d+)/);
|
|
@@ -310,11 +308,14 @@ async function captureSlides(page, context, outDir) {
|
|
|
310
308
|
await downloadVideo(context, current.videoSrc, videoFile);
|
|
311
309
|
duration = probeDuration(videoFile) || duration;
|
|
312
310
|
sceneTimes = detectSceneTimes(videoFile, { threshold: 0.1, maxFrames: 10 });
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
311
|
+
const dense = extractDenseFramesAt(
|
|
312
|
+
videoFile,
|
|
313
|
+
outDir,
|
|
314
|
+
'slide_' + String(slideNo).padStart(2, '0') + '_frame',
|
|
315
|
+
{ duration, sceneTimes, maxFrames: 12 },
|
|
316
|
+
);
|
|
317
|
+
frames = dense.frames;
|
|
318
|
+
if (dense.times.length) times = dense.times;
|
|
318
319
|
} catch (err) {
|
|
319
320
|
log('[note] video slide ' + slideNo + ' download/frames failed: ' + (err && err.message ? err.message : String(err)));
|
|
320
321
|
}
|
|
@@ -489,7 +490,7 @@ async function main() {
|
|
|
489
490
|
description: meta.description,
|
|
490
491
|
body: meta.body,
|
|
491
492
|
slides: captured.slides,
|
|
492
|
-
frame_version:
|
|
493
|
+
frame_version: 3,
|
|
493
494
|
screenshots: captured.screenshots,
|
|
494
495
|
crawled_at: new Date().toISOString(),
|
|
495
496
|
dir: outDir,
|
|
@@ -19,8 +19,7 @@ const {
|
|
|
19
19
|
safeGateResult, printJson, fail, ensureDir,
|
|
20
20
|
} = require('./lib.cjs');
|
|
21
21
|
const {
|
|
22
|
-
probeDuration, detectSceneTimes, fixedVideoTimes,
|
|
23
|
-
extractSharpFramesAt,
|
|
22
|
+
probeDuration, detectSceneTimes, fixedVideoTimes, extractDenseFramesAt,
|
|
24
23
|
} = require('./video-frames.cjs');
|
|
25
24
|
|
|
26
25
|
const UA = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36';
|
|
@@ -229,7 +228,7 @@ async function main() {
|
|
|
229
228
|
frames: [],
|
|
230
229
|
first5s: [],
|
|
231
230
|
video_file: '',
|
|
232
|
-
frame_version:
|
|
231
|
+
frame_version: 3,
|
|
233
232
|
crawled_at: new Date().toISOString(),
|
|
234
233
|
dir: outDir,
|
|
235
234
|
};
|
|
@@ -278,7 +277,7 @@ async function main() {
|
|
|
278
277
|
frames: [],
|
|
279
278
|
first5s: [],
|
|
280
279
|
video_file: videoFile,
|
|
281
|
-
frame_version:
|
|
280
|
+
frame_version: 3,
|
|
282
281
|
crawled_at: new Date().toISOString(),
|
|
283
282
|
dir: outDir,
|
|
284
283
|
};
|
|
@@ -303,8 +302,13 @@ async function main() {
|
|
|
303
302
|
if (videoFile && fs.existsSync(videoFile)) {
|
|
304
303
|
try {
|
|
305
304
|
sceneTimes = detectSceneTimes(videoFile, { threshold: 0.15, maxFrames: 16 });
|
|
306
|
-
|
|
307
|
-
|
|
305
|
+
const dense = extractDenseFramesAt(videoFile, outDir, 'video_frame', {
|
|
306
|
+
duration,
|
|
307
|
+
sceneTimes,
|
|
308
|
+
maxFrames: 24,
|
|
309
|
+
});
|
|
310
|
+
frames = dense.frames;
|
|
311
|
+
if (dense.times.length) times = dense.times;
|
|
308
312
|
} catch (err) {
|
|
309
313
|
log('[video] ffmpeg frame extraction failed: ' + (err && err.message ? err.message : String(err)));
|
|
310
314
|
}
|
|
@@ -344,7 +348,7 @@ async function main() {
|
|
|
344
348
|
fixed_times: fixedTimes,
|
|
345
349
|
video_file: videoFile,
|
|
346
350
|
download_error: downloadError,
|
|
347
|
-
frame_version:
|
|
351
|
+
frame_version: 3,
|
|
348
352
|
crawled_at: new Date().toISOString(),
|
|
349
353
|
dir: outDir,
|
|
350
354
|
};
|
|
@@ -2,14 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Shared ffmpeg helpers for Douyin note carousel video slides and standalone
|
|
5
|
-
* videos.
|
|
6
|
-
*
|
|
5
|
+
* videos. Videos are sampled densely, then split into stable visual states so
|
|
6
|
+
* animated captions are represented after they finish appearing instead of at
|
|
7
|
+
* a transition frame.
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
const fs = require('fs');
|
|
10
11
|
const path = require('path');
|
|
11
12
|
const { spawnSync } = require('child_process');
|
|
12
13
|
|
|
14
|
+
const SHORT_VIDEO_SECONDS = 15;
|
|
15
|
+
const MEDIUM_VIDEO_SECONDS = 60;
|
|
16
|
+
const SHORT_VIDEO_INTERVAL = 0.2;
|
|
17
|
+
const MEDIUM_VIDEO_INTERVAL = 0.5;
|
|
18
|
+
const LONG_VIDEO_INTERVAL = 1;
|
|
19
|
+
|
|
13
20
|
function probeDuration(file) {
|
|
14
21
|
const result = spawnSync('ffprobe', [
|
|
15
22
|
'-v', 'error',
|
|
@@ -127,93 +134,262 @@ function extractFramesAt(file, outDir, prefix, times, { timeout = 60000 } = {})
|
|
|
127
134
|
return frames;
|
|
128
135
|
}
|
|
129
136
|
|
|
130
|
-
/**
|
|
131
|
-
function
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
137
|
+
/** 15 秒以内每 0.2 秒采样,15–60 秒每 0.5 秒采样。 */
|
|
138
|
+
function denseSampleInterval(duration) {
|
|
139
|
+
const total = Number(duration);
|
|
140
|
+
if (!Number.isFinite(total) || total <= 0) return SHORT_VIDEO_INTERVAL;
|
|
141
|
+
if (total <= SHORT_VIDEO_SECONDS) return SHORT_VIDEO_INTERVAL;
|
|
142
|
+
if (total <= MEDIUM_VIDEO_SECONDS) return MEDIUM_VIDEO_INTERVAL;
|
|
143
|
+
return LONG_VIDEO_INTERVAL;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function parseMetric(text, name) {
|
|
147
|
+
const re = new RegExp('lavfi\\.' + name + '=([0-9]+(?:\\.[0-9]+)?)', 'g');
|
|
148
|
+
const values = [];
|
|
149
|
+
let match;
|
|
150
|
+
while ((match = re.exec(text))) {
|
|
151
|
+
const value = Number(match[1]);
|
|
152
|
+
if (Number.isFinite(value)) values.push(value);
|
|
153
|
+
}
|
|
154
|
+
return values;
|
|
145
155
|
}
|
|
146
156
|
|
|
147
157
|
/**
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
158
|
+
* One ffmpeg pass over the clip. The fps filter reduces the stream to the
|
|
159
|
+
* requested sample interval, select computes the difference against the
|
|
160
|
+
* previous sample, and blurdetect scores the sharpness of that sample.
|
|
151
161
|
*/
|
|
152
|
-
function
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
162
|
+
function analyzeDenseFrames(file, { duration = null, interval = null, timeout = 240000 } = {}) {
|
|
163
|
+
const total = Number.isFinite(Number(duration)) && Number(duration) > 0
|
|
164
|
+
? Number(duration)
|
|
165
|
+
: probeDuration(file);
|
|
166
|
+
const step = Number.isFinite(Number(interval)) && Number(interval) > 0
|
|
167
|
+
? Number(interval)
|
|
168
|
+
: denseSampleInterval(total);
|
|
169
|
+
const fps = Math.round((1 / step) * 1000) / 1000;
|
|
170
|
+
const offset = Math.round(step * 50) / 100;
|
|
171
|
+
const baseArgs = ['-hide_banner', '-loglevel', 'info', '-i', file];
|
|
172
|
+
const outputArgs = ['-an', '-f', 'null', '-'];
|
|
173
|
+
const prefix = (total && total > offset ? "trim=start=" + offset + ",setpts=PTS-STARTPTS," : "setpts=PTS-STARTPTS,");
|
|
174
|
+
const withBlur = prefix + "fps=" + fps
|
|
175
|
+
+ ",select='gte(scene,0)',metadata=print:key=lavfi.scene_score"
|
|
176
|
+
+ ",blurdetect,metadata=print:key=lavfi.blur";
|
|
177
|
+
let result = spawnSync('ffmpeg', [
|
|
178
|
+
...baseArgs, '-vf', withBlur, ...outputArgs,
|
|
179
|
+
], { encoding: 'utf8', timeout, maxBuffer: 64 * 1024 * 1024 });
|
|
180
|
+
let text = String(result.stderr || '');
|
|
181
|
+
let scores = parseMetric(text, 'scene_score');
|
|
182
|
+
let blurs = parseMetric(text, 'blur');
|
|
183
|
+
if (result.status !== 0 || !scores.length) {
|
|
184
|
+
// blurdetect is optional in some ffmpeg builds; keep scene-only metrics.
|
|
185
|
+
result = spawnSync('ffmpeg', [
|
|
186
|
+
...baseArgs,
|
|
187
|
+
'-vf', prefix + "fps=" + fps
|
|
188
|
+
+ ",select='gte(scene,0)',metadata=print:key=lavfi.scene_score",
|
|
189
|
+
...outputArgs,
|
|
190
|
+
], { encoding: 'utf8', timeout, maxBuffer: 64 * 1024 * 1024 });
|
|
191
|
+
text = String(result.stderr || '');
|
|
192
|
+
scores = parseMetric(text, 'scene_score');
|
|
193
|
+
blurs = [];
|
|
194
|
+
}
|
|
195
|
+
const count = Math.max(scores.length, blurs.length);
|
|
196
|
+
const samples = [];
|
|
197
|
+
for (let index = 0; index < count; index += 1) {
|
|
198
|
+
samples.push({
|
|
199
|
+
time: Math.round((offset + index * step) * 100) / 100,
|
|
200
|
+
scene: index < scores.length ? scores[index] : 0,
|
|
201
|
+
blur: index < blurs.length ? blurs[index] : null,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return { interval: step, samples };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Split dense samples into stable states. A frame is stable when neither the
|
|
209
|
+
* step into it nor the step out of it changes the picture much. Runs of stable
|
|
210
|
+
* frames become visual states; the latest sharp frame in each run represents
|
|
211
|
+
* the state, which gives animated text time to finish appearing. Uncovered
|
|
212
|
+
* stretches (constant motion, no stable plateau) still get a sharp frame so a
|
|
213
|
+
* long clip cannot lose a whole segment.
|
|
214
|
+
*/
|
|
215
|
+
function selectDenseFrameTimes(samples, {
|
|
216
|
+
interval = SHORT_VIDEO_INTERVAL,
|
|
217
|
+
sceneTimes = [],
|
|
156
218
|
maxFrames = 24,
|
|
219
|
+
stableThreshold = 0.012,
|
|
220
|
+
maxGap = null,
|
|
157
221
|
} = {}) {
|
|
158
|
-
const
|
|
159
|
-
.map((
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
222
|
+
const list = (Array.isArray(samples) ? samples : [])
|
|
223
|
+
.map((sample) => ({
|
|
224
|
+
time: Number(sample.time),
|
|
225
|
+
scene: Number.isFinite(Number(sample.scene)) ? Number(sample.scene) : 0,
|
|
226
|
+
blur: Number.isFinite(Number(sample.blur)) ? Number(sample.blur) : null,
|
|
227
|
+
}))
|
|
228
|
+
.filter((sample) => Number.isFinite(sample.time) && sample.time >= 0)
|
|
229
|
+
.sort((a, b) => a.time - b.time);
|
|
230
|
+
if (!list.length) return [];
|
|
231
|
+
const step = Number.isFinite(Number(interval)) && Number(interval) > 0
|
|
232
|
+
? Number(interval)
|
|
233
|
+
: SHORT_VIDEO_INTERVAL;
|
|
234
|
+
const total = list[list.length - 1].time + step;
|
|
235
|
+
const gapLimit = Number.isFinite(Number(maxGap)) && Number(maxGap) > 0
|
|
236
|
+
? Number(maxGap)
|
|
237
|
+
: (total <= SHORT_VIDEO_SECONDS ? 2.5 : 5);
|
|
238
|
+
|
|
239
|
+
// A scene cut must never be folded into the previous state.
|
|
240
|
+
const cutIndexes = new Set();
|
|
241
|
+
for (const raw of sceneTimes || []) {
|
|
242
|
+
const time = Number(raw);
|
|
243
|
+
if (!Number.isFinite(time)) continue;
|
|
244
|
+
const index = list.findIndex((sample) => sample.time >= time - step * 0.25);
|
|
245
|
+
if (index > 0) cutIndexes.add(index);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const stableFlags = list.map((sample, index) => {
|
|
249
|
+
if (sample.scene >= stableThreshold) return false;
|
|
250
|
+
const next = index + 1 < list.length ? list[index + 1].scene : 0;
|
|
251
|
+
if (next >= stableThreshold) return false;
|
|
252
|
+
return !cutIndexes.has(index + 1);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const runs = [];
|
|
256
|
+
let current = [];
|
|
257
|
+
let gap = 0;
|
|
258
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
259
|
+
if (stableFlags[index]) {
|
|
260
|
+
current.push(list[index]);
|
|
261
|
+
gap = 0;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (!current.length) continue;
|
|
265
|
+
const hardCut = cutIndexes.has(index) || list[index].scene >= 0.08;
|
|
266
|
+
// A single noisy sample inside an otherwise static state should not split
|
|
267
|
+
// it; a real cut always does.
|
|
268
|
+
if (!hardCut && gap < 1) {
|
|
269
|
+
gap += 1;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
runs.push(current);
|
|
273
|
+
current = [];
|
|
274
|
+
gap = 0;
|
|
275
|
+
}
|
|
276
|
+
if (current.length) runs.push(current);
|
|
277
|
+
|
|
278
|
+
const chooseRepresentative = (pool) => {
|
|
279
|
+
if (!pool.length) return null;
|
|
280
|
+
// Trim the edges of long runs so fade-in ghosting and fade-out frames do
|
|
281
|
+
// not win. The sharpest frame in the stable middle is the representative.
|
|
282
|
+
const trim = pool.length >= 4 ? Math.max(1, Math.floor(pool.length * 0.2)) : 0;
|
|
283
|
+
const middle = trim ? pool.slice(trim, pool.length - trim) : pool;
|
|
284
|
+
const candidates = middle.length ? middle : pool;
|
|
285
|
+
const scored = candidates.filter((sample) => sample.blur !== null);
|
|
286
|
+
if (!scored.length) return candidates[candidates.length - 1];
|
|
287
|
+
const best = Math.min(...scored.map((sample) => sample.blur));
|
|
288
|
+
const acceptable = candidates.filter(
|
|
289
|
+
(sample) => sample.blur === null || sample.blur <= best * 1.25 + 0.5,
|
|
290
|
+
);
|
|
291
|
+
let chosen = null;
|
|
292
|
+
for (const sample of acceptable) {
|
|
293
|
+
if (sample.blur === null) continue;
|
|
294
|
+
if (!chosen || sample.blur < chosen.blur - 0.05) chosen = sample;
|
|
295
|
+
else if (Math.abs(sample.blur - chosen.blur) <= 0.05 && sample.time > chosen.time) chosen = sample;
|
|
296
|
+
}
|
|
297
|
+
return chosen || acceptable[acceptable.length - 1] || candidates[candidates.length - 1];
|
|
298
|
+
};
|
|
299
|
+
|
|
167
300
|
const selected = [];
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
// Prefer the latest acceptable frame: animated text has had time to
|
|
200
|
-
// finish, while blurdetect still rejects transition ghosting.
|
|
201
|
-
selected.push(acceptable[acceptable.length - 1]);
|
|
202
|
-
} else {
|
|
203
|
-
// No blurdetect support: use the largest JPEG as a sharpness proxy.
|
|
204
|
-
selected.push(candidates.slice().sort((a, b) => b.size - a.size)[0]);
|
|
205
|
-
}
|
|
301
|
+
const covered = [];
|
|
302
|
+
for (const run of runs) {
|
|
303
|
+
// A single frame at the very start is usually the fade-in black/white
|
|
304
|
+
// frame. A real static title lasts multiple samples and is kept.
|
|
305
|
+
if (run.length === 1 && run[0].time <= step * 1.5 + 0.001 && list.length > 2) continue;
|
|
306
|
+
const representative = chooseRepresentative(run);
|
|
307
|
+
if (!representative) continue;
|
|
308
|
+
selected.push({ sample: representative, stable: true });
|
|
309
|
+
covered.push([run[0].time, run[run.length - 1].time]);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Fill parts of the clip that have no stable run at all. This keeps a
|
|
313
|
+
// continuously animated intro or a long moving shot from disappearing.
|
|
314
|
+
covered.sort((a, b) => a[0] - b[0]);
|
|
315
|
+
const uncovered = [];
|
|
316
|
+
let cursor = 0;
|
|
317
|
+
for (const [start, end] of covered) {
|
|
318
|
+
if (start - cursor > step / 2) uncovered.push([cursor, start]);
|
|
319
|
+
cursor = Math.max(cursor, end);
|
|
320
|
+
}
|
|
321
|
+
if (total - cursor > step / 2) uncovered.push([cursor, total]);
|
|
322
|
+
for (const [start, end] of uncovered) {
|
|
323
|
+
const span = end - start;
|
|
324
|
+
if (span < gapLimit * 0.6) continue;
|
|
325
|
+
const chunks = Math.max(1, Math.ceil(span / gapLimit));
|
|
326
|
+
for (let chunk = 0; chunk < chunks; chunk += 1) {
|
|
327
|
+
const from = start + (span * chunk) / chunks;
|
|
328
|
+
const to = start + (span * (chunk + 1)) / chunks;
|
|
329
|
+
const pool = list.filter((sample) => sample.time >= from && sample.time < to);
|
|
330
|
+
const representative = chooseRepresentative(pool);
|
|
331
|
+
if (representative) selected.push({ sample: representative, stable: false });
|
|
206
332
|
}
|
|
207
|
-
} finally {
|
|
208
|
-
try { fs.rmSync(tempDir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
209
333
|
}
|
|
210
334
|
|
|
335
|
+
selected.sort((a, b) => a.sample.time - b.sample.time);
|
|
211
336
|
const distinct = [];
|
|
212
337
|
for (const item of selected) {
|
|
213
|
-
|
|
338
|
+
const minGap = Math.max(0.3, step * 1.5);
|
|
339
|
+
const clash = distinct.findIndex((other) => Math.abs(other.sample.time - item.sample.time) < minGap);
|
|
340
|
+
if (clash >= 0) {
|
|
341
|
+
// A stable-state frame is a better representative than a coverage
|
|
342
|
+
// filler that happened to land on the transition next to it.
|
|
343
|
+
if (item.stable && !distinct[clash].stable) distinct[clash] = item;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
214
346
|
distinct.push(item);
|
|
215
347
|
}
|
|
216
|
-
|
|
348
|
+
distinct.sort((a, b) => a.sample.time - b.sample.time);
|
|
349
|
+
const limit = Math.max(1, Number(maxFrames) || 24);
|
|
350
|
+
if (distinct.length <= limit) {
|
|
351
|
+
return distinct.map((item) => Math.round(item.sample.time * 100) / 100);
|
|
352
|
+
}
|
|
353
|
+
const picked = [];
|
|
354
|
+
for (let index = 0; index < limit; index += 1) {
|
|
355
|
+
const sourceIndex = Math.round((index * (distinct.length - 1)) / (limit - 1));
|
|
356
|
+
picked.push(distinct[sourceIndex]);
|
|
357
|
+
}
|
|
358
|
+
return [...new Set(picked.map((item) => Math.round(item.sample.time * 100) / 100))].sort((a, b) => a - b);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Dense sample + stable-state selection + frame extraction. Returns the
|
|
363
|
+
* selected frames along with the sampling metadata for note.json/video.json.
|
|
364
|
+
*/
|
|
365
|
+
function extractDenseFramesAt(file, outDir, prefix, {
|
|
366
|
+
duration = null,
|
|
367
|
+
interval = null,
|
|
368
|
+
sceneTimes = [],
|
|
369
|
+
maxFrames = 24,
|
|
370
|
+
timeout = 240000,
|
|
371
|
+
extractTimeout = 60000,
|
|
372
|
+
} = {}) {
|
|
373
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
374
|
+
const total = Number.isFinite(Number(duration)) && Number(duration) > 0
|
|
375
|
+
? Number(duration)
|
|
376
|
+
: probeDuration(file);
|
|
377
|
+
if (!total) return { frames: [], times: [], interval: null, sample_count: 0 };
|
|
378
|
+
const step = Number.isFinite(Number(interval)) && Number(interval) > 0
|
|
379
|
+
? Number(interval)
|
|
380
|
+
: denseSampleInterval(total);
|
|
381
|
+
const analysis = analyzeDenseFrames(file, { duration: total, interval: step, timeout });
|
|
382
|
+
const fallback = fixedShortTimes(total, Math.min(8, Math.max(2, Number(maxFrames) || 8)));
|
|
383
|
+
const times = analysis.samples.length
|
|
384
|
+
? selectDenseFrameTimes(analysis.samples, { interval: step, sceneTimes, maxFrames })
|
|
385
|
+
: fallback;
|
|
386
|
+
const frames = extractFramesAt(file, outDir, prefix, times, { timeout: extractTimeout });
|
|
387
|
+
return {
|
|
388
|
+
frames,
|
|
389
|
+
times: frames.map((frame) => frame.time),
|
|
390
|
+
interval: step,
|
|
391
|
+
sample_count: analysis.samples.length,
|
|
392
|
+
};
|
|
217
393
|
}
|
|
218
394
|
|
|
219
395
|
module.exports = {
|
|
@@ -223,6 +399,8 @@ module.exports = {
|
|
|
223
399
|
fixedVideoTimes,
|
|
224
400
|
mergeFrameTimes,
|
|
225
401
|
extractFramesAt,
|
|
226
|
-
|
|
227
|
-
|
|
402
|
+
denseSampleInterval,
|
|
403
|
+
analyzeDenseFrames,
|
|
404
|
+
selectDenseFrameTimes,
|
|
405
|
+
extractDenseFramesAt,
|
|
228
406
|
};
|