Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

feat(video): last-frame, reference-to-video, video-extend and video-edit through Grok Imagine

Soohong Kim2ef5112865fd
diff --git a/sprite_gen/cli.py b/sprite_gen/cli.py
index 45fcadc..0fff49e 100644
--- a/sprite_gen/cli.py
+++ b/sprite_gen/cli.py
@@ -308,10 +308,22 @@ COMMANDS: dict[str, tuple[str, Callable[[argparse.ArgumentParser], None], Callab
gen_set.run,
),
"video": (
- "Animate one still into a verified mp4 via Grok Imagine (your own grok login or XAI_API_KEY).",
+ "Animate a still (or pin a last frame / guide with references) into a verified mp4 via Grok Imagine (your own grok login or XAI_API_KEY).",
_add_video,
video.run,
),
+ # Same declaration-once rule: the module owns the argument surface. Both verbs force
+ # the classic model and inherit resolution/length from the input clip (docs/video.md).
+ "video-extend": (
+ "Continue an existing clip by N seconds via Grok Imagine (POST /v1/videos/extensions).",
+ video.add_extend_arguments,
+ video.run_extend,
+ ),
+ "video-edit": (
+ "Edit an existing clip (≤ 8.7 s) with a prompt via Grok Imagine (POST /v1/videos/edits).",
+ video.add_edit_arguments,
+ video.run_edit,
+ ),
# Video -> sprite pipeline (docs/video-pipeline.md): canvas -> video -> frames -> loop, or video-set for a batch.
"video-canvas": (
"Pad a base still into the canvas a motion state needs (tall for jumps, wide for attacks, square otherwise).",
diff --git a/sprite_gen/gen/video.py b/sprite_gen/gen/video.py
index 89df4ad..1d53b43 100644
--- a/sprite_gen/gen/video.py
+++ b/sprite_gen/gen/video.py
@@ -1,8 +1,20 @@
# SPDX-License-Identifier: Apache-2.0
-"""Image-to-video through Grok Imagine (xAI `POST /v1/videos/generations`).
+"""Video generation through Grok Imagine (xAI `/v1/videos/*`).
-One call = one still (+ prompt) -> one verified mp4 on disk. This is the engine
-module behind the sprite-gen skill's standalone `video` command.
+One call = one verified mp4 on disk. This is the engine module behind the
+sprite-gen skill's standalone `video`, `video-extend` and `video-edit` commands:
+
+- `video` `POST /v1/videos/generations` — image-to-video (`--image`), a
+ pinned last frame (`--last-frame`, alone or with `--image`) and
+ reference-to-video (`--reference` x1..7, `<IMAGE_n>` tags, 0-based).
+- `video-extend` `POST /v1/videos/extensions` — continue a clip by N seconds.
+- `video-edit` `POST /v1/videos/edits` — change a clip with a prompt.
+
+Extension and editing only work on the classic `grok-imagine-video` model (the
+1.5 model answers `400 … not supported for this model`, measured 2026-09-13), take
+no resolution / aspect / model flags and inherit both from the input clip. The
+`--image`-only body is unchanged by the other modes; the video pipeline
+(`sprite_gen/video/*`) keeps calling exactly that.
Credentials are the user's own and never live in this repository. Two sources,
resolved in a fixed order and always reported (`auth_source`):
@@ -29,6 +41,9 @@ import base64
import json
import mimetypes
import os
+import re
+import shutil
+import subprocess
import sys
import time
import urllib.error
@@ -45,9 +60,32 @@ from .xai import (
)
DEFAULT_MODEL = "grok-imagine-video-1.5"
+# Extension and editing: the API rejects the 1.5 model on both endpoints, so the
+# verbs force the classic model and offer no --model flag (2026-09-13 measurement).
+CLASSIC_MODEL = "grok-imagine-video"
RESOLUTIONS = ("480p", "720p", "1080p")
ASPECT_RATIOS = ("1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3")
DURATION_MIN, DURATION_MAX = 1, 15
+# docs.x.ai model-capabilities/video/reference-to-video (2026-09-13): at most 7
+# references, output capped at 720p. Tags are 0-based (`<IMAGE_0>` = first
+# --reference): the probe's three-reference run took `<IMAGE_0>..<IMAGE_2>` and the
+# docs' prose says "<IMAGE_0> … when you also pass images"; its samples say <IMAGE_1>.
+REFERENCE_MAX = 7
+REFERENCE_MAX_RESOLUTION = "720p"
+REFERENCE_TAG = re.compile(r"<IMAGE_(\d+)>")
+# docs.x.ai model-capabilities/video/extension|editing (2026-09-13).
+EXTEND_INPUT_SECONDS = (2.0, 15.0)
+# A "15-second" clip's container reports 15.04 s (24 fps padding) and the API took
+# it (probe m4c, 2026-09-13), so the documented ceiling gets frame-padding slack.
+EXTEND_INPUT_SLACK_SECONDS = 0.1
+EXTEND_DURATION_MIN, EXTEND_DURATION_MAX, EXTEND_DURATION_DEFAULT = 2, 10, 6
+EDIT_INPUT_MAX_SECONDS = 8.7
+MODE_IMAGE_TO_VIDEO = "image-to-video"
+MODE_FIRST_LAST = "first-last"
+MODE_LAST_FRAME = "last-frame"
+MODE_REFERENCE = "reference"
+MODE_EXTEND = "extend"
+MODE_EDIT = "edit"
POLL_INTERVAL_SECONDS = 4.0
POLL_TIMEOUT_SECONDS = int(os.environ.get("SPRITE_GEN_VIDEO_TIMEOUT_SECONDS", "600"))
# Every mp4/mov starts with a size-prefixed `ftyp` box: bytes 4..8 spell it.
@@ -100,9 +138,49 @@ def verify_mp4(data: bytes) -> None:
)
+def _resolved(path: Path | None) -> Path | None:
+ return None if path is None else path.expanduser().resolve()
+
+
+def _require_still(path: Path, flag: str) -> None:
+ if not path.is_file():
+ raise SystemExit(f"video: {flag} still image not found: {path}")
+
+
+def probe_duration_seconds(clip: Path, *, verb: str) -> float:
+ """Container duration of a local clip via ffprobe. Loud when ffprobe is missing."""
+ ffprobe = shutil.which("ffprobe")
+ if not ffprobe:
+ raise SystemExit(f"{verb}: ffprobe is required to check the input clip's length before uploading (install ffmpeg)")
+ proc = subprocess.run(
+ [ffprobe, "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", str(clip)],
+ capture_output=True, text=True,
+ )
+ text = proc.stdout.strip()
+ if proc.returncode != 0 or not text:
+ raise SystemExit(f"{verb}: ffprobe could not read {clip}: {proc.stderr.strip()[:300] or 'no duration reported'}")
+ try:
+ return float(text)
+ except ValueError as exc:
+ raise SystemExit(f"{verb}: ffprobe reported an unreadable duration {text!r} for {clip}") from exc
+
+
+def _require_mp4_clip(clip: Path, *, verb: str) -> None:
+ if clip.suffix.lower() != ".mp4":
+ raise SystemExit(f"{verb}: --video must be an .mp4 file (the API accepts only mp4), got {clip.name!r}")
+ if not clip.is_file():
+ raise SystemExit(f"{verb}: input clip not found: {clip}")
+ with clip.open("rb") as handle:
+ head = handle.read(MP4_FTYP_OFFSET + len(MP4_FTYP))
+ if len(head) < MP4_FTYP_OFFSET + len(MP4_FTYP) or head[MP4_FTYP_OFFSET:] != MP4_FTYP:
+ raise SystemExit(f"{verb}: {clip} is not an mp4 (no ftyp box); refusing to upload it")
+
+
@dataclass
class VideoRequest:
- image: Path
+ """`sprite-gen video`: one of image / last_frame / reference_images is required."""
+
+ image: Path | None
prompt: str
out: Path
duration: int = 6
@@ -110,6 +188,39 @@ class VideoRequest:
aspect_ratio: str | None = None
model: str = DEFAULT_MODEL
generate_audio: bool | None = None # None = API default
+ last_frame: Path | None = None
+ reference_images: list[Path] = field(default_factory=list)
+
+ @property
+ def mode(self) -> str:
+ if self.reference_images:
+ return MODE_REFERENCE
+ if self.image is not None and self.last_frame is not None:
+ return MODE_FIRST_LAST
+ if self.last_frame is not None:
+ return MODE_LAST_FRAME
+ return MODE_IMAGE_TO_VIDEO
+
+
+@dataclass
+class ExtendRequest:
+ """`sprite-gen video-extend`: continue `video` by `duration` seconds (classic model)."""
+
+ video: Path
+ prompt: str
+ out: Path
+ duration: int = EXTEND_DURATION_DEFAULT
+ model: str = CLASSIC_MODEL
+
+
+@dataclass
+class EditRequest:
+ """`sprite-gen video-edit`: change `video` as the prompt says (classic model)."""
+
+ video: Path
+ prompt: str
+ out: Path
+ model: str = CLASSIC_MODEL
@dataclass
@@ -129,6 +240,9 @@ class VideoResult:
resolution: str = ""
aspect_ratio: str | None = None
generate_audio: bool | None = None
+ mode: str = MODE_IMAGE_TO_VIDEO
+ inputs: dict[str, Any] = field(default_factory=dict) # role -> local path(s); never URLs
+ input_duration: float | None = None # extend / edit: ffprobe seconds of the input clip
extra: dict[str, Any] = field(default_factory=dict)
def to_dict(self) -> dict[str, Any]:
@@ -137,14 +251,17 @@ class VideoResult:
"provider": "grok-imagine",
"auth_source": self.auth_source,
"model": self.model,
+ "mode": self.mode,
"request_id": self.request_id,
"image": str(self.image) if self.image else None,
+ "inputs": self.inputs,
"prompt": self.prompt,
"out": str(self.out),
"bytes": self.bytes,
"host": self.host,
"duration_requested": self.duration_requested,
"duration_reported": self.duration_reported,
+ **({"input_duration": self.input_duration} if self.input_duration is not None else {}),
"resolution": self.resolution,
"aspect_ratio": self.aspect_ratio,
"generate_audio": self.generate_audio,
@@ -157,8 +274,37 @@ class VideoResult:
def _validate(request: VideoRequest) -> None:
if not request.prompt.strip():
raise SystemExit("video: empty prompt; pass --prompt or --prompt-file")
- if not request.image.is_file():
- raise SystemExit(f"video: still image not found: {request.image}")
+ if request.image is None and request.last_frame is None and not request.reference_images:
+ raise SystemExit("video: nothing to animate — pass --image, --last-frame and/or --reference")
+ if request.image is not None:
+ _require_still(request.image, "--image")
+ if request.last_frame is not None:
+ _require_still(request.last_frame, "--last-frame")
+ if request.model == CLASSIC_MODEL:
+ raise SystemExit(f"video: --last-frame needs {DEFAULT_MODEL}; the classic {CLASSIC_MODEL} rejects last_frame")
+ references = request.reference_images
+ tags = sorted({int(n) for n in REFERENCE_TAG.findall(request.prompt)})
+ if references:
+ if len(references) > REFERENCE_MAX:
+ raise SystemExit(f"video: at most {REFERENCE_MAX} --reference images, got {len(references)}")
+ for path in references:
+ _require_still(path, "--reference")
+ if request.resolution == "1080p":
+ raise SystemExit(
+ f"video: reference-to-video is capped at {REFERENCE_MAX_RESOLUTION}; --resolution 1080p is not available with --reference"
+ )
+ if request.model == CLASSIC_MODEL and request.image is not None:
+ raise SystemExit(f"video: the classic {CLASSIC_MODEL} rejects --image combined with --reference; use {DEFAULT_MODEL}")
+ out_of_range = [n for n in tags if n >= len(references)]
+ if out_of_range:
+ raise SystemExit(
+ f"video: prompt tags {', '.join(f'<IMAGE_{n}>' for n in out_of_range)} but only {len(references)} --reference "
+ f"image(s) were given — tags are 0-based: <IMAGE_0>..<IMAGE_{len(references) - 1}>"
+ )
+ elif tags:
+ raise SystemExit(
+ f"video: prompt mentions {', '.join(f'<IMAGE_{n}>' for n in tags)} but no --reference image was given"
+ )
if not (DURATION_MIN <= request.duration <= DURATION_MAX):
raise SystemExit(f"video: --duration must be {DURATION_MIN}..{DURATION_MAX} seconds, got {request.duration}")
if request.resolution not in RESOLUTIONS:
@@ -167,43 +313,61 @@ def _validate(request: VideoRequest) -> None:
raise SystemExit(f"video: --aspect-ratio must be one of {', '.join(ASPECT_RATIOS)}, got {request.aspect_ratio!r}")
-def generate_video(
- request: VideoRequest,
- *,
- credential: Credential | None = None,
- call: HttpCall | None = None,
- download: Callable[[str, str], bytes] | None = None,
- sleep: Callable[[float], None] | None = None,
- poll_timeout: float | None = None,
-) -> VideoResult:
- """Generate one clip and return a VideoResult. Raises SystemExit on any failure."""
- # Late-bound so a test that monkeypatches the module-level transport never
- # reaches the network through a default captured at definition time.
- call = call or http_json
- download = download or http_download
- sleep = sleep or time.sleep
- _validate(request)
- credential = credential or resolve_credential()
- out = request.out.expanduser().resolve()
- image = request.image.expanduser().resolve()
-
+def _build_generation_body(request: VideoRequest, image: Path | None, last_frame: Path | None, references: list[Path]) -> dict[str, Any]:
+ # Key order matters to nobody but the snapshot test: the --image-only body is
+ # exactly what it was before the other modes existed.
body: dict[str, Any] = {
"model": request.model,
"prompt": request.prompt,
"duration": request.duration,
"resolution": request.resolution,
- "image": {"url": _data_url(image)},
}
+ if image is not None:
+ body["image"] = {"url": _data_url(image)}
+ if last_frame is not None:
+ body["last_frame"] = {"url": _data_url(last_frame)}
+ if references:
+ body["reference_images"] = [{"url": _data_url(path)} for path in references]
if request.aspect_ratio:
body["aspect_ratio"] = request.aspect_ratio
if request.generate_audio is not None:
body["generate_audio"] = request.generate_audio
+ return body
+
+
+@dataclass
+class _Published:
+ data: bytes
+ request_id: str
+ model: str | None
+ duration_reported: float | None
+ host: str | None
+ polls: int
+ elapsed_seconds: float
+
+def _submit_poll_publish(
+ *,
+ verb: str,
+ endpoint: str,
+ body: dict[str, Any],
+ out: Path,
+ credential: Credential,
+ call: HttpCall,
+ download: Callable[[str, str], bytes],
+ sleep: Callable[[float], None],
+ poll_timeout: float | None,
+ accept: Callable[[bytes, dict[str, Any]], None] | None = None,
+) -> _Published:
+ """POST `body` to `endpoint`, poll `/videos/{id}`, download, verify, publish atomically.
+
+ `accept(data, final_poll)` may refuse the clip (raise SystemExit) before anything
+ is written — `video-extend` uses it to verify the returned length."""
started = time.monotonic()
- status, reply = call("POST", f"{API_BASE}/videos/generations", credential.token, body)
+ status, reply = call("POST", f"{API_BASE}/videos/{endpoint}", credential.token, body)
if status in (401, 403):
raise SystemExit(
- f"video: xAI rejected the credential ({credential.source}) with HTTP {status}: {_error_detail(reply)}\n"
+ f"{verb}: xAI rejected the credential ({credential.source}) with HTTP {status}: {_error_detail(reply)}\n"
+ (
f" the grok login token may have been revoked or rotated — run `{GROK_REFRESH_COMMAND}` {GROK_REFRESH_WHERE} or `{GROK_LOGIN_COMMAND}`."
if credential.source == AUTH_SOURCE_GROK_LOGIN
@@ -212,18 +376,20 @@ def generate_video(
)
request_id = reply.get("request_id") if isinstance(reply, dict) else None
if status not in (200, 202) or not request_id:
- raise SystemExit(f"video: generation request refused (HTTP {status}): {_error_detail(reply)}")
+ raise SystemExit(f"{verb}: generation request refused (HTTP {status}): {_error_detail(reply)}")
deadline = time.monotonic() + (POLL_TIMEOUT_SECONDS if poll_timeout is None else poll_timeout)
polls = 0
video_url: str | None = None
duration_reported: float | None = None
model_reported: str | None = None
+ final: dict[str, Any] = {}
while True:
polls += 1
http, poll = call("GET", f"{API_BASE}/videos/{request_id}", credential.token, None)
poll_status = poll.get("status") if isinstance(poll, dict) else None
if http in (200, 202) and poll_status in _DONE_STATUSES:
+ final = poll
video = poll.get("video") or {}
video_url = video.get("url") if isinstance(video, dict) else None
duration_reported = video.get("duration") if isinstance(video, dict) else None
@@ -231,42 +397,205 @@ def generate_video(
break
if http not in (200, 202) or poll_status in _FAILED_STATUSES:
raise SystemExit(
- f"video: generation {request_id} ended with status={poll_status!r} (HTTP {http}): {_error_detail(poll)}"
+ f"{verb}: generation {request_id} ended with status={poll_status!r} (HTTP {http}): {_error_detail(poll)}"
)
if time.monotonic() >= deadline:
raise SystemExit(
- f"video: generation {request_id} still {poll_status!r} after {polls} polls — poll timeout; "
+ f"{verb}: generation {request_id} still {poll_status!r} after {polls} polls — poll timeout; "
"nothing was written"
)
sleep(POLL_INTERVAL_SECONDS)
if not video_url:
- raise SystemExit(f"video: generation {request_id} is done but reported no video url")
+ raise SystemExit(f"{verb}: generation {request_id} is done but reported no video url")
data = download(video_url, credential.token)
verify_mp4(data)
+ if accept is not None:
+ accept(data, final)
elapsed = time.monotonic() - started
out.parent.mkdir(parents=True, exist_ok=True)
tmp = out.with_name(out.name + ".part")
tmp.write_bytes(data)
os.replace(tmp, out)
+ return _Published(
+ data=data, request_id=str(request_id), model=model_reported, duration_reported=duration_reported,
+ host=_redact_host(video_url), polls=polls, elapsed_seconds=elapsed,
+ )
+
+def generate_video(
+ request: VideoRequest,
+ *,
+ credential: Credential | None = None,
+ call: HttpCall | None = None,
+ download: Callable[[str, str], bytes] | None = None,
+ sleep: Callable[[float], None] | None = None,
+ poll_timeout: float | None = None,
+) -> VideoResult:
+ """Generate one clip (`/videos/generations`) and return a VideoResult. Raises SystemExit on any failure."""
+ # Late-bound so a test that monkeypatches the module-level transport never
+ # reaches the network through a default captured at definition time.
+ call = call or http_json
+ download = download or http_download
+ sleep = sleep or time.sleep
+ _validate(request)
+ credential = credential or resolve_credential()
+ out = request.out.expanduser().resolve()
+ image = _resolved(request.image)
+ last_frame = _resolved(request.last_frame)
+ references = [path.expanduser().resolve() for path in request.reference_images]
+
+ body = _build_generation_body(request, image, last_frame, references)
+ published = _submit_poll_publish(
+ verb="video", endpoint="generations", body=body, out=out, credential=credential,
+ call=call, download=download, sleep=sleep, poll_timeout=poll_timeout,
+ )
+ inputs: dict[str, Any] = {}
+ if image is not None:
+ inputs["image"] = str(image)
+ if last_frame is not None:
+ inputs["last_frame"] = str(last_frame)
+ if references:
+ inputs["reference_images"] = [str(path) for path in references]
return VideoResult(
out=out,
- bytes=len(data),
- model=model_reported or request.model,
- request_id=str(request_id),
+ bytes=len(published.data),
+ model=published.model or request.model,
+ request_id=published.request_id,
auth_source=credential.source,
- elapsed_seconds=elapsed,
- polls=polls,
+ elapsed_seconds=published.elapsed_seconds,
+ polls=published.polls,
duration_requested=request.duration,
- duration_reported=duration_reported,
- host=_redact_host(video_url),
+ duration_reported=published.duration_reported,
+ host=published.host,
image=image,
prompt=request.prompt,
resolution=request.resolution,
aspect_ratio=request.aspect_ratio,
generate_audio=request.generate_audio,
+ mode=request.mode,
+ inputs=inputs,
+ )
+
+
+def _validate_extend(request: ExtendRequest, *, probe: Callable[[Path], float]) -> float:
+ if not request.prompt.strip():
+ raise SystemExit("video-extend: empty prompt; pass --prompt or --prompt-file")
+ if request.model != CLASSIC_MODEL:
+ raise SystemExit(f"video-extend: only the classic {CLASSIC_MODEL} supports extension (the API answers 400 for {request.model})")
+ if not (EXTEND_DURATION_MIN <= request.duration <= EXTEND_DURATION_MAX):
+ raise SystemExit(
+ f"video-extend: --duration is the extension length, {EXTEND_DURATION_MIN}..{EXTEND_DURATION_MAX} seconds, got {request.duration}"
+ )
+ clip = request.video.expanduser().resolve()
+ _require_mp4_clip(clip, verb="video-extend")
+ seconds = probe(clip)
+ low, high = EXTEND_INPUT_SECONDS
+ if not (low <= seconds <= high + EXTEND_INPUT_SLACK_SECONDS):
+ raise SystemExit(
+ f"video-extend: the input clip is {seconds:.2f}s; the API extends clips of {low:g}..{high:g}s — trim it first"
+ )
+ return seconds
+
+
+def extend_video(
+ request: ExtendRequest,
+ *,
+ credential: Credential | None = None,
+ call: HttpCall | None = None,
+ download: Callable[[str, str], bytes] | None = None,
+ sleep: Callable[[float], None] | None = None,
+ poll_timeout: float | None = None,
+ probe: Callable[[Path], float] | None = None,
+) -> VideoResult:
+ """Continue one clip (`/videos/extensions`): the result is input + `duration` seconds in one mp4."""
+ call = call or http_json
+ download = download or http_download
+ sleep = sleep or time.sleep
+ probe = probe or (lambda clip: probe_duration_seconds(clip, verb="video-extend"))
+ input_seconds = _validate_extend(request, probe=probe)
+ credential = credential or resolve_credential()
+ out = request.out.expanduser().resolve()
+ clip = request.video.expanduser().resolve()
+ body: dict[str, Any] = {
+ "model": request.model,
+ "prompt": request.prompt,
+ "duration": request.duration,
+ "video": {"url": _data_url(clip)},
+ }
+
+ def accept(data: bytes, final: dict[str, Any]) -> None:
+ # The API returns original + extension as one clip; anything shorter than the
+ # input means it did not extend and must not be published as if it had.
+ video = final.get("video") or {}
+ reported = video.get("duration") if isinstance(video, dict) else None
+ if isinstance(reported, (int, float)) and reported + 0.5 < input_seconds:
+ raise SystemExit(
+ f"video-extend: the API reported a {reported}s clip for a {input_seconds:.2f}s input — not an extension; nothing was written"
+ )
+
+ published = _submit_poll_publish(
+ verb="video-extend", endpoint="extensions", body=body, out=out, credential=credential,
+ call=call, download=download, sleep=sleep, poll_timeout=poll_timeout, accept=accept,
+ )
+ return VideoResult(
+ out=out, bytes=len(published.data), model=published.model or request.model, request_id=published.request_id,
+ auth_source=credential.source, elapsed_seconds=published.elapsed_seconds, polls=published.polls,
+ duration_requested=request.duration, duration_reported=published.duration_reported, host=published.host,
+ prompt=request.prompt, mode=MODE_EXTEND, inputs={"video": str(clip)}, input_duration=input_seconds,
+ )
+
+
+def _validate_edit(request: EditRequest, *, probe: Callable[[Path], float]) -> float:
+ if not request.prompt.strip():
+ raise SystemExit("video-edit: empty prompt; pass --prompt or --prompt-file")
+ if request.model != CLASSIC_MODEL:
+ raise SystemExit(f"video-edit: only the classic {CLASSIC_MODEL} supports editing (the API answers 400 for {request.model})")
+ clip = request.video.expanduser().resolve()
+ _require_mp4_clip(clip, verb="video-edit")
+ seconds = probe(clip)
+ if seconds > EDIT_INPUT_MAX_SECONDS:
+ raise SystemExit(
+ f"video-edit: the input clip is {seconds:.2f}s; the API edits clips of at most {EDIT_INPUT_MAX_SECONDS}s — trim it first "
+ f"(e.g. ffmpeg -i in.mp4 -t 8 -c copy in-8s.mp4)"
+ )
+ return seconds
+
+
+def edit_video(
+ request: EditRequest,
+ *,
+ credential: Credential | None = None,
+ call: HttpCall | None = None,
+ download: Callable[[str, str], bytes] | None = None,
+ sleep: Callable[[float], None] | None = None,
+ poll_timeout: float | None = None,
+ probe: Callable[[Path], float] | None = None,
+) -> VideoResult:
+ """Edit one clip (`/videos/edits`); length, aspect and resolution come from the input."""
+ call = call or http_json
+ download = download or http_download
+ sleep = sleep or time.sleep
+ probe = probe or (lambda clip: probe_duration_seconds(clip, verb="video-edit"))
+ input_seconds = _validate_edit(request, probe=probe)
+ credential = credential or resolve_credential()
+ out = request.out.expanduser().resolve()
+ clip = request.video.expanduser().resolve()
+ body: dict[str, Any] = {
+ "model": request.model,
+ "prompt": request.prompt,
+ "video": {"url": _data_url(clip)},
+ }
+ published = _submit_poll_publish(
+ verb="video-edit", endpoint="edits", body=body, out=out, credential=credential,
+ call=call, download=download, sleep=sleep, poll_timeout=poll_timeout,
+ )
+ return VideoResult(
+ out=out, bytes=len(published.data), model=published.model or request.model, request_id=published.request_id,
+ auth_source=credential.source, elapsed_seconds=published.elapsed_seconds, polls=published.polls,
+ duration_requested=0, duration_reported=published.duration_reported, host=published.host,
+ prompt=request.prompt, mode=MODE_EDIT, inputs={"video": str(clip)}, input_duration=input_seconds,
)
@@ -280,11 +609,22 @@ def _print_json(payload: dict) -> None:
sys.stdout.write(text)
-def add_arguments(parser: argparse.ArgumentParser) -> None:
- parser.add_argument("--image", required=True, type=Path, help="the still to animate (PNG/JPEG/WebP)")
+def _add_prompt_and_report(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--prompt")
parser.add_argument("--prompt-file", type=Path)
parser.add_argument("--out", required=True, type=Path, help="destination .mp4")
+ parser.add_argument("--report", type=Path, help="write the sprite-gen-video-report JSON here")
+
+
+def add_arguments(parser: argparse.ArgumentParser) -> None:
+ parser.add_argument("--image", type=Path, help="the still to animate / the first frame (PNG/JPEG/WebP)")
+ parser.add_argument("--last-frame", type=Path, help=f"pin the closing frame ({DEFAULT_MODEL} only); alone or with --image")
+ parser.add_argument(
+ "--reference", dest="reference_images", action="append", type=Path, default=None, metavar="IMAGE",
+ help=f"reference image, repeatable up to {REFERENCE_MAX}; refer to them in the prompt as <IMAGE_0>, <IMAGE_1>, … "
+ f"({DEFAULT_MODEL} only, {REFERENCE_MAX_RESOLUTION} max)",
+ )
+ _add_prompt_and_report(parser)
parser.add_argument("--duration", type=int, default=6, help=f"seconds, {DURATION_MIN}..{DURATION_MAX} (default 6)")
parser.add_argument("--resolution", choices=RESOLUTIONS, default="720p")
parser.add_argument("--aspect-ratio", choices=ASPECT_RATIOS, default=None, help="default: the still's own ratio")
@@ -292,31 +632,64 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
audio = parser.add_mutually_exclusive_group()
audio.add_argument("--audio", dest="generate_audio", action="store_true", default=None, help="ask for generated audio")
audio.add_argument("--no-audio", dest="generate_audio", action="store_false", help="silent clip")
- parser.add_argument("--report", type=Path, help="write the sprite-gen-video-report JSON here")
-def _run(args: argparse.Namespace) -> int:
+def add_extend_arguments(parser: argparse.ArgumentParser) -> None:
+ parser.add_argument("--video", required=True, type=Path, help=f"the mp4 to continue ({EXTEND_INPUT_SECONDS[0]:g}..{EXTEND_INPUT_SECONDS[1]:g} s)")
+ _add_prompt_and_report(parser)
+ parser.add_argument(
+ "--duration", type=int, default=EXTEND_DURATION_DEFAULT,
+ help=f"seconds to ADD, {EXTEND_DURATION_MIN}..{EXTEND_DURATION_MAX} (default {EXTEND_DURATION_DEFAULT}); "
+ f"resolution and aspect ratio are inherited from the input (no flags), model is {CLASSIC_MODEL}",
+ )
+
+
+def add_edit_arguments(parser: argparse.ArgumentParser) -> None:
+ parser.add_argument("--video", required=True, type=Path, help=f"the mp4 to edit (at most {EDIT_INPUT_MAX_SECONDS} s)")
+ _add_prompt_and_report(parser)
+
+
+def _read_prompt(args: argparse.Namespace) -> str:
prompt = args.prompt
if args.prompt_file:
prompt = Path(args.prompt_file).expanduser().read_text(encoding="utf-8")
+ return (prompt or "").strip()
+
+
+def _publish_report(args: argparse.Namespace, result: VideoResult) -> int:
+ payload = result.to_dict()
+ if args.report:
+ report_path = Path(args.report).expanduser().resolve()
+ atomic_write_text(report_path, json.dumps(payload, ensure_ascii=False, indent=2) + "\n")
+ payload["report"] = str(report_path)
+ _print_json(payload)
+ return 0
+
+
+def _run(args: argparse.Namespace) -> int:
request = VideoRequest(
image=args.image,
- prompt=(prompt or "").strip(),
+ prompt=_read_prompt(args),
out=args.out,
duration=args.duration,
resolution=args.resolution,
aspect_ratio=args.aspect_ratio,
model=args.model,
generate_audio=args.generate_audio,
+ last_frame=args.last_frame,
+ reference_images=list(args.reference_images or []),
)
- result = generate_video(request)
- payload = result.to_dict()
- if args.report:
- report_path = Path(args.report).expanduser().resolve()
- atomic_write_text(report_path, json.dumps(payload, ensure_ascii=False, indent=2) + "\n")
- payload["report"] = str(report_path)
- _print_json(payload)
- return 0
+ return _publish_report(args, generate_video(request))
+
+
+def _run_extend(args: argparse.Namespace) -> int:
+ request = ExtendRequest(video=args.video, prompt=_read_prompt(args), out=args.out, duration=args.duration)
+ return _publish_report(args, extend_video(request))
+
+
+def _run_edit(args: argparse.Namespace) -> int:
+ request = EditRequest(video=args.video, prompt=_read_prompt(args), out=args.out)
+ return _publish_report(args, edit_video(request))
def _build_parser() -> argparse.ArgumentParser:
@@ -325,14 +698,37 @@ def _build_parser() -> argparse.ArgumentParser:
return parser
-def run(**kwargs: object) -> int:
- parser = _build_parser()
+def _build_extend_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(prog="sprite-gen video-extend", description=f"Continue a clip by N seconds via {CLASSIC_MODEL} (POST /v1/videos/extensions).")
+ add_extend_arguments(parser)
+ return parser
+
+
+def _build_edit_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(prog="sprite-gen video-edit", description=f"Edit a clip with a prompt via {CLASSIC_MODEL} (POST /v1/videos/edits).")
+ add_edit_arguments(parser)
+ return parser
+
+
+def _run_kwargs(parser: argparse.ArgumentParser, runner: Callable[[argparse.Namespace], int], kwargs: dict[str, object]) -> int:
known = {action.dest for action in parser._actions if action.dest != "help"}
unexpected = set(kwargs) - known
if unexpected:
raise TypeError(f"unexpected keyword argument(s): {', '.join(sorted(unexpected))}")
namespace = argparse.Namespace(**{dest: kwargs.get(dest, parser.get_default(dest)) for dest in known})
- return _run(namespace)
+ return runner(namespace)
+
+
+def run(**kwargs: object) -> int:
+ return _run_kwargs(_build_parser(), _run, kwargs)
+
+
+def run_extend(**kwargs: object) -> int:
+ return _run_kwargs(_build_extend_parser(), _run_extend, kwargs)
+
+
+def run_edit(**kwargs: object) -> int:
+ return _run_kwargs(_build_edit_parser(), _run_edit, kwargs)
def main(argv: list[str] | None = None) -> int:
diff --git a/tests/gen/test_video_modes.py b/tests/gen/test_video_modes.py
new file mode 100644
index 0000000..85f4628
--- /dev/null
+++ b/tests/gen/test_video_modes.py
@@ -0,0 +1,357 @@
+# SPDX-License-Identifier: Apache-2.0
+"""Grok video modes wired into `sprite-gen video` / `video-extend` / `video-edit`:
+request bodies per mode, the unchanged image-to-video body, local pre-checks that
+run before any call, and the report's `mode` / `inputs`. No network — HTTP is a fake."""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+import pytest
+from PIL import Image
+
+from sprite_gen.gen import video
+
+MP4 = b"\x00\x00\x00\x18ftypisom" + b"\x00" * 64
+CRED = video.Credential(token="tok", source=video.AUTH_SOURCE_GROK_LOGIN)
+
+
+def _still(tmp_path: Path, name: str = "still.png", size=(8, 8)) -> Path:
+ path = tmp_path / name
+ Image.new("RGBA", size, (255, 120, 0, 255)).save(path)
+ return path
+
+
+def _clip(tmp_path: Path, name: str = "in.mp4", payload: bytes = MP4) -> Path:
+ path = tmp_path / name
+ path.write_bytes(payload)
+ return path
+
+
+class _FakeApi:
+ def __init__(self, post=(200, {"request_id": "req-1"}), done=None):
+ self.post = post
+ self.done = done or {"status": "done", "video": {"url": "https://vidgen.x.ai/v/1.mp4", "duration": 5.04}, "model": "grok-imagine-video-1.5"}
+ self.calls: list[tuple[str, str, str, dict | None]] = []
+
+ def call(self, method, url, token, body):
+ self.calls.append((method, url, token, body))
+ return self.post if method == "POST" else (200, self.done)
+
+ def download(self, url, token):
+ return MP4
+
+ def kw(self):
+ return dict(credential=CRED, call=self.call, download=self.download, sleep=lambda s: None)
+
+ @property
+ def body(self) -> dict:
+ return self.calls[0][3]
+
+ @property
+ def endpoint(self) -> str:
+ return self.calls[0][1]
+
+
+def _gen(tmp_path: Path, **overrides) -> video.VideoRequest:
+ kwargs = dict(image=None, prompt="she charges at the oni", out=tmp_path / "clip.mp4")
+ kwargs.update(overrides)
+ return video.VideoRequest(**kwargs)
+
+
+# --- the image-to-video body is what it was -------------------------------------
+
+
+def test_image_only_body_is_the_pre_modes_snapshot(tmp_path: Path) -> None:
+ still = _still(tmp_path)
+ api = _FakeApi()
+ result = video.generate_video(_gen(tmp_path, image=still, duration=6, resolution="720p"), **api.kw())
+ assert api.endpoint == f"{video.API_BASE}/videos/generations"
+ assert list(api.body) == ["model", "prompt", "duration", "resolution", "image"]
+ assert api.body == {
+ "model": "grok-imagine-video-1.5",
+ "prompt": "she charges at the oni",
+ "duration": 6,
+ "resolution": "720p",
+ "image": {"url": video._data_url(still)},
+ }
+ assert result.mode == "image-to-video"
+ payload = result.to_dict()
+ assert payload["mode"] == "image-to-video" and payload["inputs"] == {"image": str(still.resolve())}
+ assert payload["image"] == str(still.resolve()) # the pre-modes field stays
+ assert "input_duration" not in payload
+
+
+def test_image_only_body_with_aspect_and_audio_keeps_its_key_order(tmp_path: Path) -> None:
+ api = _FakeApi()
+ video.generate_video(_gen(tmp_path, image=_still(tmp_path), aspect_ratio="1:1", generate_audio=False), **api.kw())
+ assert list(api.body) == ["model", "prompt", "duration", "resolution", "image", "aspect_ratio", "generate_audio"]
+ assert "last_frame" not in api.body and "reference_images" not in api.body
+
+
+def test_report_keeps_every_pre_modes_field(tmp_path: Path) -> None:
+ api = _FakeApi()
+ payload = video.generate_video(_gen(tmp_path, image=_still(tmp_path)), **api.kw()).to_dict()
+ assert set(payload) >= {
+ "kind", "provider", "auth_source", "model", "request_id", "image", "prompt", "out", "bytes", "host",
+ "duration_requested", "duration_reported", "resolution", "aspect_ratio", "generate_audio", "elapsed_seconds", "polls",
+ }
+
+
+# --- first/last frame and reference bodies --------------------------------------
+
+
+def test_first_and_last_frame_posts_image_and_last_frame(tmp_path: Path) -> None:
+ first, last = _still(tmp_path, "first.png"), _still(tmp_path, "last.png")
+ api = _FakeApi()
+ result = video.generate_video(_gen(tmp_path, image=first, last_frame=last, resolution="480p", duration=5, aspect_ratio="16:9"), **api.kw())
+ assert api.endpoint.endswith("/videos/generations") and api.body["model"] == "grok-imagine-video-1.5"
+ assert api.body["image"] == {"url": video._data_url(first)}
+ assert api.body["last_frame"] == {"url": video._data_url(last)}
+ assert "reference_images" not in api.body
+ assert result.mode == "first-last"
+ assert result.to_dict()["inputs"] == {"image": str(first.resolve()), "last_frame": str(last.resolve())}
+
+
+def test_last_frame_alone_posts_no_image(tmp_path: Path) -> None:
+ last = _still(tmp_path, "last.png")
+ api = _FakeApi()
+ result = video.generate_video(_gen(tmp_path, last_frame=last), **api.kw())
+ assert "image" not in api.body and api.body["last_frame"] == {"url": video._data_url(last)}
+ assert result.mode == "last-frame"
+ payload = result.to_dict()
+ assert payload["image"] is None and payload["inputs"] == {"last_frame": str(last.resolve())}
+
+
+def test_reference_to_video_posts_reference_images_in_order(tmp_path: Path) -> None:
+ refs = [_still(tmp_path, f"ref{i}.png") for i in range(3)]
+ api = _FakeApi()
+ result = video.generate_video(
+ _gen(tmp_path, prompt="<IMAGE_0> fights <IMAGE_1> in <IMAGE_2>", reference_images=refs, resolution="480p"), **api.kw()
+ )
+ assert api.body["reference_images"] == [{"url": video._data_url(r)} for r in refs]
+ assert "image" not in api.body and "last_frame" not in api.body
+ assert result.mode == "reference"
+ payload = result.to_dict()
+ assert payload["inputs"] == {"reference_images": [str(r.resolve()) for r in refs]}
+ assert "data:" not in json.dumps(payload) and "vidgen.x.ai/v/1.mp4" not in json.dumps(payload)
+
+
+def test_reference_with_image_keeps_both(tmp_path: Path) -> None:
+ api = _FakeApi()
+ result = video.generate_video(_gen(tmp_path, image=_still(tmp_path, "a.png"), reference_images=[_still(tmp_path, "r.png")], prompt="<IMAGE_0>"), **api.kw())
+ assert "image" in api.body and len(api.body["reference_images"]) == 1
+ assert result.mode == "reference"
+
+
+# --- local pre-checks run before any call ---------------------------------------
+
+
+ ("build", "expected"),
+ [
+ (lambda t: _gen(t), "pass --image, --last-frame and/or --reference"),
+ (lambda t: _gen(t, last_frame=t / "missing.png"), "--last-frame still image not found"),
+ (lambda t: _gen(t, reference_images=[t / "missing.png"], prompt="<IMAGE_0>"), "--reference still image not found"),
+ (lambda t: _gen(t, reference_images=[_still(t, f"r{i}.png") for i in range(8)]), "at most 7 --reference"),
+ (lambda t: _gen(t, reference_images=[_still(t)], resolution="1080p"), "capped at 720p"),
+ (lambda t: _gen(t, reference_images=[_still(t), _still(t, "b.png")], prompt="<IMAGE_0> and <IMAGE_2>"), "<IMAGE_2> but only 2 --reference"),
+ (lambda t: _gen(t, image=_still(t), prompt="<IMAGE_0> walks"), "<IMAGE_0> but no --reference"),
+ (lambda t: _gen(t, image=_still(t), last_frame=_still(t, "l.png"), model=video.CLASSIC_MODEL), "classic grok-imagine-video rejects last_frame"),
+ (lambda t: _gen(t, last_frame=_still(t, "l.png"), model=video.CLASSIC_MODEL), "classic grok-imagine-video rejects last_frame"),
+ (lambda t: _gen(t, image=_still(t), reference_images=[_still(t, "r.png")], model=video.CLASSIC_MODEL), "rejects --image combined with --reference"),
+ ],
+)
+def test_mode_precheck_rejects_before_any_call(tmp_path: Path, build, expected) -> None:
+ api = _FakeApi()
+ with pytest.raises(SystemExit, match=expected):
+ video.generate_video(build(tmp_path), **api.kw())
+ assert api.calls == []
+
+
+def test_seven_references_and_tag_zero_to_six_are_accepted(tmp_path: Path) -> None:
+ refs = [_still(tmp_path, f"r{i}.png") for i in range(7)]
+ api = _FakeApi()
+ video.generate_video(_gen(tmp_path, reference_images=refs, prompt=" ".join(f"<IMAGE_{i}>" for i in range(7))), **api.kw())
+ assert len(api.body["reference_images"]) == 7
+
+
+# --- video-extend ----------------------------------------------------------------
+
+
+def _ext(tmp_path: Path, **overrides) -> video.ExtendRequest:
+ kwargs = dict(video=_clip(tmp_path), prompt="she presses the attack", out=tmp_path / "longer.mp4")
+ kwargs.update(overrides)
+ return video.ExtendRequest(**kwargs)
+
+
+def test_extend_posts_classic_model_to_extensions_and_inherits_the_rest(tmp_path: Path) -> None:
+ api = _FakeApi(done={"status": "done", "video": {"url": "https://vidgen.x.ai/v/2.mp4", "duration": 20.04}, "model": "grok-imagine-video"})
+ result = video.extend_video(_ext(tmp_path, duration=5), probe=lambda clip: 15.04, **api.kw())
+ assert api.endpoint == f"{video.API_BASE}/videos/extensions"
+ assert list(api.body) == ["model", "prompt", "duration", "video"]
+ assert api.body["model"] == "grok-imagine-video" and api.body["duration"] == 5
+ assert api.body["video"]["url"].startswith("data:video/mp4;base64,")
+ assert "resolution" not in api.body and "aspect_ratio" not in api.body
+ payload = result.to_dict()
+ assert payload["mode"] == "extend" and payload["inputs"] == {"video": str((tmp_path / "in.mp4").resolve())}
+ assert payload["input_duration"] == 15.04 and payload["duration_reported"] == 20.04 and payload["duration_requested"] == 5
+ assert result.out.read_bytes() == MP4
+
+
+def test_extend_accepts_the_frame_padded_fifteen_second_clip(tmp_path: Path) -> None:
+ # probe m4c: a 15.04 s container went through and came back 20.04 s.
+ api = _FakeApi(done={"status": "done", "video": {"url": "https://vidgen.x.ai/v/2.mp4", "duration": 20.04}, "model": "grok-imagine-video"})
+ video.extend_video(_ext(tmp_path, duration=5), probe=lambda clip: 15.04, **api.kw())
+ assert api.body["duration"] == 5
+
+
+def test_extend_refuses_a_result_shorter_than_the_input(tmp_path: Path) -> None:
+ api = _FakeApi(done={"status": "done", "video": {"url": "https://vidgen.x.ai/v/2.mp4", "duration": 5.0}, "model": "grok-imagine-video"})
+ request = _ext(tmp_path)
+ with pytest.raises(SystemExit, match="not an extension"):
+ video.extend_video(request, probe=lambda clip: 12.0, **api.kw())
+ assert not request.out.exists() and not list(tmp_path.glob("*.part"))
+
+
+ ("build", "seconds", "expected"),
+ [
+ (lambda t: _ext(t, prompt=" "), 5.0, "empty prompt"),
+ (lambda t: _ext(t, model=video.DEFAULT_MODEL), 5.0, "only the classic grok-imagine-video supports extension"),
+ (lambda t: _ext(t, duration=1), 5.0, "2..10 seconds"),
+ (lambda t: _ext(t, duration=11), 5.0, "2..10 seconds"),
+ (lambda t: _ext(t, video=_clip(t, "in.mov")), 5.0, "must be an .mp4"),
+ (lambda t: _ext(t, video=t / "nope.mp4"), 5.0, "input clip not found"),
+ (lambda t: _ext(t, video=_clip(t, "bad.mp4", b"<html>")), 5.0, "not an mp4"),
+ (lambda t: _ext(t), 1.5, "extends clips of 2..15s"),
+ (lambda t: _ext(t), 15.11, "extends clips of 2..15s"),
+ ],
+)
+def test_extend_precheck_rejects_before_any_call(tmp_path: Path, build, seconds, expected) -> None:
+ api = _FakeApi()
+ with pytest.raises(SystemExit, match=expected):
+ video.extend_video(build(tmp_path), probe=lambda clip: seconds, **api.kw())
+ assert api.calls == []
+
+
+# --- video-edit ------------------------------------------------------------------
+
+
+def _edit(tmp_path: Path, **overrides) -> video.EditRequest:
+ kwargs = dict(video=_clip(tmp_path), prompt="change her hakama to solid black", out=tmp_path / "edited.mp4")
+ kwargs.update(overrides)
+ return video.EditRequest(**kwargs)
+
+
+def test_edit_posts_classic_model_to_edits_without_duration(tmp_path: Path) -> None:
+ api = _FakeApi(done={"status": "done", "video": {"url": "https://vidgen.x.ai/v/3.mp4", "duration": 7.71}, "model": "grok-imagine-video"})
+ result = video.edit_video(_edit(tmp_path), probe=lambda clip: 8.0, **api.kw())
+ assert api.endpoint == f"{video.API_BASE}/videos/edits"
+ assert list(api.body) == ["model", "prompt", "video"]
+ assert api.body["model"] == "grok-imagine-video"
+ payload = result.to_dict()
+ assert payload["mode"] == "edit" and payload["input_duration"] == 8.0 and payload["duration_reported"] == 7.71
+ assert payload["inputs"] == {"video": str((tmp_path / "in.mp4").resolve())}
+
+
+ ("build", "seconds", "expected"),
+ [
+ (lambda t: _edit(t), 8.71, "at most 8.7s"),
+ (lambda t: _edit(t), 15.04, "at most 8.7s"),
+ (lambda t: _edit(t, video=_clip(t, "in.webm")), 5.0, "must be an .mp4"),
+ (lambda t: _edit(t, model=video.DEFAULT_MODEL), 5.0, "only the classic grok-imagine-video supports editing"),
+ (lambda t: _edit(t, prompt=""), 5.0, "empty prompt"),
+ ],
+)
+def test_edit_precheck_rejects_before_any_call(tmp_path: Path, build, seconds, expected) -> None:
+ api = _FakeApi()
+ with pytest.raises(SystemExit, match=expected):
+ video.edit_video(build(tmp_path), probe=lambda clip: seconds, **api.kw())
+ assert api.calls == []
+
+
+def test_edit_accepts_exactly_the_limit(tmp_path: Path) -> None:
+ api = _FakeApi()
+ video.edit_video(_edit(tmp_path), probe=lambda clip: 8.7, **api.kw())
+ assert len(api.calls) >= 1
+
+
+# --- ffprobe ---------------------------------------------------------------------
+
+
+def test_missing_ffprobe_is_loud(tmp_path: Path, monkeypatch) -> None:
+ monkeypatch.setattr(video.shutil, "which", lambda name: None)
+ with pytest.raises(SystemExit, match="ffprobe is required"):
+ video.probe_duration_seconds(_clip(tmp_path), verb="video-edit")
+
+
+def test_probe_reads_ffprobe_output(tmp_path: Path, monkeypatch) -> None:
+ class Proc:
+ returncode, stdout, stderr = 0, "8.004000\n", ""
+
+ monkeypatch.setattr(video.shutil, "which", lambda name: "/usr/bin/ffprobe")
+ monkeypatch.setattr(video.subprocess, "run", lambda *a, **k: Proc())
+ assert video.probe_duration_seconds(_clip(tmp_path), verb="video-edit") == pytest.approx(8.004)
+
+
+# --- CLI parsers and the unified CLI ---------------------------------------------
+
+
+def test_video_parser_takes_last_frame_and_repeated_references() -> None:
+ parser = video._build_parser()
+ args = parser.parse_args(["--last-frame", "b.png", "--prompt", "p", "--out", "o.mp4"])
+ assert args.image is None and args.last_frame == Path("b.png") and args.reference_images is None
+ args = parser.parse_args(["--reference", "x.png", "--reference", "y.png", "--image", "a.png", "--prompt", "<IMAGE_0>", "--out", "o.mp4"])
+ assert args.reference_images == [Path("x.png"), Path("y.png")] and args.image == Path("a.png")
+
+
+def test_extend_and_edit_parsers_have_no_resolution_aspect_or_model_flags() -> None:
+ extend = video._build_extend_parser()
+ args = extend.parse_args(["--video", "in.mp4", "--prompt", "p", "--out", "o.mp4"])
+ assert args.duration == video.EXTEND_DURATION_DEFAULT == 6
+ for bad in (["--resolution", "480p"], ["--aspect-ratio", "16:9"], ["--model", "grok-imagine-video-1.5"]):
+ with pytest.raises(SystemExit):
+ extend.parse_args(["--video", "in.mp4", "--prompt", "p", "--out", "o.mp4", *bad])
+ edit = video._build_edit_parser()
+ edit.parse_args(["--video", "in.mp4", "--prompt", "p", "--out", "o.mp4"])
+ for bad in (["--duration", "5"], ["--resolution", "480p"], ["--aspect-ratio", "16:9"], ["--model", "x"]):
+ with pytest.raises(SystemExit):
+ edit.parse_args(["--video", "in.mp4", "--prompt", "p", "--out", "o.mp4", *bad])
+
+
+def test_unified_cli_exposes_extend_and_edit_under_gen() -> None:
+ from sprite_gen import cli
+
+ assert {"video-extend", "video-edit"} <= set(cli.COMMANDS)
+ assert {"video", "video-extend", "video-edit"} <= set(cli.command_domains()["gen"])
+ assert not ({"video-extend", "video-edit"} & set(cli.command_domains().get("video", [])))
+ parser = cli._build_parser()
+ assert parser.parse_args(["video-extend", "--video", "in.mp4", "--prompt", "p", "--out", "o.mp4", "--duration", "5"]).command == "video-extend"
+ assert parser.parse_args(["video-edit", "--video", "in.mp4", "--prompt", "p", "--out", "o.mp4"]).command == "video-edit"
+ assert parser.parse_args(["video", "--last-frame", "b.png", "--prompt", "p", "--out", "o.mp4"]).command == "video"
+
+
+def test_pipeline_verbs_are_untouched_by_the_new_modes() -> None:
+ from sprite_gen import _modules
+
+ pipeline_b = next(p for p in _modules.PIPELINES if p["key"] == "B")
+ assert "video-extend" not in pipeline_b["verbs"] and "video-edit" not in pipeline_b["verbs"]
+
+
+def test_run_extend_kwargs_writes_report(tmp_path: Path, monkeypatch) -> None:
+ api = _FakeApi(done={"status": "done", "video": {"url": "https://vidgen.x.ai/v/2.mp4", "duration": 11.0}, "model": "grok-imagine-video"})
+ monkeypatch.setattr(video, "resolve_credential", lambda **_: CRED)
+ monkeypatch.setattr(video, "http_json", api.call)
+ monkeypatch.setattr(video, "http_download", api.download)
+ monkeypatch.setattr(video.time, "sleep", lambda s: None)
+ monkeypatch.setattr(video, "probe_duration_seconds", lambda clip, *, verb: 6.0)
+ out, report = tmp_path / "longer.mp4", tmp_path / "longer.report.json"
+ rc = video.run_extend(video=_clip(tmp_path), prompt="go on", prompt_file=None, out=out, duration=5, report=report)
+ assert rc == 0 and out.read_bytes() == MP4
+ payload = json.loads(report.read_text(encoding="utf-8"))
+ assert payload["mode"] == "extend" and payload["model"] == "grok-imagine-video" and payload["input_duration"] == 6.0
+ with pytest.raises(TypeError, match="unexpected keyword"):
+ video.run_edit(video=_clip(tmp_path), prompt="p", prompt_file=None, out=out, report=None, duration=5)