diff --git a/app.py b/app.py index daeb77d..0476020 100644 --- a/app.py +++ b/app.py @@ -31,7 +31,7 @@ os.makedirs(OUTPUT_DIR, exist_ok=True) # Default to 1 if not set, but user can set higher for powerful servers MAX_CONCURRENT_JOBS = int(os.environ.get("MAX_CONCURRENT_JOBS", "5")) MAX_FILE_SIZE_MB = 2048 # 2GB limit -JOB_RETENTION_SECONDS = 3600 # 1 hour retention +JOB_RETENTION_SECONDS = int(os.environ.get("JOB_RETENTION_SECONDS", "3600")) # job/file retention (issue #46) DISABLE_YOUTUBE_URL = os.environ.get("DISABLE_YOUTUBE_URL", "false").lower() in ("1", "true", "yes") # ---- Cloud billing (paid / managed-keys) integration -------------------------- @@ -1218,6 +1218,7 @@ class HookRequest(BaseModel): input_filename: Optional[str] = None position: Optional[str] = "top" # top, center, bottom size: Optional[str] = "M" # S, M, L + duration_seconds: Optional[float] = None # None = hook visible for the whole clip @app.post("/api/hook") async def add_hook(req: HookRequest, request: Request): @@ -1271,7 +1272,7 @@ async def add_hook(req: HookRequest, request: Request): try: # Run in thread pool def run_hook(): - add_hook_to_video(input_path, req.text, output_path, position=req.position, font_scale=font_scale) + add_hook_to_video(input_path, req.text, output_path, position=req.position, font_scale=font_scale, duration=req.duration_seconds) loop = asyncio.get_event_loop() await loop.run_in_executor(None, run_hook) diff --git a/dashboard/src/components/ResultCard.jsx b/dashboard/src/components/ResultCard.jsx index 6893944..050dd32 100644 --- a/dashboard/src/components/ResultCard.jsx +++ b/dashboard/src/components/ResultCard.jsx @@ -271,6 +271,7 @@ export default function ResultCard({ clip, index, jobId, durableUrl, uploadPostK text: payload.text, position: payload.position, size: payload.size, + duration_seconds: payload.remotion?.displayDurationSec ?? null, input_filename: currentVideoUrl.split('/').pop() }) }); diff --git a/hooks.py b/hooks.py index 3649008..4d0a2f0 100644 --- a/hooks.py +++ b/hooks.py @@ -168,7 +168,7 @@ def create_hook_image(text, target_width, output_image_path="hook_overlay.png", img.save(output_image_path) return output_image_path, canvas_w, canvas_h -def add_hook_to_video(video_path, text, output_path, position="top", font_scale=1.0): +def add_hook_to_video(video_path, text, output_path, position="top", font_scale=1.0, duration=None): """ Overlays text hook onto video. position: 'top', 'center', 'bottom' @@ -219,7 +219,8 @@ def add_hook_to_video(video_path, text, output_path, position="top", font_scale= 'ffmpeg', '-y', '-i', video_path, '-i', img_path, - '-filter_complex', f"[0:v][1:v]overlay={overlay_x}:{overlay_y}", + '-filter_complex', f"[0:v][1:v]overlay={overlay_x}:{overlay_y}" + + (f":enable='between(t,0,{float(duration)})'" if duration else ""), '-c:a', 'copy', '-c:v', 'libx264', '-preset', 'fast', '-crf', '22', output_path diff --git a/main.py b/main.py index 27f6005..3c7752d 100644 --- a/main.py +++ b/main.py @@ -832,11 +832,29 @@ def get_viral_clips(transcript_result, video_duration): ) try: - response = client.models.generate_content( - model=model_name, - contents=prompt - ) - + # Retry transient Gemini failures (503 high demand, 429 quota, 500) + # with exponential backoff before giving up (issue #27). + max_attempts = 3 + response = None + for attempt in range(1, max_attempts + 1): + try: + response = client.models.generate_content( + model=model_name, + contents=prompt + ) + break + except Exception as e: + msg = str(e) + transient = any(tok in msg for tok in ( + '503', 'UNAVAILABLE', '429', 'RESOURCE_EXHAUSTED', + '500', 'INTERNAL', 'overloaded', 'Deadline' + )) + if attempt == max_attempts or not transient: + raise + wait = 5 * (2 ** (attempt - 1)) + print(f"⚠️ Gemini transient error (attempt {attempt}/{max_attempts}), retrying in {wait}s: {msg[:150]}") + time.sleep(wait) + # --- Cost Calculation --- try: usage = response.usage_metadata