#!/usr/bin/env python3
"""Generate videos via APIYI's Wan2.7 / HappyHorse (text / image / reference to video, video editing). Pure stdlib, zero dependencies."""
import argparse
import base64
import json
import os
import shutil
import sys
import time
import urllib.error
import urllib.request
# Line-buffer stdout even when redirected, so agents can tail progress from the background
sys.stdout.reconfigure(line_buffering=True)
# DashScope passthrough endpoint. NEVER use the flat /v1/videos path — it drops the media field
CREATE_URL = "https://api.apiyi.com/wan/api/v1/services/aigc/video-generation/video-synthesis"
TASK_URL = "https://api.apiyi.com/v1/tasks/{}"
# family x mode -> model ID. Edit-model naming is irregular (happyhorse is 1.0 with a hyphen) — don't hand-build these
FAMILY_MODELS = {
"wan": {"t2v": "wan2.7-t2v", "i2v": "wan2.7-i2v",
"r2v": "wan2.7-r2v", "edit": "wan2.7-videoedit"},
"happyhorse": {"t2v": "happyhorse-1.1-t2v", "i2v": "happyhorse-1.1-i2v",
"r2v": "happyhorse-1.1-r2v", "edit": "happyhorse-1.0-video-edit"},
}
# r2v reference caps: wan 5 images+videos combined, happyhorse images only, up to 9
MAX_REFS = {"wan": 5, "happyhorse": 9}
RATIOS = ("16:9", "9:16", "1:1", "4:3", "3:4")
# Generation is async: 720P/5s takes ~70-140s in practice, 1080P/long clips can exceed 5 minutes
POLL_FIRST_DELAY = 15
POLL_INTERVAL = 8
POLL_TIMEOUT = 20 * 60
def load_api_key():
"""Prefer the environment variable; otherwise look for .env in the script dir and its parent."""
key = os.environ.get("APIYI_API_KEY")
if key:
return key
here = os.path.dirname(os.path.abspath(__file__))
for d in (here, os.path.dirname(here)):
env_path = os.path.join(d, ".env")
if os.path.exists(env_path):
with open(env_path, encoding="utf-8") as f:
for line in f:
line = line.strip()
if line.startswith("APIYI_API_KEY") and "=" in line:
return line.split("=", 1)[1].strip().strip('"').strip("'")
return None
def project_root():
"""Walk up from the script location to the first dir containing .git or .claude; else use cwd."""
d = os.path.dirname(os.path.abspath(__file__))
while True:
if os.path.isdir(os.path.join(d, ".git")) or os.path.isdir(os.path.join(d, ".claude")):
return d
parent = os.path.dirname(d)
if parent == d:
return os.getcwd()
d = parent
def resolve_path(out):
"""Bare file name -> save under <project root>/wan-output/ so it's easy to find; else use the given path."""
if os.path.dirname(out):
return os.path.abspath(out)
out_dir = os.path.join(project_root(), "wan-output")
os.makedirs(out_dir, exist_ok=True)
return os.path.join(out_dir, out)
def media_source(src):
"""Asset inputs: pass URLs / data: through as-is; encode local files as base64 data URIs (verified on both series)."""
if src.startswith(("http://", "https://", "data:")):
return src
if not os.path.exists(src):
sys.exit(f"Asset file not found: {src}")
ext = src.lower().rsplit(".", 1)[-1]
mime = {"png": "image/png", "webp": "image/webp", "mp4": "video/mp4",
"mov": "video/quicktime"}.get(ext, "image/jpeg")
with open(src, "rb") as f:
return f"data:{mime};base64,{base64.b64encode(f.read()).decode()}"
def api_request(url, api_key, body=None, extra_headers=None):
"""The gateway labels responses content-encoding: gzip without compressing them — Accept-Encoding: identity is required."""
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
"Accept-Encoding": "identity",
}
if extra_headers:
headers.update(extra_headers)
data = json.dumps(body).encode() if body is not None else None
req = urllib.request.Request(url, data=data, headers=headers,
method="POST" if body is not None else "GET")
try:
with urllib.request.urlopen(req, timeout=60) as r:
return json.loads(r.read())
except urllib.error.HTTPError as e:
raise RuntimeError(f"Request failed HTTP {e.code}: {e.read().decode(errors='replace')[:800]}")
def download(url, path):
"""Download the result video: it's a signed OSS URL — NEVER send the Authorization header (403 if you do)."""
req = urllib.request.Request(url)
with urllib.request.urlopen(req, timeout=300) as r, open(path, "wb") as f:
shutil.copyfileobj(r, f)
return os.path.getsize(path)
def detect_mode(args):
if args.video:
return "edit"
if args.image:
return "i2v"
if args.ref_image or args.ref_video:
return "r2v"
return "t2v"
def build_media(args, mode):
media = []
if mode == "i2v":
media.append({"type": "first_frame", "url": media_source(args.image)})
elif mode == "r2v":
for src in args.ref_image:
media.append({"type": "reference_image", "url": media_source(src)})
for src in args.ref_video:
media.append({"type": "reference_video", "url": media_source(src)})
elif mode == "edit":
media.append({"type": "video", "url": media_source(args.video)})
for src in args.ref_image:
media.append({"type": "reference_image", "url": media_source(src)})
return media
def main():
api_key = load_api_key()
if not api_key:
sys.exit("API key not found: add a line APIYI_API_KEY=sk-xxx to .env in the skill folder"
" (the token must have the Wan&HappyHorse group enabled, pay-as-you-go billing)")
parser = argparse.ArgumentParser(description="Wan2.7 / HappyHorse video generation")
parser.add_argument("prompt", help="prompt (scene + camera + mood; refer to assets as 'image 1 / video 1')")
parser.add_argument("--model", default="wan", choices=sorted(FAMILY_MODELS),
help="wan=Wan2.7 (default, cheaper) / happyhorse=HappyHorse-1.1 (quality-oriented)")
parser.add_argument("--resolution", default="720P", type=str.upper,
choices=("720P", "1080P"), help="resolution, default 720P (note: no 480P)")
parser.add_argument("--ratio", default=None, choices=RATIOS,
help="aspect ratio (ignored with a first-frame image; undocumented for happyhorse — sent only when passed)")
parser.add_argument("--duration", type=int, default=5,
help="duration 2-15 integer seconds, default 5; capped at 10 with reference videos; edit mode follows the source")
parser.add_argument("--negative", help="negative prompt (things to avoid, under 500 chars)")
parser.add_argument("--no-prompt-extend", action="store_true",
help="disable prompt auto-expansion (on by default; helps short prompts)")
parser.add_argument("--seed", type=int, default=None, help="random seed, for reproducibility")
parser.add_argument("-i", "--image", help="first-frame image (local path or URL); enables image-to-video")
parser.add_argument("--ref-image", action="append", default=[],
help="reference image, repeatable (wan: 5 combined with videos; happyhorse: up to 9)")
parser.add_argument("--ref-video", action="append", default=[],
help="reference video URL, repeatable (wan only)")
parser.add_argument("--video", help="video to edit (URL; edit mode, requires --ref-image)")
parser.add_argument("-o", "--out", default="output.mp4", help="output file name")
args = parser.parse_args()
if args.image and (args.ref_image or args.ref_video):
sys.exit("First-frame mode (-i) and reference mode (--ref-image/--ref-video) are mutually exclusive.")
if args.video and args.image:
sys.exit("Video-edit mode (--video) and first-frame mode (-i) are mutually exclusive.")
if args.video and not args.ref_image:
sys.exit("Video-edit mode needs at least 1 reference image (--ref-image).")
if args.model == "happyhorse" and args.ref_video:
sys.exit("happyhorse does not support reference videos (--ref-video); wan only.")
n_refs = len(args.ref_image) + len(args.ref_video)
if n_refs > MAX_REFS[args.model]:
sys.exit(f"{args.model} allows at most {MAX_REFS[args.model]} reference assets, got {n_refs}.")
if not 2 <= args.duration <= 15:
sys.exit("Duration must be an integer of 2-15 seconds.")
if args.ref_video and args.duration > 10:
sys.exit("Duration is capped at 10 seconds when reference videos are included.")
mode = detect_mode(args)
model = FAMILY_MODELS[args.model][mode]
input_part = {"prompt": args.prompt}
if args.negative:
input_part["negative_prompt"] = args.negative
media = build_media(args, mode)
if media:
input_part["media"] = media
parameters = {
"resolution": args.resolution,
"duration": args.duration,
"prompt_extend": not args.no_prompt_extend,
}
if args.ratio:
parameters["ratio"] = args.ratio
if args.seed is not None:
parameters["seed"] = args.seed
body = {"model": model, "input": input_part, "parameters": parameters}
try:
resp = api_request(CREATE_URL, api_key, body,
extra_headers={"X-DashScope-Async": "enable"})
except (RuntimeError, OSError) as e:
sys.exit(f"Submission failed: {e}")
task_id = (resp.get("output") or {}).get("task_id") or resp.get("task_id")
if not task_id:
sys.exit(f"Submission failed, response: {json.dumps(resp, ensure_ascii=False)[:500]}")
print(f"Task submitted model={model} task_id={task_id}; generation usually takes 2-5 minutes, polling...")
t0 = time.time()
time.sleep(POLL_FIRST_DELAY)
while True:
try:
task = api_request(TASK_URL.format(task_id), api_key)
except (RuntimeError, OSError) as e: # network hiccups don't stop the poll loop
print(f" poll error (continuing): {e}")
time.sleep(POLL_INTERVAL)
continue
status = str(task.get("status", "unknown")).lower()
progress = task.get("progress", "")
elapsed = round(time.time() - t0)
# progress often sits at 30 (upstream only reports 0/10/30/100) — it is not stuck
print(f" [{elapsed:>4}s] status={status}" + (f" progress={progress}" if progress != "" else ""))
if status in ("completed", "failed"):
break
if time.time() - t0 > POLL_TIMEOUT:
sys.exit(f"Polling timed out ({POLL_TIMEOUT}s). The task is still server-side; query it later:\n"
f" GET {TASK_URL.format(task_id)}")
time.sleep(POLL_INTERVAL)
if status != "completed":
err = task.get("error") or task.get("fail_reason") or task
sys.exit(f"Generation failed (status={status}): {json.dumps(err, ensure_ascii=False)[:500]}"
"\n(failed tasks are never billed)")
result_url = task.get("result_url")
if not result_url:
sys.exit(f"Task completed but no video URL returned: {json.dumps(task, ensure_ascii=False)[:500]}")
path = resolve_path(args.out)
size = download(result_url, path)
print(f"Video saved to {path} ({size / 1e6:.1f} MB, {round(time.time() - t0)}s elapsed, "
f"model {model})")
if __name__ == "__main__":
main()