import os import re import sys import uuid import subprocess import threading import json import shutil import glob import time import zipfile import math import itertools import asyncio from datetime import datetime, timezone from dotenv import load_dotenv from typing import Any, Dict, Optional, List from contextlib import asynccontextmanager from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Request, Header, BackgroundTasks from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from fastapi.responses import HTMLResponse, FileResponse, JSONResponse from starlette.background import BackgroundTask from pydantic import BaseModel from s3_uploader import upload_job_artifacts, list_all_clips, upload_actor_to_s3, list_actor_gallery, upload_video_to_gallery, list_video_gallery import recut load_dotenv() # Constants UPLOAD_DIR = "uploads" OUTPUT_DIR = "output" os.makedirs(UPLOAD_DIR, exist_ok=True) os.makedirs(OUTPUT_DIR, exist_ok=True) # Configuration # 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 # How TikTok receives our uploads. MEDIA_UPLOAD lands the video in the user's # TikTok drafts so they finish the post inside TikTok's own editor; DIRECT_POST # publishes straight to their feed, which is Upload-Post's default. # # Drafts are the safer default for an automated pipeline: nothing reaches an # audience without the account owner seeing it first, and TikTok's own editor is # where covers, sounds and hashtags actually get chosen. The UI must say so — # a user who expects a published post and finds a draft will read it as a bug. TIKTOK_POST_MODE = os.environ.get("TIKTOK_POST_MODE", "MEDIA_UPLOAD").strip() JOB_RETENTION_SECONDS = int(os.environ.get("JOB_RETENTION_SECONDS", "3600")) # job/file retention (issue #46) # Ceiling for the working directory once it lives on a persistent volume: the # age-based sweep alone can't stop a burst of long videos from filling the disk. # 0 disables the cap. OUTPUT_MAX_GB = int(os.environ.get("OUTPUT_MAX_GB", "25")) # Same idea for source uploads, which are the biggest single files on disk. UPLOADS_MAX_GB = int(os.environ.get("UPLOADS_MAX_GB", "15")) # Pre-flight quality gate: warn before processing a YouTube source below this # height (0 disables). Only applies to URLs; uploads are whatever the user gave. QUALITY_GATE_MIN_HEIGHT = int(os.environ.get("QUALITY_GATE_MIN_HEIGHT", "720")) QUALITY_PROBE_SCRIPT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "quality_probe.py") DISABLE_YOUTUBE_URL = os.environ.get("DISABLE_YOUTUBE_URL", "false").lower() in ("1", "true", "yes") # ---- Cloud billing (paid / managed-keys) integration -------------------------- # All paid-mode code lives in the optional `cloud/` package and is imported ONLY # when BILLING_ENABLED is set. With the flag off, the app behaves exactly as the # self-hosted BYOK app does today (no extra dependencies required). BILLING_ENABLED = os.environ.get("BILLING_ENABLED", "").lower() in ("1", "true", "yes") # Force full pipeline logs to the client even under billing (local debugging). DEBUG_LOGS = os.environ.get("DEBUG_LOGS", "").lower() in ("1", "true", "yes") if BILLING_ENABLED: import cloud from cloud import managed_keys, metering as _metering, config as _cloud_config, alerts as _alerts from cloud.auth import get_current_user_optional else: cloud = None managed_keys = None _metering = None _cloud_config = None _alerts = None async def get_current_user_optional(request: Request): # No-op dependency in self-host mode: every request is anonymous / BYOK. return None async def _user_from_request(request: Request): """Load the authenticated cloud user (or None). Cheap indexed lookup.""" return await get_current_user_optional(request) async def resolve_gemini(request: Request) -> Optional[str]: """Resolve the Gemini API key for a request. Cloud (hosted) is PAID-ONLY: there is no BYOK for the core pipeline, so the ``X-Gemini-Key`` header is ignored — an entitled user (active plan or trial) gets the managed server key, everyone else gets ``None`` (→ 402, start trial). Self-host keeps BYOK: header wins, else the env fallback. """ if BILLING_ENABLED: user = await _user_from_request(request) if managed_keys.has_active_entitlement(user): return managed_keys.gemini_key() return None header = request.headers.get("X-Gemini-Key") if header: return header return os.environ.get("GEMINI_API_KEY") async def resolve_upload_post(request: Request, body_key: Optional[str] = None): """Resolve the Upload-Post key and the profile to post as. Returns ``(api_key, forced_profile_username_or_None)``. Cloud is paid-only: an entitled user gets the managed key + their own forced profile (body key / user_id ignored); a non-entitled user gets ``(None, None)``. Self-host keeps BYOK: header, then body key, then env. """ if BILLING_ENABLED: user = await _user_from_request(request) if managed_keys.has_active_entitlement(user): profile = await cloud.social_profiles.ensure_profile(user) return managed_keys.upload_post_key(), profile return None, None header = request.headers.get("X-Upload-Post-Key") key = header or body_key or os.environ.get("UPLOAD_POST_API_KEY") return key, None def gemini_missing_error(): """The right 4xx when no Gemini key could be resolved. 402 for a signed-in-but-not-entitled cloud user (needs a plan); 400 otherwise (BYOK header simply missing). """ if BILLING_ENABLED: return HTTPException(status_code=402, detail={ "error": "no_plan", "message": "This action needs an active plan. Choose a plan or add your own API key.", }) return HTTPException(status_code=400, detail="Missing X-Gemini-Key header") # Probe rate limiter. In-memory, resets on restart by design — the hard monthly # quota lives in the metering ledger; this only stops someone hammering the # proxy with metadata probes. _probe_times: dict = {} # user_id -> [monotonic timestamps] PROBES_PER_HOUR = 15 # Out-of-minutes upsell email: at most one per user per day (a client may # retry the same 402 many times). _last_quota_email: dict = {} _QUOTA_EMAIL_COOLDOWN = 24 * 3600 def _maybe_send_quota_email(user): if user is None or user.plan != "free" or not user.email: return now = time.monotonic() last = _last_quota_email.get(str(user.id)) if last is not None and now - last < _QUOTA_EMAIL_COOLDOWN: return _last_quota_email[str(user.id)] = now from cloud.emails import send_out_of_minutes_email upgrade_url = f"{_cloud_config.settings.frontend_url}/#/pricing" asyncio.create_task(send_out_of_minutes_email(user.email, upgrade_url)) def _check_probe_rate(user_id): now = time.monotonic() times = _probe_times.setdefault(str(user_id), []) times[:] = [t for t in times if now - t < 3600] if len(times) >= PROBES_PER_HOUR: raise HTTPException(status_code=429, detail="Too many requests this hour. Please slow down.") times.append(now) async def reserve_process_minutes(request, url, input_path, job_id): """Meter a managed /api/process request. Returns (user_id, priority, reservation_id, plan). BYOK / self-host requests don't consume minutes (priority 2, no reservation). For a managed (entitled, no BYOK header) request this probes the input duration, enforces the per-user concurrent-job limit, and reserves minutes — raising 402 (quota) or 429 (too many jobs) as needed. NOTE: in cloud mode ``resolve_gemini`` ignores ``X-Gemini-Key`` (paid-only, no BYOK), so we must NOT skip metering just because that header is present — otherwise a client could send a dummy header and run unlimited managed jobs on the operator's key for free. Only skip metering when billing is off. """ if not BILLING_ENABLED: return None, 2, None, None user = await _user_from_request(request) if not managed_keys.has_active_entitlement(user): return None, 2, None, None # shouldn't happen (resolve_gemini would have 402'd) priority = _cloud_config.PLAN_PRIORITY.get(user.plan, 1) # Per-user simultaneous job cap. limit = _cloud_config.PLAN_JOB_LIMIT.get(user.plan, 2) active = sum(1 for j in jobs.values() if j.get('user_id') == user.id and j.get('status') in ('queued', 'processing')) if active >= limit: raise HTTPException(status_code=429, detail="You already have the maximum number of jobs running. Please wait.") # Probe rate limit: probing costs a (cheap) proxied metadata call. The # 20-minute monthly quota is the real bound on free usage; there is no daily # job cap. _check_probe_rate(user.id) # Probe input duration (blocking → run in a thread). loop = asyncio.get_event_loop() try: if url: minutes = await loop.run_in_executor(None, _metering.probe_url_minutes, url) else: minutes = await loop.run_in_executor(None, _metering.probe_file_minutes, input_path) except Exception: raise HTTPException(status_code=400, detail="Could not determine the video duration. Try a different source.") minutes = max(1, math.ceil(minutes)) try: reservation_id = await _metering.reserve_minutes(user.id, minutes, job_id) except _metering.QuotaExceeded as e: _maybe_send_quota_email(user) raise HTTPException(status_code=402, detail={ "error": "quota_exceeded", "minutes_required": e.required, "minutes_remaining": e.remaining, }) return user.id, priority, reservation_id, user.plan async def reserve_managed_action(request, minutes, job_id, job_type): """Reserve quota for a synchronous managed action (e.g. thumbnail image gen). Returns a reservation_id to commit/release around the work, or None for BYOK / self-host. Raises 402 when the user is out of minutes. """ if not BILLING_ENABLED: return None if minutes <= 0: # Free action (e.g. burning captions). Skip the ledger entirely rather # than writing a 0-minute row on every call — the endpoint's own # entitlement gate is what bounds it. return None user = await _user_from_request(request) if not managed_keys.has_active_entitlement(user): return None # BYOK header path (self-host) — not metered try: return await _metering.reserve_minutes(user.id, minutes, job_id, job_type) except _metering.QuotaExceeded as e: _maybe_send_quota_email(user) raise HTTPException(status_code=402, detail={ "error": "quota_exceeded", "minutes_required": e.required, "minutes_remaining": e.remaining, }) async def require_managed_entitlement(request): """Gate a managed compute endpoint that doesn't resolve a Gemini key itself. Some endpoints (subtitle/hook FFmpeg re-encodes, render proxy, the thumbnail upload that kicks off a YouTube download + Whisper) do expensive server work without ever calling ``resolve_gemini``, so nothing was stopping an anonymous or non-entitled caller from driving unbounded compute in cloud mode. In cloud mode this rejects them with 402; it's a no-op for self-host (BILLING off). """ if not BILLING_ENABLED: return None user = await _user_from_request(request) if not managed_keys.has_active_entitlement(user): raise gemini_missing_error() return user async def _owner_id(request): """The authenticated cloud user's id to stamp on a new job/session, or None for self-host / BYOK / anonymous (BILLING off → nothing to scope).""" if not BILLING_ENABLED: return None user = await _user_from_request(request) return user.id if user else None async def _assert_job_owner(request, record): """Cloud multi-tenant guard: reject unless the caller owns this in-memory job/session record. No-op for self-host (BILLING off) and for records with no owner stamped (BYOK / self-host jobs never set ``user_id``). Returns 404 rather than 403 so a non-owner can't even confirm the id exists. UUID ids already make these stores hard to enumerate; this closes the gap for a shared/leaked id. """ if not BILLING_ENABLED: return owner = record.get("user_id") if isinstance(record, dict) else None if owner is None: return user = await _user_from_request(request) # Compare as strings: live jobs store a uuid.UUID, but jobs recovered from # the .owner sidecar store its string form — UUID != str is always True. if user is None or str(user.id) != str(owner): raise HTTPException(status_code=404, detail="Not found") # Application State # PriorityQueue holds (priority, seq, job_id). Lower priority dispatches first: # pro=0, starter/creator=1, BYOK/anonymous/self-host=2. The seq counter keeps # FIFO order within a priority and makes the tuples always comparable. With # BILLING disabled every job enqueues at priority 2 → plain FIFO as before. job_queue = asyncio.PriorityQueue() _job_seq = itertools.count() jobs: Dict[str, Dict] = {} thumbnail_sessions: Dict[str, Dict] = {} publish_jobs: Dict[str, Dict] = {} # {publish_id: {status, result, error}} # Semester to limit concurrency to MAX_CONCURRENT_JOBS concurrency_semaphore = asyncio.Semaphore(MAX_CONCURRENT_JOBS) def _enqueue_job(job_id: str, priority: int = 2): job_queue.put_nowait((priority, next(_job_seq), job_id)) def _relocate_root_job_artifacts(job_id: str, job_output_dir: str) -> bool: """ Backward-compat rescue: If main.py accidentally wrote metadata/clips into OUTPUT_DIR root (e.g. output/_...), move them into output// so the API can find and serve them. """ try: os.makedirs(job_output_dir, exist_ok=True) root = OUTPUT_DIR pattern = os.path.join(root, f"{job_id}_*_metadata.json") meta_candidates = sorted(glob.glob(pattern), key=lambda p: os.path.getmtime(p), reverse=True) if not meta_candidates: return False # Move the newest metadata and its associated clips. metadata_path = meta_candidates[0] base_name = os.path.basename(metadata_path).replace("_metadata.json", "") # Move metadata dest_metadata = os.path.join(job_output_dir, os.path.basename(metadata_path)) if os.path.abspath(metadata_path) != os.path.abspath(dest_metadata): shutil.move(metadata_path, dest_metadata) # Move any clips that match the same base_name into the job folder clip_pattern = os.path.join(root, f"{base_name}_clip_*.mp4") for clip_path in glob.glob(clip_pattern): dest_clip = os.path.join(job_output_dir, os.path.basename(clip_path)) if os.path.abspath(clip_path) != os.path.abspath(dest_clip): shutil.move(clip_path, dest_clip) # Also move any temp_ clips that might remain temp_clip_pattern = os.path.join(root, f"temp_{base_name}_clip_*.mp4") for clip_path in glob.glob(temp_clip_pattern): dest_clip = os.path.join(job_output_dir, os.path.basename(clip_path)) if os.path.abspath(clip_path) != os.path.abspath(dest_clip): shutil.move(clip_path, dest_clip) return True except Exception: return False def _canonical_clip_file(output_dir, base_name, index): """The file to serve for clip ``index``, preferring a derived version. The pipeline writes the clean reframe as ``_clip_.mp4`` and any post-processing (auto-captions, /api/subtitle re-styles, and clip-editor recuts) as ``subtitled__.mp4`` / ``recut__.mp4``, keeping the original for re-styling. Every place that rebuilds the canonical name from disk — restore after a restart, the R2 upload, the download bundle — must therefore resolve to the newest derived file, or clips silently lose their captions (or their recut) on a redeploy. """ clean = f"{base_name}_clip_{index + 1}.mp4" try: # subtitled_*_{clean} also matches subtitled__recut__{clean} # and subtitled__hooked__{clean}, i.e. captioned recuts and # captioned hooks; the bare recut_/hooked_/hook_ patterns cover # derivations that shipped uncaptioned (hook_ is the legacy manual- # hook prefix, kept so old jobs still resolve). derived = (glob.glob(os.path.join(output_dir, f"subtitled_*_{clean}")) + glob.glob(os.path.join(output_dir, f"recut_*_{clean}")) + glob.glob(os.path.join(output_dir, f"hooked_*_{clean}")) + glob.glob(os.path.join(output_dir, f"hook_{clean}"))) except Exception: derived = [] if not derived: return clean # Highest timestamp wins — that's the most recent styling. return os.path.basename(max(derived, key=os.path.getmtime)) def _strip_burned_captions(output_dir, filename): """Walk ``subtitled__`` prefixes back to the file without burned captions. Returns the name unchanged when there is nothing to strip (or when the underlying file is gone, e.g. a library restore that only kept the current version). """ while True: m = re.match(r'^subtitled_\d+_(.+)$', filename) if not m or not os.path.exists(os.path.join(output_dir, m.group(1))): return filename filename = m.group(1) def _strip_burned_hook(output_dir, filename): """Walk ``hooked__`` (and legacy ``hook_``) prefixes back to the file without a burned hook. Same fail-safe contract as _strip_burned_captions: the name is returned unchanged when there is nothing to strip or the underlying file is gone.""" while True: m = re.match(r'^(?:hooked_\d+_|hook_)(.+)$', filename) if not m or not os.path.exists(os.path.join(output_dir, m.group(1))): return filename filename = m.group(1) def _reapply_captions(job_id, clip_index, video_path): """Re-burn the default captions onto a freshly derived file. Captions must always be the LAST layer. Editing or hooking a clip that already had them burned in produced `edited_subtitled_<...>`, and the next subtitle pass then stacked a second caption layer on top of the first — visibly doubled and unreadable in real user clips (26-jul-2026). So the derivation runs on the clean file and captions go back on afterwards. Returns the captioned path, or None if there was nothing to caption. """ try: meta_files = glob.glob(os.path.join(OUTPUT_DIR, job_id, "*_metadata.json")) if not meta_files: return None with open(meta_files[0], 'r') as f: data = json.load(f) transcript = data.get('transcript') clips = data.get('shorts', []) if not transcript or clip_index >= len(clips): return None clip = clips[clip_index] import main as _main # A recut clip is a concatenation of source segments, so the flat # start..end window is wrong for it — caption against the clip-relative # remapped transcript instead (same trick /api/subtitle uses). recipe_segments = (clip.get('recipe') or {}).get('segments') if recipe_segments: v_transcript = recut.virtual_transcript(transcript, recipe_segments) return _main.auto_caption_clip( video_path, v_transcript, 0.0, recut.total_duration(recipe_segments)) return _main.auto_caption_clip(video_path, transcript, clip['start'], clip['end']) except Exception as e: print(f"⚠️ Could not re-apply captions to {video_path}: {e}") return None def _recover_jobs_from_disk(): """Rebuild completed jobs from OUTPUT_DIR after a restart (issue #46 / #18). Jobs live in memory, so a restart used to orphan finished clips that are still on disk: the frontend restores the job_id from localStorage but every endpoint answers 404 "Job not found". Rebuild a minimal completed record for each job directory that has a metadata JSON. """ recovered = 0 try: entries = os.listdir(OUTPUT_DIR) except FileNotFoundError: return for job_id in entries: job_path = os.path.join(OUTPUT_DIR, job_id) if not os.path.isdir(job_path) or job_id in jobs: continue json_files = glob.glob(os.path.join(job_path, "*_metadata.json")) if not json_files: continue try: with open(json_files[0], 'r') as f: data = json.load(f) base_name = os.path.basename(json_files[0]).replace('_metadata.json', '') clips = data.get('shorts', []) for i, clip in enumerate(clips): if not clip.get('video_url'): clip['video_url'] = ( f"/videos/{job_id}/" f"{_canonical_clip_file(job_path, base_name, i)}") owner = None owner_path = os.path.join(job_path, ".owner") if os.path.exists(owner_path): with open(owner_path) as f: raw = f.read().strip() owner = int(raw) if raw.isdigit() else (raw or None) jobs[job_id] = { 'status': 'completed', 'logs': ["♻️ Job recovered from disk after server restart."], 'output_dir': job_path, 'user_id': owner, 'result': {'clips': clips, 'cost_analysis': data.get('cost_analysis')}, } recovered += 1 except Exception as e: print(f"⚠️ Could not recover job {job_id}: {e}") if recovered: print(f"♻️ Recovered {recovered} completed job(s) from disk.") # --- Mid-flight job resume (survive a redeploy without losing work) ---------- # A job lives only in memory, so killing the container mid-processing used to # lose it: the user's clip just stops. We persist a tiny manifest per job and, # on startup, re-enqueue any that were interrupted — the user sees it resume # instead of vanish. Bounded by MAX_RESUME_ATTEMPTS so a video that reliably # crashes the worker can't crashloop the service. _RESUME_FILE = ".resume.json" MAX_RESUME_ATTEMPTS = 2 def _write_resume_manifest(job_id, cmd, priority, user_id, reservation_id, watermark, webhook_url=None, webhook_secret=None, base_url=None): try: path = os.path.join(OUTPUT_DIR, job_id, _RESUME_FILE) with open(path, "w") as f: json.dump({ "cmd": cmd, "priority": priority, "user_id": None if user_id is None else str(user_id), "reservation_id": reservation_id, "watermark": bool(watermark), "attempts": 0, # The caller's webhook must survive a redeploy: a pipeline that # relies on the callback would otherwise hang forever on a job # that resumed fine. The secret is the caller's own HMAC value, # stored next to their video on the same disk — not a server # credential (those are rebuilt from os.environ on resume). "webhook_url": webhook_url, "webhook_secret": webhook_secret, "base_url": base_url, }, f) except Exception as e: print(f"⚠️ Could not write resume manifest for {job_id}: {e}") def _clear_resume_manifest(job_id): """Drop the manifest once a job reaches a terminal state, so it is never re-run on a later restart. Only an interrupted (still-running) job keeps it.""" try: os.remove(os.path.join(OUTPUT_DIR, job_id, _RESUME_FILE)) except FileNotFoundError: pass except Exception as e: print(f"⚠️ Could not clear resume manifest for {job_id}: {e}") def _resume_interrupted_jobs() -> set: """Re-enqueue jobs that were mid-processing when the server last stopped. Runs after _recover_jobs_from_disk: a job whose clips already finished has a metadata JSON and is recovered as 'completed', so we only resume manifests with no metadata yet (analysis never finished). Returns the set of reservation ids for the resumed jobs, so the caller can keep them out of the orphaned-reservation refund. Does NO DB work — the DB engine isn't up yet at this point in startup. A poison job (too many attempts) is simply not resumed; its reservation is then refunded as a normal orphan. """ keep_reservations: set = set() try: entries = os.listdir(OUTPUT_DIR) except FileNotFoundError: return keep_reservations resumed = 0 for job_id in entries: job_path = os.path.join(OUTPUT_DIR, job_id) manifest_path = os.path.join(job_path, _RESUME_FILE) if not os.path.isfile(manifest_path): continue # Already finished generating clips → recovered as completed elsewhere. if glob.glob(os.path.join(job_path, "*_metadata.json")): _clear_resume_manifest(job_id) continue try: with open(manifest_path) as f: m = json.load(f) except Exception as e: print(f"⚠️ Bad resume manifest for {job_id}: {e}") continue attempts = int(m.get("attempts", 0)) + 1 user_id = m.get("user_id") reservation_id = m.get("reservation_id") if attempts > MAX_RESUME_ATTEMPTS: # Poison job: don't resume. Leaving its reservation out of the keep # set lets the orphan sweep refund it, and the user can retry by hand. print(f"🛑 Job {job_id} exceeded {MAX_RESUME_ATTEMPTS} resume attempts — giving up.") _clear_resume_manifest(job_id) continue # Rebuild env from scratch — the manifest holds no secrets. Managed # (cloud) jobs get the server key; self-host falls back to its env key. env = os.environ.copy() if BILLING_ENABLED and user_id is not None: try: env["GEMINI_API_KEY"] = managed_keys.gemini_key() except Exception: pass if m.get("watermark"): env["WATERMARK"] = "1" else: env.pop("WATERMARK", None) m["attempts"] = attempts try: with open(manifest_path, "w") as f: json.dump(m, f) except Exception: pass jobs[job_id] = { 'status': 'queued', 'logs': [f"♻️ Resuming your video after a server update (attempt {attempts})."], 'cmd': m.get("cmd"), 'env': env, 'output_dir': job_path, 'user_id': None if user_id is None else user_id, 'reservation_id': reservation_id, 'watermark': bool(m.get("watermark")), 'webhook_url': m.get("webhook_url"), 'webhook_secret': m.get("webhook_secret"), 'base_url': m.get("base_url"), } if reservation_id: keep_reservations.add(str(reservation_id)) _enqueue_job(job_id, int(m.get("priority", 2))) resumed += 1 if resumed: print(f"♻️ Re-enqueued {resumed} interrupted job(s) after restart.") return keep_reservations def _dir_size(path: str) -> int: total = 0 for root, _dirs, files in os.walk(path): for f in files: try: total += os.path.getsize(os.path.join(root, f)) except OSError: pass return total def _enforce_uploads_size_cap(): """Delete the oldest source uploads while UPLOAD_DIR is over UPLOADS_MAX_GB. Sources are only needed while a job runs (and for the preview afterwards), but they're the biggest files on disk — up to MAX_FILE_SIZE_MB each. """ cap = UPLOADS_MAX_GB * 1024 ** 3 if cap <= 0: return used = _dir_size(UPLOAD_DIR) if used <= cap: return files = [] for name in os.listdir(UPLOAD_DIR): p = os.path.join(UPLOAD_DIR, name) if os.path.isfile(p): try: files.append((os.path.getmtime(p), p, os.path.getsize(p))) except OSError: pass files.sort() print(f"🧹 Uploads at {used / 1024**3:.1f} GB (cap {UPLOADS_MAX_GB} GB) — trimming.") for _mtime, path, size in files: if used <= cap: break try: os.remove(path) used -= size print(f"🧹 Size cap: removed upload {os.path.basename(path)}") except OSError: pass def _enforce_output_size_cap(): """Delete the oldest job dirs while OUTPUT_DIR is over OUTPUT_MAX_GB.""" cap = OUTPUT_MAX_GB * 1024 ** 3 if cap <= 0: return used = _dir_size(OUTPUT_DIR) if used <= cap: return thumbs = os.path.basename(THUMBNAILS_DIR) candidates = [] for job_id in os.listdir(OUTPUT_DIR): if job_id == thumbs: continue p = os.path.join(OUTPUT_DIR, job_id) if os.path.isdir(p): try: candidates.append((os.path.getmtime(p), p, job_id)) except OSError: pass candidates.sort() # oldest first print(f"🧹 Output dir at {used / 1024**3:.1f} GB (cap {OUTPUT_MAX_GB} GB) — trimming.") for _mtime, path, job_id in candidates: if used <= cap: break size = _dir_size(path) shutil.rmtree(path, ignore_errors=True) jobs.pop(job_id, None) used -= size print(f"🧹 Size cap: purged {job_id} ({size / 1024**2:.0f} MB)") async def cleanup_jobs(): """Background task to remove old jobs and files.""" import time print("🧹 Cleanup task started.") while True: try: await asyncio.sleep(300) # Check every 5 minutes now = time.time() # Simple directory cleanup based on modification time # Check OUTPUT_DIR for job_id in os.listdir(OUTPUT_DIR): # Not a job: the thumbnails dir backs a StaticFiles mount, so # deleting it would 500 every /thumbnails request until reboot. if job_id == os.path.basename(THUMBNAILS_DIR): continue job_path = os.path.join(OUTPUT_DIR, job_id) if os.path.isdir(job_path): if now - os.path.getmtime(job_path) > JOB_RETENTION_SECONDS: print(f"🧹 Purging old job: {job_id}") shutil.rmtree(job_path, ignore_errors=True) if job_id in jobs: del jobs[job_id] # Hard disk cap. The time-based sweep above bounds the *age* of what # we keep, not its size: a burst of long videos can fill the volume # inside one retention window. Drop the oldest jobs until we're back # under the cap — clips are already archived to R2 and get restored # on demand, so this only costs a re-download. _enforce_output_size_cap() _enforce_uploads_size_cap() # Cleanup SaaSShorts jobs from memory try: saas_expired = [ jid for jid, jdata in list(saas_jobs.items()) if jdata.get("status") in ("completed", "failed") and jdata.get("output_dir") and os.path.isdir(jdata["output_dir"]) and now - os.path.getmtime(jdata["output_dir"]) > JOB_RETENTION_SECONDS ] for jid in saas_expired: del saas_jobs[jid] except NameError: pass # Cleanup Uploads for filename in os.listdir(UPLOAD_DIR): file_path = os.path.join(UPLOAD_DIR, filename) try: if now - os.path.getmtime(file_path) > JOB_RETENTION_SECONDS: os.remove(file_path) except Exception: pass except Exception as e: print(f"⚠️ Cleanup error: {e}") async def process_queue(): """Background worker to process jobs from the queue with concurrency limit.""" print(f"🚀 Job Queue Worker started with {MAX_CONCURRENT_JOBS} concurrent slots.") while True: try: # Wait for a job (priority, seq, job_id) — lowest priority first. _priority, _seq, job_id = await job_queue.get() # Acquire semaphore slot (waits if max jobs are running) await concurrency_semaphore.acquire() print(f"🔄 Acquired slot for job: {job_id}") # Process in background task to not block the loop (allowing other slots to fill) asyncio.create_task(run_job_wrapper(job_id)) except Exception as e: print(f"❌ Queue dispatch error: {e}") await asyncio.sleep(1) # Monthly proxy bandwidth counter (in-memory; an alert threshold, not a bill — # losing it on a deploy just means the alert re-arms from 0 mid-month). _proxy_month = {"month": None, "bytes": 0, "alerted": False} PROXY_ALERT_GB = 100 async def _track_proxy_usage(job_id): nbytes = (jobs.get(job_id) or {}).get('proxy_bytes') or 0 if not nbytes: return month = datetime.now(timezone.utc).strftime("%Y-%m") if _proxy_month["month"] != month: _proxy_month.update(month=month, bytes=0, alerted=False) _proxy_month["bytes"] += nbytes gb = _proxy_month["bytes"] / 1e9 if gb >= PROXY_ALERT_GB and not _proxy_month["alerted"] and _alerts: _proxy_month["alerted"] = True try: await _alerts.send_admin_alert( "Proxy bandwidth threshold", f"Managed downloads have used {gb:.1f} GB of proxy bandwidth in {month} " f"(threshold {PROXY_ALERT_GB} GB). Review free-plan usage.", ) except Exception as e: print(f"⚠️ Proxy alert failed: {e}") async def run_job_wrapper(job_id): """Wrapper to run job and release semaphore""" try: job = jobs.get(job_id) if job: await run_job(job_id, job) except Exception as e: print(f"❌ Job wrapper error {job_id}: {e}") finally: # The subprocess returned (success or genuine failure) — a terminal # state, so drop the resume manifest. It only survives if the container # was killed mid-run, which is exactly when we want to resume. _clear_resume_manifest(job_id) # Settle the minute reservation (managed jobs only): commit on success, # release otherwise so the minutes go back to the user. await _settle_reservation(job_id) # Archive the completed clips to the user's durable R2 library (history). await _archive_managed_job(job_id) # Fire the caller's webhook (after archive, so durable links exist). await _notify_job_webhook(job_id) # Operational alerting for managed jobs (proxy out of credits / failures). await _record_job_alert(job_id) # Accumulate proxy bandwidth for the monthly cost alert. await _track_proxy_usage(job_id) # Tell the owner their clips are ready (managed jobs, once per job). await _notify_clips_ready(job_id) # Telegram pulse for high-signal activity (first clip / paid user). await _notify_clip_activity(job_id) # Always release semaphore and mark queue task done concurrency_semaphore.release() job_queue.task_done() print(f"✅ Released slot for job: {job_id}") async def _archive_managed_job(job_id): if not BILLING_ENABLED: return job = jobs.get(job_id) or {} if not job.get('user_id') or job.get('status') != 'completed': return clips = (job.get('result') or {}).get('clips') or [] if not clips: return try: await cloud.videos.archive_job(job['user_id'], job_id, clips, job['output_dir']) except Exception as e: print(f"⚠️ R2 archive error for {job_id}: {e}") def _archive_clip_edit_bg(job_id: str, clip_index: int, filename: str): """Fire-and-forget R2 re-archive of an edited clip (managed jobs only). Keeps the user's durable library (history/projects) pointing at the current version of each clip without blocking the edit response.""" if not BILLING_ENABLED: return user_id = (jobs.get(job_id) or {}).get('user_id') if not user_id: return output_dir = os.path.join(OUTPUT_DIR, job_id) async def _run(): try: await cloud.videos.archive_clip_edit(user_id, job_id, clip_index, output_dir, filename) except Exception as e: print(f"⚠️ R2 edit archive error for {job_id}: {e}") asyncio.create_task(_run()) async def _notify_clips_ready(job_id): """Email the owner when their clips finish — processing takes minutes, so this lets them close the tab. Once per job (email_sent flag).""" if not BILLING_ENABLED: return job = jobs.get(job_id) or {} if not job.get('user_id') or job.get('status') != 'completed' or job.get('email_sent'): return clips = (job.get('result') or {}).get('clips') or [] if not clips: return job['email_sent'] = True try: from cloud.database import session as cloud_session from cloud.models import User from cloud.emails import send_clips_ready_email async with cloud_session() as s: user = await s.get(User, job['user_id']) if not user or not user.email: return title = clips[0].get('video_title_for_youtube_short') or clips[0].get('title') or "Your video" # #app opens the app itself. The bare frontend URL showed the marketing # landing to anyone whose browser had not already set the skip flag, # which is the wrong page for someone clicking "View my clips". await send_clips_ready_email(user.email, title, len(clips), f"{_cloud_config.settings.frontend_url}/#app") except Exception as e: print(f"⚠️ Clips-ready email error for {job_id}: {e}") async def _notify_clip_activity(job_id): """Telegram pulse when a PAID user's clips are created. Free-tier activity (including first clips) is deliberately silent: at current signup volume it drowned the ops channel without being actionable. Telegram-only (best effort, no email).""" if not BILLING_ENABLED: return job = jobs.get(job_id) or {} if not job.get('user_id') or job.get('status') != 'completed': return clips = (job.get('result') or {}).get('clips') or [] if not clips: return try: from cloud.database import session as cloud_session from cloud.models import User from cloud import metering async with cloud_session() as s: user = await s.get(User, job['user_id']) if not user: return sub = await metering._active_subscription(s, user.id) if sub is None: return title = clips[0].get('video_title_for_youtube_short') or clips[0].get('title') or "video" n = len(clips) await _alerts.send_telegram( f"🎬 Clips created\n{user.email} ({sub.plan}) — “{title}” ({n} clip{'s' if n != 1 else ''})") except Exception as e: print(f"⚠️ Clip-activity notify error for {job_id}: {e}") # Markers that identify a line as an actual error rather than progress noise. _ERROR_MARKERS = ("❌", "ERROR:", "Traceback", "FATAL", "Exception", "Process failed with exit code", "No metadata file generated", "Execution error:") def _job_error_text(logs) -> str: """The lines that explain WHY a job failed, for the alert's classifier. The tail of the log is usually progress noise (scene detection, ffmpeg banners), which made alerts blame whatever word happened to be nearby — a silent upload got reported as a broken download path, and a Gemini blip as an ffmpeg problem. Pick the error-bearing lines instead, newest last. """ hits = [ln for ln in logs if any(m in ln for m in _ERROR_MARKERS)] if not hits: return " ".join(logs[-10:]) # nothing recognisable — fall back to the tail return " ".join(hits[-6:]) async def _record_job_alert(job_id): if not BILLING_ENABLED: return job = jobs.get(job_id) or {} if not job.get('user_id'): return # only track managed jobs ok = job.get('status') == 'completed' err = "" if ok else _job_error_text(job.get('logs', [])) try: await _alerts.record_job_outcome(ok, err) except Exception as e: print(f"⚠️ Alert recording error for {job_id}: {e}") await _track_job_outcome(job, ok, err) async def _track_job_outcome(job, ok, err): """Report the job to OpenPanel, with the user's job index. The index is what makes the retention question answerable: on 26-jul-2026, 491 of 564 users who ever processed a video did it exactly once. Counting distinct users at index 1 versus index >= 2 measures whether the clip quality work moved that, which nothing in the stack could do before. Client-side analytics cannot cover this: a render finishes minutes later, often after the tab is closed, and ad-blockers eat a share of the rest. """ try: from cloud import analytics as _an from sqlalchemy import text as _sa_text from cloud import database as _db user_id = job.get('user_id') job_index = None try: async with _db.session() as s: job_index = (await s.execute(_sa_text( "select count(*) from usage_ledger " "where user_id = :uid and job_type = 'process'"), {"uid": user_id})).scalar() except Exception: pass # an index we cannot read is not worth failing a job over clips = len(((job.get('result') or {}).get('clips')) or []) _an.track( "ClipsDelivered" if ok else "JobFailed", user_id=user_id, job_index=job_index, clips=clips if ok else None, plan=job.get('user_plan'), source="url" if job.get('url') else "upload", reason=(_alerts._classify_failure(err) if not ok and err else None), ) except Exception as e: print(f"⚠️ Analytics error: {e}") # --- Job completion webhooks -------------------------------------------------- # Agents and pipelines (n8n, cron, MCP clients) need push, not poll: a caller # passes webhook_url on /api/process and gets one POST when the job reaches a # terminal state. The URL goes through assert_public_url both at submit and at # delivery time — the second check is what defeats DNS rebinding between them. WEBHOOK_TIMEOUT = 10.0 WEBHOOK_RETRY_DELAYS = (0, 10, 60) # seconds before each attempt def _sign_webhook(body: bytes, secret: str) -> str: import hmac as _hmac import hashlib as _hashlib return "sha256=" + _hmac.new(secret.encode(), body, _hashlib.sha256).hexdigest() async def _webhook_clip_entries(job_id, job): """The payload's clip list: absolute URLs, plus durable R2 links when the job was archived (a webhook consumer usually fetches later, after the 1-hour local retention would have expired the /videos path).""" base = (job.get('base_url') or os.environ.get("PUBLIC_API_URL", "")).rstrip("/") clips = (job.get('result') or {}).get('clips') or [] entries = [] for i, clip in enumerate(clips): rel = clip.get('video_url') or "" entries.append({ "index": i, "title": clip.get('title') or clip.get('video_title_for_youtube_short'), "video_url": f"{base}{rel}" if rel.startswith("/") and base else rel, }) if BILLING_ENABLED and job.get('user_id'): try: from sqlalchemy import select as _select from cloud.database import session as cloud_session from cloud.models import UserVideo from cloud import storage as _storage async with cloud_session() as s: vids = list((await s.execute( _select(UserVideo).where(UserVideo.job_id == job_id) )).scalars()) for v in vids: if v.clip_index is not None and v.clip_index < len(entries): entries[v.clip_index]["download_url"] = _storage.presigned_get( v.r2_key, expires=24 * 3600) except Exception as e: print(f"⚠️ Webhook R2 links failed for {job_id}: {e}") return entries async def _deliver_webhook(url, body: bytes, secret): headers = {"Content-Type": "application/json", "User-Agent": "OpenShorts-Webhook/1.0"} if secret: headers["X-OpenShorts-Signature"] = _sign_webhook(body, secret) from security_utils import assert_public_url, UnsafeURLError loop = asyncio.get_event_loop() for attempt, delay in enumerate(WEBHOOK_RETRY_DELAYS, 1): if delay: await asyncio.sleep(delay) try: # Re-resolve on every attempt: the submit-time check is stale by now. await loop.run_in_executor(None, assert_public_url, url) async with httpx.AsyncClient(timeout=WEBHOOK_TIMEOUT, follow_redirects=False) as client: resp = await client.post(url, content=body, headers=headers) if resp.status_code < 300: print(f"🪝 Webhook delivered to {url} (attempt {attempt})") return print(f"⚠️ Webhook attempt {attempt} to {url}: HTTP {resp.status_code}") except UnsafeURLError as e: print(f"🛑 Webhook URL no longer safe, dropping: {e}") return except Exception as e: print(f"⚠️ Webhook attempt {attempt} to {url} failed: {e}") print(f"❌ Webhook to {url} gave up after {len(WEBHOOK_RETRY_DELAYS)} attempts.") async def _notify_job_webhook(job_id): """Fire the caller's webhook for a terminal job. Runs inside run_job_wrapper's finally AFTER the R2 archive, so durable links exist; the actual delivery (with its retry sleeps) is detached so the worker slot frees immediately.""" job = jobs.get(job_id) or {} url = job.get('webhook_url') if not url or job.get('webhook_sent'): return job['webhook_sent'] = True completed = job.get('status') == 'completed' payload = { "event": "job.completed" if completed else "job.failed", "job_id": job_id, "status": job.get('status'), "clips": (await _webhook_clip_entries(job_id, job)) if completed else [], } if not completed: payload["error"] = _job_error_text(job.get('logs', []))[-500:] body = json.dumps(payload).encode() asyncio.create_task(_deliver_webhook(url, body, job.get('webhook_secret'))) async def _settle_reservation(job_id): if not BILLING_ENABLED: return job = jobs.get(job_id) or {} reservation_id = job.get('reservation_id') if not reservation_id: return try: if job.get('status') == 'completed': await cloud.metering.commit_reservation(reservation_id) else: await cloud.metering.release_reservation(reservation_id) except Exception as e: print(f"⚠️ Reservation settle error for {job_id}: {e}") @asynccontextmanager async def lifespan(app: FastAPI): # Rehydrate finished jobs from disk before serving (survives restarts). _recover_jobs_from_disk() # Re-enqueue jobs that were mid-processing when we stopped (redeploy). Their # reservations must survive the orphan sweep so the resumed run can settle them. _resumed_reservation_ids = _resume_interrupted_jobs() # Start worker and cleanup worker_task = asyncio.create_task(process_queue()) cleanup_task = asyncio.create_task(cleanup_jobs()) if BILLING_ENABLED: await cloud.setup_async(app, keep_reservation_ids=_resumed_reservation_ids) # Nag on Telegram while the residential proxy is down/out of credits — # a single job-failure alert is easy to miss and ingest stays broken # until someone tops the balance up. asyncio.create_task(_alerts.proxy_watch_loop()) yield # Cleanup (optional: cancel worker) app = FastAPI(lifespan=lifespan) # Cloud mode: attach middleware + routers at import time (before the app serves). if BILLING_ENABLED: cloud.setup_sync(app) # MCP server (/mcp): the pipeline as agent-callable tools. Works in both modes — # cloud requires an osk_ API key, self-host keeps BYOK (see mcp_server.py). import mcp_server as _mcp_server app.include_router(_mcp_server.router) # Enable CORS for frontend. Cloud mode locks this down to the configured origins; # self-host keeps the permissive wildcard it has always used. app.add_middleware( CORSMiddleware, allow_origins=cloud.settings.allowed_origins if BILLING_ENABLED else ["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Mount static files for serving videos app.mount("/videos", StaticFiles(directory=OUTPUT_DIR), name="videos") # Mount static files for serving thumbnails THUMBNAILS_DIR = os.path.join(OUTPUT_DIR, "thumbnails") os.makedirs(THUMBNAILS_DIR, exist_ok=True) app.mount("/thumbnails", StaticFiles(directory=THUMBNAILS_DIR), name="thumbnails") def _safe_under(base_dir: str, user_rel_path: str) -> Optional[str]: """Resolve ``user_rel_path`` under ``base_dir`` and reject path traversal. Returns the absolute path only if it stays inside ``base_dir`` (after following ``..``); otherwise None. Used to sanitize client-supplied file references so ``../../.env`` can't escape the output directories. """ base = os.path.realpath(base_dir) target = os.path.realpath(os.path.join(base, user_rel_path)) if target == base or target.startswith(base + os.sep): return target return None class ProcessRequest(BaseModel): url: str # Masks user:password credentials embedded in any URL (e.g. the residential # proxy URL that yt-dlp echoes in its verbose debug output) before the line is # ever printed to the server console or stored in the job log. _CREDENTIAL_URL_RE = re.compile(r'(\w+://)[^:/@\s]+:[^@/\s]+@') def _scrub_secrets(line: str) -> str: return _CREDENTIAL_URL_RE.sub(r'\1***:***@', line) # Cloud users don't need (and shouldn't see) implementation details: the ingest # plumbing (proxy / downloader / cookies) OR which AI model powers it, token # usage and cost. These are dropped from the client view even when the line is # emoji-prefixed. Never applied under DEBUG_LOGS (local dev sees everything). _SENSITIVE_LOG_RE = re.compile( # Ingest plumbing r'proxy|yt[-_ ]?dlp|youtube-?dl|cookie|residential|po[_ ]?token' r'|player_client|extractor|\bdownload|descarg' # AI model / provider / cost / pipeline internals r'|gemini|openai|anthropic|\bflash\b|\bmodel\b|token|thinking' r'|\bcost\b|\$\s*[0-9]|scoring window|shortlist', re.IGNORECASE, ) def _visible_logs(logs): """Logs to surface to the client. Self-host (BILLING off) shows the full pipeline output so people running their own instance can debug. Cloud shows a curated whitelist view (log_view.friendly_logs): plain progress for normal users — transcription percentage, clip counters — with no file paths, model names or pipeline internals. DEBUG_LOGS=true forces the full output even under billing — for local dev where you run in paid mode but still want the raw logs. """ if not BILLING_ENABLED or DEBUG_LOGS: return logs from log_view import friendly_logs return friendly_logs(logs) def enqueue_output(out, job_id): """Reads output from a subprocess and appends it to jobs logs.""" try: for line in iter(out.readline, b''): decoded_line = _scrub_secrets(line.decode('utf-8').strip()) if decoded_line: # Internal marker from main.py's downloader, not a log line. if decoded_line.startswith("PROXY_BYTES="): try: if job_id in jobs: jobs[job_id]['proxy_bytes'] = int(decoded_line.split("=", 1)[1]) except ValueError: pass continue print(f"📝 [Job Output] {decoded_line}") if job_id in jobs: jobs[job_id]['logs'].append(decoded_line) except Exception as e: print(f"Error reading output for job {job_id}: {e}") finally: out.close() async def run_job(job_id, job_data): """Executes the subprocess for a specific job.""" cmd = job_data['cmd'] env = job_data['env'] output_dir = job_data['output_dir'] jobs[job_id]['status'] = 'processing' jobs[job_id]['logs'].append("Job started by worker.") print(f"🎬 [run_job] Executing command for {job_id}: {' '.join(cmd)}") try: process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, # Merge stderr to stdout env=env, cwd=os.getcwd() ) # We need to capture logs in a thread because Popen isn't async t_log = threading.Thread(target=enqueue_output, args=(process.stdout, job_id)) t_log.daemon = True t_log.start() # Async wait for process with incremental updates start_wait = time.time() while process.poll() is None: await asyncio.sleep(2) # Check for partial results every 2 seconds # Look for metadata file try: json_files = glob.glob(os.path.join(output_dir, "*_metadata.json")) if json_files: target_json = json_files[0] # Read metadata (it might be being written to, so simple try/except or just read) # Use a lock or just robust read? json.load might fail if file is partial. # Usually main.py writes it once at start (based on my review). if os.path.getsize(target_json) > 0: with open(target_json, 'r') as f: data = json.load(f) base_name = os.path.basename(target_json).replace('_metadata.json', '') clips = data.get('shorts', []) cost_analysis = data.get('cost_analysis') # Check which clips actually exist on disk ready_clips = [] for i, clip in enumerate(clips): clip_filename = f"{base_name}_clip_{i+1}.mp4" clip_path = os.path.join(output_dir, clip_filename) if os.path.exists(clip_path) and os.path.getsize(clip_path) > 0: # Checking if file is growing? For now assume if it exists and main.py moves it there, it's done. # main.py writes to temp_... then moves to final name. So presence means ready! clip['video_url'] = f"/videos/{job_id}/{clip_filename}" ready_clips.append(clip) if ready_clips: jobs[job_id]['result'] = {'clips': ready_clips, 'cost_analysis': cost_analysis} except Exception as e: # Ignore read errors during processing pass returncode = process.returncode if returncode == 0: jobs[job_id]['status'] = 'completed' jobs[job_id]['logs'].append("Process finished successfully.") # Self-host: silent AWS S3 backup. Cloud mode stores to R2 instead # (see _archive_managed_job), so skip the redundant/paid AWS upload. if not BILLING_ENABLED: loop = asyncio.get_event_loop() loop.run_in_executor(None, upload_job_artifacts, output_dir, job_id) # Find result JSON json_files = glob.glob(os.path.join(output_dir, "*_metadata.json")) if not json_files: # Backward-compat rescue if outputs were written to OUTPUT_DIR root if _relocate_root_job_artifacts(job_id, output_dir): json_files = glob.glob(os.path.join(output_dir, "*_metadata.json")) if json_files: target_json = json_files[0] with open(target_json, 'r') as f: data = json.load(f) # Enhance result with video URLs base_name = os.path.basename(target_json).replace('_metadata.json', '') clips = data.get('shorts', []) cost_analysis = data.get('cost_analysis') for i, clip in enumerate(clips): clip_filename = _canonical_clip_file(output_dir, base_name, i) clip['video_url'] = f"/videos/{job_id}/{clip_filename}" jobs[job_id]['result'] = {'clips': clips, 'cost_analysis': cost_analysis} else: jobs[job_id]['status'] = 'failed' jobs[job_id]['logs'].append("No metadata file generated.") else: jobs[job_id]['status'] = 'failed' jobs[job_id]['logs'].append(_scrub_secrets(f"Process failed with exit code {returncode}")) except Exception as e: jobs[job_id]['status'] = 'failed' # Exception text can embed URLs with credentials (e.g. the proxy URL # inside a yt-dlp/httpx error) — scrub before it reaches client logs. jobs[job_id]['logs'].append(_scrub_secrets(f"Execution error: {str(e)}")) @app.get("/health") async def health(): """Lightweight liveness probe for uptime monitoring / Coolify health checks.""" return {"status": "ok"} @app.get("/api/config") async def get_config(): return { "youtubeUrlEnabled": not DISABLE_YOUTUBE_URL, "billingEnabled": BILLING_ENABLED, "googleAuthEnabled": bool(BILLING_ENABLED and cloud.settings.google_auth_enabled), } async def _probe_youtube_quality(url: str) -> dict: """Run quality_probe.py in a worker thread; {} on any failure (fail-open).""" def _run(): try: proc = subprocess.run( [sys.executable, QUALITY_PROBE_SCRIPT, "--url", url], capture_output=True, timeout=75, ) return json.loads(proc.stdout.decode(errors="replace").strip() or "{}") except Exception as e: print(f"⚠️ Quality probe failed ({e}); starting job without gate.") return {} loop = asyncio.get_event_loop() return await loop.run_in_executor(None, _run) # Layouts the caller can let the renderer choose from, mapped to the env var # each one is gated on. The renderer only ever picks between layouts that are # switched on here. # # This is opt-in per job, not a detector running on every video, because the # detection is not good enough to be trusted unprompted: measured over the # 48-clip corpus, routing every video through the on-screen-content check fixed # 13 clips and spoiled 13 others (talking heads and corner tickers demoted to a # layout they do not need). Asking the person who knows what they uploaded costs # them one click and removes that whole class of error. It is also what OpusClip # does — its "applicable auto layout" panel lets the user pick which layouts the # AI may apply. LAYOUT_ENV = { "split": "SPLIT_LAYOUT", # two speakers stacked "screencast": "SCREENCAST_LAYOUT", # slides/screen share over the speaker "speaker_cut": "SPEAKER_CUT", # hard cuts to whoever is talking "punch_in": "PUNCH_IN", # small push on the clip's beats } # Stacking and cutting both need to know who is speaking. LAYOUT_IMPLIES = { "split": ["SPEAKER_SIGNAL"], "speaker_cut": ["SPEAKER_SIGNAL"], } def layout_env(requested): """Env overrides for the layouts this job allows. Unknown names are ignored rather than rejected: a newer dashboard must not break an older API. The special value "auto" hands the choice to Gemini (one call per video). It composes with explicit picks: layout_picker only ever adds, so asking for "auto,punch_in" means "decide the layout yourself, and punch in regardless". """ env = {} for name in requested or []: key = str(name).strip().lower() if key == "auto": env["AUTO_LAYOUT"] = "1" continue var = LAYOUT_ENV.get(key) if not var: continue env[var] = "1" for extra in LAYOUT_IMPLIES.get(key, []): env[extra] = "1" return env @app.post("/api/process") async def process_endpoint( request: Request, file: Optional[UploadFile] = File(None), url: Optional[str] = Form(None), acknowledged: Optional[str] = Form(None), output_format: Optional[str] = Form(None), layouts: Optional[str] = Form(None), force_low_quality: Optional[str] = Form(None), webhook_url: Optional[str] = Form(None), webhook_secret: Optional[str] = Form(None), target_clips: Optional[str] = Form(None), clip_min_seconds: Optional[str] = Form(None), clip_max_seconds: Optional[str] = Form(None), auto_hook: Optional[str] = Form(None), auto_hook_style: Optional[str] = Form(None) ): api_key = await resolve_gemini(request) if not api_key: raise gemini_missing_error() ack_flag = str(acknowledged).lower() in ("1", "true", "yes") force_low = str(force_low_quality).lower() in ("1", "true", "yes") # Handle JSON body manually for URL payload content_type = request.headers.get("content-type", "") if "application/json" in content_type: body = await request.json() url = body.get("url") ack_flag = bool(body.get("acknowledged")) force_low = bool(body.get("force_low_quality")) output_format = body.get("output_format") layouts = body.get("layouts") webhook_url = body.get("webhook_url") webhook_secret = body.get("webhook_secret") target_clips = body.get("target_clips") clip_min_seconds = body.get("clip_min_seconds") clip_max_seconds = body.get("clip_max_seconds") auto_hook = body.get("auto_hook") auto_hook_style = body.get("auto_hook_style") # Normalize output format (auto = keep pipeline default). if output_format not in ("vertical", "horizontal", "square"): output_format = "auto" # Accepts a JSON list or a comma-separated form field. if isinstance(layouts, str): layouts = [p for p in layouts.split(",") if p.strip()] elif not isinstance(layouts, list): layouts = [] if not url and not file: raise HTTPException(status_code=400, detail="Must provide URL or File") # Completion callback: reject unsafe targets NOW (clear 400) — delivery # re-validates anyway, but failing at submit is the debuggable behavior. if webhook_url: from security_utils import assert_public_url, UnsafeURLError try: await asyncio.get_event_loop().run_in_executor( None, assert_public_url, webhook_url) except UnsafeURLError as e: raise HTTPException(status_code=400, detail=f"Invalid webhook_url: {e}") if not ack_flag: raise HTTPException(status_code=400, detail="You must confirm you own the content or have rights to process it.") if url and DISABLE_YOUTUBE_URL: raise HTTPException(status_code=403, detail="YouTube URL ingest is disabled on this deployment. Please upload a file you own.") # Pre-flight quality gate: probe the offered resolution BEFORE starting, so # the user can abort (refresh cookies / update yt-dlp) instead of burning # 20 min on a 360p-only source. Fail-open: any probe error starts normally. if url and not force_low and QUALITY_GATE_MIN_HEIGHT > 0: probe = await _probe_youtube_quality(url) max_height = int(probe.get("max_height") or 0) if 0 < max_height < QUALITY_GATE_MIN_HEIGHT: print(f"⚠️ Quality gate: only {max_height}p available for {url} — asking user first.") return JSONResponse({ "needs_confirmation": True, "quality_check": { "max_height": max_height, "min_height": QUALITY_GATE_MIN_HEIGHT, "cookies_invalid": bool(probe.get("cookies_invalid")), }, }) # Capture attestation context for legal record (IP + timestamp + UA) client_ip = request.client.host if request.client else "unknown" fwd = request.headers.get("x-forwarded-for") if fwd: client_ip = fwd.split(",")[0].strip() user_agent = request.headers.get("user-agent", "") attestation = { "acknowledged": True, "ip": client_ip, "user_agent": user_agent, "timestamp": time.time(), "source": "url" if url else "file", } job_id = str(uuid.uuid4()) job_output_dir = os.path.join(OUTPUT_DIR, job_id) os.makedirs(job_output_dir, exist_ok=True) # Prepare Command cmd = ["python", "-u", "main.py"] # -u for unbuffered env = os.environ.copy() env["GEMINI_API_KEY"] = api_key # Override with key from request # Optional layouts are per job. The renderer reads these at import time in # the subprocess, so they must be set before Popen — same path WATERMARK # already takes. chosen = layout_env(layouts) env.update(chosen) if chosen: print(f"[layouts] job={job_id} enabled={sorted(chosen)}") # Auto-hook: burn each clip's Gemini hook text during the render. Off when # the field is absent, so API/MCP/webhook callers keep their old output # byte-for-byte; the dashboard sends an explicit value either way. if str(auto_hook).lower() in ("1", "true", "yes"): env["AUTO_HOOK"] = "1" from hooks import HOOK_STYLES if auto_hook_style in HOOK_STYLES: env["AUTO_HOOK_STYLE"] = auto_hook_style print(f"[auto-hook] job={job_id} style={env.get('AUTO_HOOK_STYLE', 'classic')}") # Manual generation controls (discussion #65): optional clip-count target # and duration band, forwarded to the selection prompts via the same env # overrides the A/B harness already reads (clip_selection.py). All three # are honest TARGETS, not guarantees — the model may return fewer clips # when the material doesn't hold them. Bad values 400 instead of silently # producing something the user didn't ask for. def _gen_control(raw, name, lo, hi, integer=False): if raw in (None, ""): return None try: val = float(raw) except (TypeError, ValueError): raise HTTPException(status_code=400, detail=f"{name} must be a number") if integer and val != int(val): raise HTTPException(status_code=400, detail=f"{name} must be an integer") if not (lo <= val <= hi): raise HTTPException(status_code=400, detail=f"{name} must be between {lo:g} and {hi:g}") return int(val) if integer else val n_clips = _gen_control(target_clips, "target_clips", 1, 15, integer=True) min_secs = _gen_control(clip_min_seconds, "clip_min_seconds", 5, 175) max_secs = _gen_control(clip_max_seconds, "clip_max_seconds", 10, 180) if min_secs is not None and max_secs is not None and max_secs < min_secs + 5: raise HTTPException(status_code=400, detail="clip_max_seconds must be at least 5s above clip_min_seconds") if n_clips is not None: env["CLIP_TARGET_MIN"] = env["CLIP_TARGET_MAX"] = str(n_clips) if min_secs is not None: env["CLIP_MIN_SECONDS"] = str(min_secs) if max_secs is not None: env["CLIP_MAX_SECONDS"] = str(max_secs) if n_clips is not None or min_secs is not None or max_secs is not None: print(f"[gen-controls] job={job_id} clips={n_clips} band={min_secs}-{max_secs}") input_path = None if url: # Keep the downloaded source inside the job dir: the clip editor's # re-render path cuts new segments from it, and it ages out with the # rest of the job (retention window + OUTPUT_MAX_GB cap) either way. cmd.extend(["-u", url, "--keep-original"]) else: # Save uploaded file with size limit check. # basename() strips any path components from the client-supplied # filename so a name like "../../main.py" can't escape UPLOAD_DIR. safe_name = os.path.basename(file.filename or "upload") or "upload" input_path = os.path.join(UPLOAD_DIR, f"{job_id}_{safe_name}") # Read file in chunks to check size size = 0 limit_bytes = MAX_FILE_SIZE_MB * 1024 * 1024 with open(input_path, "wb") as buffer: while content := await file.read(1024 * 1024): # Read 1MB chunks size += len(content) if size > limit_bytes: os.remove(input_path) shutil.rmtree(job_output_dir) raise HTTPException(status_code=413, detail=f"File too large. Max size {MAX_FILE_SIZE_MB}MB") buffer.write(content) cmd.extend(["-i", input_path]) cmd.extend(["-o", job_output_dir]) if output_format and output_format != "auto": cmd.extend(["--format", output_format]) print(f"[attestation] job={job_id} ip={attestation['ip']} source={attestation['source']} ack=true") # Meter + reserve minutes for managed users (no-op for BYOK / self-host). user_id, priority, reservation_id, user_plan = await reserve_process_minutes(request, url, input_path, job_id) if user_plan == "free": # Free-plan clips carry a burned-in watermark (applied by the main.py # subprocess after each clip renders). env["WATERMARK"] = "1" # Absolute-URL base for the webhook payload: explicit env wins (the API may # sit behind a proxy whose forwarded headers we can't trust), else what the # caller connected to. api_base = os.environ.get("PUBLIC_API_URL", "").rstrip("/") or str(request.base_url).rstrip("/") # Enqueue Job jobs[job_id] = { 'status': 'queued', 'logs': [f"Job {job_id} queued."], 'cmd': cmd, 'env': env, 'output_dir': job_output_dir, 'attestation': attestation, 'user_id': user_id, 'reservation_id': reservation_id, 'watermark': env.get("WATERMARK") == "1", 'webhook_url': webhook_url, 'webhook_secret': webhook_secret, 'base_url': api_base, } # Persist the owner so recovered jobs keep their multi-tenant guard after a # restart (see _recover_jobs_from_disk). if user_id is not None: try: os.makedirs(job_output_dir, exist_ok=True) with open(os.path.join(job_output_dir, ".owner"), "w") as f: f.write(str(user_id)) except Exception as e: print(f"⚠️ Could not persist job owner for {job_id}: {e}") # Resume manifest: enough to re-run this job if the container dies mid-flight # (a redeploy). No secrets — the env is rebuilt from os.environ on resume. _write_resume_manifest(job_id, cmd, priority, user_id, reservation_id, watermark=jobs[job_id]['watermark'], webhook_url=webhook_url, webhook_secret=webhook_secret, base_url=api_base) _enqueue_job(job_id, priority) return {"job_id": job_id, "status": "queued"} @app.get("/api/status/{job_id}") async def get_status(job_id: str, request: Request): if job_id not in jobs: raise HTTPException(status_code=404, detail="Job not found") job = jobs[job_id] await _assert_job_owner(request, job) return { "status": job['status'], "logs": _visible_logs(job['logs']), "result": job.get('result') } def _locate_source(job_id: str): """Find a job's source video on disk, or None. Upload jobs keep it in uploads/{job_id}_*; URL jobs keep the download in the job dir (--keep-original) under the name recorded as ``source_video`` in metadata.json. Either way it ages out with the normal retention caps. """ matches = [ f for f in glob.glob(os.path.join(UPLOAD_DIR, f"{glob.escape(job_id)}_*")) if not os.path.basename(f).startswith("thumb_") ] if matches: return matches[0] try: meta_files = glob.glob(os.path.join(OUTPUT_DIR, job_id, "*_metadata.json")) if meta_files: with open(meta_files[0], 'r') as f: name = json.load(f).get('source_video') if name: candidate = os.path.join(OUTPUT_DIR, job_id, os.path.basename(name)) if os.path.exists(candidate): return candidate except Exception: pass return None @app.get("/api/source/{job_id}") async def get_source_video(job_id: str): """Stream a job's original source video for the live-analysis preview and the clip editor's source monitor. Uploaded sources are blob URLs in the browser and don't survive a reload, so the recovered session points the preview here instead. Unauthenticated like the /videos mount — the UUID job_id is the capability. """ source_path = _locate_source(job_id) if not source_path: raise HTTPException(status_code=404, detail="Source not found") return FileResponse(source_path, media_type="video/mp4") @app.get("/api/jobs/{job_id}/download-all") async def download_all_clips(job_id: str, request: Request): """Bundle the current version of every clip of a job into one ZIP.""" await _ensure_job_files(job_id, request) if job_id in jobs: await _assert_job_owner(request, jobs[job_id]) output_dir = os.path.join(OUTPUT_DIR, job_id) json_files = glob.glob(os.path.join(output_dir, "*_metadata.json")) if not json_files: raise HTTPException(status_code=404, detail="Job not found") with open(json_files[0], 'r', encoding='utf-8') as f: data = json.load(f) # The metadata file on disk never carries video_url — the pipeline doesn't # write it, it's injected into the in-memory job record. So prefer the live # record (it also tracks edits like subtitled_/hook_ renames) and fall back # to the canonical name a job/restore rebuilds, instead of finding nothing. base_name = os.path.basename(json_files[0]).replace('_metadata.json', '') mem_clips = ((jobs.get(job_id) or {}).get('result') or {}).get('clips') or [] files = [] for i, clip in enumerate(data.get('shorts', [])): url = None if i < len(mem_clips): url = (mem_clips[i] or {}).get('video_url') url = url or clip.get('video_url') filename = (os.path.basename(url.split('/')[-1]) if url else _canonical_clip_file(output_dir, base_name, i)) path = os.path.join(output_dir, filename) if filename and os.path.exists(path): files.append((i, path)) if not files: raise HTTPException(status_code=404, detail="No clip files found for this job") zip_path = os.path.join(output_dir, f"clips_{int(time.time())}.zip") def build_zip(): # Videos are already compressed; store instead of deflate for speed. with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_STORED) as zf: for i, path in files: zf.write(path, arcname=f"clip_{i + 1:02d}_{os.path.basename(path)}") loop = asyncio.get_event_loop() await loop.run_in_executor(None, build_zip) return FileResponse( zip_path, media_type="application/zip", filename=f"openshorts_clips_{job_id[:8]}.zip", background=BackgroundTask(os.remove, zip_path), ) # --- Project restore (paid mode) -------------------------------------------- # Re-hydrates an archived project from R2 back into output/{job_id}/ so every # edit endpoint works on it again. Restored files land with a fresh mtime, so # the retention clock restarts; re-restoring after a purge is cheap. _restore_locks: Dict[str, asyncio.Lock] = {} @app.post("/api/projects/{job_id}/restore") async def restore_project(job_id: str, request: Request): if not BILLING_ENABLED: raise HTTPException(status_code=404, detail="Not found") from sqlalchemy import select from cloud.auth import get_current_user_required from cloud.models import Project from cloud import database as cloud_db, storage as cloud_storage user = await get_current_user_required(request) async with cloud_db.session() as s: proj = (await s.execute( select(Project).where(Project.job_id == job_id) )).scalar_one_or_none() if proj is None or str(proj.user_id) != str(user.id): raise HTTPException(status_code=404, detail="Project not found") # Per-job lock: a double click must not download the project twice. lock = _restore_locks.setdefault(job_id, asyncio.Lock()) async with lock: job_dir = os.path.join(OUTPUT_DIR, job_id) # Idempotent fast path: everything the project needs is already on disk. needed = {os.path.basename(proj.metadata_r2_key)} for c in (proj.state or {}).get("clips", []): for k in ("original_file", "server_file"): if c.get(k): needed.add(c[k]) if os.path.isdir(job_dir) and all( os.path.exists(os.path.join(job_dir, f)) for f in needed ): os.utime(job_dir, None) # restart the retention clock else: prefix = cloud_storage.job_key(user.id, job_id, "") keys = await asyncio.to_thread(cloud_storage.list_keys, prefix) if not keys: raise HTTPException(status_code=502, detail="Project files are no longer available") # Download into a temp dir first so a partial failure never leaves a # half-restored job dir that the fast path would mistake for complete. tmp_dir = job_dir + ".restoring" shutil.rmtree(tmp_dir, ignore_errors=True) os.makedirs(tmp_dir, exist_ok=True) sem = asyncio.Semaphore(3) async def _download(key): fname = os.path.basename(key) if not fname: return async with sem: await asyncio.to_thread( cloud_storage.download_file, key, os.path.join(tmp_dir, fname)) try: await asyncio.gather(*(_download(k) for k in keys)) except Exception as e: shutil.rmtree(tmp_dir, ignore_errors=True) raise HTTPException(status_code=502, detail=f"Restore download failed: {e}") # Owner sidecar keeps the multi-tenant guard after a server restart. with open(os.path.join(tmp_dir, ".owner"), "w") as f: f.write(str(user.id)) if os.path.isdir(job_dir): for fname in os.listdir(tmp_dir): shutil.move(os.path.join(tmp_dir, fname), os.path.join(job_dir, fname)) shutil.rmtree(tmp_dir, ignore_errors=True) os.utime(job_dir, None) else: os.rename(tmp_dir, job_dir) # Register (or refresh) the in-memory job — same shape as # _recover_jobs_from_disk, so every edit endpoint works unchanged. json_files = glob.glob(os.path.join(job_dir, "*_metadata.json")) if not json_files: raise HTTPException(status_code=502, detail="Project metadata missing") with open(json_files[0], 'r') as f: data = json.load(f) base_name = os.path.basename(json_files[0]).replace('_metadata.json', '') clips = data.get('shorts', []) for i, clip in enumerate(clips): if not clip.get('video_url'): clip['video_url'] = ( f"/videos/{job_id}/" f"{_canonical_clip_file(job_dir, base_name, i)}") jobs[job_id] = { 'status': 'completed', 'logs': ["♻️ Project restored from your library."], 'output_dir': job_dir, 'user_id': str(user.id), 'result': {'clips': clips, 'cost_analysis': data.get('cost_analysis')}, } return { "job_id": job_id, "status": "completed", "result": jobs[job_id]['result'], "project_state": proj.state, "title": proj.title, } async def _ensure_job_files(job_id: str, request: Request) -> bool: """Make a completed job usable again after its working files vanished. OUTPUT_DIR is not durable — a container restart or redeploy wipes it — so endpoints that read a job's files would 404 on a project the user can still see in their library. Pull it back from R2 on demand (same path as the explicit /restore), so editing keeps working instead of dead-ending. Returns True when the job is available afterwards. Never raises: callers keep their own 404s for jobs that genuinely don't exist. """ job_dir = os.path.join(OUTPUT_DIR, job_id) if job_id in jobs and glob.glob(os.path.join(job_dir, "*_metadata.json")): return True if not BILLING_ENABLED: return False try: await restore_project(job_id, request) print(f"♻️ Auto-restored {job_id} from the library (working files were gone).") return True except HTTPException: return False except Exception as e: print(f"⚠️ Auto-restore failed for {job_id}: {e}") return False from editor import VideoEditor from subtitles import generate_srt, generate_ass, burn_subtitles, generate_srt_from_video from hooks import add_hook_to_video from translate import translate_video, get_supported_languages from thumbnail import analyze_video_for_titles, refine_titles, generate_thumbnail, generate_youtube_description class EditRequest(BaseModel): job_id: str clip_index: int api_key: Optional[str] = None input_filename: Optional[str] = None @app.post("/api/edit") async def edit_clip( req: EditRequest, request: Request, ): # Cloud (paid) mode disables BYOK: ignore any body api_key so it can't skip # the entitlement gate or metering (mirrors resolve_gemini ignoring the # header). Self-host keeps BYOK — the body key wins there. body_key = None if BILLING_ENABLED else req.api_key final_api_key = body_key or await resolve_gemini(request) if not final_api_key: raise gemini_missing_error() await _ensure_job_files(req.job_id, request) if req.job_id not in jobs: raise HTTPException(status_code=404, detail="Job not found") job = jobs[req.job_id] await _assert_job_owner(request, job) if 'result' not in job or 'clips' not in job['result']: raise HTTPException(status_code=400, detail="Job result not available") # Meter the managed Gemini call so it can't be looped for free. Skip only for # genuine BYOK (self-host body key) — in cloud, body_key is always None. edit_minutes = _cloud_config.MANAGED_ANALYSIS_MINUTES if BILLING_ENABLED else 0 reservation_id = None if body_key else await reserve_managed_action( request, edit_minutes, req.job_id, "edit") try: # Resolve Input Path: Prefer explict input_filename from frontend (chaining edits) if req.input_filename: # Security: Ensure just a filename, no paths safe_name = os.path.basename(req.input_filename) input_path = os.path.join(OUTPUT_DIR, req.job_id, safe_name) filename = safe_name else: # Fallback to original clip clip = job['result']['clips'][req.clip_index] filename = clip['video_url'].split('/')[-1] input_path = os.path.join(OUTPUT_DIR, req.job_id, filename) if not os.path.exists(input_path): raise HTTPException(status_code=404, detail=f"Video file not found: {input_path}") # Edit the clip WITHOUT its burned captions, then put them back on top — # otherwise the captions are baked into the edit and the next subtitle # pass stacks a second layer over them (see _reapply_captions). clean_name = _strip_burned_captions(os.path.join(OUTPUT_DIR, req.job_id), filename) had_captions = clean_name != filename if had_captions: filename = clean_name input_path = os.path.join(OUTPUT_DIR, req.job_id, clean_name) # Define output path for edited video edited_filename = f"edited_{filename}" output_path = os.path.join(OUTPUT_DIR, req.job_id, edited_filename) # Run editing in a thread to avoid blocking main loop # Since VideoEditor uses blocking calls (subprocess, API wait) def run_edit(): editor = VideoEditor(api_key=final_api_key) # SAFE FILE RENAMING STRATEGY (Avoid UnicodeEncodeError in Docker) # Create a safe ASCII filename in the same directory safe_filename = f"temp_input_{req.job_id}.mp4" safe_input_path = os.path.join(OUTPUT_DIR, req.job_id, safe_filename) # Copy original file to safe path # (Copy is safer than rename if something crashes, we keep original) shutil.copy(input_path, safe_input_path) try: # 1. Upload (using safe path) vid_file = editor.upload_video(safe_input_path) # 2. Get duration import cv2 cap = cv2.VideoCapture(safe_input_path) fps = cap.get(cv2.CAP_PROP_FPS) frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) duration = frame_count / fps if fps else 0 cap.release() # Load transcript from metadata transcript = None try: meta_files = glob.glob(os.path.join(OUTPUT_DIR, req.job_id, "*_metadata.json")) if meta_files: with open(meta_files[0], 'r') as f: data = json.load(f) transcript = data.get('transcript') except Exception as e: print(f"⚠️ Could not load transcript for editing context: {e}") # 3. Get Plan (Filter String) # Zooms would crop burned-in captions/hooks off screen, so tell # the editor when the source already carries them. `filename` is # the original clip name (safe_input_path is an ASCII temp copy). has_captions = ("subtitled_" in filename) or ("hook_" in filename) or ("hooked_" in filename) filter_data = editor.get_ffmpeg_filter(vid_file, duration, fps=fps, width=width, height=height, transcript=transcript, has_captions=has_captions) # 4. Apply # Use safe output name first safe_output_path = os.path.join(OUTPUT_DIR, req.job_id, f"temp_output_{req.job_id}.mp4") editor.apply_edits(safe_input_path, safe_output_path, filter_data) # Move result to final destination (rename works even if dest name has unicode if filesystem supports it, # but python might still struggle if locale is broken? No, os.rename usually handles it better than subprocess args) # Actually, output_path is defined above: f"edited_{filename}" # If filename has unicode, output_path has unicode. # Let's hope shutil.move / os.rename works. if os.path.exists(safe_output_path): shutil.move(safe_output_path, output_path) return filter_data finally: # Cleanup temp safe input if os.path.exists(safe_input_path): os.remove(safe_input_path) # Run in thread pool loop = asyncio.get_event_loop() plan = await loop.run_in_executor(None, run_edit) # Captions back on top, so the clip the user sees keeps them and the # clean edited file stays available for a later restyle. if had_captions: recap = await loop.run_in_executor( None, _reapply_captions, req.job_id, req.clip_index, output_path) if recap: edited_filename = os.path.basename(recap) new_video_url = f"/videos/{req.job_id}/{edited_filename}" # Persist the new current file like /api/subtitle does: in-memory job # result + metadata.json, so reload/recovery/re-archive see this version. if req.clip_index < len(job['result']['clips']): job['result']['clips'][req.clip_index]['video_url'] = new_video_url try: meta_files = glob.glob(os.path.join(OUTPUT_DIR, req.job_id, "*_metadata.json")) if meta_files: with open(meta_files[0], 'r') as f: meta = json.load(f) shorts = meta.get('shorts', []) if req.clip_index < len(shorts): shorts[req.clip_index]['video_url'] = new_video_url meta['shorts'] = shorts with open(meta_files[0], 'w') as f: json.dump(meta, f, indent=4) except Exception as e: print(f"⚠️ Failed to update metadata.json: {e}") _archive_clip_edit_bg(req.job_id, req.clip_index, edited_filename) if reservation_id: await _metering.commit_reservation(reservation_id) return { "success": True, "new_video_url": new_video_url, "edit_plan": plan } except Exception as e: if reservation_id: await _metering.release_reservation(reservation_id) print(f"❌ Edit Error: {e}") raise HTTPException(status_code=500, detail=str(e)) class CaptionWordIn(BaseModel): """One user-edited caption word, clip-relative ms — the same shape the /transcript endpoint hands the subtitle modal.""" text: str startMs: int endMs: int class SubtitleRequest(BaseModel): job_id: str clip_index: int position: str = "bottom" # top, middle, bottom font_size: int = 16 font_name: str = "Verdana" font_color: str = "#FFFFFF" border_color: str = "#000000" border_width: int = 2 bg_color: str = "#000000" bg_opacity: float = 0.0 style: str = "classic" # classic (uniform color) or karaoke (word highlight) highlight_color: str = "#FFD700" effect: str = "none" # none | glow | pop | box (karaoke only) base_opacity: float = 1.0 # opacity of non-active words (dimmed modern look) uppercase: bool = False input_filename: Optional[str] = None # User-edited caption words. When present, the burn uses them VERBATIM # instead of regenerating from the stored transcript — without this, text # edits in the modal were silently discarded on the server render path. words: Optional[List[CaptionWordIn]] = None @app.get("/api/clip/{job_id}/{clip_index}/transcript") async def get_clip_transcript(job_id: str, clip_index: int, request: Request): """Return word-level captions for a specific clip, formatted for Remotion.""" await _ensure_job_files(job_id, request) if job_id not in jobs: raise HTTPException(status_code=404, detail="Job not found") await _assert_job_owner(request, jobs[job_id]) output_dir = os.path.join(OUTPUT_DIR, job_id) json_files = glob.glob(os.path.join(output_dir, "*_metadata.json")) if not json_files: raise HTTPException(status_code=404, detail="Metadata not found") with open(json_files[0], 'r') as f: data = json.load(f) transcript = data.get('transcript') if not transcript: raise HTTPException(status_code=400, detail="Transcript not found in metadata") clips = data.get('shorts', []) if clip_index >= len(clips): raise HTTPException(status_code=404, detail="Clip not found") clip_data = clips[clip_index] # Recut clips are concatenations of source segments; remap the transcript # onto the clip timeline so every consumer keeps its flat start..end logic. recipe_segments = (clip_data.get('recipe') or {}).get('segments') if recipe_segments: transcript = recut.virtual_transcript(transcript, recipe_segments) clip_start, clip_end = 0.0, recut.total_duration(recipe_segments) else: clip_start = clip_data.get('start', 0) clip_end = clip_data.get('end', 0) # Extract words within clip range and convert to CaptionWord format captions = [] for segment in transcript.get('segments', []): for word_info in segment.get('words', []): if word_info['end'] > clip_start and word_info['start'] < clip_end: captions.append({ "text": word_info.get('word', '').strip(), "startMs": int((max(0, word_info['start'] - clip_start)) * 1000), "endMs": int((max(0, word_info['end'] - clip_start)) * 1000), }) duration_sec = clip_end - clip_start return { "captions": captions, "durationSec": duration_sec, "language": transcript.get('language', 'en'), } # --- Clip editor: EDL + re-render --- # How much transcript to send around the clip's segments — enough for the # editor's word-level trimming without shipping a whole podcast's words. EDL_WORD_CONTEXT_SECONDS = 45.0 def _source_duration_seconds(path): """Probe a video's duration; None when it can't be read.""" try: import cv2 cap = cv2.VideoCapture(path) fps = cap.get(cv2.CAP_PROP_FPS) frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) cap.release() # OpenCV reports -1/-1 for files it can't read — a naive truthiness # check would turn that into a phantom 1.0s duration. if fps > 0 and frames > 0: return round(frames / fps, 3) except Exception: pass return None def _clip_recipe_parts(clip): """(segments, canonical_range) for a clip — synthesized from the flat start/end for clips that were never recut.""" recipe = clip.get('recipe') or {} fallback = {"start": float(clip.get('start', 0) or 0), "end": float(clip.get('end', 0) or 0)} segments = recipe.get('segments') or [dict(fallback)] canonical_range = recipe.get('canonical_range') or dict(fallback) return segments, canonical_range @app.get("/api/clip/{job_id}/{clip_index}/edl") async def get_clip_edl(job_id: str, clip_index: int, request: Request): """The clip's editable recipe: which source segments it was cut from, the word timeline around them, and whether the source is still available for cuts outside the original range.""" await _ensure_job_files(job_id, request) if job_id not in jobs: raise HTTPException(status_code=404, detail="Job not found") job = jobs[job_id] await _assert_job_owner(request, job) output_dir = os.path.join(OUTPUT_DIR, job_id) json_files = glob.glob(os.path.join(output_dir, "*_metadata.json")) if not json_files: raise HTTPException(status_code=404, detail="Metadata not found") with open(json_files[0], 'r') as f: data = json.load(f) clips = data.get('shorts', []) if clip_index < 0 or clip_index >= len(clips): raise HTTPException(status_code=404, detail="Clip not found") clip = clips[clip_index] segments, canonical_range = _clip_recipe_parts(clip) transcript = data.get('transcript') or {} words = recut.transcript_words(transcript) source_path = _locate_source(job_id) source_duration = _source_duration_seconds(source_path) if source_path else None duration_estimated = source_duration is None if duration_estimated: # Best remaining scale for the source track: the last spoken word or # the furthest point any recipe touches. candidates = [canonical_range['end']] + [s['end'] for s in segments] if words: candidates.append(words[-1]['e']) source_duration = round(max(candidates), 3) lo = min(s['start'] for s in segments) - EDL_WORD_CONTEXT_SECONDS hi = max(s['end'] for s in segments) + EDL_WORD_CONTEXT_SECONDS words_out = [ {"w": w["w"], "s": round(w["s"], 3), "e": round(w["e"], 3)} for w in words if w["e"] >= lo and w["s"] <= hi ] current_file = (clip.get('video_url') or '').split('/')[-1] if not current_file: base_name = os.path.basename(json_files[0]).replace('_metadata.json', '') current_file = _canonical_clip_file(output_dir, base_name, clip_index) total = recut.total_duration(segments) return { "job_id": job_id, "clip_index": clip_index, "title": clip.get('video_title_for_youtube_short') or '', "segments": segments, "framing": (clip.get('recipe') or {}).get('framing') or 'auto', "canonical_range": canonical_range, "duration": total, "current_file": current_file, "has_captions": bool(re.match(r'^subtitled_\d+_', current_file)), "words": words_out, "source": { "available": bool(source_path), "url": f"/api/source/{job_id}" if source_path else None, "duration": source_duration, "duration_estimated": duration_estimated, }, "limits": { "max_segments": recut.MAX_SEGMENTS, "min_segment_seconds": recut.MIN_SEGMENT_SECONDS, "max_total_seconds": recut.MAX_TOTAL_SECONDS, }, "rerender_minutes": (max(1, math.ceil(total / 60.0)) if BILLING_ENABLED else 0), } class RerenderSegment(BaseModel): start: float end: float class RerenderRequest(BaseModel): job_id: str clip_index: int segments: List[RerenderSegment] snap_to_words: bool = False reapply_captions: bool = True # None = inherit the recipe's framing (so plain trims keep the look); # 'auto' resets to the classifier; 'full'/'track' force a layout. framing: Optional[str] = None # Manual framing -> reframe-engine strategy. 'full' shows the whole source # frame (WIDE: no side-cropping, blurred filler bands); 'track' forces the # subject-tracking crop. Anything non-auto needs the retained source video. _FRAMING_STRATEGIES = {"auto": None, "full": "WIDE", "track": "TRACK"} # One lock per job (same pattern as _restore_locks): rerenders on the same job # share metadata.json and the canonical files, so they must not interleave. _rerender_locks: Dict[str, asyncio.Lock] = {} # Scene-listing builds write stable preview/thumbnail names per job; serialize # them so overlapping editor opens don't tear each other's files. _scenes_locks: Dict[str, asyncio.Lock] = {} @app.post("/api/clip/rerender") async def rerender_clip(req: RerenderRequest, request: Request): """Re-render a clip from an edited EDL (the clip editor's save button). Two paths, chosen automatically: - FAST: every segment stays inside the range the canonical clip was cut from → recut straight from the already-reframed canonical file. No ML, no source needed. - SOURCE: a segment reaches outside → recut from the retained source and re-reframe with the same engine the pipeline used. 409 when the source already aged out. """ await require_managed_entitlement(request) await _ensure_job_files(req.job_id, request) if req.job_id not in jobs: raise HTTPException(status_code=404, detail="Job not found") job = jobs[req.job_id] await _assert_job_owner(request, job) # Serialize rerenders per job: concurrent saves (easy for an MCP agent to # produce) would otherwise race on the shared metadata.json # read-modify-write below, with the last writer silently reverting the # other clip's recipe/video_url. lock = _rerender_locks.setdefault(req.job_id, asyncio.Lock()) async with lock: return await _rerender_locked(req, request, job) async def _rerender_locked(req: RerenderRequest, request: Request, job): output_dir = os.path.join(OUTPUT_DIR, req.job_id) json_files = glob.glob(os.path.join(output_dir, "*_metadata.json")) if not json_files: raise HTTPException(status_code=404, detail="Metadata not found") with open(json_files[0], 'r') as f: data = json.load(f) clips = data.get('shorts', []) if req.clip_index < 0 or req.clip_index >= len(clips): raise HTTPException(status_code=404, detail="Clip not found") clip = clips[req.clip_index] transcript = data.get('transcript') or {} base_name = os.path.basename(json_files[0]).replace('_metadata.json', '') clean_name = f"{base_name}_clip_{req.clip_index + 1}.mp4" canonical_path = os.path.join(output_dir, clean_name) _, canonical_range = _clip_recipe_parts(clip) source_path = _locate_source(req.job_id) source_duration = _source_duration_seconds(source_path) if source_path else None framing = req.framing or (clip.get('recipe') or {}).get('framing') or 'auto' if framing not in _FRAMING_STRATEGIES: raise HTTPException(status_code=400, detail="framing must be one of: auto, full, track") force_strategy = _FRAMING_STRATEGIES[framing] try: segments = recut.normalize_segments( [{"start": s.start, "end": s.end} for s in req.segments], source_duration) if req.snap_to_words: snap_bound = source_duration or max(s['end'] for s in segments) segments = recut.snap_segments(segments, transcript, snap_bound) if not source_path: # Snapping trails into silence and may nudge a boundary past # the canonical range; without a source the fast path is the # only path, so clamp back instead of failing with a 409. segments = [ {"start": round(max(s['start'], canonical_range['start']), 3), "end": round(min(s['end'], canonical_range['end']), 3)} for s in segments] # Re-validate after snapping/clamping: a segment fully outside the # canonical range clamps to an inverted (end < start) window, and # snapping can stretch the total past the cap. Without this it # reaches ffmpeg and dies as a 500 instead of a clean 400. segments = recut.normalize_segments(segments, source_duration) except recut.RecutError as e: raise HTTPException(status_code=400, detail=str(e)) # A framing override always re-reframes from the source: the canonical file # has the old layout baked into its pixels. fast = (force_strategy is None and os.path.exists(canonical_path) and recut.within_range(segments, canonical_range['start'], canonical_range['end'])) if not fast and not source_path: raise HTTPException( status_code=409, detail=("The source video is no longer on the server; changing the " "framing needs it." if force_strategy is not None else "The source video is no longer on the server, so segments " "must stay within the original clip range.")) total = recut.total_duration(segments) rerender_minutes = (max(1, math.ceil(total / 60.0)) if BILLING_ENABLED else 0) reservation_id = await reserve_managed_action( request, rerender_minutes, req.job_id, "rerender") v_transcript = (recut.virtual_transcript(transcript, segments) if req.reapply_captions else None) def run_recut(): if fast: return recut.perform_recut( input_path=canonical_path, segments=recut.rebase_segments( segments, canonical_range['start'], canonical_range['end']), output_dir=output_dir, clean_name=clean_name, reframe=False, captions_transcript=v_transcript) return recut.perform_recut( input_path=source_path, segments=segments, output_dir=output_dir, clean_name=clean_name, reframe=True, output_format=data.get('output_format', 'auto'), watermark=bool(job.get('watermark')), force_strategy=force_strategy, captions_transcript=v_transcript) try: loop = asyncio.get_event_loop() served_name, _clean_recut_name = await loop.run_in_executor(None, run_recut) new_video_url = f"/videos/{req.job_id}/{served_name}" new_recipe = {"v": 1, "segments": segments, "canonical_range": canonical_range} if framing != 'auto': new_recipe["framing"] = framing # Covering range, deliberately not segments[0]/segments[-1]: segments # may legally be out of source order, and downstream consumers only # need a sane positive window (the recipe is the real timeline). new_start = min(s['start'] for s in segments) new_end = max(s['end'] for s in segments) updates = {'video_url': new_video_url, 'start': new_start, 'end': new_end, 'recipe': new_recipe} # Per-scene manual framing is keyed by scene indices of a specific cut; # this render neither applied it nor can it survive a changed cut, so # clear it rather than let /scenes serve stale overrides against the # wrong shots (re-frame after trimming to re-apply by hand). if clip.get('crop_overrides'): updates['crop_overrides'] = None clip.update(updates) data['shorts'] = clips with open(json_files[0], 'w') as f: json.dump(data, f, indent=2) mem_clips = (job.get('result') or {}).get('clips') or [] if req.clip_index < len(mem_clips): mem_clips[req.clip_index].update(updates) _archive_clip_edit_bg(req.job_id, req.clip_index, served_name) if reservation_id: await _metering.commit_reservation(reservation_id) return { "success": True, "new_video_url": new_video_url, "recipe": new_recipe, "framing": framing, "start": new_start, "end": new_end, "duration": total, "render_path": "fast" if fast else "source", } except Exception as e: if reservation_id: await _metering.release_reservation(reservation_id) print(f"❌ Rerender Error: {e}") raise HTTPException(status_code=500, detail=str(e)) # --- Manual framing ----------------------------------------------------------- # # The reframe engine picks the crop automatically, and on a podcast it is right # most of the time and grossly wrong occasionally: a wide shot of the whole # table averages over one face, so the scene falls to GENERAL (letterboxed) or # tracks the wrong person. There was no way to say "no, frame it here". # # The unit is the SCENE, not the clip, because a podcast cuts between a fixed # close camera and a fixed wide one, and the right crop differs per camera. # Scene boundaries already are the camera changes: PySceneDetect finds them. # # Scenes the user never touches keep the automatic camera, so correcting one # bad shot cannot spoil the ones the tracker got right. class ReframeRequest(BaseModel): job_id: str clip_index: int # scene index (string key, JSON-style) -> either a crop centre as a # fraction of the source width, or {"top": f, "bottom": f} to stack two # regions. Fractions travel instead of pixels so the editor never needs to # know the source dimensions. crop_overrides: Dict[str, Any] reapply_captions: bool = True def _clip_scene_workfile(source_path, segments, output_dir, token): """Cut the clip out of the source so scenes can be detected on it. The name is unique per call, unlike the thumbnails and the preview: two editor opens on the same clip would otherwise write the same temp file at once, and the first to finish deletes it out from under the second. """ work_path = os.path.join(output_dir, f"scenes_{token}.mp4") recut.run_cut_concat(source_path, segments, work_path, output_dir) return work_path @app.get("/api/clip/{job_id}/{clip_index}/scenes") async def get_clip_scenes(job_id: str, clip_index: int, request: Request): """Scenes of a clip, each with a SOURCE frame to frame it against. The frames come from the uncropped cut, not the delivered clip: the point is to show what the automatic crop threw away, which the 9:16 file no longer contains. """ # Entitlement too, not just ownership: listing scenes cuts the clip from # source, runs scene detection and encodes a preview — real compute. await require_managed_entitlement(request) await _ensure_job_files(job_id, request) if job_id not in jobs: raise HTTPException(status_code=404, detail="Job not found") job = jobs[job_id] await _assert_job_owner(request, job) output_dir = os.path.join(OUTPUT_DIR, job_id) json_files = glob.glob(os.path.join(output_dir, "*_metadata.json")) if not json_files: raise HTTPException(status_code=404, detail="Metadata not found") with open(json_files[0], 'r') as f: data = json.load(f) clips = data.get('shorts', []) if clip_index < 0 or clip_index >= len(clips): raise HTTPException(status_code=404, detail="Clip not found") if data.get('output_format') == 'horizontal': raise HTTPException( status_code=400, detail="Horizontal clips keep the full frame; there is no crop to reframe.") clip = clips[clip_index] segments, _canonical_range = _clip_recipe_parts(clip) # What was applied last time. Without this the editor reopens blank, and # since a re-render rebuilds from source using ONLY what it is sent, the # next save would silently drop every earlier adjustment. saved_overrides = clip.get('crop_overrides') or {} source_path = _locate_source(job_id) if not source_path: raise HTTPException( status_code=409, detail="The source video is no longer on the server, so the " "framing of this clip can no longer be changed.") def build(): import cv2 import main as m # Stable for the files the browser fetches (no accumulation), unique # for the temp cut (no collision between overlapping requests). # temp_ prefix keeps these editor-only artifacts out of the self-host # S3 backup (it skips temp_*); stable names still avoid accumulation. token = str(clip_index) work_token = f"{clip_index}_{uuid.uuid4().hex[:8]}" preview_name = f"temp_preview_{clip_index}.mp4" preview_path = os.path.join(output_dir, preview_name) work_path = _clip_scene_workfile(source_path, segments, output_dir, work_token) try: scenes, fps = m.detect_scenes(work_path) fps = float(fps) or 30.0 orig_w, orig_h = m.get_video_resolution(work_path) cap = cv2.VideoCapture(work_path) if not scenes: total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) or 1 bounds = [(0, total)] else: bounds = [(s.get_frames(), e.get_frames()) for s, e in scenes] out = [] for idx, (start_f, end_f) in enumerate(bounds): mid = (start_f + end_f) // 2 cap.set(cv2.CAP_PROP_POS_FRAMES, mid) ok, frame = cap.read() thumb_name = f"temp_scene_{token}_{idx:03d}.jpg" suggested = 0.5 suggested_y = 0.5 if ok: cv2.imwrite(os.path.join(output_dir, thumb_name), frame, [int(cv2.IMWRITE_JPEG_QUALITY), 80]) # Start the rectangle on the biggest face in the shot, so # the common case is a nudge rather than a hunt. try: faces = m.detect_face_candidates(frame) if faces: box = max(faces, key=lambda f: f['box'][2] * f['box'][3])['box'] suggested = min(1.0, max(0.0, (box[0] + box[2] / 2) / orig_w)) # SPLIT halves crop vertically too, so the face's # height matters there (TRACK ignores it). suggested_y = min(1.0, max(0.0, (box[1] + box[3] / 2) / orig_h)) except Exception: pass else: thumb_name = None out.append({ "index": idx, "start": round(start_f / fps, 3), "end": round(end_f / fps, 3), "thumbnail_url": (f"/videos/{job_id}/{thumb_name}" if thumb_name else None), "suggested_center": round(suggested, 4), "suggested_center_y": round(suggested_y, 4), }) cap.release() return orig_w, orig_h, out, preview_name finally: # The uncropped cut becomes a light preview instead of being # discarded: judging the framing means knowing who is talking, and # the delivered 9:16 file no longer shows the rest of the room. try: if os.path.exists(work_path): subprocess.run( ["ffmpeg", "-y", "-loglevel", "error", "-i", work_path, "-vf", "scale=640:-2", "-c:v", "libx264", "-preset", "veryfast", "-crf", "30", "-c:a", "aac", "-b:a", "96k", # faststart: the editor's