ima2-gen 1.1.20 → 1.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -25
- package/bin/commands/capabilities.js +2 -2
- package/bin/commands/capabilities.ts +2 -2
- package/bin/commands/defaults.js +2 -2
- package/bin/commands/defaults.ts +2 -2
- package/bin/commands/doctor.js +3 -3
- package/bin/commands/doctor.ts +3 -3
- package/bin/commands/edit.js +1 -1
- package/bin/commands/edit.ts +1 -1
- package/bin/commands/gen.js +1 -1
- package/bin/commands/gen.ts +1 -1
- package/bin/commands/grok.js +16 -11
- package/bin/commands/grok.ts +16 -11
- package/bin/commands/multimode.js +1 -1
- package/bin/commands/multimode.ts +1 -1
- package/bin/commands/observability.js +2 -2
- package/bin/commands/observability.ts +2 -2
- package/bin/commands/video.js +335 -13
- package/bin/commands/video.ts +249 -12
- package/bin/ima2.js +9 -9
- package/bin/ima2.ts +9 -9
- package/bin/lib/error-hints.js +2 -2
- package/bin/lib/error-hints.ts +2 -2
- package/docs/API.md +112 -3
- package/docs/CLI.md +61 -7
- package/docs/FAQ.ko.md +15 -20
- package/docs/FAQ.md +14 -19
- package/docs/NPX_QUICKSTART.md +40 -0
- package/docs/PROMPT_STUDIO.ko.md +1 -1
- package/docs/PROMPT_STUDIO.md +1 -1
- package/docs/README.ja.md +6 -16
- package/docs/README.ko.md +10 -20
- package/docs/README.zh-CN.md +7 -17
- package/docs/migration/runtime-test-inventory.md +8 -1
- package/lib/agentRuntime.js +19 -5
- package/lib/agentRuntime.ts +17 -5
- package/lib/capabilities.js +1 -1
- package/lib/capabilities.ts +1 -1
- package/lib/generationErrors.js +1 -1
- package/lib/generationErrors.ts +1 -1
- package/lib/grokProxyLauncher.js +26 -3
- package/lib/grokProxyLauncher.ts +27 -3
- package/lib/grokVideoAdapter.js +18 -89
- package/lib/grokVideoAdapter.ts +27 -88
- package/lib/grokVideoCanvas.js +25 -0
- package/lib/grokVideoCanvas.ts +26 -0
- package/lib/grokVideoDownload.js +58 -0
- package/lib/grokVideoDownload.ts +59 -0
- package/lib/grokVideoPlannerPrompt.js +64 -0
- package/lib/grokVideoPlannerPrompt.ts +67 -0
- package/lib/historyList.js +7 -1
- package/lib/historyList.ts +5 -1
- package/lib/oauthLauncher.js +21 -6
- package/lib/oauthLauncher.ts +22 -6
- package/lib/videoContinuity.js +149 -0
- package/lib/videoContinuity.ts +180 -0
- package/lib/videoFrameExtract.js +80 -0
- package/lib/videoFrameExtract.ts +78 -0
- package/node_modules/progrok/dist/index.js +187 -88
- package/node_modules/progrok/dist/index.js.map +1 -1
- package/node_modules/progrok/package.json +1 -1
- package/node_modules/progrok/skills/progrok/SKILL.md +33 -4
- package/package.json +2 -2
- package/routes/index.js +4 -0
- package/routes/index.ts +4 -0
- package/routes/quota.js +66 -0
- package/routes/quota.ts +89 -0
- package/routes/video.js +77 -15
- package/routes/video.ts +82 -14
- package/routes/videoExtended.js +293 -0
- package/routes/videoExtended.ts +284 -0
- package/server.js +6 -2
- package/server.ts +5 -2
- package/skills/ima2/SKILL.md +320 -7
- package/ui/dist/.vite/manifest.json +12 -12
- package/ui/dist/assets/{AgentWorkspace-DS8uvoLI.js → AgentWorkspace-B_hq9CLg.js} +2 -2
- package/ui/dist/assets/{CardNewsWorkspace-CYxMsE67.js → CardNewsWorkspace-wD12J7qk.js} +1 -1
- package/ui/dist/assets/{NodeCanvas-DccIc347.js → NodeCanvas-CI_wuPMf.js} +1 -1
- package/ui/dist/assets/{PromptBuilderPanel-BvxxwSJp.js → PromptBuilderPanel-CUTujJUV.js} +1 -1
- package/ui/dist/assets/{PromptImportDialog-u1_BFDRd.js → PromptImportDialog-CUi66jPK.js} +2 -2
- package/ui/dist/assets/{PromptImportDiscoverySection-C5uvkVSz.js → PromptImportDiscoverySection-Cm3vrjY4.js} +1 -1
- package/ui/dist/assets/{PromptImportFolderSection-D3E_O1SD.js → PromptImportFolderSection-DOtWTD9n.js} +1 -1
- package/ui/dist/assets/{PromptLibraryPanel-4gyf9CB9.js → PromptLibraryPanel-BMjQegRa.js} +2 -2
- package/ui/dist/assets/SettingsWorkspace-PiaVnsdA.js +1 -0
- package/ui/dist/assets/{index-DoKtXbod.js → index-31uVIdt4.js} +1 -1
- package/ui/dist/assets/index-CjgnNtgt.css +1 -0
- package/ui/dist/assets/index-Da2s4_-5.js +36 -0
- package/ui/dist/index.html +2 -2
- package/vendor/progrok-0.2.0.tgz +0 -0
- package/ui/dist/assets/SettingsWorkspace-F3eNu3mJ.js +0 -1
- package/ui/dist/assets/index-B6tcw_UF.css +0 -1
- package/ui/dist/assets/index-DYOh6gQD.js +0 -32
- package/vendor/progrok-0.1.1.tgz +0 -0
package/bin/commands/video.js
CHANGED
|
@@ -4,10 +4,54 @@ import { streamSse } from "../lib/sse.js";
|
|
|
4
4
|
import { out, die, color, json, exitCodeForError } from "../lib/output.js";
|
|
5
5
|
import { config } from "../../config.js";
|
|
6
6
|
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
7
|
-
import { dirname, join } from "node:path";
|
|
7
|
+
import { basename, dirname, join } from "node:path";
|
|
8
8
|
const VALID_RESOLUTIONS = new Set(["480p", "720p"]);
|
|
9
9
|
const VALID_ASPECT_RATIOS = new Set(["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "auto"]);
|
|
10
10
|
const VALID_MODELS = new Set(["grok-imagine-video", "grok-imagine-video-1.5-preview"]);
|
|
11
|
+
const ACTIVE_VIDEO_PROMPT_GUIDANCE = "Active video prompt required: describe visual flow, motion flow, sound/no-music intent, dialogue/no-dialogue intent, and the desired ending frame. Pace the scene to naturally fill the selected duration with an opening composition, connected motion/emotion change, and stable ending frame.";
|
|
12
|
+
function parseIntegerFlag(value, fallback, label) {
|
|
13
|
+
const raw = value === undefined ? String(fallback) : String(value);
|
|
14
|
+
if (!/^\d+$/.test(raw))
|
|
15
|
+
die(2, `${label} must be an integer`);
|
|
16
|
+
return Number(raw);
|
|
17
|
+
}
|
|
18
|
+
function rejectUnknownFlags(args) {
|
|
19
|
+
if (args._unknown?.length)
|
|
20
|
+
die(2, `unknown option: ${args._unknown[0]}`);
|
|
21
|
+
}
|
|
22
|
+
async function readJsonResponse(res, label) {
|
|
23
|
+
const text = await res.text();
|
|
24
|
+
try {
|
|
25
|
+
return text ? JSON.parse(text) : {};
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
die(1, `${label} failed: expected JSON response, got ${text.slice(0, 120) || `HTTP ${res.status}`}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function parseTimeoutSeconds(seconds) {
|
|
32
|
+
const sec = parseIntegerFlag(seconds, 600, "--timeout");
|
|
33
|
+
if (sec < 1)
|
|
34
|
+
die(2, "--timeout must be at least 1");
|
|
35
|
+
return sec;
|
|
36
|
+
}
|
|
37
|
+
function timeoutSignal(seconds) {
|
|
38
|
+
const sec = parseTimeoutSeconds(seconds);
|
|
39
|
+
return AbortSignal.timeout(sec * 1000);
|
|
40
|
+
}
|
|
41
|
+
async function writeBuffer(path, buf) {
|
|
42
|
+
await mkdir(dirname(path), { recursive: true }).catch(() => { });
|
|
43
|
+
await writeFile(path, buf);
|
|
44
|
+
}
|
|
45
|
+
async function downloadReturnedVideo(serverBase, data, outPath, signal) {
|
|
46
|
+
const rawUrl = typeof data.url === "string" ? data.url : "";
|
|
47
|
+
const url = rawUrl.startsWith("/") ? `${serverBase}${rawUrl}` : rawUrl;
|
|
48
|
+
if (!url)
|
|
49
|
+
die(1, "server did not return a video url");
|
|
50
|
+
const res = await fetch(url, { signal });
|
|
51
|
+
if (!res.ok)
|
|
52
|
+
die(1, `failed to download video: HTTP ${res.status}`);
|
|
53
|
+
await writeBuffer(outPath, Buffer.from(await res.arrayBuffer()));
|
|
54
|
+
}
|
|
11
55
|
const SPEC = {
|
|
12
56
|
flags: {
|
|
13
57
|
duration: { type: "string", default: "5" },
|
|
@@ -27,11 +71,24 @@ const SPEC = {
|
|
|
27
71
|
};
|
|
28
72
|
const HELP = `
|
|
29
73
|
ima2 video <prompt...> [options]
|
|
74
|
+
ima2 video edit <prompt> --video <url|file_id|generated-file>
|
|
75
|
+
ima2 video extend <prompt> --video <url|file_id|generated-file> [--duration 6]
|
|
76
|
+
ima2 video continue <prompt> --video <generated-file>
|
|
77
|
+
ima2 video frame <file> [--last] [-o output.png]
|
|
78
|
+
ima2 video analyze <generated-file>
|
|
30
79
|
|
|
31
|
-
Generate
|
|
80
|
+
Generate, edit, extend, or analyze video via Grok.
|
|
32
81
|
|
|
33
|
-
|
|
34
|
-
|
|
82
|
+
Subcommands:
|
|
83
|
+
(default) Generate video (T2V / I2V / Ref2V)
|
|
84
|
+
edit Edit existing video with text prompt (V2V)
|
|
85
|
+
extend Continue video from last frame
|
|
86
|
+
continue Generate a new I2V clip from a generated video's last frame with lineage
|
|
87
|
+
frame Extract a frame from video (requires ffmpeg on server)
|
|
88
|
+
analyze Analyze video with Grok 4.3 vision
|
|
89
|
+
|
|
90
|
+
Options (generate mode):
|
|
91
|
+
--duration <1..15> Duration in seconds. Default: 5. Prompt motion should naturally fill this length
|
|
35
92
|
--resolution <480p|720p> Default: 480p
|
|
36
93
|
--aspect-ratio <ratio|auto> 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, auto. Default: auto
|
|
37
94
|
--model <name> grok-imagine-video, grok-imagine-video-1.5-preview
|
|
@@ -44,6 +101,10 @@ const HELP = `
|
|
|
44
101
|
--server <url> Override server URL
|
|
45
102
|
--session <id> Session ID
|
|
46
103
|
|
|
104
|
+
Edit/extend subcommands:
|
|
105
|
+
--video <value> HTTPS URL, xAI file_id, data URL, or generated filename
|
|
106
|
+
--duration <2..10> Extension duration only. Default: 6
|
|
107
|
+
|
|
47
108
|
Modes (auto-detected from --ref count):
|
|
48
109
|
0 refs → text-to-video
|
|
49
110
|
1 ref → image-to-video
|
|
@@ -52,18 +113,34 @@ const HELP = `
|
|
|
52
113
|
Examples:
|
|
53
114
|
ima2 video "a cat playing piano"
|
|
54
115
|
ima2 video "animate this" --ref photo.png --duration 10
|
|
55
|
-
ima2 video "
|
|
116
|
+
ima2 video edit "make it sunset" --video https://vidgen.x.ai/.../clip.mp4
|
|
117
|
+
ima2 video extend "camera pulls back" --video https://vidgen.x.ai/.../clip.mp4 --duration 5
|
|
118
|
+
ima2 video continue "she turns back as the music cuts to room tone" --video 1780226256355_50252101.mp4
|
|
119
|
+
ima2 video frame 1780226256355_50252101.mp4 --last -o lastframe.png
|
|
120
|
+
ima2 video analyze 1780226256355_50252101.mp4
|
|
56
121
|
`;
|
|
57
122
|
export default async function videoCmd(argv) {
|
|
123
|
+
const sub = argv[0];
|
|
124
|
+
if (sub === "edit")
|
|
125
|
+
return videoEditCmd(argv.slice(1));
|
|
126
|
+
if (sub === "extend")
|
|
127
|
+
return videoExtendCmd(argv.slice(1));
|
|
128
|
+
if (sub === "continue")
|
|
129
|
+
return videoContinueCmd(argv.slice(1));
|
|
130
|
+
if (sub === "frame")
|
|
131
|
+
return videoFrameCmd(argv.slice(1));
|
|
132
|
+
if (sub === "analyze")
|
|
133
|
+
return videoAnalyzeCmd(argv.slice(1));
|
|
58
134
|
const args = parseArgs(argv, SPEC);
|
|
135
|
+
rejectUnknownFlags(args);
|
|
59
136
|
if (args.help) {
|
|
60
137
|
out(HELP);
|
|
61
138
|
return;
|
|
62
139
|
}
|
|
63
140
|
const prompt = args.positional.join(" ");
|
|
64
|
-
if (!prompt)
|
|
65
|
-
die(2,
|
|
66
|
-
const duration =
|
|
141
|
+
if (!prompt.trim())
|
|
142
|
+
die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
|
|
143
|
+
const duration = parseIntegerFlag(args.duration, 5, "--duration");
|
|
67
144
|
if (duration < 1 || duration > 15)
|
|
68
145
|
die(2, "--duration must be between 1 and 15");
|
|
69
146
|
const resolution = String(args.resolution);
|
|
@@ -78,6 +155,12 @@ export default async function videoCmd(argv) {
|
|
|
78
155
|
const refs = (Array.isArray(args.ref) ? args.ref : []);
|
|
79
156
|
if (refs.length > 7)
|
|
80
157
|
die(2, "max 7 --ref attachments for video");
|
|
158
|
+
if (refs.length >= 2 && duration > 10)
|
|
159
|
+
die(2, "--duration must be between 1 and 10 when using 2 or more --ref attachments");
|
|
160
|
+
const timeoutSeconds = parseIntegerFlag(args.timeout, 600, "--timeout");
|
|
161
|
+
if (timeoutSeconds < 1)
|
|
162
|
+
die(2, "--timeout must be at least 1");
|
|
163
|
+
const timeoutMs = timeoutSeconds * 1000;
|
|
81
164
|
let server;
|
|
82
165
|
try {
|
|
83
166
|
server = await resolveServer({ serverFlag: args.server });
|
|
@@ -90,7 +173,6 @@ export default async function videoCmd(argv) {
|
|
|
90
173
|
const buf = await readFile(p);
|
|
91
174
|
return buf.toString("base64");
|
|
92
175
|
}));
|
|
93
|
-
const timeoutMs = (parseInt(String(args.timeout)) || 600) * 1000;
|
|
94
176
|
const requestId = `req_cli_video_${Date.now().toString(36)}`;
|
|
95
177
|
const body = {
|
|
96
178
|
prompt,
|
|
@@ -149,7 +231,7 @@ export default async function videoCmd(argv) {
|
|
|
149
231
|
case "error":
|
|
150
232
|
if (!args.json && lastProgress >= 0)
|
|
151
233
|
process.stdout.write("\n");
|
|
152
|
-
die(1, `video error: ${ev.data.error || ev.data}${ev.data.code ? ` (${ev.data.code})` : ""}`);
|
|
234
|
+
die(1, `video error: ${ev.data.error || ev.data}${ev.data.guidance ? `\n${ev.data.guidance}` : ""}${ev.data.code ? ` (${ev.data.code})` : ""}`);
|
|
153
235
|
}
|
|
154
236
|
}
|
|
155
237
|
}
|
|
@@ -183,12 +265,11 @@ export default async function videoCmd(argv) {
|
|
|
183
265
|
}
|
|
184
266
|
// Download the video file from server
|
|
185
267
|
const videoUrl = `${server.base}${doneData.url || `/generated/${encodeURIComponent(filename)}`}`;
|
|
186
|
-
const dlRes = await fetch(videoUrl, { signal:
|
|
268
|
+
const dlRes = await fetch(videoUrl, { signal: timeoutSignal(args.timeout) });
|
|
187
269
|
if (!dlRes.ok)
|
|
188
270
|
die(1, `failed to download video: HTTP ${dlRes.status}`);
|
|
189
271
|
const videoBuf = Buffer.from(await dlRes.arrayBuffer());
|
|
190
|
-
await
|
|
191
|
-
await writeFile(target, videoBuf);
|
|
272
|
+
await writeBuffer(target, videoBuf);
|
|
192
273
|
if (args.json) {
|
|
193
274
|
json({
|
|
194
275
|
ok: true,
|
|
@@ -213,3 +294,244 @@ function renderBar(pct) {
|
|
|
213
294
|
const filled = Math.round((pct / 100) * width);
|
|
214
295
|
return color.green("█".repeat(filled)) + color.dim("░".repeat(width - filled));
|
|
215
296
|
}
|
|
297
|
+
async function runVideoGenerateRequest(serverBase, body, timeout, silent) {
|
|
298
|
+
let doneData = null;
|
|
299
|
+
let lastProgress = -1;
|
|
300
|
+
for await (const ev of streamSse(`${serverBase}/api/video/generate`, {
|
|
301
|
+
body: { provider: "grok", ...body },
|
|
302
|
+
signal: timeoutSignal(timeout),
|
|
303
|
+
headers: typeof body.requestId === "string" ? { "X-Request-Id": body.requestId } : undefined,
|
|
304
|
+
})) {
|
|
305
|
+
if (ev.event === "progress") {
|
|
306
|
+
const pct = typeof ev.data.progress === "number" ? Math.round(ev.data.progress * 100) : null;
|
|
307
|
+
if (pct !== null && pct !== lastProgress && !silent) {
|
|
308
|
+
process.stdout.write(`\r ${renderBar(pct)} ${pct}%`);
|
|
309
|
+
lastProgress = pct;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
else if (ev.event === "done") {
|
|
313
|
+
if (!silent && lastProgress >= 0)
|
|
314
|
+
process.stdout.write("\n");
|
|
315
|
+
doneData = ev.data;
|
|
316
|
+
}
|
|
317
|
+
else if (ev.event === "error") {
|
|
318
|
+
if (!silent && lastProgress >= 0)
|
|
319
|
+
process.stdout.write("\n");
|
|
320
|
+
die(1, `video error: ${ev.data.error || ev.data}${ev.data.guidance ? `\n${ev.data.guidance}` : ""}${ev.data.code ? ` (${ev.data.code})` : ""}`);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (!doneData)
|
|
324
|
+
die(1, "server did not return a video result");
|
|
325
|
+
return doneData;
|
|
326
|
+
}
|
|
327
|
+
// --- Subcommands ---
|
|
328
|
+
async function videoEditCmd(argv) {
|
|
329
|
+
const spec = { flags: { video: { type: "string" }, out: { short: "o", type: "string" }, output: { type: "string" }, json: { type: "boolean" }, timeout: { type: "string", default: "600" }, server: { type: "string" }, help: { short: "h", type: "boolean" } } };
|
|
330
|
+
const args = parseArgs(argv, spec);
|
|
331
|
+
rejectUnknownFlags(args);
|
|
332
|
+
if (args.help) {
|
|
333
|
+
out(` ima2 video edit <prompt> --video <url|file_id|generated-file>\n\n Edit existing video with text prompt (real V2V).\n Model: grok-imagine-video only. Input: mp4, max 8.7s.\n\n Options:\n --video <value> Source video HTTPS URL, xAI file_id, data URL, or generated filename (required)\n -o, --out <file> Download edited video to file\n --output <file> Alias for --out\n --json Print JSON result\n --timeout <sec> Default: 600\n --server <url> Override server URL`);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const prompt = args.positional.join(" ");
|
|
337
|
+
if (!prompt.trim())
|
|
338
|
+
die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
|
|
339
|
+
if (!args.video)
|
|
340
|
+
die(2, "--video <url> is required");
|
|
341
|
+
parseTimeoutSeconds(args.timeout);
|
|
342
|
+
let server;
|
|
343
|
+
try {
|
|
344
|
+
server = await resolveServer({ serverFlag: args.server });
|
|
345
|
+
}
|
|
346
|
+
catch (e) {
|
|
347
|
+
die(exitCodeForError(e), e.message);
|
|
348
|
+
throw e;
|
|
349
|
+
}
|
|
350
|
+
const res = await fetch(`${server.base}/api/video/edit`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ prompt, videoUrl: args.video }), signal: timeoutSignal(args.timeout) });
|
|
351
|
+
const data = await readJsonResponse(res, "edit");
|
|
352
|
+
if (!res.ok)
|
|
353
|
+
die(1, `edit failed: ${data.error ?? res.status}`);
|
|
354
|
+
const outPath = (args.out || args.output);
|
|
355
|
+
if (outPath)
|
|
356
|
+
await downloadReturnedVideo(server.base, data, outPath, timeoutSignal(args.timeout));
|
|
357
|
+
if (args.json) {
|
|
358
|
+
out(JSON.stringify(data, null, 2));
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
out(color.green("✓ ") + `Edited video: ${data.url}`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
async function videoExtendCmd(argv) {
|
|
365
|
+
const spec = { flags: { video: { type: "string" }, duration: { type: "string", default: "6" }, out: { short: "o", type: "string" }, output: { type: "string" }, json: { type: "boolean" }, timeout: { type: "string", default: "600" }, server: { type: "string" }, help: { short: "h", type: "boolean" } } };
|
|
366
|
+
const args = parseArgs(argv, spec);
|
|
367
|
+
rejectUnknownFlags(args);
|
|
368
|
+
if (args.help) {
|
|
369
|
+
out(` ima2 video extend <prompt> --video <url|file_id|generated-file> [--duration 6]\n\n Extend video from its last frame.\n Model: grok-imagine-video only. Extension: 2-10s.\n\n Options:\n --video <value> Source video HTTPS URL, xAI file_id, data URL, or generated filename (required)\n --duration <2-10> Extension duration (default: 6)\n -o, --out <file> Download extended video to file\n --output <file> Alias for --out\n --json Print JSON result\n --timeout <sec> Default: 600\n --server <url> Override server URL`);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
const prompt = args.positional.join(" ");
|
|
373
|
+
if (!prompt.trim())
|
|
374
|
+
die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
|
|
375
|
+
if (!args.video)
|
|
376
|
+
die(2, "--video <url> is required");
|
|
377
|
+
const duration = parseIntegerFlag(args.duration, 6, "--duration");
|
|
378
|
+
if (duration < 2 || duration > 10)
|
|
379
|
+
die(2, "--duration must be between 2 and 10");
|
|
380
|
+
parseTimeoutSeconds(args.timeout);
|
|
381
|
+
let server;
|
|
382
|
+
try {
|
|
383
|
+
server = await resolveServer({ serverFlag: args.server });
|
|
384
|
+
}
|
|
385
|
+
catch (e) {
|
|
386
|
+
die(exitCodeForError(e), e.message);
|
|
387
|
+
throw e;
|
|
388
|
+
}
|
|
389
|
+
const res = await fetch(`${server.base}/api/video/extend`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ prompt, videoUrl: args.video, duration }), signal: timeoutSignal(args.timeout) });
|
|
390
|
+
const data = await readJsonResponse(res, "extend");
|
|
391
|
+
if (!res.ok)
|
|
392
|
+
die(1, `extend failed: ${data.error ?? res.status}`);
|
|
393
|
+
const outPath = (args.out || args.output);
|
|
394
|
+
if (outPath)
|
|
395
|
+
await downloadReturnedVideo(server.base, data, outPath, timeoutSignal(args.timeout));
|
|
396
|
+
if (args.json) {
|
|
397
|
+
out(JSON.stringify(data, null, 2));
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
out(color.green("✓ ") + `Extended video (${data.duration}s): ${data.url}`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
async function videoContinueCmd(argv) {
|
|
404
|
+
const spec = {
|
|
405
|
+
flags: {
|
|
406
|
+
video: { type: "string" },
|
|
407
|
+
duration: { type: "string", default: "5" },
|
|
408
|
+
resolution: { type: "string", default: "720p" },
|
|
409
|
+
"aspect-ratio": { type: "string", default: "auto" },
|
|
410
|
+
model: { type: "string" },
|
|
411
|
+
out: { short: "o", type: "string" },
|
|
412
|
+
output: { type: "string" },
|
|
413
|
+
json: { type: "boolean" },
|
|
414
|
+
timeout: { type: "string", default: "600" },
|
|
415
|
+
server: { type: "string" },
|
|
416
|
+
help: { short: "h", type: "boolean" },
|
|
417
|
+
},
|
|
418
|
+
};
|
|
419
|
+
const args = parseArgs(argv, spec);
|
|
420
|
+
rejectUnknownFlags(args);
|
|
421
|
+
if (args.help) {
|
|
422
|
+
out(` ima2 video continue <prompt> --video <generated-file>\n\n Generate a new clip from a generated video's last frame and carry branch-local revisedPrompt lineage.\n\n Prompt must describe visual flow, motion, sound/music/no-music, dialogue/no-dialogue, ending frame, and how the selected duration should feel naturally filled.\n\n Options:\n --video <file> Generated .mp4 filename (required)\n --duration <1..15> Default: 5. Prompt motion should naturally fill this length\n --resolution <480p|720p> Default: 720p\n --aspect-ratio <ratio|auto> Default: auto\n --model <name> grok-imagine-video, grok-imagine-video-1.5-preview\n -o, --out <file> Download continued video to file\n --output <file> Alias for --out\n --json Print JSON result\n --timeout <sec> Default: 600\n --server <url> Override server URL`);
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
const prompt = args.positional.join(" ");
|
|
426
|
+
if (!prompt.trim())
|
|
427
|
+
die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
|
|
428
|
+
if (!args.video)
|
|
429
|
+
die(2, "--video <generated-file> is required");
|
|
430
|
+
const duration = parseIntegerFlag(args.duration, 5, "--duration");
|
|
431
|
+
if (duration < 1 || duration > 15)
|
|
432
|
+
die(2, "--duration must be between 1 and 15");
|
|
433
|
+
const resolution = String(args.resolution);
|
|
434
|
+
if (!VALID_RESOLUTIONS.has(resolution))
|
|
435
|
+
die(2, "--resolution must be one of: 480p, 720p");
|
|
436
|
+
const aspectRatio = String(args["aspect-ratio"]);
|
|
437
|
+
if (!VALID_ASPECT_RATIOS.has(aspectRatio))
|
|
438
|
+
die(2, "--aspect-ratio must be one of: 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, auto");
|
|
439
|
+
if (args.model && !VALID_MODELS.has(String(args.model))) {
|
|
440
|
+
die(2, "--model must be one of: grok-imagine-video, grok-imagine-video-1.5-preview");
|
|
441
|
+
}
|
|
442
|
+
parseTimeoutSeconds(args.timeout);
|
|
443
|
+
let server;
|
|
444
|
+
try {
|
|
445
|
+
server = await resolveServer({ serverFlag: args.server });
|
|
446
|
+
}
|
|
447
|
+
catch (e) {
|
|
448
|
+
die(exitCodeForError(e), e.message);
|
|
449
|
+
throw e;
|
|
450
|
+
}
|
|
451
|
+
const requestId = `req_cli_video_continue_${Date.now().toString(36)}`;
|
|
452
|
+
const body = {
|
|
453
|
+
prompt,
|
|
454
|
+
requestId,
|
|
455
|
+
duration,
|
|
456
|
+
resolution,
|
|
457
|
+
aspectRatio,
|
|
458
|
+
continueFromVideo: args.video,
|
|
459
|
+
};
|
|
460
|
+
if (args.model)
|
|
461
|
+
body.model = args.model;
|
|
462
|
+
const data = await runVideoGenerateRequest(server.base, body, args.timeout, Boolean(args.json));
|
|
463
|
+
const outPath = (args.out || args.output);
|
|
464
|
+
if (outPath)
|
|
465
|
+
await downloadReturnedVideo(server.base, data, outPath, timeoutSignal(args.timeout));
|
|
466
|
+
if (args.json)
|
|
467
|
+
out(JSON.stringify(data, null, 2));
|
|
468
|
+
else
|
|
469
|
+
out(color.green("✓ ") + `Continued video: ${data.url}`);
|
|
470
|
+
}
|
|
471
|
+
async function videoFrameCmd(argv) {
|
|
472
|
+
const spec = { flags: { last: { type: "boolean" }, position: { type: "string" }, out: { type: "string" }, output: { short: "o", type: "string" }, timeout: { type: "string", default: "60" }, server: { type: "string" }, help: { short: "h", type: "boolean" } } };
|
|
473
|
+
const args = parseArgs(argv, spec);
|
|
474
|
+
rejectUnknownFlags(args);
|
|
475
|
+
if (args.help) {
|
|
476
|
+
out(` ima2 video frame <generated-file> [--last] [--position <sec>] [-o output.png]\n\n Extract a frame from a generated video file.\n\n Options:\n --last Extract last frame (default)\n --position <sec> Extract frame at specific second\n -o, --output <path> Output file path\n --out <path> Alias for --output\n --timeout <sec> Default: 60\n --server <url> Override server URL`);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const file = args.positional[0];
|
|
480
|
+
if (!file)
|
|
481
|
+
die(2, "file argument required");
|
|
482
|
+
if (args.last && args.position)
|
|
483
|
+
die(2, "use either --last or --position, not both");
|
|
484
|
+
const position = args.last ? "last" : (String(args.position || "last"));
|
|
485
|
+
if (position !== "last" && !/^\d+(\.\d+)?$/.test(position))
|
|
486
|
+
die(2, "--position must be a non-negative number");
|
|
487
|
+
parseTimeoutSeconds(args.timeout);
|
|
488
|
+
let server;
|
|
489
|
+
try {
|
|
490
|
+
server = await resolveServer({ serverFlag: args.server });
|
|
491
|
+
}
|
|
492
|
+
catch (e) {
|
|
493
|
+
die(exitCodeForError(e), e.message);
|
|
494
|
+
throw e;
|
|
495
|
+
}
|
|
496
|
+
const url = `${server.base}/api/video/frame?file=${encodeURIComponent(file)}&position=${encodeURIComponent(position)}`;
|
|
497
|
+
const res = await fetch(url, { signal: timeoutSignal(args.timeout) });
|
|
498
|
+
if (!res.ok) {
|
|
499
|
+
const d = await readJsonResponse(res, "frame extraction");
|
|
500
|
+
die(1, `frame extraction failed: ${d.error || res.status}`);
|
|
501
|
+
}
|
|
502
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
503
|
+
const outPath = (args.output || args.out) || `frame-${basename(file).replace(/\.[^.]+$/, "")}.png`;
|
|
504
|
+
await writeBuffer(outPath, buf);
|
|
505
|
+
out(color.green("✓ ") + `Frame saved: ${outPath} (${buf.length} bytes)`);
|
|
506
|
+
}
|
|
507
|
+
async function videoAnalyzeCmd(argv) {
|
|
508
|
+
const spec = { flags: { json: { type: "boolean" }, timeout: { type: "string", default: "180" }, server: { type: "string" }, help: { short: "h", type: "boolean" } } };
|
|
509
|
+
const args = parseArgs(argv, spec);
|
|
510
|
+
rejectUnknownFlags(args);
|
|
511
|
+
if (args.help) {
|
|
512
|
+
out(` ima2 video analyze <generated-file>\n\n Analyze first/last frames from a generated .mp4 with Grok 4.3 image understanding. Outputs structured recreation prompt.\n\n Options:\n --json Print JSON result\n --timeout <sec> Default: 180\n --server <url> Override server URL`);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
const videoUrl = args.positional[0];
|
|
516
|
+
if (!videoUrl)
|
|
517
|
+
die(2, "generated video filename required");
|
|
518
|
+
parseTimeoutSeconds(args.timeout);
|
|
519
|
+
let server;
|
|
520
|
+
try {
|
|
521
|
+
server = await resolveServer({ serverFlag: args.server });
|
|
522
|
+
}
|
|
523
|
+
catch (e) {
|
|
524
|
+
die(exitCodeForError(e), e.message);
|
|
525
|
+
throw e;
|
|
526
|
+
}
|
|
527
|
+
const res = await fetch(`${server.base}/api/video/analyze`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ videoUrl }), signal: timeoutSignal(args.timeout) });
|
|
528
|
+
const data = await readJsonResponse(res, "analyze");
|
|
529
|
+
if (!res.ok)
|
|
530
|
+
die(1, `analyze failed: ${data.error || res.status}`);
|
|
531
|
+
if (args.json) {
|
|
532
|
+
out(JSON.stringify(data, null, 2));
|
|
533
|
+
}
|
|
534
|
+
else {
|
|
535
|
+
out(data.analysis);
|
|
536
|
+
}
|
|
537
|
+
}
|