API字节跳动Seedance 系列模型
完整示例
创建、轮询并下载视频。
创建
API_KEY="$TUIWO_API_KEY"
BASE="https://ai.tuiwo.vip"
CREATE=$(curl -sS "$BASE/ai/api/v3/contents/generations/tasks" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "<seedance-model-id>",
"content": [{"type": "text", "text": "A calm lake at sunrise"}]
}')
echo "$CREATE"
TASK_ID=$(printf '%s' "$CREATE" | python -c "import sys,json;print(json.load(sys.stdin)['task_id'])")轮询
while true; do
BODY=$(curl -sS "$BASE/ai/api/v3/contents/generations/tasks/$TASK_ID" \
-H "Authorization: Bearer $API_KEY")
STATUS=$(printf '%s' "$BODY" | python -c "import sys,json;print(json.load(sys.stdin)['status'])")
echo "$STATUS"
case "$STATUS" in
succeeded|failed|cancelled|expired) break ;;
esac
sleep 5
done
printf '%s\n' "$BODY"下载
URL=$(printf '%s' "$BODY" | python -c "import sys,json;print(json.load(sys.stdin).get('content',{}).get('video_url',''))")
if [ -n "$URL" ]; then
curl -L "$URL" -o video.mp4
fi回调接收示例(Python)
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers.get("Content-Length", "0"))
body = json.loads(self.rfile.read(length) or b"{}")
print("task", body.get("task_id"), "status", body.get("status"))
self.send_response(200)
self.end_headers()
HTTPServer(("0.0.0.0", 8080), Handler).serve_forever()回调需要在公网 HTTPS 地址上提供;本示例仅用于本地联调,不能直接作为回调地址。