|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate one sample video (beach_waves) and convert it to GIF. |
| 4 | +Saves to examples/sample_outputs/beach_waves.mp4 and beach_waves.gif. |
| 5 | +
|
| 6 | +Requires: VISGATE_API_KEY. For GIF: ffmpeg (preferred) or pip install imageio imageio-ffmpeg. |
| 7 | +
|
| 8 | + cd examples && python make_video_and_gif.py |
| 9 | +""" |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import shutil |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | +import time |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import httpx |
| 19 | + |
| 20 | +# Allow running from repo root or examples/ |
| 21 | +sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 22 | +from _common import create_client |
| 23 | + |
| 24 | +OUTPUT_DIR = Path(__file__).resolve().parent / "sample_outputs" |
| 25 | +VIDEO_NAME = "beach_waves" |
| 26 | +VIDEO_PROMPT = "Waves crashing on a quiet beach at sunset, cinematic, 4k" |
| 27 | +VIDEO_DURATION = 4.0 |
| 28 | +POLL_SLEEP = 25 |
| 29 | +POLL_ATTEMPTS = 6 |
| 30 | + |
| 31 | +# GIF: fps and max duration to keep size reasonable |
| 32 | +GIF_FPS = 8 |
| 33 | +GIF_MAX_DURATION_SEC = 4.0 |
| 34 | + |
| 35 | + |
| 36 | +def download_url(url: str, path: Path, timeout: float = 120.0) -> None: |
| 37 | + with httpx.Client(timeout=timeout, follow_redirects=True) as client: |
| 38 | + r = client.get(url) |
| 39 | + r.raise_for_status() |
| 40 | + path.write_bytes(r.content) |
| 41 | + |
| 42 | + |
| 43 | +def convert_ffmpeg(mp4_path: Path, gif_path: Path) -> bool: |
| 44 | + ffmpeg = shutil.which("ffmpeg") |
| 45 | + if not ffmpeg: |
| 46 | + return False |
| 47 | + # Scale down and limit duration for smaller GIF |
| 48 | + cmd = [ |
| 49 | + ffmpeg, |
| 50 | + "-y", |
| 51 | + "-i", str(mp4_path), |
| 52 | + "-vf", f"fps={GIF_FPS},scale=480:-1:flags=lanczos", |
| 53 | + "-t", str(GIF_MAX_DURATION_SEC), |
| 54 | + str(gif_path), |
| 55 | + ] |
| 56 | + result = subprocess.run(cmd, capture_output=True, text=True, timeout=60) |
| 57 | + return result.returncode == 0 and gif_path.exists() |
| 58 | + |
| 59 | + |
| 60 | +def convert_imageio(mp4_path: Path, gif_path: Path) -> bool: |
| 61 | + try: |
| 62 | + import imageio |
| 63 | + except ImportError: |
| 64 | + return False |
| 65 | + try: |
| 66 | + reader = imageio.get_reader(mp4_path) |
| 67 | + meta = reader.get_meta_data() |
| 68 | + fps = meta.get("fps") or GIF_FPS |
| 69 | + duration_ms = 1000.0 / fps |
| 70 | + frames = [] |
| 71 | + for i, frame in enumerate(reader): |
| 72 | + if i >= int(GIF_MAX_DURATION_SEC * fps): |
| 73 | + break |
| 74 | + frames.append(frame) |
| 75 | + reader.close() |
| 76 | + if not frames: |
| 77 | + return False |
| 78 | + imageio.mimsave(gif_path, frames, duration=duration_ms, loop=0) |
| 79 | + return True |
| 80 | + except Exception: |
| 81 | + return False |
| 82 | + |
| 83 | + |
| 84 | +def main() -> int: |
| 85 | + import argparse |
| 86 | + parser = argparse.ArgumentParser(description="Generate beach_waves video + GIF or convert existing MP4 to GIF") |
| 87 | + parser.add_argument("--mp4", type=Path, help="Existing MP4 path; if set, only convert to GIF (no API call)") |
| 88 | + args = parser.parse_args() |
| 89 | + |
| 90 | + OUTPUT_DIR.mkdir(parents=True, exist_ok=True) |
| 91 | + mp4_path = args.mp4 if args.mp4 else (OUTPUT_DIR / f"{VIDEO_NAME}.mp4") |
| 92 | + gif_path = OUTPUT_DIR / f"{mp4_path.stem}.gif" |
| 93 | + |
| 94 | + if args.mp4: |
| 95 | + if not args.mp4.exists(): |
| 96 | + print(f"File not found: {args.mp4}") |
| 97 | + return 1 |
| 98 | + print(f"Converting {args.mp4} -> {gif_path}") |
| 99 | + if convert_ffmpeg(mp4_path, gif_path): |
| 100 | + print(f" Saved {gif_path} (ffmpeg)") |
| 101 | + elif convert_imageio(mp4_path, gif_path): |
| 102 | + print(f" Saved {gif_path} (imageio)") |
| 103 | + else: |
| 104 | + print(" Install ffmpeg or: pip install imageio imageio-ffmpeg") |
| 105 | + return 1 |
| 106 | + return 0 |
| 107 | + |
| 108 | + with create_client() as client: |
| 109 | + print(f"Generating video '{VIDEO_NAME}' (up to {POLL_ATTEMPTS * POLL_SLEEP}s wait) ...") |
| 110 | + for attempt in range(POLL_ATTEMPTS): |
| 111 | + try: |
| 112 | + vresult = client.videos.generate( |
| 113 | + model="fal-ai/flux-pro/video", |
| 114 | + prompt=VIDEO_PROMPT, |
| 115 | + duration_seconds=VIDEO_DURATION, |
| 116 | + ) |
| 117 | + if vresult.video_url: |
| 118 | + download_url(vresult.video_url, mp4_path, timeout=120.0) |
| 119 | + print(f" Saved {mp4_path}") |
| 120 | + break |
| 121 | + except Exception as e: |
| 122 | + print(f" Attempt {attempt + 1}: {e}") |
| 123 | + if attempt < POLL_ATTEMPTS - 1: |
| 124 | + print(f" Sleeping {POLL_SLEEP}s ...") |
| 125 | + time.sleep(POLL_SLEEP) |
| 126 | + else: |
| 127 | + print(" Video URL not ready. Run again later or run fetch_sample_outputs.py.") |
| 128 | + return 1 |
| 129 | + |
| 130 | + if not mp4_path.exists(): |
| 131 | + return 1 |
| 132 | + |
| 133 | + print("Converting to GIF ...") |
| 134 | + if convert_ffmpeg(mp4_path, gif_path): |
| 135 | + print(f" Saved {gif_path} (ffmpeg)") |
| 136 | + elif convert_imageio(mp4_path, gif_path): |
| 137 | + print(f" Saved {gif_path} (imageio)") |
| 138 | + else: |
| 139 | + print(" GIF conversion skipped: install ffmpeg or pip install imageio imageio-ffmpeg") |
| 140 | + return 1 |
| 141 | + |
| 142 | + print(f"\nDone: {mp4_path}, {gif_path}") |
| 143 | + return 0 |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + sys.exit(main()) |
0 commit comments