Video task status and downloads

interface

Purpose Interface
Submit task POST /v1/videos
Query task GET /v1/videos/{task_id}
Get video content GET /v1/videos/{task_id}/content

calling process

  1. Submits a JSON request and saves the real id in the response.
  2. polls GET /v1/videos/{task_id}.
  3. queued and in_progress indicate that they are still being processed.
  4. failed indicates that the task failed and the error should be logged and polling stopped.
  5. After the task is completed, call GET /v1/videos/{task_id}/content to obtain the video content. Some models will also return limited-time video addresses, so please download them as soon as possible.

Successful submission does not mean that the video has been generated. The response of the task status contains at least id and status; id should be saved and used as task_id for subsequent queries. If the submission times out and id is not received, do not blindly resend it to avoid repeated billing; first go to the task list of this site to check.

After completion, read the actual token count from usage.total_tokens. A missing field does not mean zero usage. WAN3 measures video duration, not tokens; seconds are not converted into tokens.

Automatically query until completed

The following Python example only queries submitted tasks and will not create them again. Set SHANHAI_BASE_URL, SHANHAI_API_KEY and TASK_ID before running; wait up to 20 minutes and query every 15 seconds. You can continue to query the same ID after waiting for timeout.

python -m pip install requests
import json
import os
import time
from pathlib import Path
import requests

base = os.environ["SHANHAI_BASE_URL"].rstrip("/")
task_id = os.environ["TASK_ID"]
headers = {"Authorization": "Bearer " + os.environ["SHANHAI_API_KEY"]}
deadline = time.monotonic() + 1200
while time.monotonic() < deadline:
    req = base + "/v1/videos/" + task_id
    with requests.get(req, headers=headers, timeout=60) as response:
        response.raise_for_status()
        task = response.json()
    status = task.get("status")
    if status == "completed":
        total_tokens = (task.get("usage") or {}).get("total_tokens")
        if total_tokens is not None:
            print("Token:", total_tokens)
        download = base + "/v1/videos/" + task_id + "/content"
        with requests.get(download, headers=headers, stream=True, timeout=180) as response:
            response.raise_for_status()
            if not response.headers.get("Content-Type", "").startswith(("video/", "application/octet-stream")):
                raise RuntimeError("下载响应不是视频,请保留任务编号联系站内客服")
            with Path("result.mp4").open("wb") as output:
                for chunk in response.iter_content(1024 * 1024):
                    output.write(chunk)
        print("已保存 result.mp4")
        break
    if status in ("failed", "cancelled", "canceled"):
        raise RuntimeError("任务结束:" + json.dumps(task.get("error", {}), ensure_ascii=False))
    time.sleep(15)
else:
    raise TimeoutError("等待超时,请继续查询原任务:" + task_id)

encountered a problem

If you encounter problems with calling or configuration, please contact the site customer service and provide the model name, error time and error information to facilitate troubleshooting.