@lumy-pack/scene-sieve 0.0.14 → 0.1.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.
@@ -1,1588 +1,1491 @@
1
- // src/core/pipeline-worker.ts
2
- import { parentPort, workerData } from "worker_threads";
3
-
4
- // src/core/orchestrator.ts
5
- import { randomUUID as randomUUID2 } from "crypto";
6
- import { filter as filter6 } from "@winglet/common-utils";
7
-
8
- // src/utils/logger.ts
1
+ import { createRequire } from "node:module";
2
+ import { parentPort, workerData } from "node:worker_threads";
3
+ import { randomUUID } from "node:crypto";
4
+ import { filter, map } from "@winglet/common-utils";
9
5
  import pc from "picocolors";
10
- var debugMode = false;
11
- var jsonMode = false;
6
+ import sharp from "sharp";
7
+ import { homedir, tmpdir } from "node:os";
8
+ import { basename, extname, join, resolve } from "node:path";
9
+ import { mkdir, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
10
+ import { path } from "@ffprobe-installer/ffprobe";
11
+ import { execa } from "execa";
12
+ import ffmpegPath from "ffmpeg-static";
13
+
14
+ //#region src/utils/logger.ts
15
+ let debugMode = false;
16
+ let jsonMode = false;
12
17
  function setDebugMode(enabled) {
13
- debugMode = enabled;
18
+ debugMode = enabled;
14
19
  }
15
20
  function timestamp() {
16
- return (/* @__PURE__ */ new Date()).toLocaleTimeString("en-US", { hour12: false });
21
+ return (/* @__PURE__ */ new Date()).toLocaleTimeString("en-US", { hour12: false });
17
22
  }
18
- var logger = {
19
- info(message) {
20
- if (jsonMode) {
21
- process.stderr.write(`${pc.blue("info")} ${message}
22
- `);
23
- } else {
24
- console.log(`${pc.blue("info")} ${message}`);
25
- }
26
- },
27
- success(message) {
28
- if (jsonMode) {
29
- process.stderr.write(`${pc.green("done")} ${message}
30
- `);
31
- } else {
32
- console.log(`
33
- ${pc.green("done")} ${message}`);
34
- }
35
- },
36
- warn(message) {
37
- console.warn(`${pc.yellow("warn")} ${message}`);
38
- },
39
- error(message) {
40
- console.error(`${pc.red("error")} ${message}`);
41
- },
42
- debug(message) {
43
- if (debugMode) {
44
- if (jsonMode) {
45
- process.stderr.write(`${pc.gray(`[${timestamp()}] debug`)} ${message}
46
- `);
47
- } else {
48
- console.log(`${pc.gray(`[${timestamp()}] debug`)} ${message}`);
49
- }
50
- }
51
- }
23
+ const logger = {
24
+ info(message) {
25
+ if (jsonMode) process.stderr.write(`${pc.blue("info")} ${message}\n`);
26
+ else console.log(`${pc.blue("info")} ${message}`);
27
+ },
28
+ success(message) {
29
+ if (jsonMode) process.stderr.write(`${pc.green("done")} ${message}\n`);
30
+ else console.log(`\n${pc.green("done")} ${message}`);
31
+ },
32
+ warn(message) {
33
+ console.warn(`${pc.yellow("warn")} ${message}`);
34
+ },
35
+ error(message) {
36
+ console.error(`${pc.red("error")} ${message}`);
37
+ },
38
+ debug(message) {
39
+ if (debugMode) if (jsonMode) process.stderr.write(`${pc.gray(`[${timestamp()}] debug`)} ${message}\n`);
40
+ else console.log(`${pc.gray(`[${timestamp()}] debug`)} ${message}`);
41
+ }
52
42
  };
53
43
 
54
- // src/core/analyzer.ts
55
- import { createRequire } from "module";
56
- import { filter, map } from "@winglet/common-utils";
57
- import sharp from "sharp";
58
-
59
- // src/constants.ts
60
- import { tmpdir } from "os";
61
- import { join } from "path";
62
- var APP_NAME = "scene-sieve";
63
- var DEFAULT_COUNT = 20;
64
- var DEFAULT_THRESHOLD = 0.5;
65
- var DEFAULT_FPS = 5;
66
- var DEFAULT_SCALE = 720;
67
- var DEFAULT_QUALITY = 80;
68
- var DEFAULT_MAX_FRAMES = 300;
69
- var NORMALIZATION_LOGISTIC_K = 3;
70
- var NORMALIZATION_ALPHA = 0.4;
71
- var NORMALIZATION_MAD_COEFFICIENT = 1.4826;
72
- var NORMALIZATION_MIN_SAMPLE_SIZE = 10;
73
- var WORKSPACE_PREFIX = `${APP_NAME}-`;
74
- var TEMP_BASE_DIR = tmpdir();
75
- var FRAME_OUTPUT_EXTENSION = ".jpg";
76
- var FRAME_FILENAME_PATTERN = "frame_%06d.jpg";
77
- var OPENCV_BATCH_SIZE = 10;
78
- var DBSCAN_ALPHA = 0.03;
79
- var DBSCAN_MIN_PTS = 4;
80
- var IOU_THRESHOLD = 0.9;
81
- var DECAY_LAMBDA = 0.95;
82
- var ANIMATION_FRAME_THRESHOLD = 5;
83
- var MATCH_DISTANCE_THRESHOLD = 0.25;
84
- var PIXELDIFF_GAUSSIAN_KERNEL = 3;
85
- var PIXELDIFF_BINARY_THRESHOLD = 30;
86
- var PIXELDIFF_CONTOUR_MIN_AREA = 100;
87
- var PIXELDIFF_SAMPLE_SPACING = 8;
88
- var DEFAULT_MAX_SEGMENT_DURATION = 300;
89
- var DEFAULT_SEGMENT_CONCURRENCY = 2;
44
+ //#endregion
45
+ //#region src/constants.ts
46
+ const APP_NAME = "scene-sieve";
47
+ const DEFAULT_THRESHOLD = .5;
48
+ const NORMALIZATION_ALPHA = .4;
49
+ const NORMALIZATION_MAD_COEFFICIENT = 1.4826;
50
+ const WORKSPACE_PREFIX = `${APP_NAME}-`;
51
+ const TEMP_BASE_DIR = tmpdir();
52
+ const FRAME_OUTPUT_EXTENSION = ".jpg";
53
+ const FRAME_FILENAME_PATTERN = "frame_%06d.jpg";
54
+ const DBSCAN_ALPHA = .03;
55
+ const IOU_THRESHOLD = .9;
56
+ const DECAY_LAMBDA = .95;
57
+ const MATCH_DISTANCE_THRESHOLD = .25;
90
58
  function getTempWorkspaceDir(sessionId) {
91
- return join(TEMP_BASE_DIR, `${WORKSPACE_PREFIX}${sessionId}`);
59
+ return join(TEMP_BASE_DIR, `${WORKSPACE_PREFIX}${sessionId}`);
92
60
  }
93
61
 
94
- // src/core/dbscan.ts
95
- var UNVISITED = -2;
96
- var NOISE = -1;
62
+ //#endregion
63
+ //#region src/core/dbscan.ts
64
+ const UNVISITED = -2;
65
+ const NOISE = -1;
66
+ /**
67
+ * DBSCAN clustering with resolution-independent eps.
68
+ * eps = alpha * sqrt(width^2 + height^2)
69
+ */
97
70
  function dbscan(points, imageWidth, imageHeight, alpha, minPts) {
98
- if (points.length === 0) {
99
- return { labels: [], boundingBoxes: [] };
100
- }
101
- const eps = (alpha ?? DBSCAN_ALPHA) * Math.sqrt(imageWidth ** 2 + imageHeight ** 2);
102
- const epsSquared = eps * eps;
103
- const minPoints = minPts ?? DBSCAN_MIN_PTS;
104
- const labels = new Array(points.length).fill(UNVISITED);
105
- let clusterId = 0;
106
- for (let i = 0; i < points.length; i++) {
107
- if (labels[i] !== UNVISITED) continue;
108
- const neighbors = findNeighbors(points, i, epsSquared);
109
- if (neighbors.length < minPoints) {
110
- labels[i] = NOISE;
111
- continue;
112
- }
113
- labels[i] = clusterId;
114
- const seeds = [...neighbors];
115
- const seedSet = new Set(seeds);
116
- for (let si = 0; si < seeds.length; si++) {
117
- const q = seeds[si];
118
- if (labels[q] === NOISE) {
119
- labels[q] = clusterId;
120
- }
121
- if (labels[q] !== UNVISITED) continue;
122
- labels[q] = clusterId;
123
- const qNeighbors = findNeighbors(points, q, epsSquared);
124
- if (qNeighbors.length >= minPoints) {
125
- for (const n of qNeighbors) {
126
- if (!seedSet.has(n)) {
127
- seedSet.add(n);
128
- seeds.push(n);
129
- }
130
- }
131
- }
132
- }
133
- clusterId++;
134
- }
135
- const boundingBoxes = [];
136
- for (let c = 0; c < clusterId; c++) {
137
- let minX = Infinity;
138
- let minY = Infinity;
139
- let maxX = -Infinity;
140
- let maxY = -Infinity;
141
- for (let i = 0; i < points.length; i++) {
142
- if (labels[i] !== c) continue;
143
- const p = points[i];
144
- if (p.x < minX) minX = p.x;
145
- if (p.y < minY) minY = p.y;
146
- if (p.x > maxX) maxX = p.x;
147
- if (p.y > maxY) maxY = p.y;
148
- }
149
- boundingBoxes.push({
150
- x: minX,
151
- y: minY,
152
- width: maxX - minX,
153
- height: maxY - minY
154
- });
155
- }
156
- return { labels, boundingBoxes };
71
+ if (points.length === 0) return {
72
+ labels: [],
73
+ boundingBoxes: []
74
+ };
75
+ const eps = (alpha ?? .03) * Math.sqrt(imageWidth ** 2 + imageHeight ** 2);
76
+ const epsSquared = eps * eps;
77
+ const minPoints = minPts ?? 4;
78
+ const labels = new Array(points.length).fill(UNVISITED);
79
+ let clusterId = 0;
80
+ for (let i = 0; i < points.length; i++) {
81
+ if (labels[i] !== UNVISITED) continue;
82
+ const neighbors = findNeighbors(points, i, epsSquared);
83
+ if (neighbors.length < minPoints) {
84
+ labels[i] = NOISE;
85
+ continue;
86
+ }
87
+ labels[i] = clusterId;
88
+ const seeds = [...neighbors];
89
+ const seedSet = new Set(seeds);
90
+ for (let si = 0; si < seeds.length; si++) {
91
+ const q = seeds[si];
92
+ if (labels[q] === NOISE) labels[q] = clusterId;
93
+ if (labels[q] !== UNVISITED) continue;
94
+ labels[q] = clusterId;
95
+ const qNeighbors = findNeighbors(points, q, epsSquared);
96
+ if (qNeighbors.length >= minPoints) {
97
+ for (const n of qNeighbors) if (!seedSet.has(n)) {
98
+ seedSet.add(n);
99
+ seeds.push(n);
100
+ }
101
+ }
102
+ }
103
+ clusterId++;
104
+ }
105
+ const boundingBoxes = [];
106
+ for (let c = 0; c < clusterId; c++) {
107
+ let minX = Infinity;
108
+ let minY = Infinity;
109
+ let maxX = -Infinity;
110
+ let maxY = -Infinity;
111
+ for (let i = 0; i < points.length; i++) {
112
+ if (labels[i] !== c) continue;
113
+ const p = points[i];
114
+ if (p.x < minX) minX = p.x;
115
+ if (p.y < minY) minY = p.y;
116
+ if (p.x > maxX) maxX = p.x;
117
+ if (p.y > maxY) maxY = p.y;
118
+ }
119
+ boundingBoxes.push({
120
+ x: minX,
121
+ y: minY,
122
+ width: maxX - minX,
123
+ height: maxY - minY
124
+ });
125
+ }
126
+ return {
127
+ labels,
128
+ boundingBoxes
129
+ };
157
130
  }
158
131
  function findNeighbors(points, idx, epsSquared) {
159
- const p = points[idx];
160
- const neighbors = [];
161
- for (let i = 0; i < points.length; i++) {
162
- if (i === idx) continue;
163
- const q = points[i];
164
- const distSq = (p.x - q.x) ** 2 + (p.y - q.y) ** 2;
165
- if (distSq <= epsSquared) {
166
- neighbors.push(i);
167
- }
168
- }
169
- return neighbors;
132
+ const p = points[idx];
133
+ const neighbors = [];
134
+ for (let i = 0; i < points.length; i++) {
135
+ if (i === idx) continue;
136
+ const q = points[i];
137
+ if ((p.x - q.x) ** 2 + (p.y - q.y) ** 2 <= epsSquared) neighbors.push(i);
138
+ }
139
+ return neighbors;
170
140
  }
171
141
 
172
- // src/core/analyzer.ts
173
- var OPENCV_INIT_TIMEOUT_MS = 3e4;
174
- var require2 = createRequire(import.meta.url);
175
- var cvReady = null;
142
+ //#endregion
143
+ //#region src/core/analyzer.ts
144
+ const OPENCV_INIT_TIMEOUT_MS = 3e4;
145
+ const require = createRequire(import.meta.url);
146
+ let cvReady = null;
176
147
  async function ensureOpenCV() {
177
- if (!cvReady) {
178
- cvReady = (async () => {
179
- const cvObj = require2("@techstark/opencv-js");
180
- delete cvObj.then;
181
- if (cvObj.Mat) return cvObj;
182
- return new Promise((resolve2, reject) => {
183
- const timeout = setTimeout(() => {
184
- reject(new Error("OpenCV WASM initialization timed out after 30s"));
185
- }, OPENCV_INIT_TIMEOUT_MS);
186
- cvObj.onRuntimeInitialized = () => {
187
- clearTimeout(timeout);
188
- resolve2(cvObj);
189
- };
190
- });
191
- })();
192
- }
193
- return cvReady;
148
+ if (!cvReady) cvReady = (async () => {
149
+ const cvObj = require("@techstark/opencv-js");
150
+ delete cvObj.then;
151
+ if (cvObj.Mat) return cvObj;
152
+ return new Promise((resolve, reject) => {
153
+ const timeout = setTimeout(() => {
154
+ reject(/* @__PURE__ */ new Error("OpenCV WASM initialization timed out after 30s"));
155
+ }, OPENCV_INIT_TIMEOUT_MS);
156
+ cvObj.onRuntimeInitialized = () => {
157
+ clearTimeout(timeout);
158
+ resolve(cvObj);
159
+ };
160
+ });
161
+ })();
162
+ return cvReady;
194
163
  }
195
164
  async function preprocessFrame(framePath, scale) {
196
- const { data, info } = await sharp(framePath).resize({ width: scale, withoutEnlargement: true }).grayscale().blur(1).raw().toBuffer({ resolveWithObject: true });
197
- return {
198
- data: new Uint8Array(data.buffer, data.byteOffset, data.byteLength),
199
- width: info.width,
200
- height: info.height
201
- };
165
+ const { data, info } = await sharp(framePath).resize({
166
+ width: scale,
167
+ withoutEnlargement: true
168
+ }).grayscale().blur(1).raw().toBuffer({ resolveWithObject: true });
169
+ return {
170
+ data: new Uint8Array(data.buffer, data.byteOffset, data.byteLength),
171
+ width: info.width,
172
+ height: info.height
173
+ };
202
174
  }
203
175
  function computeIoU(a, b) {
204
- const ix1 = Math.max(a.x, b.x);
205
- const iy1 = Math.max(a.y, b.y);
206
- const ix2 = Math.min(a.x + a.width, b.x + b.width);
207
- const iy2 = Math.min(a.y + a.height, b.y + b.height);
208
- const iw = Math.max(0, ix2 - ix1);
209
- const ih = Math.max(0, iy2 - iy1);
210
- const intersection = iw * ih;
211
- if (intersection === 0) return 0;
212
- const aArea = a.width * a.height;
213
- const bArea = b.width * b.height;
214
- const union = aArea + bArea - intersection;
215
- return union === 0 ? 0 : intersection / union;
176
+ const ix1 = Math.max(a.x, b.x);
177
+ const iy1 = Math.max(a.y, b.y);
178
+ const ix2 = Math.min(a.x + a.width, b.x + b.width);
179
+ const iy2 = Math.min(a.y + a.height, b.y + b.height);
180
+ const intersection = Math.max(0, ix2 - ix1) * Math.max(0, iy2 - iy1);
181
+ if (intersection === 0) return 0;
182
+ const union = a.width * a.height + b.width * b.height - intersection;
183
+ return union === 0 ? 0 : intersection / union;
216
184
  }
217
185
  var IoUTracker = class {
218
- constructor(fps = DEFAULT_FPS, iouThreshold = IOU_THRESHOLD, animationThreshold = ANIMATION_FRAME_THRESHOLD) {
219
- this.fps = fps;
220
- this.iouThreshold = iouThreshold;
221
- this.animationThreshold = animationThreshold;
222
- }
223
- regions = [];
224
- extractedAnimations = [];
225
- update(boxes, pairIndex) {
226
- const animationIndices = /* @__PURE__ */ new Set();
227
- const matched = /* @__PURE__ */ new Set();
228
- for (let bi = 0; bi < boxes.length; bi++) {
229
- const box = boxes[bi];
230
- let bestIoU = 0;
231
- let bestRegionIdx = -1;
232
- for (let ri = 0; ri < this.regions.length; ri++) {
233
- if (matched.has(ri)) continue;
234
- const iou = computeIoU(box, this.regions[ri].box);
235
- if (iou > bestIoU) {
236
- bestIoU = iou;
237
- bestRegionIdx = ri;
238
- }
239
- }
240
- if (bestIoU > this.iouThreshold && bestRegionIdx !== -1) {
241
- const region = this.regions[bestRegionIdx];
242
- const gap = pairIndex - region.lastSeen;
243
- region.box = box;
244
- region.consecutiveCount++;
245
- region.lastSeen = pairIndex;
246
- region.weight *= Math.pow(DECAY_LAMBDA, gap);
247
- matched.add(bestRegionIdx);
248
- if (region.consecutiveCount >= this.animationThreshold) {
249
- animationIndices.add(bi);
250
- }
251
- } else {
252
- this.regions.push({
253
- box,
254
- consecutiveCount: 1,
255
- firstSeen: pairIndex,
256
- lastSeen: pairIndex,
257
- weight: 1
258
- });
259
- }
260
- }
261
- for (let ri = 0; ri < this.regions.length; ri++) {
262
- if (!matched.has(ri)) {
263
- const gap = pairIndex - this.regions[ri].lastSeen;
264
- this.regions[ri].weight *= Math.pow(DECAY_LAMBDA, gap);
265
- }
266
- }
267
- for (let i = 0; i < this.regions.length; i++) {
268
- const region = this.regions[i];
269
- if (region.weight <= 0.01 && !matched.has(i)) {
270
- this.collectAnimation(region);
271
- }
272
- }
273
- this.regions = filter(
274
- this.regions,
275
- (r, i) => r.weight > 0.01 || matched.has(i)
276
- );
277
- return animationIndices;
278
- }
279
- collectAnimation(region) {
280
- if (region.consecutiveCount >= this.animationThreshold) {
281
- const durationMs = region.consecutiveCount / this.fps * 1e3;
282
- this.extractedAnimations.push({
283
- type: "loading_spinner",
284
- // 기본값으로 loading_spinner 사용
285
- boundingBox: region.box,
286
- startFrameId: region.firstSeen,
287
- endFrameId: region.lastSeen,
288
- durationMs
289
- });
290
- }
291
- }
292
- flushAndGetAnimations() {
293
- for (const region of this.regions) {
294
- this.collectAnimation(region);
295
- }
296
- this.regions = [];
297
- return this.extractedAnimations;
298
- }
299
- getAnimationWeight(boxIndex, boxes) {
300
- if (boxIndex >= boxes.length) return 0;
301
- const box = boxes[boxIndex];
302
- let maxWeight = 0;
303
- for (const region of this.regions) {
304
- if (region.consecutiveCount >= this.animationThreshold) {
305
- const iou = computeIoU(box, region.box);
306
- if (iou > this.iouThreshold) {
307
- maxWeight = Math.max(maxWeight, region.weight);
308
- }
309
- }
310
- }
311
- return maxWeight;
312
- }
186
+ fps;
187
+ iouThreshold;
188
+ animationThreshold;
189
+ regions = [];
190
+ extractedAnimations = [];
191
+ constructor(fps = 5, iouThreshold = IOU_THRESHOLD, animationThreshold = 5) {
192
+ this.fps = fps;
193
+ this.iouThreshold = iouThreshold;
194
+ this.animationThreshold = animationThreshold;
195
+ }
196
+ update(boxes, pairIndex) {
197
+ const animationIndices = /* @__PURE__ */ new Set();
198
+ const matched = /* @__PURE__ */ new Set();
199
+ for (let bi = 0; bi < boxes.length; bi++) {
200
+ const box = boxes[bi];
201
+ let bestIoU = 0;
202
+ let bestRegionIdx = -1;
203
+ for (let ri = 0; ri < this.regions.length; ri++) {
204
+ if (matched.has(ri)) continue;
205
+ const iou = computeIoU(box, this.regions[ri].box);
206
+ if (iou > bestIoU) {
207
+ bestIoU = iou;
208
+ bestRegionIdx = ri;
209
+ }
210
+ }
211
+ if (bestIoU > this.iouThreshold && bestRegionIdx !== -1) {
212
+ const region = this.regions[bestRegionIdx];
213
+ const gap = pairIndex - region.lastSeen;
214
+ region.box = box;
215
+ region.consecutiveCount++;
216
+ region.lastSeen = pairIndex;
217
+ region.weight *= Math.pow(DECAY_LAMBDA, gap);
218
+ matched.add(bestRegionIdx);
219
+ if (region.consecutiveCount >= this.animationThreshold) animationIndices.add(bi);
220
+ } else this.regions.push({
221
+ box,
222
+ consecutiveCount: 1,
223
+ firstSeen: pairIndex,
224
+ lastSeen: pairIndex,
225
+ weight: 1
226
+ });
227
+ }
228
+ for (let ri = 0; ri < this.regions.length; ri++) if (!matched.has(ri)) {
229
+ const gap = pairIndex - this.regions[ri].lastSeen;
230
+ this.regions[ri].weight *= Math.pow(DECAY_LAMBDA, gap);
231
+ }
232
+ for (let i = 0; i < this.regions.length; i++) {
233
+ const region = this.regions[i];
234
+ if (region.weight <= .01 && !matched.has(i)) this.collectAnimation(region);
235
+ }
236
+ this.regions = filter(this.regions, (r, i) => r.weight > .01 || matched.has(i));
237
+ return animationIndices;
238
+ }
239
+ collectAnimation(region) {
240
+ if (region.consecutiveCount >= this.animationThreshold) {
241
+ const durationMs = region.consecutiveCount / this.fps * 1e3;
242
+ this.extractedAnimations.push({
243
+ type: "loading_spinner",
244
+ boundingBox: region.box,
245
+ startFrameId: region.firstSeen,
246
+ endFrameId: region.lastSeen,
247
+ durationMs
248
+ });
249
+ }
250
+ }
251
+ flushAndGetAnimations() {
252
+ for (const region of this.regions) this.collectAnimation(region);
253
+ this.regions = [];
254
+ return this.extractedAnimations;
255
+ }
256
+ getAnimationWeight(boxIndex, boxes) {
257
+ if (boxIndex >= boxes.length) return 0;
258
+ const box = boxes[boxIndex];
259
+ let maxWeight = 0;
260
+ for (const region of this.regions) if (region.consecutiveCount >= this.animationThreshold) {
261
+ if (computeIoU(box, region.box) > this.iouThreshold) maxWeight = Math.max(maxWeight, region.weight);
262
+ }
263
+ return maxWeight;
264
+ }
313
265
  };
314
266
  async function computeAKAZEDiff(cvLib, frame1, frame2) {
315
- const cv = cvLib;
316
- const mat1 = new cv.Mat(frame1.height, frame1.width, cv.CV_8UC1);
317
- mat1.data.set(frame1.data);
318
- const mat2 = new cv.Mat(frame2.height, frame2.width, cv.CV_8UC1);
319
- mat2.data.set(frame2.data);
320
- const kp1 = new cvLib.KeyPointVector();
321
- const kp2 = new cvLib.KeyPointVector();
322
- const desc1 = new cvLib.Mat();
323
- const desc2 = new cvLib.Mat();
324
- const mask1 = new cvLib.Mat();
325
- const mask2 = new cvLib.Mat();
326
- const akaze = new cvLib.AKAZE();
327
- let matches = null;
328
- try {
329
- akaze.detectAndCompute(mat1, mask1, kp1, desc1);
330
- akaze.detectAndCompute(mat2, mask2, kp2, desc2);
331
- const matchedKp1Indices = /* @__PURE__ */ new Set();
332
- const matchedKp2Indices = /* @__PURE__ */ new Set();
333
- if (desc1.rows > 0 && desc2.rows > 0) {
334
- const matcher = new cvLib.BFMatcher(cvLib.NORM_HAMMING, false);
335
- try {
336
- matches = new cvLib.DMatchVectorVector();
337
- matcher.knnMatch(desc1, desc2, matches, 2);
338
- for (let i = 0; i < matches.size(); i++) {
339
- const pair = matches.get(i);
340
- if (pair.size() < 2) continue;
341
- const m0 = pair.get(0);
342
- const m1 = pair.get(1);
343
- if (m0.distance < MATCH_DISTANCE_THRESHOLD * m1.distance) {
344
- matchedKp1Indices.add(m0.queryIdx);
345
- matchedKp2Indices.add(m0.trainIdx);
346
- }
347
- }
348
- } finally {
349
- matcher.delete();
350
- }
351
- }
352
- const sNew = [];
353
- for (let i = 0; i < kp2.size(); i++) {
354
- if (!matchedKp2Indices.has(i)) {
355
- const pt = kp2.get(i).pt;
356
- sNew.push({ x: pt.x, y: pt.y });
357
- }
358
- }
359
- const sLoss = [];
360
- for (let i = 0; i < kp1.size(); i++) {
361
- if (!matchedKp1Indices.has(i)) {
362
- const pt = kp1.get(i).pt;
363
- sLoss.push({ x: pt.x, y: pt.y });
364
- }
365
- }
366
- return { sNew, sLoss };
367
- } finally {
368
- mat1.delete();
369
- mat2.delete();
370
- kp1.delete();
371
- kp2.delete();
372
- desc1.delete();
373
- desc2.delete();
374
- mask1.delete();
375
- mask2.delete();
376
- akaze.delete();
377
- if (matches) matches.delete();
378
- }
267
+ const cv = cvLib;
268
+ const mat1 = new cv.Mat(frame1.height, frame1.width, cv.CV_8UC1);
269
+ mat1.data.set(frame1.data);
270
+ const mat2 = new cv.Mat(frame2.height, frame2.width, cv.CV_8UC1);
271
+ mat2.data.set(frame2.data);
272
+ const kp1 = new cvLib.KeyPointVector();
273
+ const kp2 = new cvLib.KeyPointVector();
274
+ const desc1 = new cvLib.Mat();
275
+ const desc2 = new cvLib.Mat();
276
+ const mask1 = new cvLib.Mat();
277
+ const mask2 = new cvLib.Mat();
278
+ const akaze = new cvLib.AKAZE();
279
+ let matches = null;
280
+ try {
281
+ akaze.detectAndCompute(mat1, mask1, kp1, desc1);
282
+ akaze.detectAndCompute(mat2, mask2, kp2, desc2);
283
+ const matchedKp1Indices = /* @__PURE__ */ new Set();
284
+ const matchedKp2Indices = /* @__PURE__ */ new Set();
285
+ if (desc1.rows > 0 && desc2.rows > 0) {
286
+ const matcher = new cvLib.BFMatcher(cvLib.NORM_HAMMING, false);
287
+ try {
288
+ matches = new cvLib.DMatchVectorVector();
289
+ matcher.knnMatch(desc1, desc2, matches, 2);
290
+ for (let i = 0; i < matches.size(); i++) {
291
+ const pair = matches.get(i);
292
+ if (pair.size() < 2) continue;
293
+ const m0 = pair.get(0);
294
+ const m1 = pair.get(1);
295
+ if (m0.distance < .25 * m1.distance) {
296
+ matchedKp1Indices.add(m0.queryIdx);
297
+ matchedKp2Indices.add(m0.trainIdx);
298
+ }
299
+ }
300
+ } finally {
301
+ matcher.delete();
302
+ }
303
+ }
304
+ const sNew = [];
305
+ for (let i = 0; i < kp2.size(); i++) if (!matchedKp2Indices.has(i)) {
306
+ const pt = kp2.get(i).pt;
307
+ sNew.push({
308
+ x: pt.x,
309
+ y: pt.y
310
+ });
311
+ }
312
+ const sLoss = [];
313
+ for (let i = 0; i < kp1.size(); i++) if (!matchedKp1Indices.has(i)) {
314
+ const pt = kp1.get(i).pt;
315
+ sLoss.push({
316
+ x: pt.x,
317
+ y: pt.y
318
+ });
319
+ }
320
+ return {
321
+ sNew,
322
+ sLoss
323
+ };
324
+ } finally {
325
+ mat1.delete();
326
+ mat2.delete();
327
+ kp1.delete();
328
+ kp2.delete();
329
+ desc1.delete();
330
+ desc2.delete();
331
+ mask1.delete();
332
+ mask2.delete();
333
+ akaze.delete();
334
+ if (matches) matches.delete();
335
+ }
379
336
  }
337
+ /**
338
+ * Pixel-level difference fallback for AKAZE blind spots.
339
+ *
340
+ * When AKAZE produces sparse results (typical for UI screen recordings
341
+ * where form fields, dropdowns, or overlays change), this function
342
+ * detects changed regions via cv.absdiff and generates synthetic
343
+ * Point2D[] that feed into the existing DBSCAN → IoU → G(t) pipeline.
344
+ *
345
+ * Algorithm:
346
+ * 1. absdiff(frame1, frame2) → grayscale difference
347
+ * 2. GaussianBlur → reduce JPEG compression noise
348
+ * 3. threshold → binary mask of significant changes
349
+ * 4. findContours → bounding rects of changed regions
350
+ * 5. Grid sampling within each bounding rect → Point2D[]
351
+ */
380
352
  function computePixelDiff(cvLib, frame1, frame2) {
381
- const cv = cvLib;
382
- const mat1 = new cv.Mat(frame1.height, frame1.width, cv.CV_8UC1);
383
- const mat2 = new cv.Mat(frame2.height, frame2.width, cv.CV_8UC1);
384
- const diff = new cv.Mat();
385
- const blurred = new cv.Mat();
386
- const binary = new cv.Mat();
387
- const contours = new cv.MatVector();
388
- const hierarchy = new cv.Mat();
389
- try {
390
- mat1.data.set(frame1.data);
391
- mat2.data.set(frame2.data);
392
- cv.absdiff(mat1, mat2, diff);
393
- const ksize = new cv.Size(
394
- PIXELDIFF_GAUSSIAN_KERNEL,
395
- PIXELDIFF_GAUSSIAN_KERNEL
396
- );
397
- cv.GaussianBlur(diff, blurred, ksize, 0);
398
- cv.threshold(
399
- blurred,
400
- binary,
401
- PIXELDIFF_BINARY_THRESHOLD,
402
- 255,
403
- cv.THRESH_BINARY
404
- );
405
- cv.findContours(
406
- binary,
407
- contours,
408
- hierarchy,
409
- cv.RETR_EXTERNAL,
410
- cv.CHAIN_APPROX_SIMPLE
411
- );
412
- const points = [];
413
- for (let c = 0; c < contours.size(); c++) {
414
- const contour = contours.get(c);
415
- const rect = cv.boundingRect(contour);
416
- if (rect.width * rect.height < PIXELDIFF_CONTOUR_MIN_AREA) continue;
417
- for (let y = rect.y; y < rect.y + rect.height; y += PIXELDIFF_SAMPLE_SPACING) {
418
- for (let x = rect.x; x < rect.x + rect.width; x += PIXELDIFF_SAMPLE_SPACING) {
419
- points.push({ x, y });
420
- }
421
- }
422
- }
423
- return points;
424
- } finally {
425
- mat1.delete();
426
- mat2.delete();
427
- diff.delete();
428
- blurred.delete();
429
- binary.delete();
430
- contours.delete();
431
- hierarchy.delete();
432
- }
353
+ const cv = cvLib;
354
+ const mat1 = new cv.Mat(frame1.height, frame1.width, cv.CV_8UC1);
355
+ const mat2 = new cv.Mat(frame2.height, frame2.width, cv.CV_8UC1);
356
+ const diff = new cv.Mat();
357
+ const blurred = new cv.Mat();
358
+ const binary = new cv.Mat();
359
+ const contours = new cv.MatVector();
360
+ const hierarchy = new cv.Mat();
361
+ try {
362
+ mat1.data.set(frame1.data);
363
+ mat2.data.set(frame2.data);
364
+ cv.absdiff(mat1, mat2, diff);
365
+ const ksize = new cv.Size(3, 3);
366
+ cv.GaussianBlur(diff, blurred, ksize, 0);
367
+ cv.threshold(blurred, binary, 30, 255, cv.THRESH_BINARY);
368
+ cv.findContours(binary, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE);
369
+ const points = [];
370
+ for (let c = 0; c < contours.size(); c++) {
371
+ const contour = contours.get(c);
372
+ const rect = cv.boundingRect(contour);
373
+ if (rect.width * rect.height < 100) continue;
374
+ for (let y = rect.y; y < rect.y + rect.height; y += 8) for (let x = rect.x; x < rect.x + rect.width; x += 8) points.push({
375
+ x,
376
+ y
377
+ });
378
+ }
379
+ return points;
380
+ } finally {
381
+ mat1.delete();
382
+ mat2.delete();
383
+ diff.delete();
384
+ blurred.delete();
385
+ binary.delete();
386
+ contours.delete();
387
+ hierarchy.delete();
388
+ }
433
389
  }
434
390
  function computeInformationGain(clusters, clusterPoints, imageArea, animationIndices, animationWeights) {
435
- if (clusters.length === 0) return 0;
436
- let gain = 0;
437
- for (let i = 0; i < clusters.length; i++) {
438
- const box = clusters[i];
439
- const clusterArea = box.width * box.height;
440
- if (clusterArea <= 0) continue;
441
- const normalizedArea = clusterArea / imageArea;
442
- const featureDensity = clusterPoints[i] / clusterArea;
443
- let contribution = normalizedArea * featureDensity;
444
- if (animationIndices.has(i)) {
445
- const animWeight = animationWeights[i] ?? 0;
446
- contribution *= 1 - animWeight;
447
- }
448
- gain += contribution;
449
- }
450
- return gain;
391
+ if (clusters.length === 0) return 0;
392
+ let gain = 0;
393
+ for (let i = 0; i < clusters.length; i++) {
394
+ const box = clusters[i];
395
+ const clusterArea = box.width * box.height;
396
+ if (clusterArea <= 0) continue;
397
+ let contribution = clusterArea / imageArea * (clusterPoints[i] / clusterArea);
398
+ if (animationIndices.has(i)) {
399
+ const animWeight = animationWeights[i] ?? 0;
400
+ contribution *= 1 - animWeight;
401
+ }
402
+ gain += contribution;
403
+ }
404
+ return gain;
451
405
  }
452
406
  async function analyzeBatch(cvLib, frames, scale, tracker, pairOffset) {
453
- const edges = [];
454
- const preprocessed = await Promise.all(
455
- map(frames, (f) => preprocessFrame(f.extractPath, scale))
456
- );
457
- const imageWidth = preprocessed[0]?.width ?? scale;
458
- const imageHeight = preprocessed[0]?.height ?? Math.round(scale * 9 / 16);
459
- const imageArea = imageWidth * imageHeight;
460
- for (let i = 0; i < frames.length - 1; i++) {
461
- const pairIndex = pairOffset + i;
462
- try {
463
- const { sNew } = await computeAKAZEDiff(
464
- cvLib,
465
- preprocessed[i],
466
- preprocessed[i + 1]
467
- );
468
- let dbscanResult = dbscan(sNew, imageWidth, imageHeight);
469
- let clusters = dbscanResult.boundingBoxes;
470
- if (clusters.length === 0) {
471
- const pixelDiffPoints = computePixelDiff(
472
- cvLib,
473
- preprocessed[i],
474
- preprocessed[i + 1]
475
- );
476
- if (pixelDiffPoints.length > 0) {
477
- logger.debug(
478
- `Edge ${frames[i].id}->${frames[i + 1].id}: pixel-diff fallback (${pixelDiffPoints.length} points)`
479
- );
480
- dbscanResult = dbscan(
481
- pixelDiffPoints,
482
- imageWidth,
483
- imageHeight,
484
- void 0,
485
- 2
486
- );
487
- clusters = dbscanResult.boundingBoxes;
488
- }
489
- }
490
- const clusterPointCounts = new Array(clusters.length).fill(0);
491
- for (const label of dbscanResult.labels) {
492
- if (label >= 0) {
493
- clusterPointCounts[label]++;
494
- }
495
- }
496
- const animationIndices = tracker.update(clusters, pairIndex);
497
- const animationWeights = map(
498
- clusters,
499
- (_, ci) => animationIndices.has(ci) ? tracker.getAnimationWeight(ci, clusters) : 0
500
- );
501
- const score = computeInformationGain(
502
- clusters,
503
- clusterPointCounts,
504
- imageArea,
505
- animationIndices,
506
- animationWeights
507
- );
508
- logger.debug(
509
- `Edge ${frames[i].id}->${frames[i + 1].id} G(t)=${score.toFixed(6)}`
510
- );
511
- edges.push({
512
- sourceId: frames[i].id,
513
- targetId: frames[i + 1].id,
514
- score
515
- });
516
- } catch (err) {
517
- logger.debug(`Frame pair analysis failed: ${String(err)}`);
518
- edges.push({
519
- sourceId: frames[i].id,
520
- targetId: frames[i + 1].id,
521
- score: 0
522
- });
523
- }
524
- }
525
- return edges;
407
+ const edges = [];
408
+ const preprocessed = await Promise.all(map(frames, (f) => preprocessFrame(f.extractPath, scale)));
409
+ const imageWidth = preprocessed[0]?.width ?? scale;
410
+ const imageHeight = preprocessed[0]?.height ?? Math.round(scale * 9 / 16);
411
+ const imageArea = imageWidth * imageHeight;
412
+ for (let i = 0; i < frames.length - 1; i++) {
413
+ const pairIndex = pairOffset + i;
414
+ try {
415
+ const { sNew } = await computeAKAZEDiff(cvLib, preprocessed[i], preprocessed[i + 1]);
416
+ let dbscanResult = dbscan(sNew, imageWidth, imageHeight);
417
+ let clusters = dbscanResult.boundingBoxes;
418
+ if (clusters.length === 0) {
419
+ const pixelDiffPoints = computePixelDiff(cvLib, preprocessed[i], preprocessed[i + 1]);
420
+ if (pixelDiffPoints.length > 0) {
421
+ logger.debug(`Edge ${frames[i].id}->${frames[i + 1].id}: pixel-diff fallback (${pixelDiffPoints.length} points)`);
422
+ dbscanResult = dbscan(pixelDiffPoints, imageWidth, imageHeight, void 0, 2);
423
+ clusters = dbscanResult.boundingBoxes;
424
+ }
425
+ }
426
+ const clusterPointCounts = new Array(clusters.length).fill(0);
427
+ for (const label of dbscanResult.labels) if (label >= 0) clusterPointCounts[label]++;
428
+ const animationIndices = tracker.update(clusters, pairIndex);
429
+ const animationWeights = map(clusters, (_, ci) => animationIndices.has(ci) ? tracker.getAnimationWeight(ci, clusters) : 0);
430
+ const score = computeInformationGain(clusters, clusterPointCounts, imageArea, animationIndices, animationWeights);
431
+ logger.debug(`Edge ${frames[i].id}->${frames[i + 1].id} G(t)=${score.toFixed(6)}`);
432
+ edges.push({
433
+ sourceId: frames[i].id,
434
+ targetId: frames[i + 1].id,
435
+ score
436
+ });
437
+ } catch (err) {
438
+ logger.debug(`Frame pair analysis failed: ${String(err)}`);
439
+ edges.push({
440
+ sourceId: frames[i].id,
441
+ targetId: frames[i + 1].id,
442
+ score: 0
443
+ });
444
+ }
445
+ }
446
+ return edges;
526
447
  }
448
+ /**
449
+ * Analyze adjacent frame pairs to compute information gain scores (G(t)).
450
+ * Processes frames in batches for memory efficiency.
451
+ *
452
+ * Pipeline:
453
+ * 1. AKAZE Feature Set Difference
454
+ * 2. DBSCAN Spatial Clustering
455
+ * 3. Spatio-temporal IoU Tracking
456
+ * 4. G(t) Information Gain Scoring
457
+ */
527
458
  async function analyzeFrames(ctx) {
528
- const { frames } = ctx;
529
- if (frames.length < 2) return { edges: [], animations: [] };
530
- logger.debug(
531
- `Analyzing ${frames.length} frames in batches of ${OPENCV_BATCH_SIZE}`
532
- );
533
- const cvLib = await ensureOpenCV();
534
- const edges = [];
535
- const tracker = new IoUTracker(
536
- ctx.options.fps,
537
- ctx.options.iouThreshold,
538
- ctx.options.animationThreshold
539
- );
540
- const scale = ctx.options.scale;
541
- for (let i = 0; i < frames.length - 1; i += OPENCV_BATCH_SIZE) {
542
- const batchEnd = Math.min(i + OPENCV_BATCH_SIZE + 1, frames.length);
543
- const batch = frames.slice(i, batchEnd);
544
- const batchEdges = await analyzeBatch(cvLib, batch, scale, tracker, i);
545
- edges.push(...batchEdges);
546
- const progress = Math.min(
547
- 100,
548
- (i + OPENCV_BATCH_SIZE) / (frames.length - 1) * 100
549
- );
550
- ctx.emitProgress(progress);
551
- }
552
- const animations = tracker.flushAndGetAnimations();
553
- logger.debug(
554
- `Computed ${edges.length} score edges and ${animations.length} animations`
555
- );
556
- return { edges, animations };
459
+ const { frames } = ctx;
460
+ if (frames.length < 2) return {
461
+ edges: [],
462
+ animations: []
463
+ };
464
+ logger.debug(`Analyzing ${frames.length} frames in batches of ${10}`);
465
+ const cvLib = await ensureOpenCV();
466
+ const edges = [];
467
+ const tracker = new IoUTracker(ctx.options.fps, ctx.options.iouThreshold, ctx.options.animationThreshold);
468
+ const scale = ctx.options.scale;
469
+ for (let i = 0; i < frames.length - 1; i += 10) {
470
+ const batchEnd = Math.min(i + 10 + 1, frames.length);
471
+ const batchEdges = await analyzeBatch(cvLib, frames.slice(i, batchEnd), scale, tracker, i);
472
+ edges.push(...batchEdges);
473
+ const progress = Math.min(100, (i + 10) / (frames.length - 1) * 100);
474
+ ctx.emitProgress(progress);
475
+ }
476
+ const animations = tracker.flushAndGetAnimations();
477
+ logger.debug(`Computed ${edges.length} score edges and ${animations.length} animations`);
478
+ return {
479
+ edges,
480
+ animations
481
+ };
557
482
  }
558
483
 
559
- // src/core/extractor.ts
560
- import { readdir } from "fs/promises";
561
- import { join as join2 } from "path";
562
- import { path as ffprobePath } from "@ffprobe-installer/ffprobe";
563
- import { filter as filter2, map as map2 } from "@winglet/common-utils";
564
- import { execa } from "execa";
565
- import ffmpegPath from "ffmpeg-static";
566
-
567
- // src/utils/paths.ts
568
- import { mkdir, stat } from "fs/promises";
569
- import { homedir } from "os";
570
- import { basename, extname, resolve } from "path";
484
+ //#endregion
485
+ //#region src/utils/paths.ts
571
486
  async function ensureDir(dirPath) {
572
- await mkdir(dirPath, { recursive: true });
487
+ await mkdir(dirPath, { recursive: true });
573
488
  }
574
489
  async function fileExists(filePath) {
575
- try {
576
- await stat(filePath);
577
- return true;
578
- } catch {
579
- return false;
580
- }
490
+ try {
491
+ await stat(filePath);
492
+ return true;
493
+ } catch {
494
+ return false;
495
+ }
581
496
  }
497
+ /**
498
+ * Expand leading ~ to homedir. Node's path.resolve() does not expand ~,
499
+ * so paths like ~/Desktop/foo depend on process.cwd() and can produce
500
+ * different results when run from different directories.
501
+ */
582
502
  function expandTilde(p) {
583
- if (p === "~") return homedir();
584
- if (p.startsWith("~/") || p.startsWith("~\\")) {
585
- return resolve(homedir(), p.slice(2));
586
- }
587
- return p;
503
+ if (p === "~") return homedir();
504
+ if (p.startsWith("~/") || p.startsWith("~\\")) return resolve(homedir(), p.slice(2));
505
+ return p;
588
506
  }
507
+ /**
508
+ * Resolve path to absolute. Expands ~ to homedir first so that the result
509
+ * does not depend on process.cwd().
510
+ */
589
511
  function resolveAbsolute(p) {
590
- return resolve(expandTilde(p));
512
+ return resolve(expandTilde(p));
591
513
  }
514
+ /**
515
+ * Derive default output directory name from input file path.
516
+ * e.g., /path/to/video.mp4 -> /path/to/video_scenes
517
+ */
592
518
  function deriveOutputPath(inputPath) {
593
- const dir = resolve(inputPath, "..");
594
- const name = basename(inputPath, extname(inputPath));
595
- return resolve(dir, `${name}_scenes`);
519
+ return resolve(resolve(inputPath, ".."), `${basename(inputPath, extname(inputPath))}_scenes`);
596
520
  }
597
521
 
598
- // src/core/extractor.ts
522
+ //#endregion
523
+ //#region src/core/extractor.ts
524
+ /**
525
+ * Extract frames from video/GIF using FFmpeg.
526
+ * Always uses FPS-based extraction. For long videos, FPS is automatically
527
+ * reduced to stay within maxFrames budget.
528
+ */
599
529
  async function extractFrames(ctx) {
600
- const framesDir = join2(ctx.workspacePath, "frames");
601
- const { inputPath, fps, maxFrames, scale } = ctx.options;
602
- if (!inputPath) {
603
- throw new Error("inputPath is required for frame extraction");
604
- }
605
- const exists = await fileExists(inputPath);
606
- if (!exists) {
607
- throw new Error(`Input file not found: ${inputPath}`);
608
- }
609
- const metadata = await getVideoMetadata(inputPath).catch((err) => {
610
- logger.debug(`ffprobe failed: ${err.message}`);
611
- return null;
612
- });
613
- if (!metadata || !metadata.format) {
614
- throw new Error(`Could not read file metadata: ${inputPath}`);
615
- }
616
- const formatName = metadata.format.format_name ?? "";
617
- const duration = parseFloat(metadata.format.duration ?? "0");
618
- const hasVideoStream = metadata.streams?.some((s) => s.codec_type === "video") ?? false;
619
- if (!hasVideoStream) {
620
- throw new Error(
621
- `No video stream found in file: ${inputPath} (detected format: ${formatName})`
622
- );
623
- }
624
- logger.debug(
625
- `Detected format: ${formatName} (Duration: ${duration.toFixed(1)}s), path: ${inputPath}`
626
- );
627
- await ensureDir(framesDir);
628
- let effectiveFps = fps;
629
- if (duration > 0) {
630
- const fpsCap = maxFrames / duration;
631
- effectiveFps = Math.min(fps, fpsCap);
632
- effectiveFps = Math.max(0.5, effectiveFps);
633
- logger.debug(
634
- `FPS: ${fps} \u2192 effective: ${effectiveFps.toFixed(2)} (maxFrames: ${maxFrames})`
635
- );
636
- }
637
- const frames = await extractByFps(
638
- inputPath,
639
- framesDir,
640
- effectiveFps,
641
- scale,
642
- duration
643
- );
644
- ctx.emitProgress(100);
645
- logger.debug(`Extracted ${frames.length} frames`);
646
- return frames;
530
+ const framesDir = join(ctx.workspacePath, "frames");
531
+ const { inputPath, fps, maxFrames, scale } = ctx.options;
532
+ if (!inputPath) throw new Error("inputPath is required for frame extraction");
533
+ if (!await fileExists(inputPath)) throw new Error(`Input file not found: ${inputPath}`);
534
+ const metadata = await getVideoMetadata(inputPath).catch((err) => {
535
+ logger.debug(`ffprobe failed: ${err.message}`);
536
+ return null;
537
+ });
538
+ if (!metadata || !metadata.format) throw new Error(`Could not read file metadata: ${inputPath}`);
539
+ const formatName = metadata.format.format_name ?? "";
540
+ const duration = parseFloat(metadata.format.duration ?? "0");
541
+ if (!(metadata.streams?.some((s) => s.codec_type === "video") ?? false)) throw new Error(`No video stream found in file: ${inputPath} (detected format: ${formatName})`);
542
+ logger.debug(`Detected format: ${formatName} (Duration: ${duration.toFixed(1)}s), path: ${inputPath}`);
543
+ await ensureDir(framesDir);
544
+ let effectiveFps = fps;
545
+ if (duration > 0) {
546
+ const fpsCap = maxFrames / duration;
547
+ effectiveFps = Math.min(fps, fpsCap);
548
+ effectiveFps = Math.max(.5, effectiveFps);
549
+ logger.debug(`FPS: ${fps} → effective: ${effectiveFps.toFixed(2)} (maxFrames: ${maxFrames})`);
550
+ }
551
+ const frames = await extractByFps(inputPath, framesDir, effectiveFps, scale, duration);
552
+ ctx.emitProgress(100);
553
+ logger.debug(`Extracted ${frames.length} frames`);
554
+ return frames;
647
555
  }
648
556
  async function extractByFps(inputPath, outputDir, fps, scale, duration) {
649
- const outputPattern = join2(outputDir, FRAME_FILENAME_PATTERN);
650
- await execa(ffmpegPath, [
651
- "-i",
652
- inputPath,
653
- "-vf",
654
- `fps=${fps},scale=-1:${scale}`,
655
- "-q:v",
656
- "2",
657
- outputPattern
658
- ]);
659
- return buildFrameList(outputDir, duration);
557
+ const outputPattern = join(outputDir, FRAME_FILENAME_PATTERN);
558
+ await execa(ffmpegPath, [
559
+ "-i",
560
+ inputPath,
561
+ "-vf",
562
+ `fps=${fps},scale=-1:${scale}`,
563
+ "-q:v",
564
+ "2",
565
+ outputPattern
566
+ ]);
567
+ return buildFrameList(outputDir, duration);
660
568
  }
661
569
  async function getVideoMetadata(inputPath) {
662
- const { stdout } = await execa(ffprobePath, [
663
- "-v",
664
- "quiet",
665
- "-print_format",
666
- "json",
667
- "-show_format",
668
- "-show_streams",
669
- inputPath
670
- ]);
671
- return JSON.parse(stdout);
570
+ const { stdout } = await execa(path, [
571
+ "-v",
572
+ "quiet",
573
+ "-print_format",
574
+ "json",
575
+ "-show_format",
576
+ "-show_streams",
577
+ inputPath
578
+ ]);
579
+ return JSON.parse(stdout);
672
580
  }
673
581
  async function buildFrameList(framesDir, duration) {
674
- const files = await readdir(framesDir);
675
- const jpgFiles = filter2(files, (f) => f.endsWith(".jpg")).sort();
676
- if (jpgFiles.length === 0) {
677
- return [];
678
- }
679
- return map2(jpgFiles, (file, index) => ({
680
- id: index,
681
- timestamp: duration > 0 && jpgFiles.length > 1 ? duration * index / (jpgFiles.length - 1) : index,
682
- extractPath: join2(framesDir, file)
683
- }));
582
+ const jpgFiles = filter(await readdir(framesDir), (f) => f.endsWith(".jpg")).sort();
583
+ if (jpgFiles.length === 0) return [];
584
+ return map(jpgFiles, (file, index) => ({
585
+ id: index,
586
+ timestamp: duration > 0 && jpgFiles.length > 1 ? duration * index / (jpgFiles.length - 1) : index,
587
+ extractPath: join(framesDir, file)
588
+ }));
684
589
  }
590
+ /**
591
+ * Extract frames from a specific time range of a video using FFmpeg.
592
+ * Uses input seeking (-ss before -i) for fast seek + -t for duration.
593
+ *
594
+ * @param inputPath - Path to the video file
595
+ * @param outputDir - Directory to write extracted frames
596
+ * @param fps - Frames per second for extraction
597
+ * @param scale - Height scale for vision analysis
598
+ * @param startTime - Start time in seconds
599
+ * @param duration - Duration in seconds to extract
600
+ * @returns Array of FrameNode with segment-local timestamps (starting from 0)
601
+ */
685
602
  async function extractFramesForRange(inputPath, outputDir, fps, scale, startTime, duration) {
686
- const outputPattern = join2(outputDir, FRAME_FILENAME_PATTERN);
687
- await execa(ffmpegPath, [
688
- "-ss",
689
- String(startTime),
690
- "-i",
691
- inputPath,
692
- "-t",
693
- String(duration),
694
- "-vf",
695
- `fps=${fps},scale=-1:${scale}`,
696
- "-q:v",
697
- "2",
698
- outputPattern
699
- ]);
700
- return buildFrameList(outputDir, duration);
603
+ const outputPattern = join(outputDir, FRAME_FILENAME_PATTERN);
604
+ await execa(ffmpegPath, [
605
+ "-ss",
606
+ String(startTime),
607
+ "-i",
608
+ inputPath,
609
+ "-t",
610
+ String(duration),
611
+ "-vf",
612
+ `fps=${fps},scale=-1:${scale}`,
613
+ "-q:v",
614
+ "2",
615
+ outputPattern
616
+ ]);
617
+ return buildFrameList(outputDir, duration);
701
618
  }
702
619
 
703
- // src/core/input-resolver.ts
704
- import { join as join4 } from "path";
705
-
706
- // src/core/workspace.ts
707
- import { readdir as readdir2, rename, rm, stat as stat2, writeFile } from "fs/promises";
708
- import { join as join3 } from "path";
709
- import { map as map3 } from "@winglet/common-utils";
710
- import sharp2 from "sharp";
620
+ //#endregion
621
+ //#region src/core/workspace.ts
711
622
  async function createWorkspace(sessionId) {
712
- const workspacePath = getTempWorkspaceDir(sessionId);
713
- await ensureDir(join3(workspacePath, "frames"));
714
- await ensureDir(join3(workspacePath, "output"));
715
- return workspacePath;
623
+ const workspacePath = getTempWorkspaceDir(sessionId);
624
+ await ensureDir(join(workspacePath, "frames"));
625
+ await ensureDir(join(workspacePath, "output"));
626
+ return workspacePath;
716
627
  }
717
628
  async function finalizeOutput(ctx, selectedFrames) {
718
- const stagingDir = join3(ctx.workspacePath, "output");
719
- const outputPath = ctx.options.outputPath;
720
- const quality = ctx.options.quality;
721
- const outputFiles = [];
722
- const framesMetadata = [];
723
- const totalFramesCount = ctx.frames.length;
724
- const padding = Math.max(4, String(totalFramesCount).length);
725
- for (let i = 0; i < selectedFrames.length; i++) {
726
- const frame = selectedFrames[i];
727
- const fileName = `frame_${String(frame.id + 1).padStart(padding, "0")}.jpg`;
728
- const destPath = join3(stagingDir, fileName);
729
- await sharp2(frame.extractPath).jpeg({ quality, mozjpeg: true }).toFile(destPath);
730
- outputFiles.push(join3(outputPath, fileName));
731
- framesMetadata.push({
732
- step: i + 1,
733
- fileName,
734
- frameId: frame.id + 1,
735
- timestampMs: Math.round(frame.timestamp * 1e3)
736
- });
737
- }
738
- const metadata = {
739
- video: {
740
- originalDurationMs: Math.round(
741
- (ctx.frames.length > 0 ? ctx.frames[ctx.frames.length - 1].timestamp : 0) * 1e3
742
- ),
743
- fps: ctx.options.fps,
744
- resolution: {
745
- width: ctx.options.scale,
746
- height: Math.round(ctx.options.scale * 9 / 16)
747
- }
748
- },
749
- frames: framesMetadata,
750
- animations: map3(ctx.animations || [], (anim) => ({
751
- ...anim,
752
- startFrameId: anim.startFrameId + 1,
753
- endFrameId: anim.endFrameId + 1,
754
- durationMs: Math.round(anim.durationMs)
755
- }))
756
- };
757
- const metadataPath = join3(stagingDir, ".metadata.json");
758
- await writeFile(metadataPath, JSON.stringify(metadata, null, 2));
759
- outputFiles.push(join3(outputPath, ".metadata.json"));
760
- await ensureDir(join3(outputPath, ".."));
761
- await rm(outputPath, { recursive: true, force: true });
762
- await rename(stagingDir, outputPath);
763
- return outputFiles;
629
+ const stagingDir = join(ctx.workspacePath, "output");
630
+ const outputPath = ctx.options.outputPath;
631
+ const quality = ctx.options.quality;
632
+ const outputFiles = [];
633
+ const framesMetadata = [];
634
+ const totalFramesCount = ctx.frames.length;
635
+ const padding = Math.max(4, String(totalFramesCount).length);
636
+ for (let i = 0; i < selectedFrames.length; i++) {
637
+ const frame = selectedFrames[i];
638
+ const fileName = `frame_${String(frame.id + 1).padStart(padding, "0")}.jpg`;
639
+ const destPath = join(stagingDir, fileName);
640
+ await sharp(frame.extractPath).jpeg({
641
+ quality,
642
+ mozjpeg: true
643
+ }).toFile(destPath);
644
+ outputFiles.push(join(outputPath, fileName));
645
+ framesMetadata.push({
646
+ step: i + 1,
647
+ fileName,
648
+ frameId: frame.id + 1,
649
+ timestampMs: Math.round(frame.timestamp * 1e3)
650
+ });
651
+ }
652
+ const metadata = {
653
+ video: {
654
+ originalDurationMs: Math.round((ctx.frames.length > 0 ? ctx.frames[ctx.frames.length - 1].timestamp : 0) * 1e3),
655
+ fps: ctx.options.fps,
656
+ resolution: {
657
+ width: ctx.options.scale,
658
+ height: Math.round(ctx.options.scale * 9 / 16)
659
+ }
660
+ },
661
+ frames: framesMetadata,
662
+ animations: map(ctx.animations || [], (anim) => ({
663
+ ...anim,
664
+ startFrameId: anim.startFrameId + 1,
665
+ endFrameId: anim.endFrameId + 1,
666
+ durationMs: Math.round(anim.durationMs)
667
+ }))
668
+ };
669
+ await writeFile(join(stagingDir, ".metadata.json"), JSON.stringify(metadata, null, 2));
670
+ outputFiles.push(join(outputPath, ".metadata.json"));
671
+ await ensureDir(join(outputPath, ".."));
672
+ await rm(outputPath, {
673
+ recursive: true,
674
+ force: true
675
+ });
676
+ await rename(stagingDir, outputPath);
677
+ return outputFiles;
764
678
  }
765
679
  async function createSegmentWorkspace(parentWorkspacePath, segmentIndex) {
766
- const segmentPath = join3(
767
- parentWorkspacePath,
768
- "segments",
769
- String(segmentIndex)
770
- );
771
- await ensureDir(join3(segmentPath, "frames"));
772
- return segmentPath;
680
+ const segmentPath = join(parentWorkspacePath, "segments", String(segmentIndex));
681
+ await ensureDir(join(segmentPath, "frames"));
682
+ return segmentPath;
773
683
  }
774
684
  async function cleanupWorkspace(workspacePath) {
775
- if (!workspacePath) return;
776
- try {
777
- await rm(workspacePath, { recursive: true, force: true });
778
- } catch {
779
- }
685
+ if (!workspacePath) return;
686
+ try {
687
+ await rm(workspacePath, {
688
+ recursive: true,
689
+ force: true
690
+ });
691
+ } catch {}
780
692
  }
781
- var STALE_THRESHOLD_MS = 60 * 60 * 1e3;
693
+ /**
694
+ * Write a video buffer to a temp file in the workspace and return the path.
695
+ * Used by 'buffer' input mode.
696
+ */
782
697
  async function writeInputBuffer(buffer, workspacePath) {
783
- const inputDir = join3(workspacePath, "input");
784
- await ensureDir(inputDir);
785
- const tempPath = join3(inputDir, "input.mp4");
786
- await writeFile(tempPath, buffer);
787
- return tempPath;
698
+ const inputDir = join(workspacePath, "input");
699
+ await ensureDir(inputDir);
700
+ const tempPath = join(inputDir, "input.mp4");
701
+ await writeFile(tempPath, buffer);
702
+ return tempPath;
788
703
  }
704
+ /**
705
+ * Write an array of frame Buffers as JPG files and return FrameNode[].
706
+ * Used by 'frames' input mode.
707
+ */
789
708
  async function writeInputFrames(frames, workspacePath) {
790
- const framesDir = join3(workspacePath, "frames");
791
- await ensureDir(framesDir);
792
- const frameNodes = [];
793
- for (let i = 0; i < frames.length; i++) {
794
- const filename = `frame_${String(i).padStart(6, "0")}${FRAME_OUTPUT_EXTENSION}`;
795
- const extractPath = join3(framesDir, filename);
796
- await writeFile(extractPath, frames[i]);
797
- frameNodes.push({ id: i, timestamp: i, extractPath });
798
- }
799
- return frameNodes;
709
+ const framesDir = join(workspacePath, "frames");
710
+ await ensureDir(framesDir);
711
+ const frameNodes = [];
712
+ for (let i = 0; i < frames.length; i++) {
713
+ const extractPath = join(framesDir, `frame_${String(i).padStart(6, "0")}${FRAME_OUTPUT_EXTENSION}`);
714
+ await writeFile(extractPath, frames[i]);
715
+ frameNodes.push({
716
+ id: i,
717
+ timestamp: i,
718
+ extractPath
719
+ });
720
+ }
721
+ return frameNodes;
800
722
  }
723
+ /**
724
+ * Read selected FrameNode files as Buffers with JPEG compression.
725
+ * Used to return output buffers in 'buffer' and 'frames' modes.
726
+ */
801
727
  async function readFramesAsBuffers(frameNodes, quality) {
802
- return Promise.all(
803
- map3(
804
- frameNodes,
805
- (f) => sharp2(f.extractPath).jpeg({ quality, mozjpeg: true }).toBuffer()
806
- )
807
- );
728
+ return Promise.all(map(frameNodes, (f) => sharp(f.extractPath).jpeg({
729
+ quality,
730
+ mozjpeg: true
731
+ }).toBuffer()));
808
732
  }
809
733
 
810
- // src/core/input-resolver.ts
811
- function resolveOptions(options2) {
812
- const mode = options2.mode;
813
- const inputPath = mode === "file" ? resolveAbsolute(options2.inputPath) : void 0;
814
- const outputPath = options2.outputPath ?? (inputPath ? deriveOutputPath(inputPath) : join4(process.cwd(), "scene-sieve-output"));
815
- const threshold = options2.threshold ?? DEFAULT_THRESHOLD;
816
- if (threshold <= 0 || threshold > 1) {
817
- throw new Error(
818
- `threshold must be in range (0, 1], received: ${threshold}`
819
- );
820
- }
821
- const pruneMode = "threshold-with-cap";
822
- return {
823
- mode,
824
- inputPath,
825
- count: options2.count ?? DEFAULT_COUNT,
826
- threshold,
827
- pruneMode,
828
- outputPath,
829
- fps: options2.fps ?? DEFAULT_FPS,
830
- maxFrames: options2.maxFrames ?? DEFAULT_MAX_FRAMES,
831
- scale: options2.scale ?? DEFAULT_SCALE,
832
- quality: options2.quality ?? DEFAULT_QUALITY,
833
- iouThreshold: options2.iouThreshold ?? IOU_THRESHOLD,
834
- animationThreshold: options2.animationThreshold ?? ANIMATION_FRAME_THRESHOLD,
835
- debug: options2.debug ?? false,
836
- maxSegmentDuration: options2.maxSegmentDuration ?? DEFAULT_MAX_SEGMENT_DURATION,
837
- concurrency: options2.concurrency ?? DEFAULT_SEGMENT_CONCURRENCY
838
- };
734
+ //#endregion
735
+ //#region src/core/input-resolver.ts
736
+ function resolveOptions(options) {
737
+ const mode = options.mode;
738
+ const inputPath = mode === "file" ? resolveAbsolute(options.inputPath) : void 0;
739
+ const outputPath = options.outputPath ?? (inputPath ? deriveOutputPath(inputPath) : join(process.cwd(), "scene-sieve-output"));
740
+ const threshold = options.threshold ?? .5;
741
+ if (threshold <= 0 || threshold > 1) throw new Error(`threshold must be in range (0, 1], received: ${threshold}`);
742
+ return {
743
+ mode,
744
+ inputPath,
745
+ count: options.count ?? 20,
746
+ threshold,
747
+ pruneMode: "threshold-with-cap",
748
+ outputPath,
749
+ fps: options.fps ?? 5,
750
+ maxFrames: options.maxFrames ?? 300,
751
+ scale: options.scale ?? 720,
752
+ quality: options.quality ?? 80,
753
+ iouThreshold: options.iouThreshold ?? .9,
754
+ animationThreshold: options.animationThreshold ?? 5,
755
+ debug: options.debug ?? false,
756
+ maxSegmentDuration: options.maxSegmentDuration ?? 300,
757
+ concurrency: options.concurrency ?? 2
758
+ };
839
759
  }
840
- async function resolveInput(options2, workspacePath) {
841
- if (options2.mode === "file") {
842
- return {
843
- frames: [],
844
- resolvedInputPath: resolveAbsolute(options2.inputPath)
845
- };
846
- }
847
- if (options2.mode === "buffer") {
848
- const resolvedInputPath = await writeInputBuffer(
849
- options2.inputBuffer,
850
- workspacePath
851
- );
852
- return { frames: [], resolvedInputPath };
853
- }
854
- if (options2.mode === "frames") {
855
- const frames = await writeInputFrames(options2.inputFrames, workspacePath);
856
- return { frames };
857
- }
858
- throw new Error(`Unsupported input mode: ${options2.mode}`);
760
+ /**
761
+ * Resolve the input source to a list of FrameNode[].
762
+ *
763
+ * - 'file' mode: validate file exists and delegate to extractor (caller's responsibility)
764
+ * - 'buffer' mode: write buffer as temp video file, return path via FrameNode trick (empty list)
765
+ * - 'frames' mode: write frame buffers as JPGs, return FrameNode[]
766
+ */
767
+ async function resolveInput(options, workspacePath) {
768
+ if (options.mode === "file") return {
769
+ frames: [],
770
+ resolvedInputPath: resolveAbsolute(options.inputPath)
771
+ };
772
+ if (options.mode === "buffer") return {
773
+ frames: [],
774
+ resolvedInputPath: await writeInputBuffer(options.inputBuffer, workspacePath)
775
+ };
776
+ if (options.mode === "frames") return { frames: await writeInputFrames(options.inputFrames, workspacePath) };
777
+ throw new Error(`Unsupported input mode: ${options.mode}`);
859
778
  }
860
779
 
861
- // src/core/pruner.ts
862
- import { filter as filter4, map as map5 } from "@winglet/common-utils";
863
-
864
- // src/utils/math.ts
865
- import { filter as filter3, map as map4 } from "@winglet/common-utils";
780
+ //#endregion
781
+ //#region src/utils/math.ts
782
+ /**
783
+ * Normalize raw scores to [0, 1] range via Robust Hybrid Normalization.
784
+ *
785
+ * This model combines two mathematical approaches to provide a stable "relative" threshold:
786
+ *
787
+ * 1. Logistic-Robust-Z (Intensity):
788
+ * Calculates Z-scores using Median and Median Absolute Deviation (MAD).
789
+ * Maps these to a sigmoid (logistic) curve. This suppresses noise (scores near median)
790
+ * and highlights significant signals (outliers) without letting extreme outliers
791
+ * crush other meaningful transitions.
792
+ *
793
+ * 2. CDF / Percentile Rank (Relative Position):
794
+ * Maps each score to its percentile rank in the sequence. This ensures that 't'
795
+ * always has a consistent meaning as a "relative rank" regardless of absolute values.
796
+ *
797
+ * The final score is a weighted sum (NORMALIZATION_ALPHA) of both.
798
+ *
799
+ * @param items - Array of items with scores to normalize
800
+ * @returns normalized scores array (same length as input)
801
+ */
866
802
  function normalizeScores(items) {
867
- if (items.length === 0) return [];
868
- const safeScores = map4(
869
- items,
870
- (e) => Number.isFinite(e.score) && e.score > 0 ? e.score : 0
871
- );
872
- const positiveScores = filter3(safeScores, (s) => s > 0);
873
- if (positiveScores.length === 0) return safeScores;
874
- const sorted = [...positiveScores].sort((a, b) => a - b);
875
- if (positiveScores.length <= NORMALIZATION_MIN_SAMPLE_SIZE) {
876
- const min = sorted[0];
877
- const max = sorted[sorted.length - 1];
878
- if (max === min) return map4(safeScores, (s) => s > 0 ? 1 : 0);
879
- return map4(
880
- safeScores,
881
- (s) => s <= 0 ? 0 : Math.max(0, Math.min((s - min) / (max - min), 1))
882
- );
883
- }
884
- const median = sorted[Math.floor(sorted.length / 2)];
885
- const absoluteDiffs = map4(positiveScores, (v) => Math.abs(v - median));
886
- const mad = [...absoluteDiffs].sort((a, b) => a - b)[Math.floor(absoluteDiffs.length / 2)];
887
- const scale = mad === 0 ? median : mad * NORMALIZATION_MAD_COEFFICIENT;
888
- const logisticZ = map4(safeScores, (s) => {
889
- if (s <= 0) return 0;
890
- if (scale === 0) return 1;
891
- const z = (s - median) / scale;
892
- return 1 / (1 + Math.exp(-NORMALIZATION_LOGISTIC_K * z));
893
- });
894
- const cdf = map4(safeScores, (s) => {
895
- if (s <= 0) return 0;
896
- const rank = sorted.findIndex((v) => v >= s);
897
- return rank / sorted.length;
898
- });
899
- return map4(
900
- logisticZ,
901
- (z, i) => z * (1 - NORMALIZATION_ALPHA) + cdf[i] * NORMALIZATION_ALPHA
902
- );
803
+ if (items.length === 0) return [];
804
+ const safeScores = map(items, (e) => Number.isFinite(e.score) && e.score > 0 ? e.score : 0);
805
+ const positiveScores = filter(safeScores, (s) => s > 0);
806
+ if (positiveScores.length === 0) return safeScores;
807
+ const sorted = [...positiveScores].sort((a, b) => a - b);
808
+ if (positiveScores.length <= 10) {
809
+ const min = sorted[0];
810
+ const max = sorted[sorted.length - 1];
811
+ if (max === min) return map(safeScores, (s) => s > 0 ? 1 : 0);
812
+ return map(safeScores, (s) => s <= 0 ? 0 : Math.max(0, Math.min((s - min) / (max - min), 1)));
813
+ }
814
+ const median = sorted[Math.floor(sorted.length / 2)];
815
+ const absoluteDiffs = map(positiveScores, (v) => Math.abs(v - median));
816
+ const mad = [...absoluteDiffs].sort((a, b) => a - b)[Math.floor(absoluteDiffs.length / 2)];
817
+ const scale = mad === 0 ? median : mad * NORMALIZATION_MAD_COEFFICIENT;
818
+ const logisticZ = map(safeScores, (s) => {
819
+ if (s <= 0) return 0;
820
+ if (scale === 0) return 1;
821
+ const z = (s - median) / scale;
822
+ return 1 / (1 + Math.exp(-3 * z));
823
+ });
824
+ const cdf = map(safeScores, (s) => {
825
+ if (s <= 0) return 0;
826
+ return sorted.findIndex((v) => v >= s) / sorted.length;
827
+ });
828
+ return map(logisticZ, (z, i) => z * (1 - NORMALIZATION_ALPHA) + cdf[i] * NORMALIZATION_ALPHA);
903
829
  }
904
830
 
905
- // src/utils/min-heap.ts
831
+ //#endregion
832
+ //#region src/utils/min-heap.ts
833
+ /**
834
+ * Generic binary min-heap.
835
+ *
836
+ * Elements are ordered by a numeric `score` field.
837
+ * push/pop: O(log N), size: O(1).
838
+ */
906
839
  var MinHeap = class {
907
- h = [];
908
- get size() {
909
- return this.h.length;
910
- }
911
- push(entry) {
912
- this.h.push(entry);
913
- this.siftUp(this.h.length - 1);
914
- }
915
- pop() {
916
- const n = this.h.length;
917
- if (n === 0) return void 0;
918
- const top = this.h[0];
919
- const last = this.h.pop();
920
- if (n > 1) {
921
- this.h[0] = last;
922
- this.siftDown(0);
923
- }
924
- return top;
925
- }
926
- siftUp(i) {
927
- while (i > 0) {
928
- const p = i - 1 >> 1;
929
- if (this.h[p].score <= this.h[i].score) break;
930
- [this.h[p], this.h[i]] = [this.h[i], this.h[p]];
931
- i = p;
932
- }
933
- }
934
- siftDown(i) {
935
- const n = this.h.length;
936
- for (; ; ) {
937
- let m = i;
938
- const l = 2 * i + 1;
939
- const r = 2 * i + 2;
940
- if (l < n && this.h[l].score < this.h[m].score) m = l;
941
- if (r < n && this.h[r].score < this.h[m].score) m = r;
942
- if (m === i) break;
943
- [this.h[m], this.h[i]] = [this.h[i], this.h[m]];
944
- i = m;
945
- }
946
- }
840
+ h = [];
841
+ get size() {
842
+ return this.h.length;
843
+ }
844
+ push(entry) {
845
+ this.h.push(entry);
846
+ this.siftUp(this.h.length - 1);
847
+ }
848
+ pop() {
849
+ const n = this.h.length;
850
+ if (n === 0) return void 0;
851
+ const top = this.h[0];
852
+ const last = this.h.pop();
853
+ if (n > 1) {
854
+ this.h[0] = last;
855
+ this.siftDown(0);
856
+ }
857
+ return top;
858
+ }
859
+ siftUp(i) {
860
+ while (i > 0) {
861
+ const p = i - 1 >> 1;
862
+ if (this.h[p].score <= this.h[i].score) break;
863
+ [this.h[p], this.h[i]] = [this.h[i], this.h[p]];
864
+ i = p;
865
+ }
866
+ }
867
+ siftDown(i) {
868
+ const n = this.h.length;
869
+ for (;;) {
870
+ let m = i;
871
+ const l = 2 * i + 1;
872
+ const r = 2 * i + 2;
873
+ if (l < n && this.h[l].score < this.h[m].score) m = l;
874
+ if (r < n && this.h[r].score < this.h[m].score) m = r;
875
+ if (m === i) break;
876
+ [this.h[m], this.h[i]] = [this.h[i], this.h[m]];
877
+ i = m;
878
+ }
879
+ }
947
880
  };
948
881
 
949
- // src/core/pruner.ts
882
+ //#endregion
883
+ //#region src/core/pruner.ts
884
+ /**
885
+ * Edge-aware greedy merge with re-linking — O(N log N).
886
+ *
887
+ * 1. Build a doubly-linked list of frames
888
+ * 2. Insert all edges into a min-heap
889
+ * 3. Pop the lowest-score edge (most similar pair)
890
+ * 4. Remove the later frame (tgtId), re-link neighbors
891
+ * 5. Push synthetic edge with score = max(left, right)
892
+ * 6. Repeat until surviving count === targetCount
893
+ * 7. First and last frames are never removed (boundary preservation)
894
+ *
895
+ * Stale heap entries (involving removed frames) are lazily skipped on pop.
896
+ */
950
897
  function pruneTo(graph, frames, targetCount) {
951
- if (frames.length <= targetCount) {
952
- return new Set(map5(frames, (f) => f.id));
953
- }
954
- const prev = /* @__PURE__ */ new Map();
955
- const next = /* @__PURE__ */ new Map();
956
- for (let i = 0; i < frames.length; i++) {
957
- if (i > 0) prev.set(frames[i].id, frames[i - 1].id);
958
- if (i < frames.length - 1) next.set(frames[i].id, frames[i + 1].id);
959
- }
960
- const edgeScore = /* @__PURE__ */ new Map();
961
- const heap = new MinHeap();
962
- for (const edge of graph) {
963
- edgeScore.set(`${edge.sourceId}:${edge.targetId}`, edge.score);
964
- heap.push({
965
- score: edge.score,
966
- srcId: edge.sourceId,
967
- tgtId: edge.targetId
968
- });
969
- }
970
- const surviving = new Set(map5(frames, (f) => f.id));
971
- const firstId = frames[0].id;
972
- const lastId = frames[frames.length - 1].id;
973
- while (surviving.size > targetCount && heap.size > 0) {
974
- const entry = heap.pop();
975
- if (!surviving.has(entry.srcId) || !surviving.has(entry.tgtId)) continue;
976
- const key = `${entry.srcId}:${entry.tgtId}`;
977
- if (edgeScore.get(key) !== entry.score) continue;
978
- if (entry.tgtId === firstId || entry.tgtId === lastId) continue;
979
- surviving.delete(entry.tgtId);
980
- edgeScore.delete(key);
981
- const tgtNext = next.get(entry.tgtId);
982
- if (tgtNext !== void 0) {
983
- const rightKey = `${entry.tgtId}:${tgtNext}`;
984
- const rightScore = edgeScore.get(rightKey) ?? 0;
985
- edgeScore.delete(rightKey);
986
- const newScore = Math.max(entry.score, rightScore);
987
- edgeScore.set(`${entry.srcId}:${tgtNext}`, newScore);
988
- heap.push({ score: newScore, srcId: entry.srcId, tgtId: tgtNext });
989
- next.set(entry.srcId, tgtNext);
990
- prev.set(tgtNext, entry.srcId);
991
- } else {
992
- next.delete(entry.srcId);
993
- }
994
- prev.delete(entry.tgtId);
995
- next.delete(entry.tgtId);
996
- }
997
- return surviving;
898
+ if (frames.length <= targetCount) return new Set(map(frames, (f) => f.id));
899
+ const prev = /* @__PURE__ */ new Map();
900
+ const next = /* @__PURE__ */ new Map();
901
+ for (let i = 0; i < frames.length; i++) {
902
+ if (i > 0) prev.set(frames[i].id, frames[i - 1].id);
903
+ if (i < frames.length - 1) next.set(frames[i].id, frames[i + 1].id);
904
+ }
905
+ const edgeScore = /* @__PURE__ */ new Map();
906
+ const heap = new MinHeap();
907
+ for (const edge of graph) {
908
+ edgeScore.set(`${edge.sourceId}:${edge.targetId}`, edge.score);
909
+ heap.push({
910
+ score: edge.score,
911
+ srcId: edge.sourceId,
912
+ tgtId: edge.targetId
913
+ });
914
+ }
915
+ const surviving = new Set(map(frames, (f) => f.id));
916
+ const firstId = frames[0].id;
917
+ const lastId = frames[frames.length - 1].id;
918
+ while (surviving.size > targetCount && heap.size > 0) {
919
+ const entry = heap.pop();
920
+ if (!surviving.has(entry.srcId) || !surviving.has(entry.tgtId)) continue;
921
+ const key = `${entry.srcId}:${entry.tgtId}`;
922
+ if (edgeScore.get(key) !== entry.score) continue;
923
+ if (entry.tgtId === firstId || entry.tgtId === lastId) continue;
924
+ surviving.delete(entry.tgtId);
925
+ edgeScore.delete(key);
926
+ const tgtNext = next.get(entry.tgtId);
927
+ if (tgtNext !== void 0) {
928
+ const rightKey = `${entry.tgtId}:${tgtNext}`;
929
+ const rightScore = edgeScore.get(rightKey) ?? 0;
930
+ edgeScore.delete(rightKey);
931
+ const newScore = Math.max(entry.score, rightScore);
932
+ edgeScore.set(`${entry.srcId}:${tgtNext}`, newScore);
933
+ heap.push({
934
+ score: newScore,
935
+ srcId: entry.srcId,
936
+ tgtId: tgtNext
937
+ });
938
+ next.set(entry.srcId, tgtNext);
939
+ prev.set(tgtNext, entry.srcId);
940
+ } else next.delete(entry.srcId);
941
+ prev.delete(entry.tgtId);
942
+ next.delete(entry.tgtId);
943
+ }
944
+ return surviving;
998
945
  }
946
+ /**
947
+ * Non-Maximum Suppression (NMS) for consecutive edge runs.
948
+ *
949
+ * Consecutive edges share overlapping frames (edge i: frame i->i+1,
950
+ * edge i+1: frame i+1->i+2), so consecutive passing edges indicate
951
+ * the same visual transition region. This function groups consecutive
952
+ * passing edge indices into "runs" and keeps all distinct peaks per run.
953
+ *
954
+ * Multi-peak detection: within each run, strict local maxima (score higher
955
+ * than both neighbors) are identified. Each local maximum represents a
956
+ * distinct visual transition. If no strict local maxima exist (plateau or
957
+ * monotonic sequence), the global peak of the run is selected as fallback.
958
+ *
959
+ * Single-element runs are unaffected (isolated transitions preserved).
960
+ *
961
+ * @param graph - full ScoreEdge array (for targetId lookup)
962
+ * @param passingIndices - edge indices that passed threshold filtering (sorted ascending)
963
+ * @param normalizedScores - normalized score array (same length as graph)
964
+ * @returns Set of targetIds to add to surviving set (one or more per run)
965
+ */
999
966
  function suppressConsecutiveRuns(graph, passingIndices, normalizedScores) {
1000
- const result = /* @__PURE__ */ new Set();
1001
- let runStart = 0;
1002
- while (runStart < passingIndices.length) {
1003
- let runEnd = runStart;
1004
- while (runEnd + 1 < passingIndices.length && passingIndices[runEnd + 1] === passingIndices[runEnd] + 1) {
1005
- runEnd++;
1006
- }
1007
- const runLen = runEnd - runStart + 1;
1008
- if (runLen === 1) {
1009
- result.add(graph[passingIndices[runStart]].targetId);
1010
- } else {
1011
- const peaks = [];
1012
- for (let j = runStart; j <= runEnd; j++) {
1013
- const idx = passingIndices[j];
1014
- const score = normalizedScores[idx];
1015
- const prevScore = j > runStart ? normalizedScores[passingIndices[j - 1]] : -Infinity;
1016
- const nextScore = j < runEnd ? normalizedScores[passingIndices[j + 1]] : -Infinity;
1017
- if (score > prevScore && score > nextScore) {
1018
- peaks.push(idx);
1019
- }
1020
- }
1021
- if (peaks.length > 0) {
1022
- for (const peakIdx of peaks) {
1023
- result.add(graph[peakIdx].targetId);
1024
- }
1025
- } else {
1026
- let peakIdx = passingIndices[runStart];
1027
- for (let j = runStart + 1; j <= runEnd; j++) {
1028
- const idx = passingIndices[j];
1029
- if (normalizedScores[idx] > normalizedScores[peakIdx]) {
1030
- peakIdx = idx;
1031
- }
1032
- }
1033
- result.add(graph[peakIdx].targetId);
1034
- }
1035
- }
1036
- runStart = runEnd + 1;
1037
- }
1038
- return result;
967
+ const result = /* @__PURE__ */ new Set();
968
+ let runStart = 0;
969
+ while (runStart < passingIndices.length) {
970
+ let runEnd = runStart;
971
+ while (runEnd + 1 < passingIndices.length && passingIndices[runEnd + 1] === passingIndices[runEnd] + 1) runEnd++;
972
+ if (runEnd - runStart + 1 === 1) result.add(graph[passingIndices[runStart]].targetId);
973
+ else {
974
+ const peaks = [];
975
+ for (let j = runStart; j <= runEnd; j++) {
976
+ const idx = passingIndices[j];
977
+ const score = normalizedScores[idx];
978
+ const prevScore = j > runStart ? normalizedScores[passingIndices[j - 1]] : -Infinity;
979
+ const nextScore = j < runEnd ? normalizedScores[passingIndices[j + 1]] : -Infinity;
980
+ if (score > prevScore && score > nextScore) peaks.push(idx);
981
+ }
982
+ if (peaks.length > 0) for (const peakIdx of peaks) result.add(graph[peakIdx].targetId);
983
+ else {
984
+ let peakIdx = passingIndices[runStart];
985
+ for (let j = runStart + 1; j <= runEnd; j++) {
986
+ const idx = passingIndices[j];
987
+ if (normalizedScores[idx] > normalizedScores[peakIdx]) peakIdx = idx;
988
+ }
989
+ result.add(graph[peakIdx].targetId);
990
+ }
991
+ }
992
+ runStart = runEnd + 1;
993
+ }
994
+ return result;
1039
995
  }
996
+ /**
997
+ * Threshold-based pruning with NMS -- O(N).
998
+ *
999
+ * 1. Scores are normalized to [0, 1] via percentile normalization.
1000
+ * 2. Edges with normalized score >= threshold are collected.
1001
+ * 3. Non-Maximum Suppression groups consecutive passing edges and keeps
1002
+ * only the peak per run, preventing near-duplicate frame selection
1003
+ * from a single visual transition.
1004
+ *
1005
+ * First and last frames are always preserved (boundary protection).
1006
+ */
1040
1007
  function pruneByThreshold(graph, frames, threshold) {
1041
- if (frames.length === 0) return /* @__PURE__ */ new Set();
1042
- const surviving = /* @__PURE__ */ new Set();
1043
- surviving.add(frames[0].id);
1044
- surviving.add(frames[frames.length - 1].id);
1045
- const normalized = normalizeScores(graph);
1046
- const passingIndices = [];
1047
- for (let i = 0; i < graph.length; i++) {
1048
- if (normalized[i] >= threshold) {
1049
- passingIndices.push(i);
1050
- }
1051
- }
1052
- const nmsTargets = suppressConsecutiveRuns(graph, passingIndices, normalized);
1053
- for (const id of nmsTargets) {
1054
- surviving.add(id);
1055
- }
1056
- return surviving;
1008
+ if (frames.length === 0) return /* @__PURE__ */ new Set();
1009
+ const surviving = /* @__PURE__ */ new Set();
1010
+ surviving.add(frames[0].id);
1011
+ surviving.add(frames[frames.length - 1].id);
1012
+ const normalized = normalizeScores(graph);
1013
+ const passingIndices = [];
1014
+ for (let i = 0; i < graph.length; i++) if (normalized[i] >= threshold) passingIndices.push(i);
1015
+ const nmsTargets = suppressConsecutiveRuns(graph, passingIndices, normalized);
1016
+ for (const id of nmsTargets) surviving.add(id);
1017
+ return surviving;
1057
1018
  }
1019
+ /**
1020
+ * Combined threshold + count pruning -- 2-stage pipeline.
1021
+ *
1022
+ * Stage 1: pruneByThreshold -- keep all frames with normalized score >= threshold
1023
+ * Stage 2: if result exceeds maxCount, rebuild subgraph with synthetic edges
1024
+ * (min-score over each gap) and apply pruneTo on the surviving subset
1025
+ *
1026
+ * Edge reconstruction: for consecutive survivors A, B with removed frames
1027
+ * [x1, x2, ...] between them, the synthetic edge score is:
1028
+ * min(score(A->x1), score(x1->x2), ..., score(xN->B))
1029
+ * This preserves the "weakest link" semantics.
1030
+ */
1058
1031
  function pruneByThresholdWithCap(graph, frames, threshold, maxCount) {
1059
- const thresholdSurvivors = pruneByThreshold(graph, frames, threshold);
1060
- if (thresholdSurvivors.size <= maxCount) {
1061
- return thresholdSurvivors;
1062
- }
1063
- const survivingFrames = filter4(frames, (f) => thresholdSurvivors.has(f.id));
1064
- const idToOrigIdx = /* @__PURE__ */ new Map();
1065
- for (let i = 0; i < frames.length; i++) {
1066
- idToOrigIdx.set(frames[i].id, i);
1067
- }
1068
- const edgeLookup = /* @__PURE__ */ new Map();
1069
- for (const e of graph) {
1070
- edgeLookup.set(`${e.sourceId}:${e.targetId}`, e.score);
1071
- }
1072
- const syntheticEdges = [];
1073
- for (let i = 0; i < survivingFrames.length - 1; i++) {
1074
- const srcSurvivor = survivingFrames[i];
1075
- const tgtSurvivor = survivingFrames[i + 1];
1076
- const srcOrigIdx = idToOrigIdx.get(srcSurvivor.id);
1077
- const tgtOrigIdx = idToOrigIdx.get(tgtSurvivor.id);
1078
- let minScore = Infinity;
1079
- for (let j = srcOrigIdx; j < tgtOrigIdx; j++) {
1080
- const fromId = frames[j].id;
1081
- const toId = frames[j + 1].id;
1082
- const score = edgeLookup.get(`${fromId}:${toId}`) ?? 0;
1083
- if (score < minScore) {
1084
- minScore = score;
1085
- }
1086
- }
1087
- syntheticEdges.push({
1088
- sourceId: srcSurvivor.id,
1089
- targetId: tgtSurvivor.id,
1090
- score: minScore === Infinity ? 0 : minScore
1091
- });
1092
- }
1093
- return pruneTo(syntheticEdges, survivingFrames, maxCount);
1032
+ const thresholdSurvivors = pruneByThreshold(graph, frames, threshold);
1033
+ if (thresholdSurvivors.size <= maxCount) return thresholdSurvivors;
1034
+ const survivingFrames = filter(frames, (f) => thresholdSurvivors.has(f.id));
1035
+ const idToOrigIdx = /* @__PURE__ */ new Map();
1036
+ for (let i = 0; i < frames.length; i++) idToOrigIdx.set(frames[i].id, i);
1037
+ const edgeLookup = /* @__PURE__ */ new Map();
1038
+ for (const e of graph) edgeLookup.set(`${e.sourceId}:${e.targetId}`, e.score);
1039
+ const syntheticEdges = [];
1040
+ for (let i = 0; i < survivingFrames.length - 1; i++) {
1041
+ const srcSurvivor = survivingFrames[i];
1042
+ const tgtSurvivor = survivingFrames[i + 1];
1043
+ const srcOrigIdx = idToOrigIdx.get(srcSurvivor.id);
1044
+ const tgtOrigIdx = idToOrigIdx.get(tgtSurvivor.id);
1045
+ let minScore = Infinity;
1046
+ for (let j = srcOrigIdx; j < tgtOrigIdx; j++) {
1047
+ const fromId = frames[j].id;
1048
+ const toId = frames[j + 1].id;
1049
+ const score = edgeLookup.get(`${fromId}:${toId}`) ?? 0;
1050
+ if (score < minScore) minScore = score;
1051
+ }
1052
+ syntheticEdges.push({
1053
+ sourceId: srcSurvivor.id,
1054
+ targetId: tgtSurvivor.id,
1055
+ score: minScore === Infinity ? 0 : minScore
1056
+ });
1057
+ }
1058
+ return pruneTo(syntheticEdges, survivingFrames, maxCount);
1094
1059
  }
1095
1060
 
1096
- // src/core/segmenter.ts
1097
- import { randomUUID } from "crypto";
1098
- import { join as join5 } from "path";
1099
- import { filter as filter5, map as map6 } from "@winglet/common-utils";
1100
-
1101
- // src/utils/concurrency.ts
1061
+ //#endregion
1062
+ //#region src/utils/concurrency.ts
1063
+ /**
1064
+ * Creates a concurrency limiter that runs at most `limit` tasks in parallel.
1065
+ * Lightweight replacement for p-limit to avoid external dependency.
1066
+ */
1102
1067
  function concurrencyLimit(limit) {
1103
- limit = Math.max(1, limit);
1104
- let active = 0;
1105
- const queue = [];
1106
- return async (fn) => {
1107
- while (active >= limit) {
1108
- await new Promise((resolve2) => queue.push(resolve2));
1109
- }
1110
- active++;
1111
- try {
1112
- return await fn();
1113
- } finally {
1114
- active--;
1115
- queue.shift()?.();
1116
- }
1117
- };
1068
+ limit = Math.max(1, limit);
1069
+ let active = 0;
1070
+ const queue = [];
1071
+ return async (fn) => {
1072
+ while (active >= limit) await new Promise((resolve) => queue.push(resolve));
1073
+ active++;
1074
+ try {
1075
+ return await fn();
1076
+ } finally {
1077
+ active--;
1078
+ queue.shift()?.();
1079
+ }
1080
+ };
1118
1081
  }
1119
1082
 
1120
- // src/core/segmenter.ts
1083
+ //#endregion
1084
+ //#region src/core/segmenter.ts
1085
+ /**
1086
+ * Determine whether segmentation should be used.
1087
+ * Returns false for frames mode and GIF files.
1088
+ * Actual duration check happens inside runSegmentedPipeline after metadata fetch.
1089
+ */
1121
1090
  function shouldSegment(resolvedOptions, originalOptions) {
1122
- if (resolvedOptions.mode === "frames") return false;
1123
- if (originalOptions.mode === "file") {
1124
- if (originalOptions.inputPath.toLowerCase().endsWith(".gif")) return false;
1125
- }
1126
- return true;
1091
+ if (resolvedOptions.mode === "frames") return false;
1092
+ if (originalOptions.mode === "file") {
1093
+ if (originalOptions.inputPath.toLowerCase().endsWith(".gif")) return false;
1094
+ }
1095
+ return true;
1127
1096
  }
1097
+ /**
1098
+ * Compute segment boundaries with overlap, frame allocation, and effectiveFps.
1099
+ * Pure function — no I/O.
1100
+ *
1101
+ * - effectiveFps is uniform across all segments
1102
+ * - Overlap: 1 frame at each internal boundary
1103
+ * - allocatedFrames total <= maxFrames (last segment adjusted if needed)
1104
+ */
1128
1105
  function computeSegmentPlan(totalDuration, maxSegmentDuration, maxFrames, fps) {
1129
- const effectiveFps = Math.max(0.5, Math.min(fps, maxFrames / totalDuration));
1130
- if (totalDuration <= maxSegmentDuration) {
1131
- return [
1132
- {
1133
- index: 0,
1134
- startTime: 0,
1135
- endTime: totalDuration,
1136
- duration: totalDuration,
1137
- allocatedFrames: Math.min(
1138
- Math.ceil(effectiveFps * totalDuration),
1139
- maxFrames
1140
- ),
1141
- effectiveFps,
1142
- overlapBefore: 0,
1143
- overlapAfter: 0,
1144
- extractStartTime: 0,
1145
- extractDuration: totalDuration
1146
- }
1147
- ];
1148
- }
1149
- const segmentCount = Math.ceil(totalDuration / maxSegmentDuration);
1150
- const overlapTime = 1 / effectiveFps;
1151
- const segments = [];
1152
- for (let i = 0; i < segmentCount; i++) {
1153
- const startTime = i * maxSegmentDuration;
1154
- const endTime = Math.min((i + 1) * maxSegmentDuration, totalDuration);
1155
- const duration = endTime - startTime;
1156
- const overlapBefore = i > 0 ? 1 : 0;
1157
- const overlapAfter = i < segmentCount - 1 ? 1 : 0;
1158
- const extractStartTime = Math.max(
1159
- 0,
1160
- startTime - overlapBefore * overlapTime
1161
- );
1162
- const extractEndTime = Math.min(
1163
- totalDuration,
1164
- endTime + overlapAfter * overlapTime
1165
- );
1166
- const extractDuration = extractEndTime - extractStartTime;
1167
- segments.push({
1168
- index: i,
1169
- startTime,
1170
- endTime,
1171
- duration,
1172
- allocatedFrames: Math.ceil(effectiveFps * duration),
1173
- effectiveFps,
1174
- overlapBefore,
1175
- overlapAfter,
1176
- extractStartTime,
1177
- extractDuration
1178
- });
1179
- }
1180
- const totalAllocated = segments.reduce(
1181
- (sum, s) => sum + s.allocatedFrames,
1182
- 0
1183
- );
1184
- if (totalAllocated > maxFrames) {
1185
- segments[segments.length - 1].allocatedFrames -= totalAllocated - maxFrames;
1186
- }
1187
- return segments;
1106
+ const effectiveFps = Math.max(.5, Math.min(fps, maxFrames / totalDuration));
1107
+ if (totalDuration <= maxSegmentDuration) return [{
1108
+ index: 0,
1109
+ startTime: 0,
1110
+ endTime: totalDuration,
1111
+ duration: totalDuration,
1112
+ allocatedFrames: Math.min(Math.ceil(effectiveFps * totalDuration), maxFrames),
1113
+ effectiveFps,
1114
+ overlapBefore: 0,
1115
+ overlapAfter: 0,
1116
+ extractStartTime: 0,
1117
+ extractDuration: totalDuration
1118
+ }];
1119
+ const segmentCount = Math.ceil(totalDuration / maxSegmentDuration);
1120
+ const overlapTime = 1 / effectiveFps;
1121
+ const segments = [];
1122
+ for (let i = 0; i < segmentCount; i++) {
1123
+ const startTime = i * maxSegmentDuration;
1124
+ const endTime = Math.min((i + 1) * maxSegmentDuration, totalDuration);
1125
+ const duration = endTime - startTime;
1126
+ const overlapBefore = i > 0 ? 1 : 0;
1127
+ const overlapAfter = i < segmentCount - 1 ? 1 : 0;
1128
+ const extractStartTime = Math.max(0, startTime - overlapBefore * overlapTime);
1129
+ const extractDuration = Math.min(totalDuration, endTime + overlapAfter * overlapTime) - extractStartTime;
1130
+ segments.push({
1131
+ index: i,
1132
+ startTime,
1133
+ endTime,
1134
+ duration,
1135
+ allocatedFrames: Math.ceil(effectiveFps * duration),
1136
+ effectiveFps,
1137
+ overlapBefore,
1138
+ overlapAfter,
1139
+ extractStartTime,
1140
+ extractDuration
1141
+ });
1142
+ }
1143
+ const totalAllocated = segments.reduce((sum, s) => sum + s.allocatedFrames, 0);
1144
+ if (totalAllocated > maxFrames) segments[segments.length - 1].allocatedFrames -= totalAllocated - maxFrames;
1145
+ return segments;
1188
1146
  }
1147
+ /**
1148
+ * Collect all frames from every segment, adjusting timestamps by extractStartTime.
1149
+ */
1189
1150
  function collectAllFrames(segmentResults) {
1190
- const allFrames = [];
1191
- for (const result of segmentResults) {
1192
- for (const frame of result.frames) {
1193
- allFrames.push({
1194
- frame: {
1195
- ...frame,
1196
- // Use extractStartTime for timestamp correction (Section 18 note 1)
1197
- timestamp: frame.timestamp + result.segment.extractStartTime
1198
- },
1199
- segmentIndex: result.segment.index,
1200
- localId: frame.id
1201
- });
1202
- }
1203
- }
1204
- return allFrames;
1151
+ const allFrames = [];
1152
+ for (const result of segmentResults) for (const frame of result.frames) allFrames.push({
1153
+ frame: {
1154
+ ...frame,
1155
+ timestamp: frame.timestamp + result.segment.extractStartTime
1156
+ },
1157
+ segmentIndex: result.segment.index,
1158
+ localId: frame.id
1159
+ });
1160
+ return allFrames;
1205
1161
  }
1162
+ /**
1163
+ * Sort frames by timestamp then remove overlap duplicates.
1164
+ * Threshold: 1/(effectiveFps * 2) — adaptive to fps (Section 18 note 5).
1165
+ * Keeps the first occurrence (earlier segment).
1166
+ */
1206
1167
  function deduplicateFrames(frames, effectiveFps) {
1207
- frames.sort((a, b) => a.frame.timestamp - b.frame.timestamp);
1208
- const dupThreshold = 1 / (effectiveFps * 2);
1209
- const unique = [];
1210
- for (const entry of frames) {
1211
- if (unique.length > 0) {
1212
- const last = unique[unique.length - 1];
1213
- if (Math.abs(entry.frame.timestamp - last.frame.timestamp) < dupThreshold) {
1214
- continue;
1215
- }
1216
- }
1217
- unique.push(entry);
1218
- }
1219
- return unique;
1168
+ frames.sort((a, b) => a.frame.timestamp - b.frame.timestamp);
1169
+ const dupThreshold = 1 / (effectiveFps * 2);
1170
+ const unique = [];
1171
+ for (const entry of frames) {
1172
+ if (unique.length > 0) {
1173
+ const last = unique[unique.length - 1];
1174
+ if (Math.abs(entry.frame.timestamp - last.frame.timestamp) < dupThreshold) continue;
1175
+ }
1176
+ unique.push(entry);
1177
+ }
1178
+ return unique;
1220
1179
  }
1180
+ /**
1181
+ * Assign sequential global IDs to deduplicated frames and build a lookup map.
1182
+ * Returns the remapped FrameNode array and the "segmentIndex:localId" -> globalId map.
1183
+ */
1221
1184
  function remapFrameIds(uniqueFrames) {
1222
- const globalIdMap = /* @__PURE__ */ new Map();
1223
- const frames = uniqueFrames.map((entry, globalId) => {
1224
- globalIdMap.set(`${entry.segmentIndex}:${entry.localId}`, globalId);
1225
- return {
1226
- id: globalId,
1227
- timestamp: entry.frame.timestamp,
1228
- extractPath: entry.frame.extractPath
1229
- };
1230
- });
1231
- return { frames, globalIdMap };
1185
+ const globalIdMap = /* @__PURE__ */ new Map();
1186
+ return {
1187
+ frames: uniqueFrames.map((entry, globalId) => {
1188
+ globalIdMap.set(`${entry.segmentIndex}:${entry.localId}`, globalId);
1189
+ return {
1190
+ id: globalId,
1191
+ timestamp: entry.frame.timestamp,
1192
+ extractPath: entry.frame.extractPath
1193
+ };
1194
+ }),
1195
+ globalIdMap
1196
+ };
1232
1197
  }
1198
+ /**
1199
+ * Remap edge source/target IDs using the global ID map.
1200
+ * Duplicate edges (same source-target pair) retain the higher score.
1201
+ */
1233
1202
  function remapEdges(segmentResults, globalIdMap) {
1234
- const edges = [];
1235
- const edgeMap = /* @__PURE__ */ new Map();
1236
- for (const result of segmentResults) {
1237
- for (const edge of result.edges) {
1238
- const newSourceId = globalIdMap.get(
1239
- `${result.segment.index}:${edge.sourceId}`
1240
- );
1241
- const newTargetId = globalIdMap.get(
1242
- `${result.segment.index}:${edge.targetId}`
1243
- );
1244
- if (newSourceId === void 0 || newTargetId === void 0) continue;
1245
- const edgeKey = `${newSourceId}-${newTargetId}`;
1246
- const existingIdx = edgeMap.get(edgeKey);
1247
- if (existingIdx !== void 0) {
1248
- if (edges[existingIdx].score < edge.score) {
1249
- edges[existingIdx] = {
1250
- sourceId: newSourceId,
1251
- targetId: newTargetId,
1252
- score: edge.score
1253
- };
1254
- }
1255
- } else {
1256
- edgeMap.set(edgeKey, edges.length);
1257
- edges.push({
1258
- sourceId: newSourceId,
1259
- targetId: newTargetId,
1260
- score: edge.score
1261
- });
1262
- }
1263
- }
1264
- }
1265
- return edges;
1203
+ const edges = [];
1204
+ const edgeMap = /* @__PURE__ */ new Map();
1205
+ for (const result of segmentResults) for (const edge of result.edges) {
1206
+ const newSourceId = globalIdMap.get(`${result.segment.index}:${edge.sourceId}`);
1207
+ const newTargetId = globalIdMap.get(`${result.segment.index}:${edge.targetId}`);
1208
+ if (newSourceId === void 0 || newTargetId === void 0) continue;
1209
+ const edgeKey = `${newSourceId}-${newTargetId}`;
1210
+ const existingIdx = edgeMap.get(edgeKey);
1211
+ if (existingIdx !== void 0) {
1212
+ if (edges[existingIdx].score < edge.score) edges[existingIdx] = {
1213
+ sourceId: newSourceId,
1214
+ targetId: newTargetId,
1215
+ score: edge.score
1216
+ };
1217
+ } else {
1218
+ edgeMap.set(edgeKey, edges.length);
1219
+ edges.push({
1220
+ sourceId: newSourceId,
1221
+ targetId: newTargetId,
1222
+ score: edge.score
1223
+ });
1224
+ }
1225
+ }
1226
+ return edges;
1266
1227
  }
1228
+ /**
1229
+ * Remap animation startFrameId/endFrameId using the global ID map.
1230
+ * Animations whose frame IDs were deduplicated (not in map) are dropped.
1231
+ */
1267
1232
  function remapAnimations(segmentResults, globalIdMap) {
1268
- const animations = [];
1269
- for (const result of segmentResults) {
1270
- for (const anim of result.animations) {
1271
- const newStartId = globalIdMap.get(
1272
- `${result.segment.index}:${anim.startFrameId}`
1273
- );
1274
- const newEndId = globalIdMap.get(
1275
- `${result.segment.index}:${anim.endFrameId}`
1276
- );
1277
- if (newStartId === void 0 || newEndId === void 0) continue;
1278
- animations.push({
1279
- ...anim,
1280
- startFrameId: newStartId,
1281
- endFrameId: newEndId
1282
- });
1283
- }
1284
- }
1285
- return animations;
1233
+ const animations = [];
1234
+ for (const result of segmentResults) for (const anim of result.animations) {
1235
+ const newStartId = globalIdMap.get(`${result.segment.index}:${anim.startFrameId}`);
1236
+ const newEndId = globalIdMap.get(`${result.segment.index}:${anim.endFrameId}`);
1237
+ if (newStartId === void 0 || newEndId === void 0) continue;
1238
+ animations.push({
1239
+ ...anim,
1240
+ startFrameId: newStartId,
1241
+ endFrameId: newEndId
1242
+ });
1243
+ }
1244
+ return animations;
1286
1245
  }
1246
+ /**
1247
+ * Merge multiple segment results into a single unified frame/edge/animation set.
1248
+ * - Timestamps adjusted using extractStartTime (Section 18 note 1)
1249
+ * - Overlap frames deduplicated by threshold 1/(effectiveFps*2) (Section 18 note 5)
1250
+ * - Global IDs reassigned after dedup
1251
+ * - Duplicate edges keep higher score
1252
+ */
1287
1253
  function mergeSegmentFrames(segmentResults) {
1288
- if (segmentResults.length === 0) {
1289
- return { frames: [], edges: [], animations: [] };
1290
- }
1291
- const effectiveFps = segmentResults[0].segment.effectiveFps;
1292
- const allFrames = collectAllFrames(segmentResults);
1293
- const uniqueFrames = deduplicateFrames(allFrames, effectiveFps);
1294
- const { frames, globalIdMap } = remapFrameIds(uniqueFrames);
1295
- const edges = remapEdges(segmentResults, globalIdMap);
1296
- const animations = remapAnimations(segmentResults, globalIdMap);
1297
- return { frames, edges, animations };
1254
+ if (segmentResults.length === 0) return {
1255
+ frames: [],
1256
+ edges: [],
1257
+ animations: []
1258
+ };
1259
+ const effectiveFps = segmentResults[0].segment.effectiveFps;
1260
+ const { frames, globalIdMap } = remapFrameIds(deduplicateFrames(collectAllFrames(segmentResults), effectiveFps));
1261
+ return {
1262
+ frames,
1263
+ edges: remapEdges(segmentResults, globalIdMap),
1264
+ animations: remapAnimations(segmentResults, globalIdMap)
1265
+ };
1298
1266
  }
1299
1267
  function buildSegmentContext(segment, frames, segmentWorkspacePath, resolvedOptions, onProgress) {
1300
- return {
1301
- options: {
1302
- ...resolvedOptions,
1303
- fps: segment.effectiveFps,
1304
- maxFrames: segment.allocatedFrames
1305
- },
1306
- workspacePath: segmentWorkspacePath,
1307
- frames,
1308
- graph: [],
1309
- status: "ANALYZING",
1310
- emitProgress: onProgress
1311
- };
1268
+ return {
1269
+ options: {
1270
+ ...resolvedOptions,
1271
+ fps: segment.effectiveFps,
1272
+ maxFrames: segment.allocatedFrames
1273
+ },
1274
+ workspacePath: segmentWorkspacePath,
1275
+ frames,
1276
+ graph: [],
1277
+ status: "ANALYZING",
1278
+ emitProgress: onProgress
1279
+ };
1312
1280
  }
1281
+ /**
1282
+ * Extract frames for a single segment and analyze them.
1283
+ * Each segment uses an isolated workspace directory.
1284
+ */
1313
1285
  async function processSegment(inputPath, segment, workspacePath, resolvedOptions, onProgress) {
1314
- const framesDir = join5(workspacePath, "frames");
1315
- const frames = await extractFramesForRange(
1316
- inputPath,
1317
- framesDir,
1318
- segment.effectiveFps,
1319
- resolvedOptions.scale,
1320
- segment.extractStartTime,
1321
- segment.extractDuration
1322
- );
1323
- if (frames.length < 2) {
1324
- return { segment, frames, edges: [], animations: [] };
1325
- }
1326
- const ctx = buildSegmentContext(
1327
- segment,
1328
- frames,
1329
- workspacePath,
1330
- resolvedOptions,
1331
- onProgress
1332
- );
1333
- const { edges, animations } = await analyzeFrames(ctx);
1334
- return { segment, frames, edges, animations };
1286
+ const frames = await extractFramesForRange(inputPath, join(workspacePath, "frames"), segment.effectiveFps, resolvedOptions.scale, segment.extractStartTime, segment.extractDuration);
1287
+ if (frames.length < 2) return {
1288
+ segment,
1289
+ frames,
1290
+ edges: [],
1291
+ animations: []
1292
+ };
1293
+ const { edges, animations } = await analyzeFrames(buildSegmentContext(segment, frames, workspacePath, resolvedOptions, onProgress));
1294
+ return {
1295
+ segment,
1296
+ frames,
1297
+ edges,
1298
+ animations
1299
+ };
1335
1300
  }
1336
- async function runSegmentedPipeline(options2, resolvedOptions) {
1337
- const pipelineStart = Date.now();
1338
- const sessionId = randomUUID();
1339
- let mainWorkspace = "";
1340
- try {
1341
- mainWorkspace = await createWorkspace(sessionId);
1342
- logger.debug(`Segmented pipeline: workspace at ${mainWorkspace}`);
1343
- const { resolvedInputPath } = await resolveInput(options2, mainWorkspace);
1344
- const inputPath = resolvedInputPath ?? resolvedOptions.inputPath;
1345
- if (!inputPath) {
1346
- throw new Error("No input path available for segmented pipeline");
1347
- }
1348
- const metadata = await getVideoMetadata(inputPath);
1349
- const totalDuration = parseFloat(metadata.format?.duration ?? "0");
1350
- if (totalDuration <= 0) {
1351
- throw new Error(`Invalid video duration: ${totalDuration}`);
1352
- }
1353
- logger.debug(`Video duration: ${totalDuration}s`);
1354
- const segments = computeSegmentPlan(
1355
- totalDuration,
1356
- resolvedOptions.maxSegmentDuration,
1357
- resolvedOptions.maxFrames,
1358
- resolvedOptions.fps
1359
- );
1360
- logger.debug(`Segment plan: ${segments.length} segments`);
1361
- const limit = concurrencyLimit(resolvedOptions.concurrency);
1362
- const segmentProgresses = new Array(segments.length).fill(0);
1363
- const weights = map6(segments, (s) => s.duration / totalDuration);
1364
- const emitOverallProgress = (phase) => {
1365
- if (!options2.onProgress) return;
1366
- const overall = weights.reduce(
1367
- (sum, w, i) => sum + w * (segmentProgresses[i] ?? 0),
1368
- 0
1369
- );
1370
- options2.onProgress(phase, Math.min(100, overall));
1371
- };
1372
- options2.onProgress?.("EXTRACTING", 0);
1373
- const results = await Promise.all(
1374
- map6(
1375
- segments,
1376
- (segment) => limit(async () => {
1377
- const segWorkspace = await createSegmentWorkspace(
1378
- mainWorkspace,
1379
- segment.index
1380
- );
1381
- const result = await processSegment(
1382
- inputPath,
1383
- segment,
1384
- segWorkspace,
1385
- resolvedOptions,
1386
- (percent) => {
1387
- segmentProgresses[segment.index] = percent;
1388
- emitOverallProgress("ANALYZING");
1389
- }
1390
- );
1391
- return result;
1392
- })
1393
- )
1394
- );
1395
- options2.onProgress?.("ANALYZING", 100);
1396
- const { frames, edges, animations } = mergeSegmentFrames(results);
1397
- logger.debug(
1398
- `Merged: ${frames.length} frames, ${edges.length} edges, ${animations.length} animations`
1399
- );
1400
- options2.onProgress?.("PRUNING", 0);
1401
- const survivingIds = pruneByThresholdWithCap(
1402
- edges,
1403
- frames,
1404
- resolvedOptions.threshold,
1405
- resolvedOptions.count
1406
- );
1407
- const prunedFrames = filter5(frames, (f) => survivingIds.has(f.id));
1408
- options2.onProgress?.("PRUNING", 100);
1409
- options2.onProgress?.("FINALIZING", 0);
1410
- const ctx = {
1411
- options: resolvedOptions,
1412
- workspacePath: mainWorkspace,
1413
- frames,
1414
- graph: edges,
1415
- animations,
1416
- status: "FINALIZING",
1417
- emitProgress: (percent) => options2.onProgress?.("FINALIZING", percent)
1418
- };
1419
- let outputFiles = [];
1420
- let outputBuffers;
1421
- if (resolvedOptions.mode === "buffer" || resolvedOptions.mode === "frames") {
1422
- outputBuffers = await readFramesAsBuffers(
1423
- prunedFrames,
1424
- resolvedOptions.quality
1425
- );
1426
- } else {
1427
- outputFiles = await finalizeOutput(ctx, prunedFrames);
1428
- }
1429
- options2.onProgress?.("FINALIZING", 100);
1430
- logger.success(
1431
- `Segmented pipeline: ${prunedFrames.length} scenes from ${frames.length} frames (${segments.length} segments)`
1432
- );
1433
- return {
1434
- success: true,
1435
- originalFramesCount: frames.length,
1436
- prunedFramesCount: prunedFrames.length,
1437
- outputFiles,
1438
- outputBuffers,
1439
- animations,
1440
- video: {
1441
- originalDurationMs: totalDuration * 1e3,
1442
- fps: resolvedOptions.fps,
1443
- resolution: {
1444
- width: resolvedOptions.scale,
1445
- height: Math.round(resolvedOptions.scale * 9 / 16)
1446
- }
1447
- },
1448
- executionTimeMs: Date.now() - pipelineStart
1449
- };
1450
- } catch (error) {
1451
- const err = error instanceof Error ? error : new Error(String(error));
1452
- logger.error(`Segmented pipeline failed: ${err.message}`);
1453
- throw err;
1454
- } finally {
1455
- if (!resolvedOptions.debug) {
1456
- await cleanupWorkspace(mainWorkspace);
1457
- } else {
1458
- logger.debug(`Debug mode: workspace preserved at ${mainWorkspace}`);
1459
- }
1460
- }
1301
+ /**
1302
+ * Full segmented pipeline: metadata → plan → parallel extract+analyze → merge → prune → finalize.
1303
+ * Called from runPipeline when shouldSegment() returns true.
1304
+ */
1305
+ async function runSegmentedPipeline(options, resolvedOptions) {
1306
+ const pipelineStart = Date.now();
1307
+ const sessionId = randomUUID();
1308
+ let mainWorkspace = "";
1309
+ try {
1310
+ mainWorkspace = await createWorkspace(sessionId);
1311
+ logger.debug(`Segmented pipeline: workspace at ${mainWorkspace}`);
1312
+ const { resolvedInputPath } = await resolveInput(options, mainWorkspace);
1313
+ const inputPath = resolvedInputPath ?? resolvedOptions.inputPath;
1314
+ if (!inputPath) throw new Error("No input path available for segmented pipeline");
1315
+ const metadata = await getVideoMetadata(inputPath);
1316
+ const totalDuration = parseFloat(metadata.format?.duration ?? "0");
1317
+ if (totalDuration <= 0) throw new Error(`Invalid video duration: ${totalDuration}`);
1318
+ logger.debug(`Video duration: ${totalDuration}s`);
1319
+ const segments = computeSegmentPlan(totalDuration, resolvedOptions.maxSegmentDuration, resolvedOptions.maxFrames, resolvedOptions.fps);
1320
+ logger.debug(`Segment plan: ${segments.length} segments`);
1321
+ const limit = concurrencyLimit(resolvedOptions.concurrency);
1322
+ const segmentProgresses = new Array(segments.length).fill(0);
1323
+ const weights = map(segments, (s) => s.duration / totalDuration);
1324
+ const emitOverallProgress = (phase) => {
1325
+ if (!options.onProgress) return;
1326
+ const overall = weights.reduce((sum, w, i) => sum + w * (segmentProgresses[i] ?? 0), 0);
1327
+ options.onProgress(phase, Math.min(100, overall));
1328
+ };
1329
+ options.onProgress?.("EXTRACTING", 0);
1330
+ const results = await Promise.all(map(segments, (segment) => limit(async () => {
1331
+ const segWorkspace = await createSegmentWorkspace(mainWorkspace, segment.index);
1332
+ return await processSegment(inputPath, segment, segWorkspace, resolvedOptions, (percent) => {
1333
+ segmentProgresses[segment.index] = percent;
1334
+ emitOverallProgress("ANALYZING");
1335
+ });
1336
+ })));
1337
+ options.onProgress?.("ANALYZING", 100);
1338
+ const { frames, edges, animations } = mergeSegmentFrames(results);
1339
+ logger.debug(`Merged: ${frames.length} frames, ${edges.length} edges, ${animations.length} animations`);
1340
+ options.onProgress?.("PRUNING", 0);
1341
+ const survivingIds = pruneByThresholdWithCap(edges, frames, resolvedOptions.threshold, resolvedOptions.count);
1342
+ const prunedFrames = filter(frames, (f) => survivingIds.has(f.id));
1343
+ options.onProgress?.("PRUNING", 100);
1344
+ options.onProgress?.("FINALIZING", 0);
1345
+ const ctx = {
1346
+ options: resolvedOptions,
1347
+ workspacePath: mainWorkspace,
1348
+ frames,
1349
+ graph: edges,
1350
+ animations,
1351
+ status: "FINALIZING",
1352
+ emitProgress: (percent) => options.onProgress?.("FINALIZING", percent)
1353
+ };
1354
+ let outputFiles = [];
1355
+ let outputBuffers;
1356
+ if (resolvedOptions.mode === "buffer" || resolvedOptions.mode === "frames") outputBuffers = await readFramesAsBuffers(prunedFrames, resolvedOptions.quality);
1357
+ else outputFiles = await finalizeOutput(ctx, prunedFrames);
1358
+ options.onProgress?.("FINALIZING", 100);
1359
+ logger.success(`Segmented pipeline: ${prunedFrames.length} scenes from ${frames.length} frames (${segments.length} segments)`);
1360
+ return {
1361
+ success: true,
1362
+ originalFramesCount: frames.length,
1363
+ prunedFramesCount: prunedFrames.length,
1364
+ outputFiles,
1365
+ outputBuffers,
1366
+ animations,
1367
+ video: {
1368
+ originalDurationMs: totalDuration * 1e3,
1369
+ fps: resolvedOptions.fps,
1370
+ resolution: {
1371
+ width: resolvedOptions.scale,
1372
+ height: Math.round(resolvedOptions.scale * 9 / 16)
1373
+ }
1374
+ },
1375
+ executionTimeMs: Date.now() - pipelineStart
1376
+ };
1377
+ } catch (error) {
1378
+ const err = error instanceof Error ? error : new Error(String(error));
1379
+ logger.error(`Segmented pipeline failed: ${err.message}`);
1380
+ throw err;
1381
+ } finally {
1382
+ if (!resolvedOptions.debug) await cleanupWorkspace(mainWorkspace);
1383
+ else logger.debug(`Debug mode: workspace preserved at ${mainWorkspace}`);
1384
+ }
1461
1385
  }
1462
1386
 
1463
- // src/core/orchestrator.ts
1464
- async function runPipeline(options2) {
1465
- const debug = options2.debug ?? false;
1466
- if (debug) setDebugMode(true);
1467
- const resolvedOptions = resolveOptions(options2);
1468
- if (shouldSegment(resolvedOptions, options2)) {
1469
- return runSegmentedPipeline(options2, resolvedOptions);
1470
- }
1471
- const startTime = Date.now();
1472
- const sessionId = randomUUID2();
1473
- const ctx = {
1474
- options: resolvedOptions,
1475
- workspacePath: "",
1476
- frames: [],
1477
- graph: [],
1478
- status: "INIT",
1479
- emitProgress: (percent) => {
1480
- if (options2.onProgress && ctx.status !== "INIT" && ctx.status !== "SUCCESS" && ctx.status !== "FAILED") {
1481
- options2.onProgress(ctx.status, percent);
1482
- }
1483
- }
1484
- };
1485
- try {
1486
- ctx.workspacePath = await createWorkspace(sessionId);
1487
- logger.debug(`Workspace created: ${ctx.workspacePath}`);
1488
- ctx.status = "EXTRACTING";
1489
- const { frames: resolvedFrames, resolvedInputPath } = await resolveInput(
1490
- options2,
1491
- ctx.workspacePath
1492
- );
1493
- if (resolvedOptions.mode === "frames") {
1494
- ctx.frames = resolvedFrames;
1495
- } else {
1496
- const extractCtx = {
1497
- ...ctx,
1498
- options: {
1499
- ...resolvedOptions,
1500
- inputPath: resolvedInputPath
1501
- }
1502
- };
1503
- ctx.frames = await extractFrames(extractCtx);
1504
- }
1505
- ctx.emitProgress(100);
1506
- ctx.status = "ANALYZING";
1507
- const { edges, animations } = await analyzeFrames(ctx);
1508
- ctx.graph = edges;
1509
- ctx.animations = animations;
1510
- ctx.status = "PRUNING";
1511
- const survivingIds = pruneByThresholdWithCap(
1512
- ctx.graph,
1513
- ctx.frames,
1514
- resolvedOptions.threshold,
1515
- resolvedOptions.count
1516
- );
1517
- const prunedFrames = filter6(ctx.frames, (f) => survivingIds.has(f.id));
1518
- ctx.emitProgress(100);
1519
- ctx.status = "FINALIZING";
1520
- let outputFiles = [];
1521
- let outputBuffers;
1522
- if (resolvedOptions.mode === "buffer" || resolvedOptions.mode === "frames") {
1523
- outputBuffers = await readFramesAsBuffers(
1524
- prunedFrames,
1525
- resolvedOptions.quality
1526
- );
1527
- ctx.emitProgress(100);
1528
- } else {
1529
- outputFiles = await finalizeOutput(ctx, prunedFrames);
1530
- ctx.emitProgress(100);
1531
- }
1532
- ctx.status = "SUCCESS";
1533
- logger.success(
1534
- `Extracted ${prunedFrames.length} scenes from ${ctx.frames.length} frames`
1535
- );
1536
- return {
1537
- success: true,
1538
- originalFramesCount: ctx.frames.length,
1539
- prunedFramesCount: prunedFrames.length,
1540
- outputFiles,
1541
- outputBuffers,
1542
- animations: ctx.animations,
1543
- video: {
1544
- originalDurationMs: ctx.frames.length / ctx.options.fps * 1e3,
1545
- fps: ctx.options.fps,
1546
- resolution: {
1547
- width: ctx.options.scale,
1548
- height: Math.round(ctx.options.scale * 9 / 16)
1549
- }
1550
- },
1551
- executionTimeMs: Date.now() - startTime
1552
- };
1553
- } catch (error) {
1554
- ctx.status = "FAILED";
1555
- ctx.error = error instanceof Error ? error : new Error(String(error));
1556
- logger.error(`Pipeline failed: ${ctx.error.message}`);
1557
- throw ctx.error;
1558
- } finally {
1559
- if (!resolvedOptions.debug) {
1560
- await cleanupWorkspace(ctx.workspacePath);
1561
- } else {
1562
- logger.debug(`Debug mode: workspace preserved at ${ctx.workspacePath}`);
1563
- }
1564
- }
1387
+ //#endregion
1388
+ //#region src/core/orchestrator.ts
1389
+ async function runPipeline(options) {
1390
+ if (options.debug ?? false) setDebugMode(true);
1391
+ const resolvedOptions = resolveOptions(options);
1392
+ if (shouldSegment(resolvedOptions, options)) return runSegmentedPipeline(options, resolvedOptions);
1393
+ const startTime = Date.now();
1394
+ const sessionId = randomUUID();
1395
+ const ctx = {
1396
+ options: resolvedOptions,
1397
+ workspacePath: "",
1398
+ frames: [],
1399
+ graph: [],
1400
+ status: "INIT",
1401
+ emitProgress: (percent) => {
1402
+ if (options.onProgress && ctx.status !== "INIT" && ctx.status !== "SUCCESS" && ctx.status !== "FAILED") options.onProgress(ctx.status, percent);
1403
+ }
1404
+ };
1405
+ try {
1406
+ ctx.workspacePath = await createWorkspace(sessionId);
1407
+ logger.debug(`Workspace created: ${ctx.workspacePath}`);
1408
+ ctx.status = "EXTRACTING";
1409
+ const { frames: resolvedFrames, resolvedInputPath } = await resolveInput(options, ctx.workspacePath);
1410
+ if (resolvedOptions.mode === "frames") ctx.frames = resolvedFrames;
1411
+ else ctx.frames = await extractFrames({
1412
+ ...ctx,
1413
+ options: {
1414
+ ...resolvedOptions,
1415
+ inputPath: resolvedInputPath
1416
+ }
1417
+ });
1418
+ ctx.emitProgress(100);
1419
+ ctx.status = "ANALYZING";
1420
+ const { edges, animations } = await analyzeFrames(ctx);
1421
+ ctx.graph = edges;
1422
+ ctx.animations = animations;
1423
+ ctx.status = "PRUNING";
1424
+ const survivingIds = pruneByThresholdWithCap(ctx.graph, ctx.frames, resolvedOptions.threshold, resolvedOptions.count);
1425
+ const prunedFrames = filter(ctx.frames, (f) => survivingIds.has(f.id));
1426
+ ctx.emitProgress(100);
1427
+ ctx.status = "FINALIZING";
1428
+ let outputFiles = [];
1429
+ let outputBuffers;
1430
+ if (resolvedOptions.mode === "buffer" || resolvedOptions.mode === "frames") {
1431
+ outputBuffers = await readFramesAsBuffers(prunedFrames, resolvedOptions.quality);
1432
+ ctx.emitProgress(100);
1433
+ } else {
1434
+ outputFiles = await finalizeOutput(ctx, prunedFrames);
1435
+ ctx.emitProgress(100);
1436
+ }
1437
+ ctx.status = "SUCCESS";
1438
+ logger.success(`Extracted ${prunedFrames.length} scenes from ${ctx.frames.length} frames`);
1439
+ return {
1440
+ success: true,
1441
+ originalFramesCount: ctx.frames.length,
1442
+ prunedFramesCount: prunedFrames.length,
1443
+ outputFiles,
1444
+ outputBuffers,
1445
+ animations: ctx.animations,
1446
+ video: {
1447
+ originalDurationMs: ctx.frames.length / ctx.options.fps * 1e3,
1448
+ fps: ctx.options.fps,
1449
+ resolution: {
1450
+ width: ctx.options.scale,
1451
+ height: Math.round(ctx.options.scale * 9 / 16)
1452
+ }
1453
+ },
1454
+ executionTimeMs: Date.now() - startTime
1455
+ };
1456
+ } catch (error) {
1457
+ ctx.status = "FAILED";
1458
+ ctx.error = error instanceof Error ? error : new Error(String(error));
1459
+ logger.error(`Pipeline failed: ${ctx.error.message}`);
1460
+ throw ctx.error;
1461
+ } finally {
1462
+ if (!resolvedOptions.debug) await cleanupWorkspace(ctx.workspacePath);
1463
+ else logger.debug(`Debug mode: workspace preserved at ${ctx.workspacePath}`);
1464
+ }
1565
1465
  }
1566
1466
 
1567
- // src/core/pipeline-worker.ts
1568
- var options = workerData;
1467
+ //#endregion
1468
+ //#region src/core/pipeline-worker.ts
1569
1469
  runPipeline({
1570
- ...options,
1571
- onProgress: (phase, percent) => {
1572
- parentPort?.postMessage({
1573
- type: "progress",
1574
- phase,
1575
- percent
1576
- });
1577
- }
1470
+ ...workerData,
1471
+ onProgress: (phase, percent) => {
1472
+ parentPort?.postMessage({
1473
+ type: "progress",
1474
+ phase,
1475
+ percent
1476
+ });
1477
+ }
1578
1478
  }).then((result) => {
1579
- parentPort?.postMessage({
1580
- type: "result",
1581
- result
1582
- });
1479
+ parentPort?.postMessage({
1480
+ type: "result",
1481
+ result
1482
+ });
1583
1483
  }).catch((error) => {
1584
- parentPort?.postMessage({
1585
- type: "error",
1586
- message: error instanceof Error ? error.message : String(error)
1587
- });
1484
+ parentPort?.postMessage({
1485
+ type: "error",
1486
+ message: error instanceof Error ? error.message : String(error)
1487
+ });
1588
1488
  });
1489
+
1490
+ //#endregion
1491
+ export { };