fix: Gemini retry with backoff, hook overlay duration, configurable job retention
- Retry transient Gemini errors (503/429/500) up to 3x with exponential backoff before falling back to whole-video conversion (#27) - Thread the hook Duration slider through /api/hook down to the FFmpeg overlay enable filter so hooks can end after N seconds (#2) - Read JOB_RETENTION_SECONDS from env so self-hosters can keep finished jobs longer than the 1h default (#46)
This commit is contained in:
parent
3bc51e1f7c
commit
b1113d7a3f
4 changed files with 30 additions and 9 deletions
5
app.py
5
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
|
# 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_CONCURRENT_JOBS = int(os.environ.get("MAX_CONCURRENT_JOBS", "5"))
|
||||||
MAX_FILE_SIZE_MB = 2048 # 2GB limit
|
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")
|
DISABLE_YOUTUBE_URL = os.environ.get("DISABLE_YOUTUBE_URL", "false").lower() in ("1", "true", "yes")
|
||||||
|
|
||||||
# ---- Cloud billing (paid / managed-keys) integration --------------------------
|
# ---- Cloud billing (paid / managed-keys) integration --------------------------
|
||||||
|
|
@ -1218,6 +1218,7 @@ class HookRequest(BaseModel):
|
||||||
input_filename: Optional[str] = None
|
input_filename: Optional[str] = None
|
||||||
position: Optional[str] = "top" # top, center, bottom
|
position: Optional[str] = "top" # top, center, bottom
|
||||||
size: Optional[str] = "M" # S, M, L
|
size: Optional[str] = "M" # S, M, L
|
||||||
|
duration_seconds: Optional[float] = None # None = hook visible for the whole clip
|
||||||
|
|
||||||
@app.post("/api/hook")
|
@app.post("/api/hook")
|
||||||
async def add_hook(req: HookRequest, request: Request):
|
async def add_hook(req: HookRequest, request: Request):
|
||||||
|
|
@ -1271,7 +1272,7 @@ async def add_hook(req: HookRequest, request: Request):
|
||||||
try:
|
try:
|
||||||
# Run in thread pool
|
# Run in thread pool
|
||||||
def run_hook():
|
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()
|
loop = asyncio.get_event_loop()
|
||||||
await loop.run_in_executor(None, run_hook)
|
await loop.run_in_executor(None, run_hook)
|
||||||
|
|
|
||||||
|
|
@ -271,6 +271,7 @@ export default function ResultCard({ clip, index, jobId, durableUrl, uploadPostK
|
||||||
text: payload.text,
|
text: payload.text,
|
||||||
position: payload.position,
|
position: payload.position,
|
||||||
size: payload.size,
|
size: payload.size,
|
||||||
|
duration_seconds: payload.remotion?.displayDurationSec ?? null,
|
||||||
input_filename: currentVideoUrl.split('/').pop()
|
input_filename: currentVideoUrl.split('/').pop()
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
|
||||||
5
hooks.py
5
hooks.py
|
|
@ -168,7 +168,7 @@ def create_hook_image(text, target_width, output_image_path="hook_overlay.png",
|
||||||
img.save(output_image_path)
|
img.save(output_image_path)
|
||||||
return output_image_path, canvas_w, canvas_h
|
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.
|
Overlays text hook onto video.
|
||||||
position: 'top', 'center', 'bottom'
|
position: 'top', 'center', 'bottom'
|
||||||
|
|
@ -219,7 +219,8 @@ def add_hook_to_video(video_path, text, output_path, position="top", font_scale=
|
||||||
'ffmpeg', '-y',
|
'ffmpeg', '-y',
|
||||||
'-i', video_path,
|
'-i', video_path,
|
||||||
'-i', img_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:a', 'copy',
|
||||||
'-c:v', 'libx264', '-preset', 'fast', '-crf', '22',
|
'-c:v', 'libx264', '-preset', 'fast', '-crf', '22',
|
||||||
output_path
|
output_path
|
||||||
|
|
|
||||||
26
main.py
26
main.py
|
|
@ -832,10 +832,28 @@ def get_viral_clips(transcript_result, video_duration):
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = client.models.generate_content(
|
# Retry transient Gemini failures (503 high demand, 429 quota, 500)
|
||||||
model=model_name,
|
# with exponential backoff before giving up (issue #27).
|
||||||
contents=prompt
|
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 ---
|
# --- Cost Calculation ---
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue