agentcut 0.1.1 → 0.2.2

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.
@@ -0,0 +1,754 @@
1
+ import { spawn } from "node:child_process";
2
+ import { createHash, randomUUID } from "node:crypto";
3
+ import { chmodSync, createWriteStream, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, } from "node:fs";
4
+ import { homedir } from "node:os";
5
+ import { dirname, join } from "node:path";
6
+ import { Readable, Transform } from "node:stream";
7
+ import { pipeline } from "node:stream/promises";
8
+ import { createGunzip } from "node:zlib";
9
+ import { debugLog } from "../log.js";
10
+ import { killTree } from "./proc.js";
11
+ import { assertSlug, projectDir, safeJoin, StudioError } from "./projects.js";
12
+ // Style Spy references: the helper downloads a public Instagram, TikTok, YouTube or X video with
13
+ // yt-dlp, then samples frames with ffmpeg so the agent can look at it. Both tools are fetched on
14
+ // first use into ~/.agentcut/tools (checksums verified), never installed system-wide.
15
+ export const REFERENCE_DOMAINS = ["instagram.com", "tiktok.com", "youtube.com", "youtu.be", "x.com", "twitter.com"];
16
+ export const LOGIN_BROWSERS = ["chrome", "safari", "firefox", "edge", "brave"];
17
+ const MAX_BYTES = 200 * 1024 * 1024;
18
+ const MAX_SECONDS = 600;
19
+ // Frames: one per shot (several for long shots), capped.
20
+ const SCENE_THRESHOLD = 0.3;
21
+ const MIN_SHOT_SECONDS = 0.4;
22
+ const LONG_SHOT_SECONDS = 6;
23
+ const MAX_FRAMES = 48;
24
+ const MAX_CUT_STRIPS = 30;
25
+ const YTDLP_CHECK_MS = 3 * 24 * 60 * 60 * 1000;
26
+ const VIDEO_EXT = /\.(mp4|webm|mov|m4v)$/i;
27
+ // ffmpeg doesn't move fast; pin a build and its checksum (github.com/eugeneware/ffmpeg-static).
28
+ const FFMPEG_RELEASE = "b6.1.1";
29
+ const FFMPEG_BUILDS = {
30
+ "darwin-arm64": { asset: "ffmpeg-darwin-arm64.gz", sha256: "8923876afa8db5585022d7860ec7e589af192f441c56793971276d450ed3bbfa" },
31
+ "darwin-x64": { asset: "ffmpeg-darwin-x64.gz", sha256: "929b375c1182d956c51f7ac25e0b2b0411fb01f6f407aa15c9758efeb4242106" },
32
+ "linux-x64": { asset: "ffmpeg-linux-x64.gz", sha256: "bfe8a8fc511530457b528c48d77b5737527b504a3797a9bc4866aeca69c2dffa" },
33
+ "linux-arm64": { asset: "ffmpeg-linux-arm64.gz", sha256: "754a678672298bc68156adff58aa7385a592c2b30b1d0ae8750c45c915c4bac0" },
34
+ "win32-x64": { asset: "ffmpeg-win32-x64.gz", sha256: "8883a3dffbd0a16cf4ef95206ea05283f78908dbfb118f73c83f4951dcc06d77" },
35
+ // Windows on ARM runs the x64 build.
36
+ "win32-arm64": { asset: "ffmpeg-win32-x64.gz", sha256: "8883a3dffbd0a16cf4ef95206ea05283f78908dbfb118f73c83f4951dcc06d77" },
37
+ };
38
+ // Sites change often and yt-dlp follows within days: track its latest release (SHA2-256SUMS verified).
39
+ const YTDLP_ASSETS = {
40
+ "darwin-arm64": "yt-dlp_macos",
41
+ "darwin-x64": "yt-dlp_macos",
42
+ "linux-x64": "yt-dlp_linux",
43
+ "linux-arm64": "yt-dlp_linux_aarch64",
44
+ "win32-x64": "yt-dlp.exe",
45
+ "win32-arm64": "yt-dlp_arm64.exe",
46
+ };
47
+ const platformKey = `${process.platform}-${process.arch}`;
48
+ const exe = process.platform === "win32" ? ".exe" : "";
49
+ class ReferenceError extends Error {
50
+ code;
51
+ constructor(code, message) {
52
+ super(message);
53
+ this.code = code;
54
+ }
55
+ }
56
+ export function normalizeReferenceUrl(raw) {
57
+ let url;
58
+ try {
59
+ url = new URL(String(raw ?? "").trim());
60
+ }
61
+ catch {
62
+ throw new StudioError("Paste an HTTPS video link from Instagram, TikTok, YouTube, or X.", "bad_input");
63
+ }
64
+ if (url.protocol !== "https:" ||
65
+ url.username ||
66
+ url.password ||
67
+ url.port ||
68
+ !REFERENCE_DOMAINS.some((domain) => url.hostname === domain || url.hostname.endsWith(`.${domain}`))) {
69
+ throw new StudioError("Paste an HTTPS video link from Instagram, TikTok, YouTube, or X.", "bad_input");
70
+ }
71
+ return url.href;
72
+ }
73
+ function loginBrowser(value) {
74
+ if (value === undefined || value === null || value === "")
75
+ return null;
76
+ if (!LOGIN_BROWSERS.includes(value))
77
+ throw new StudioError(`Unknown browser "${String(value)}".`, "bad_input");
78
+ return value;
79
+ }
80
+ const BROWSER_NAMES = {
81
+ chrome: "Chrome",
82
+ safari: "Safari",
83
+ firefox: "Firefox",
84
+ edge: "Edge",
85
+ brave: "Brave",
86
+ };
87
+ /** Browsers with a profile on this computer (where yt-dlp reads logins), most convenient first. */
88
+ export function installedBrowsers() {
89
+ const home = homedir();
90
+ const local = process.env.LOCALAPPDATA ?? join(home, "AppData", "Local");
91
+ const roaming = process.env.APPDATA ?? join(home, "AppData", "Roaming");
92
+ const support = join(home, "Library", "Application Support");
93
+ const profiles = process.platform === "darwin"
94
+ ? {
95
+ chrome: join(support, "Google", "Chrome"),
96
+ brave: join(support, "BraveSoftware", "Brave-Browser"),
97
+ edge: join(support, "Microsoft Edge"),
98
+ firefox: join(support, "Firefox", "Profiles"),
99
+ safari: "/Applications/Safari.app",
100
+ }
101
+ : process.platform === "win32"
102
+ ? {
103
+ chrome: join(local, "Google", "Chrome", "User Data"),
104
+ brave: join(local, "BraveSoftware", "Brave-Browser", "User Data"),
105
+ edge: join(local, "Microsoft", "Edge", "User Data"),
106
+ firefox: join(roaming, "Mozilla", "Firefox", "Profiles"),
107
+ safari: null,
108
+ }
109
+ : {
110
+ chrome: join(home, ".config", "google-chrome"),
111
+ brave: join(home, ".config", "BraveSoftware", "Brave-Browser"),
112
+ edge: join(home, ".config", "microsoft-edge"),
113
+ firefox: join(home, ".mozilla", "firefox"),
114
+ safari: null,
115
+ };
116
+ const order = ["chrome", "brave", "edge", "firefox", "safari"];
117
+ return order.filter((browser) => {
118
+ const dir = profiles[browser];
119
+ return dir !== null && existsSync(dir);
120
+ });
121
+ }
122
+ function toolsDir() {
123
+ return process.env.AGENTCUT_TOOLS || join(homedir(), ".agentcut", "tools");
124
+ }
125
+ function readManifest() {
126
+ try {
127
+ return JSON.parse(readFileSync(join(toolsDir(), "tools.json"), "utf8"));
128
+ }
129
+ catch {
130
+ return {};
131
+ }
132
+ }
133
+ function writeManifest(update) {
134
+ mkdirSync(toolsDir(), { recursive: true });
135
+ writeFileSync(join(toolsDir(), "tools.json"), `${JSON.stringify({ ...readManifest(), ...update }, null, 2)}\n`);
136
+ }
137
+ /** Download to `target`, checking the SHA-256 of the bytes as served (before gunzip). */
138
+ async function downloadVerified(url, sha256, target, gunzip) {
139
+ const response = await fetch(url, { signal: AbortSignal.timeout(5 * 60_000) });
140
+ if (!response.ok || !response.body)
141
+ throw new Error(`Download failed (${response.status}) for ${url}`);
142
+ mkdirSync(dirname(target), { recursive: true });
143
+ const temp = `${target}.${randomUUID()}.tmp`;
144
+ const hash = createHash("sha256");
145
+ let size = 0;
146
+ const tap = new Transform({
147
+ transform(chunk, _encoding, done) {
148
+ size += chunk.length;
149
+ if (size > 300 * 1024 * 1024)
150
+ return done(new Error("Tool download is unexpectedly large."));
151
+ hash.update(chunk);
152
+ done(null, chunk);
153
+ },
154
+ });
155
+ try {
156
+ const source = Readable.fromWeb(response.body);
157
+ if (gunzip)
158
+ await pipeline(source, tap, createGunzip(), createWriteStream(temp));
159
+ else
160
+ await pipeline(source, tap, createWriteStream(temp));
161
+ const digest = hash.digest("hex");
162
+ if (digest !== sha256.toLowerCase())
163
+ throw new Error(`Checksum mismatch for ${url}`);
164
+ chmodSync(temp, 0o755);
165
+ renameSync(temp, target);
166
+ }
167
+ finally {
168
+ rmSync(temp, { force: true });
169
+ }
170
+ }
171
+ let ffmpegReady = null;
172
+ let ytdlpReady = null;
173
+ function ffmpegPath() {
174
+ return join(toolsDir(), "ffmpeg", FFMPEG_RELEASE, `ffmpeg${exe}`);
175
+ }
176
+ function ytdlpPath() {
177
+ return join(toolsDir(), "yt-dlp", `yt-dlp${exe}`);
178
+ }
179
+ export function ensureFfmpeg(onDownload) {
180
+ ffmpegReady ??= (async () => {
181
+ const target = ffmpegPath();
182
+ if (existsSync(target))
183
+ return target;
184
+ const build = FFMPEG_BUILDS[platformKey];
185
+ if (!build)
186
+ throw new ReferenceError("setup", `Video tools aren't available for ${platformKey}. Upload the video instead.`);
187
+ onDownload?.();
188
+ await downloadVerified(`https://github.com/eugeneware/ffmpeg-static/releases/download/${FFMPEG_RELEASE}/${build.asset}`, build.sha256, target, true);
189
+ writeManifest({ ffmpeg: { version: FFMPEG_RELEASE } });
190
+ debugLog(`references: installed ffmpeg ${FFMPEG_RELEASE}`);
191
+ return target;
192
+ })().catch((error) => {
193
+ ffmpegReady = null;
194
+ throw error;
195
+ });
196
+ return ffmpegReady;
197
+ }
198
+ async function latestYtdlpVersion() {
199
+ // /latest/download/ redirects to /download/<version>/: read the version without the API rate limit.
200
+ const response = await fetch("https://github.com/yt-dlp/yt-dlp/releases/latest/download/SHA2-256SUMS", {
201
+ redirect: "manual",
202
+ signal: AbortSignal.timeout(20_000),
203
+ });
204
+ const match = /\/releases\/download\/([^/]+)\//.exec(response.headers.get("location") ?? "");
205
+ if (!match)
206
+ throw new Error(`Couldn't find the latest yt-dlp release (${response.status}).`);
207
+ return match[1];
208
+ }
209
+ /** yt-dlp, updated every few days (or now with `force`, after an unexplained failure). */
210
+ export function ensureYtdlp({ force = false, onDownload } = {}) {
211
+ if (ytdlpReady && !force)
212
+ return ytdlpReady;
213
+ ytdlpReady = (async () => {
214
+ const target = ytdlpPath();
215
+ const current = readManifest().ytdlp;
216
+ const installed = existsSync(target) && current;
217
+ if (installed && !force && Date.now() - current.checkedAt < YTDLP_CHECK_MS)
218
+ return target;
219
+ const asset = YTDLP_ASSETS[platformKey];
220
+ if (!asset)
221
+ throw new ReferenceError("setup", `Video tools aren't available for ${platformKey}. Upload the video instead.`);
222
+ let version;
223
+ try {
224
+ version = await latestYtdlpVersion();
225
+ }
226
+ catch (error) {
227
+ if (installed)
228
+ return target; // offline: keep the copy we have
229
+ throw error;
230
+ }
231
+ if (installed && current.version === version) {
232
+ writeManifest({ ytdlp: { version, checkedAt: Date.now() } });
233
+ return target;
234
+ }
235
+ onDownload?.();
236
+ const base = `https://github.com/yt-dlp/yt-dlp/releases/download/${version}`;
237
+ const sums = await fetch(`${base}/SHA2-256SUMS`, { signal: AbortSignal.timeout(20_000) }).then((r) => {
238
+ if (!r.ok)
239
+ throw new Error(`Couldn't fetch yt-dlp checksums (${r.status}).`);
240
+ return r.text();
241
+ });
242
+ const line = sums.split(/\r?\n/).find((l) => l.trim().endsWith(` ${asset}`) || l.trim().endsWith(` *${asset}`));
243
+ const sha256 = line?.trim().split(/\s+/)[0];
244
+ if (!sha256 || !/^[a-f0-9]{64}$/i.test(sha256))
245
+ throw new Error(`No checksum for ${asset} in yt-dlp ${version}.`);
246
+ await downloadVerified(`${base}/${asset}`, sha256, target, false);
247
+ writeManifest({ ytdlp: { version, checkedAt: Date.now() } });
248
+ debugLog(`references: installed yt-dlp ${version}`);
249
+ return target;
250
+ })().catch((error) => {
251
+ ytdlpReady = null;
252
+ throw error;
253
+ });
254
+ return ytdlpReady;
255
+ }
256
+ function run(command, args, { timeoutMs, onLine, onChild }) {
257
+ return new Promise((resolve, reject) => {
258
+ const child = spawn(command, args, {
259
+ shell: false,
260
+ windowsHide: true,
261
+ detached: process.platform !== "win32",
262
+ stdio: ["ignore", "pipe", "pipe"],
263
+ });
264
+ onChild?.(child);
265
+ let stdout = "";
266
+ let stderr = "";
267
+ let partial = "";
268
+ const keep = (text, more) => (text + more).slice(-64_000);
269
+ const timer = setTimeout(() => killTree(child), timeoutMs);
270
+ child.stdout?.on("data", (chunk) => {
271
+ const text = chunk.toString("utf8");
272
+ stdout = keep(stdout, text);
273
+ if (!onLine)
274
+ return;
275
+ const lines = (partial + text).split(/\r?\n/);
276
+ partial = lines.pop() ?? "";
277
+ for (const line of lines)
278
+ onLine(line);
279
+ });
280
+ child.stderr?.on("data", (chunk) => {
281
+ stderr = keep(stderr, chunk.toString("utf8"));
282
+ });
283
+ child.on("error", (error) => {
284
+ clearTimeout(timer);
285
+ onChild?.(null);
286
+ reject(error);
287
+ });
288
+ child.on("close", (code) => {
289
+ clearTimeout(timer);
290
+ onChild?.(null);
291
+ if (partial && onLine)
292
+ onLine(partial);
293
+ resolve({ code, stdout, stderr });
294
+ });
295
+ });
296
+ }
297
+ /** Turn yt-dlp's output into something the user can act on. */
298
+ export function classifyFailure(output, browser) {
299
+ const text = output.toLowerCase();
300
+ const name = browser ? BROWSER_NAMES[browser] : "";
301
+ if (browser && /could not find .* cookies database|failed to find .* profile/.test(text)) {
302
+ return new ReferenceError("cookies", `Couldn't find a ${name} profile on this computer. Pick the browser you use for this site.`);
303
+ }
304
+ if (browser &&
305
+ /(failed to (?:decrypt|read)|unable to (?:read|get key)|keyring|keychain|cookies? database|could not copy|operation not permitted|permission denied)/.test(text)) {
306
+ return new ReferenceError("cookies", browser === "safari"
307
+ ? "Couldn't read your Safari login. Safari needs Full Disk Access for this, so try Chrome or Firefox instead."
308
+ : `Couldn't read your ${name} login. Click Allow if your computer asks, or pick another browser.`);
309
+ }
310
+ if (/does not pass filter/.test(text)) {
311
+ return new ReferenceError("too_long", "This video is longer than 10 minutes or is a live stream. Upload a shorter clip instead.");
312
+ }
313
+ if (/larger than max-filesize/.test(text)) {
314
+ return new ReferenceError("too_large", "This video is larger than 200 MB. Upload a shorter clip instead.");
315
+ }
316
+ if (/unsupported url/.test(text)) {
317
+ return new ReferenceError("unsupported", "That link doesn't point to a video. Copy the link of the reel or video itself.");
318
+ }
319
+ if (/(login required|log in|logged-in|sign in|--cookies|empty media response|rate-limit reached|private|age[- ]restricted|inappropriate|confirm your age|not a bot)/.test(text)) {
320
+ return browser
321
+ ? new ReferenceError("login_required", `Your ${name} login couldn't open this video. Check that you're signed in there and can watch it, or upload it instead.`)
322
+ : new ReferenceError("login_required", "This video needs a login to download.");
323
+ }
324
+ if (/(video unavailable|not available|has been removed|does not exist|http error 404|no video formats|no video could be found)/.test(text)) {
325
+ return new ReferenceError("unavailable", "This video is unavailable. It may have been removed or restricted. Upload it instead.");
326
+ }
327
+ if (/(unable to download webpage|timed out|getaddrinfo|connection (?:reset|refused)|network is unreachable|ssl)/.test(text)) {
328
+ return new ReferenceError("network", "Couldn't reach the site. Check your connection and try again.");
329
+ }
330
+ return new ReferenceError("failed", "Couldn't download this video. Try again, or upload it instead.");
331
+ }
332
+ async function probeDuration(ffmpeg, file) {
333
+ // `ffmpeg -i` with no output exits 1 but prints the input's duration.
334
+ const result = await run(ffmpeg, ["-hide_banner", "-i", file], { timeoutMs: 30_000 });
335
+ const match = /Duration:\s*(\d+):(\d+):(\d+(?:\.\d+)?)/.exec(result.stderr);
336
+ if (!match)
337
+ throw new ReferenceError("failed", "This file doesn't look like a video ffmpeg can read.");
338
+ return Number(match[1]) * 3600 + Number(match[2]) * 60 + Number(match[3]);
339
+ }
340
+ /** Cut times (seconds) where the picture changes: hard cuts and layout switches (split screens, cards). */
341
+ async function detectCuts(ffmpeg, video) {
342
+ const result = await run(ffmpeg, ["-hide_banner", "-i", video, "-an", "-vf", `scale=160:-2,select='gt(scene,${SCENE_THRESHOLD})',showinfo`, "-f", "null", "-"], { timeoutMs: 5 * 60_000 });
343
+ const cuts = [];
344
+ for (const match of result.stderr.matchAll(/pts_time:([\d.]+)/g)) {
345
+ const time = Number(match[1]);
346
+ if (time - (cuts.at(-1) ?? 0) >= MIN_SHOT_SECONDS)
347
+ cuts.push(Math.round(time * 100) / 100);
348
+ }
349
+ return cuts;
350
+ }
351
+ /**
352
+ * Shots from cut times, and when to grab frames: the opening, the middle of every shot (more for
353
+ * long ones), and the ending. Capped at MAX_FRAMES, keeping the opening and an even spread.
354
+ */
355
+ export function planFrames(duration, cuts) {
356
+ const bounds = [0, ...cuts.filter((t) => t > 0 && t < duration), duration];
357
+ const shots = bounds.slice(0, -1).map((start, i) => ({ start, end: bounds[i + 1] }));
358
+ let times = [Math.min(0.3, duration / 2)];
359
+ for (const shot of shots) {
360
+ const length = shot.end - shot.start;
361
+ const count = Math.max(1, Math.ceil(length / LONG_SHOT_SECONDS));
362
+ for (let i = 0; i < count; i += 1)
363
+ times.push(shot.start + ((i + 0.5) * length) / count);
364
+ }
365
+ times.push(Math.max(0, duration - 0.3));
366
+ times = [...new Set(times.map((t) => Math.round(t * 100) / 100))].sort((a, b) => a - b);
367
+ if (times.length > MAX_FRAMES) {
368
+ const step = (times.length - 1) / (MAX_FRAMES - 1);
369
+ times = Array.from({ length: MAX_FRAMES }, (_, i) => times[Math.round(i * step)]);
370
+ }
371
+ return { shots, times };
372
+ }
373
+ /** Windows worth seeing in motion: the hook, around every cut, and a stretch of captions. */
374
+ export function planStrips(duration, shots) {
375
+ const round = (t) => Math.round(t * 100) / 100;
376
+ const strips = [{ kind: "hook", start: 0, end: round(Math.min(3, duration)), fps: 6 }];
377
+ const cuts = shots.slice(1).map((shot) => shot.start);
378
+ const step = cuts.length > MAX_CUT_STRIPS ? cuts.length / MAX_CUT_STRIPS : 1;
379
+ for (let i = 0; i < cuts.length && strips.length <= MAX_CUT_STRIPS; i += step) {
380
+ const at = cuts[Math.floor(i)];
381
+ strips.push({ kind: "cut", at, start: round(Math.max(0, at - 0.25)), end: round(Math.min(duration, at + 0.75)), fps: 8 });
382
+ }
383
+ const longest = [...shots].sort((a, b) => b.end - b.start - (a.end - a.start))[0];
384
+ if (longest && longest.end - longest.start >= 1.5) {
385
+ const start = round(longest.start + Math.min(0.5, (longest.end - longest.start) / 4));
386
+ strips.push({ kind: "captions", start, end: round(Math.min(longest.end, start + 2)), fps: 8 });
387
+ }
388
+ return strips;
389
+ }
390
+ async function renderStrip(ffmpeg, video, strip, file) {
391
+ const count = Math.max(1, Math.round((strip.end - strip.start) * strip.fps));
392
+ const columns = Math.min(count, strip.kind === "hook" ? 6 : 8);
393
+ const rows = Math.ceil(count / columns);
394
+ await run(ffmpeg, [
395
+ "-hide_banner",
396
+ "-loglevel",
397
+ "error",
398
+ "-ss",
399
+ String(strip.start),
400
+ "-t",
401
+ String(strip.end - strip.start),
402
+ "-i",
403
+ video,
404
+ "-an",
405
+ "-vf",
406
+ `fps=${strip.fps},scale=-2:320,tile=${columns}x${rows}:padding=4:color=black`,
407
+ "-frames:v",
408
+ "1",
409
+ "-q:v",
410
+ "4",
411
+ "-y",
412
+ file,
413
+ ], { timeoutMs: 60_000 });
414
+ return existsSync(file);
415
+ }
416
+ /** Cuts, shots, frames and an overview sheet in `<folder>/frames`, for the agent to read. */
417
+ async function analyzeVideo(ffmpeg, video, folder, rel) {
418
+ const duration = await probeDuration(ffmpeg, video);
419
+ const cuts = await detectCuts(ffmpeg, video).catch(() => []);
420
+ const { shots, times } = planFrames(duration, cuts);
421
+ const dir = join(folder, "frames");
422
+ rmSync(dir, { recursive: true, force: true });
423
+ mkdirSync(dir, { recursive: true });
424
+ const frames = [];
425
+ for (const time of times) {
426
+ const name = `${String(frames.length + 1).padStart(2, "0")}.jpg`;
427
+ await run(ffmpeg, ["-hide_banner", "-loglevel", "error", "-ss", String(time), "-i", video, "-frames:v", "1", "-vf", "scale='if(gt(iw,ih),min(1280,iw),-2)':'if(gt(iw,ih),-2,min(1280,ih))'", "-q:v", "3", "-y", join(dir, name)], { timeoutMs: 60_000 });
428
+ if (existsSync(join(dir, name)))
429
+ frames.push({ path: `${rel}/frames/${name}`, time });
430
+ }
431
+ if (!frames.length)
432
+ throw new ReferenceError("failed", "Couldn't read frames from this video. Try uploading an MP4.");
433
+ const columns = Math.min(8, frames.length);
434
+ const rows = Math.ceil(frames.length / columns);
435
+ await run(ffmpeg, ["-hide_banner", "-loglevel", "error", "-i", join(dir, "%02d.jpg"), "-vf", `scale=-2:360,tile=${columns}x${rows}:padding=6:color=black`, "-frames:v", "1", "-q:v", "4", "-y", join(dir, "sheet.jpg")], { timeoutMs: 60_000 }).catch(() => undefined);
436
+ const motionDir = join(folder, "motion");
437
+ rmSync(motionDir, { recursive: true, force: true });
438
+ mkdirSync(motionDir, { recursive: true });
439
+ const strips = [];
440
+ for (const strip of planStrips(duration, shots)) {
441
+ const name = strip.kind === "cut" ? `cut-${String(strips.length).padStart(2, "0")}.jpg` : `${strip.kind}.jpg`;
442
+ const ok = await renderStrip(ffmpeg, video, strip, join(motionDir, name)).catch(() => false);
443
+ if (ok)
444
+ strips.push({ ...strip, path: `${rel}/motion/${name}` });
445
+ }
446
+ return {
447
+ duration: Math.round(duration * 100) / 100,
448
+ cuts,
449
+ averageShotSeconds: Math.round((duration / shots.length) * 10) / 10,
450
+ shots: shots.map((shot) => ({
451
+ start: Math.round(shot.start * 100) / 100,
452
+ end: Math.round(shot.end * 100) / 100,
453
+ frames: frames.filter((f) => f.time >= shot.start && f.time < shot.end).map((f) => f.path),
454
+ })),
455
+ frames,
456
+ sheet: existsSync(join(dir, "sheet.jpg")) ? `${rel}/frames/sheet.jpg` : null,
457
+ strips,
458
+ };
459
+ }
460
+ // ---------------------------------------------------------------------------
461
+ // Jobs
462
+ export class ReferenceImports {
463
+ paths;
464
+ emit;
465
+ onAssets;
466
+ jobs = new Map();
467
+ waiters = new Map();
468
+ children = new Map();
469
+ cancelled = new Set();
470
+ constructor(paths, emit, onAssets) {
471
+ this.paths = paths;
472
+ this.emit = emit;
473
+ this.onAssets = onAssets;
474
+ }
475
+ list(slug) {
476
+ return [...this.jobs.values()].filter((job) => job.slug === slug);
477
+ }
478
+ /** Import a link. Pass a failed job's `id` with `browser` to retry it with the user's browser login. */
479
+ importUrl(slugInput, rawUrl, options = {}) {
480
+ const slug = assertSlug(slugInput);
481
+ const browser = loginBrowser(options.browser);
482
+ let url;
483
+ let id;
484
+ const previous = typeof options.id === "string" ? this.jobs.get(options.id) : undefined;
485
+ if (previous) {
486
+ if (previous.slug !== slug || !previous.url)
487
+ throw new StudioError("Unknown reference import.", "bad_input");
488
+ if (previous.stage !== "failed")
489
+ return previous;
490
+ url = previous.url;
491
+ id = previous.id;
492
+ }
493
+ else {
494
+ url = normalizeReferenceUrl(rawUrl);
495
+ id = randomUUID();
496
+ }
497
+ return this.start({ id, slug, url, browser, stage: "setup", progress: null }, () => this.download(id));
498
+ }
499
+ /** Sample frames and write reference.json for a video already in assets/references/<id>/ (an upload). */
500
+ prepare(slugInput, pathInput) {
501
+ const slug = assertSlug(slugInput);
502
+ const path = String(pathInput ?? "");
503
+ const match = /^assets\/references\/([a-zA-Z0-9_-]{1,80})\/[^/]+$/.exec(path);
504
+ if (!match || !VIDEO_EXT.test(path))
505
+ throw new StudioError("Reference videos live in assets/references/<id>/.", "bad_input");
506
+ if (!existsSync(safeJoin(projectDir(this.paths, slug), path)))
507
+ throw new StudioError(`${path} doesn't exist.`, "bad_input");
508
+ const id = match[1];
509
+ const running = this.jobs.get(id);
510
+ if (running && running.stage !== "ready" && running.stage !== "failed")
511
+ return running;
512
+ return this.start({ id, slug, url: null, browser: null, stage: "setup", progress: null }, () => this.prepareExisting(id, path));
513
+ }
514
+ wait(id) {
515
+ return this.waiters.get(id) ?? Promise.resolve(this.jobs.get(id));
516
+ }
517
+ cancel(id) {
518
+ const job = this.jobs.get(id);
519
+ if (!job || job.stage === "ready" || job.stage === "failed")
520
+ return { cancelled: false };
521
+ this.cancelled.add(id);
522
+ const child = this.children.get(id);
523
+ if (child)
524
+ killTree(child);
525
+ return { cancelled: true };
526
+ }
527
+ cancelAll() {
528
+ for (const id of this.children.keys())
529
+ this.cancel(id);
530
+ }
531
+ update(id, patch) {
532
+ const job = this.jobs.get(id);
533
+ if (!job)
534
+ return;
535
+ const next = { ...job, ...patch };
536
+ this.jobs.set(id, next);
537
+ this.emit(next);
538
+ }
539
+ start(job, work) {
540
+ this.cancelled.delete(job.id);
541
+ this.jobs.set(job.id, job);
542
+ this.emit(job);
543
+ const done = work()
544
+ .catch((error) => {
545
+ const failure = this.cancelled.has(job.id)
546
+ ? new ReferenceError("cancelled", "Import cancelled.")
547
+ : error instanceof ReferenceError
548
+ ? error
549
+ : new ReferenceError("setup", `Couldn't set up the video tools: ${error instanceof Error ? error.message : String(error)}`);
550
+ debugLog(`references: ${job.id} failed (${failure.code}) ${error instanceof Error ? error.message : String(error)}`);
551
+ this.update(job.id, { stage: "failed", progress: null, detail: undefined, error: { code: failure.code, message: failure.message } });
552
+ })
553
+ .then(() => {
554
+ this.cancelled.delete(job.id);
555
+ this.waiters.delete(job.id);
556
+ return this.jobs.get(job.id);
557
+ });
558
+ this.waiters.set(job.id, done);
559
+ return job;
560
+ }
561
+ checkCancelled(id) {
562
+ if (this.cancelled.has(id))
563
+ throw new ReferenceError("cancelled", "Import cancelled.");
564
+ }
565
+ async tools(id) {
566
+ const announce = () => this.update(id, { stage: "setup", detail: "Downloading video tools (first import only)…" });
567
+ const [ytdlp, ffmpeg] = await Promise.all([ensureYtdlp({ onDownload: announce }), ensureFfmpeg(announce)]);
568
+ return { ytdlp, ffmpeg };
569
+ }
570
+ async download(id) {
571
+ const job = this.jobs.get(id);
572
+ const dir = projectDir(this.paths, job.slug);
573
+ const rel = `assets/references/${id}`;
574
+ const dest = join(dir, rel);
575
+ if (existsSync(dest))
576
+ rmSync(dest, { recursive: true, force: true }); // a failed attempt's leftovers
577
+ const temp = join(dir, ".studio", "reference-imports", id);
578
+ rmSync(temp, { recursive: true, force: true });
579
+ mkdirSync(temp, { recursive: true });
580
+ try {
581
+ let { ytdlp, ffmpeg } = await this.tools(id);
582
+ this.checkCancelled(id);
583
+ let meta = {};
584
+ for (let attempt = 0;; attempt += 1) {
585
+ this.update(id, { stage: "downloading", progress: 0, detail: undefined, error: undefined });
586
+ try {
587
+ meta = await this.runYtdlp(id, ytdlp, ffmpeg, job.url, job.browser, temp);
588
+ break;
589
+ }
590
+ catch (error) {
591
+ this.checkCancelled(id);
592
+ // Sites change often; an unexplained failure usually means yt-dlp needs its latest fix.
593
+ if (attempt > 0 || !(error instanceof ReferenceError) || error.code !== "failed")
594
+ throw error;
595
+ const before = readManifest().ytdlp?.version;
596
+ ytdlp = await ensureYtdlp({ force: true, onDownload: () => this.update(id, { stage: "setup", progress: null, detail: "Updating the video downloader…" }) });
597
+ if (readManifest().ytdlp?.version === before)
598
+ throw error;
599
+ }
600
+ }
601
+ this.checkCancelled(id);
602
+ const video = join(temp, "video.mp4");
603
+ if (statSync(video).size > MAX_BYTES)
604
+ throw new ReferenceError("too_large", "This video is larger than 200 MB. Upload a shorter clip instead.");
605
+ this.update(id, { stage: "frames", progress: null });
606
+ const duration = await probeDuration(ffmpeg, video);
607
+ if (duration > MAX_SECONDS + 1)
608
+ throw new ReferenceError("too_long", "This video is longer than 10 minutes. Upload a shorter clip instead.");
609
+ const analysis = await analyzeVideo(ffmpeg, video, temp, rel);
610
+ this.checkCancelled(id);
611
+ writeReferenceJson(temp, {
612
+ id,
613
+ sourceUrl: job.url,
614
+ title: meta.title ?? null,
615
+ uploader: meta.uploader ?? null,
616
+ site: meta.extractor ?? null,
617
+ width: meta.width ?? null,
618
+ height: meta.height ?? null,
619
+ usedBrowserLogin: Boolean(job.browser),
620
+ importedAt: new Date().toISOString(),
621
+ video: `${rel}/video.mp4`,
622
+ ...analysis,
623
+ });
624
+ mkdirSync(dirname(dest), { recursive: true });
625
+ renameSync(temp, dest);
626
+ this.onAssets(job.slug);
627
+ this.update(id, { stage: "ready", progress: null, path: `${rel}/video.mp4`, dir: rel, frames: analysis.frames, sheet: analysis.sheet });
628
+ }
629
+ finally {
630
+ rmSync(temp, { recursive: true, force: true });
631
+ }
632
+ }
633
+ async runYtdlp(id, ytdlp, ffmpeg, url, browser, temp) {
634
+ for (const name of readdirSync(temp))
635
+ rmSync(join(temp, name), { recursive: true, force: true });
636
+ let meta = {};
637
+ // A merged download fetches video then audio: show one bar (video ~85% of it), not two.
638
+ let parts = 1;
639
+ let part = 0;
640
+ let lastDone = 0;
641
+ let shown = -1;
642
+ const args = [
643
+ "--ignore-config",
644
+ "--no-playlist",
645
+ "--no-cache-dir",
646
+ "--max-downloads",
647
+ "1",
648
+ "--socket-timeout",
649
+ "20",
650
+ "--retries",
651
+ "2",
652
+ "--max-filesize",
653
+ "200M",
654
+ "--match-filters",
655
+ `duration <=? ${MAX_SECONDS} & !is_live`,
656
+ "--ffmpeg-location",
657
+ ffmpeg,
658
+ // YouTube needs a JavaScript runtime; the helper's own Node will do.
659
+ "--js-runtimes",
660
+ `node:${process.execPath}`,
661
+ // Sharpest up to 1080p for reading type; H.264 when there's a choice, so every browser plays it.
662
+ "-S",
663
+ "res:1080,vcodec:h264,acodec:aac",
664
+ "-f",
665
+ "bv*+ba/b",
666
+ "--merge-output-format",
667
+ "mp4",
668
+ "--remux-video",
669
+ "mp4",
670
+ "--newline",
671
+ "--progress",
672
+ "--progress-template",
673
+ "download:@@progress %(progress.downloaded_bytes)s %(progress.total_bytes)s %(progress.total_bytes_estimate)s",
674
+ "--no-simulate",
675
+ "-O",
676
+ "before_dl:@@parts %(format_id)s",
677
+ // -O implies --quiet, which hides why a video was skipped (too long, too large).
678
+ "--no-quiet",
679
+ "-O",
680
+ "after_move:@@meta %(.{title,uploader,width,height,extractor})j",
681
+ ...(browser ? ["--cookies-from-browser", browser] : []),
682
+ "-o",
683
+ join(temp, "video.%(ext)s"),
684
+ "--",
685
+ url,
686
+ ];
687
+ const result = await run(ytdlp, args, {
688
+ timeoutMs: 6 * 60_000,
689
+ onChild: (child) => (child ? this.children.set(id, child) : this.children.delete(id)),
690
+ onLine: (line) => {
691
+ if (line.startsWith("@@progress ")) {
692
+ const [done, total, estimate] = line.slice(11).split(" ").map(Number);
693
+ const size = total > 0 ? total : estimate;
694
+ if (!(done >= 0 && size > 0))
695
+ return;
696
+ if (done < lastDone)
697
+ part = Math.min(parts - 1, part + 1);
698
+ lastDone = done;
699
+ const fraction = Math.min(1, done / size);
700
+ const overall = parts > 1 ? (part === 0 ? fraction * 0.85 : 0.85 + fraction * 0.15) : fraction;
701
+ const percent = Math.floor(overall * 100);
702
+ if (percent === shown)
703
+ return;
704
+ shown = percent;
705
+ this.update(id, { progress: percent / 100 });
706
+ }
707
+ else if (line.startsWith("@@parts ")) {
708
+ parts = line.slice(8).split("+").length;
709
+ }
710
+ else if (line.startsWith("@@meta ")) {
711
+ try {
712
+ meta = JSON.parse(line.slice(7));
713
+ }
714
+ catch { }
715
+ }
716
+ },
717
+ });
718
+ this.checkCancelled(id);
719
+ // --max-downloads exits 101 once the one video is done.
720
+ if ((result.code === 0 || result.code === 101) && existsSync(join(temp, "video.mp4")))
721
+ return meta;
722
+ debugLog(`references: yt-dlp exited ${result.code}\n${result.stderr.slice(-4000)}`);
723
+ throw classifyFailure(`${result.stdout}\n${result.stderr}`, browser);
724
+ }
725
+ async prepareExisting(id, path) {
726
+ const job = this.jobs.get(id);
727
+ const dir = projectDir(this.paths, job.slug);
728
+ const rel = dirname(path);
729
+ const ffmpeg = await ensureFfmpeg(() => this.update(id, { stage: "setup", detail: "Downloading video tools (first import only)…" }));
730
+ this.update(id, { stage: "frames", detail: undefined });
731
+ const video = join(dir, path);
732
+ const analysis = await analyzeVideo(ffmpeg, video, join(dir, rel), rel);
733
+ // Keep what an earlier import recorded (source link, title).
734
+ let previous = {};
735
+ try {
736
+ previous = JSON.parse(readFileSync(join(dir, rel, "reference.json"), "utf8"));
737
+ }
738
+ catch { }
739
+ writeReferenceJson(join(dir, rel), {
740
+ sourceUrl: null,
741
+ importedAt: new Date().toISOString(),
742
+ ...previous,
743
+ id,
744
+ video: path,
745
+ ...analysis,
746
+ });
747
+ this.onAssets(job.slug);
748
+ this.update(id, { stage: "ready", path, dir: rel, frames: analysis.frames, sheet: analysis.sheet });
749
+ }
750
+ }
751
+ function writeReferenceJson(folder, data) {
752
+ writeFileSync(join(folder, "reference.json"), `${JSON.stringify(data, null, 2)}\n`);
753
+ }
754
+ //# sourceMappingURL=references.js.map