|
| 1 | +""" |
| 2 | +Step 12: Video with Veo from catalog. |
| 3 | +
|
| 4 | +Flow: |
| 5 | + 1. get_models(provider="fal", model_type="video") from API (database). |
| 6 | + 2. Filter models whose id/name contains "veo". |
| 7 | + 3. Pick the newest by first_seen_at. |
| 8 | + 4. Call videos.generate with that model; write result to sample_outputs. |
| 9 | +
|
| 10 | +Requires: VISGATE_API_KEY. Optional: VISGATE_FAL_API_KEY for BYOK. |
| 11 | + cd examples && python 12_veo_from_catalog.py |
| 12 | +""" |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import httpx |
| 18 | + |
| 19 | +from _common import create_client |
| 20 | + |
| 21 | +OUTPUT_DIR = Path(__file__).resolve().parent / "sample_outputs" |
| 22 | +OUTPUT_VIDEO = OUTPUT_DIR / "istanbul.mp4" |
| 23 | +PROMPT = "Cinematic drone over Istanbul at golden hour, Bosphorus and minarets, 4k" |
| 24 | +DURATION = 6.0 |
| 25 | +FALLBACK_MODEL = "fal-ai/veo3" |
| 26 | + |
| 27 | + |
| 28 | +def pick_newest_veo(models_response): |
| 29 | + """Filter models containing 'veo' in id/name, sort by first_seen_at (newest first), return first.""" |
| 30 | + veo = [ |
| 31 | + m |
| 32 | + for m in models_response.models |
| 33 | + if m |
| 34 | + and ("veo" in (m.id or "").lower() or "veo" in (m.name or "").lower()) |
| 35 | + ] |
| 36 | + if not veo: |
| 37 | + return None |
| 38 | + # Newest first (first_seen_at desc; None last) |
| 39 | + veo.sort(key=lambda m: m.first_seen_at or "", reverse=True) |
| 40 | + return veo[0].id |
| 41 | + |
| 42 | + |
| 43 | +def download_url(url: str, path: Path, timeout: float = 120.0) -> None: |
| 44 | + with httpx.Client(timeout=timeout, follow_redirects=True) as client: |
| 45 | + r = client.get(url) |
| 46 | + r.raise_for_status() |
| 47 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 48 | + path.write_bytes(r.content) |
| 49 | + |
| 50 | + |
| 51 | +def main() -> None: |
| 52 | + client = create_client() |
| 53 | + |
| 54 | + # 1. Get video models from API (database), newest first |
| 55 | + resp = client.models.list( |
| 56 | + provider="fal", |
| 57 | + model_type="video", |
| 58 | + sort="newest", |
| 59 | + limit=100, |
| 60 | + ) |
| 61 | + model_id = pick_newest_veo(resp) |
| 62 | + if not model_id: |
| 63 | + model_id = FALLBACK_MODEL |
| 64 | + print(f" No Veo in catalog; using fallback {model_id}") |
| 65 | + else: |
| 66 | + print(f" Using newest Veo from catalog: {model_id}") |
| 67 | + |
| 68 | + # 2. Generate video via Visgate API |
| 69 | + print(f" Generating video (prompt={PROMPT[:50]}...) ...") |
| 70 | + # Note: Veo expects duration as "4s"/"6s"/"8s". Visgate API should convert |
| 71 | + # duration_seconds to that format. If validation fails, try another provider. |
| 72 | + result = client.videos.generate( |
| 73 | + model=model_id, |
| 74 | + prompt=PROMPT, |
| 75 | + duration_seconds=6.0, |
| 76 | + ) |
| 77 | + if not result.video_url: |
| 78 | + print(" No video_url in response.") |
| 79 | + return |
| 80 | + print(f" Got video_url") |
| 81 | + |
| 82 | + # 3. Write to sample_outputs |
| 83 | + download_url(result.video_url, OUTPUT_VIDEO) |
| 84 | + print(f" Written {OUTPUT_VIDEO}") |
| 85 | + client.close() |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + main() |
0 commit comments