import os import json import re import subprocess import time from typing import List from google import genai from google.genai import types from pydantic import BaseModel from edit_builder import build_filter_string from ffmpeg_utils import video_encode_args, QUALITY, METADATA_SCRUB class EditDecision(BaseModel): type: str start: float end: float strength: float = 0.0 reason: str = "" class EditPlan(BaseModel): edits: List[EditDecision] class VideoEditor: def __init__(self, api_key): self.client = genai.Client(api_key=api_key) self.model_name = ( os.environ.get("GEMINI_MODEL_EDITOR") or os.environ.get("GEMINI_MODEL") or "gemini-3.1-flash-lite" ) def upload_video(self, video_path): """Uploads video to Gemini File API.""" print(f"๐Ÿ“ค Uploading {video_path} to Gemini...") # Ensure we are passing a path that exists if not os.path.exists(video_path): raise FileNotFoundError(f"Video file not found: {video_path}") # Using 'file' keyword instead of 'path' try: file_upload = self.client.files.upload(file=video_path) except Exception as e: print(f"โŒ Gemini Upload Error: {e}") raise e # Wait for processing print("โณ Waiting for video processing by Gemini...") deadline = time.time() + 120 while True: file_info = self.client.files.get(name=file_upload.name) state = getattr(file_info, "state", file_info) state_name = str(getattr(state, "name", state)).upper() if state_name == "ACTIVE": print("โœ… Video processed and ready.") return file_upload if state_name == "FAILED": raise Exception("Video processing failed by Gemini.") if time.time() > deadline: raise TimeoutError("Gemini file processing timed out after 120s.") time.sleep(2) def get_ffmpeg_filter(self, video_file_obj, duration, fps=30, width=None, height=None, transcript=None, has_captions=False): """Asks Gemini for an edit decision list, then builds the FFmpeg filter deterministically (edit_builder) so syntax and zoom limits are always safe.""" if width is None or height is None: # Keep prompt usable even if caller didn't pass dimensions. width, height = 1080, 1920 transcript_text = json.dumps(transcript) if transcript else "Not available." caption_rule = "" if has_captions: caption_rule = ( "\n CRITICAL: This video already has burned-in captions and/or a hook text. " "Any zoom would crop or shift them off screen. Do NOT use zoom_in, punch_in or zoom_pulse โ€” " "only color_pop, bw_moment, flash and vignette are allowed.\n" ) prompt = f""" You are a viral short-form video editor (TikTok / Reels / Shorts). You receive a video and its transcript. Decide WHERE a small set of tasteful, high-impact effects belongs. You do NOT write FFmpeg โ€” you return an edit decision list, and a deterministic renderer applies it safely. Video duration: {duration:.1f} seconds. Resolution: {width}x{height}. Available effect types: - "zoom_in": slow push-in across the segment. Builds tension or focus on a key statement. strength 0.06-0.15, segment 1.5-6s. - "punch_in": instant tighter framing for the whole segment. Emphasis, punchlines, "listen to this" moments. strength 0.06-0.15, segment 0.8-5s. - "zoom_pulse": quick in-and-out pulse peaking mid-segment. Beat drops, single impactful words. strength 0.04-0.10, segment 0.4-1.2s. - "color_pop": richer saturation/contrast. Energy, excitement, product/visual highlights. strength 0.2-1.0. - "bw_moment": black & white. Drama, serious quotes, flashbacks. - "flash": brief bright flash at segment start. Hard-cut energy, reveals. Use at most 2. - "vignette": darkened edges. Focus, intimacy, storytelling moments. Rules: 1. Match effects to the CONTENT: place them on the exact words or moments they emphasize, using the transcript timestamps. 2. Less is more: 2-6 edits per 30 seconds of video. A random effect is worse than no effect. 3. Never overlap two zoom-type edits (zoom_in/punch_in/zoom_pulse). 4. Calm, serious delivery => few or no motion effects. High-energy content => more punch. {caption_rule} TRANSCRIPT (with timestamps, the context of what is being said): {transcript_text} Return JSON only: {{"edits": [{{"type": "punch_in", "start": 3.2, "end": 5.4, "strength": 0.1, "reason": "punchline"}}]}} If no effects genuinely improve the video, return {{"edits": []}}. """ print("๐Ÿค– Asking Gemini for an edit decision list...") # Low media resolution: Gemini samples at 1 FPS and normalizes frames to # fixed token sizes, so "low" (~70 tokens/frame) cuts video-input cost # without hurting motion/scene understanding. Fall back to defaults if # the SDK or model rejects the option. try: config = types.GenerateContentConfig( response_mime_type="application/json", response_schema=EditPlan, media_resolution=types.MediaResolution.MEDIA_RESOLUTION_LOW, ) except Exception: config = types.GenerateContentConfig( response_mime_type="application/json", response_schema=EditPlan, ) try: response = self.client.models.generate_content( model=self.model_name, contents=[video_file_obj, prompt], config=config, ) except Exception as e: if getattr(config, "media_resolution", None) is None: raise print(f"โš ๏ธ media_resolution=low rejected ({e}); retrying with defaults...") response = self.client.models.generate_content( model=self.model_name, contents=[video_file_obj, prompt], config=types.GenerateContentConfig( response_mime_type="application/json", response_schema=EditPlan, ), ) raw_edits = self._extract_edits(response) if raw_edits is None: return None filter_string, applied = build_filter_string( raw_edits, duration=duration, fps=fps, width=width, height=height, has_captions=has_captions, ) if not filter_string: print("โ„น๏ธ Gemini suggested no (valid) edits โ€” keeping the clip untouched.") return {"filter_string": None, "edits": []} print(f"๐ŸŽฏ Applying {len(applied)} edits: " + ", ".join(f"{e['type']}@{e['start']:.1f}s" for e in applied)) return {"filter_string": filter_string, "edits": applied} def get_effects_config(self, video_file_obj, duration, fps=30, width=None, height=None, transcript=None): """Asks Gemini for a structured EffectsConfig JSON for Remotion rendering.""" if width is None or height is None: width, height = 1080, 1920 transcript_text = json.dumps(transcript) if transcript else "Not available." prompt = f""" You are an expert video editor analyzing a video and its transcript to generate dynamic visual effects for a Remotion-based renderer. Video Duration: {duration} seconds. Video FPS: {fps} Video Resolution: {width}x{height} TRANSCRIPT (Context of what is being said): {transcript_text} Your task is to produce a structured JSON describing time-based effect segments that cover the FULL video duration. Each segment has these fields: - "startSec" (number): Start time in seconds. - "endSec" (number): End time in seconds. - "zoom" (number): Zoom level. 1.0 = no zoom, max 1.5. Use subtle values like 1.05-1.2 for most cases. - "zoomCenterX" (number): Horizontal focus point for zoom, 0.0 (left) to 1.0 (right). 0.5 = center. - "zoomCenterY" (number): Vertical focus point for zoom, 0.0 (top) to 1.0 (bottom). 0.5 = center. - "brightness" (number): Brightness multiplier. 1.0 = normal. Range 0.8-1.2. - "contrast" (number): Contrast multiplier. 1.0 = normal. Range 0.8-1.3. - "saturate" (number): Saturation multiplier. 1.0 = normal. Range 0.8-1.3. Instructions: 1. ANALYZE the video content and transcript to understand mood, pacing, and key moments. 2. Apply CONTEXTUAL effects aligned with speech and action: - Use slow, subtle zooms toward the speaker's face during speaking moments. - Emphasize key moments, punchlines, or dramatic beats with slightly stronger zoom or contrast. - Keep transitions smooth โ€” avoid jarring jumps between segments. - If nothing significant is happening, keep values at defaults (zoom 1.0, all multipliers 1.0). 3. Segments MUST cover the entire video duration from 0 to {duration} seconds with no gaps. 4. Prefer fewer, longer segments with gradual changes over many rapid short segments. 5. Output ONLY valid JSON, no explanations. Output format: {{ "segments": [ {{ "startSec": 0, "endSec": 3.5, "zoom": 1.0, "zoomCenterX": 0.5, "zoomCenterY": 0.5, "brightness": 1.0, "contrast": 1.0, "saturate": 1.0 }} ] }} """ print("๐Ÿค– Asking Gemini for Remotion effects config...") response = self.client.models.generate_content( model=self.model_name, contents=[video_file_obj, prompt], config=types.GenerateContentConfig( response_mime_type="application/json" ) ) print(f"๐Ÿ” DEBUG: Gemini Raw Response:\n{response.text}") try: # Clean response text (remove potential markdown blocks) text = response.text if text.startswith("```json"): text = text[7:] elif text.startswith("```"): text = text[3:] if text.endswith("```"): text = text[:-3] text = text.strip() # Find the first '{' and last '}' start_idx = text.find('{') end_idx = text.rfind('}') if start_idx != -1 and end_idx != -1: text = text[start_idx:end_idx+1] print(f"๐Ÿ” DEBUG: Cleaned JSON Text:\n{text}") return json.loads(text) except json.JSONDecodeError: print(f"โŒ Failed to parse effects config JSON: {response.text}") return None @staticmethod def _extract_edits(response): """Pull the edit list out of a Gemini response (parsed schema preferred, raw-JSON fallback). Returns a list of dicts, or None on hard failure.""" parsed = getattr(response, "parsed", None) if parsed is not None: try: return [edit.model_dump() for edit in parsed.edits] except Exception: pass text = (getattr(response, "text", None) or "").strip() print(f"๐Ÿ” DEBUG: Gemini Raw Response:\n{text}") if text.startswith("```json"): text = text[7:] elif text.startswith("```"): text = text[3:] if text.endswith("```"): text = text[:-3] text = text.strip() start_idx = text.find('{') end_idx = text.rfind('}') if start_idx != -1 and end_idx != -1: text = text[start_idx:end_idx + 1] try: data = json.loads(text) except json.JSONDecodeError: print(f"โŒ Failed to parse edit plan JSON: {text[:500]}") return None edits = data.get("edits") if isinstance(data, dict) else None return edits if isinstance(edits, list) else [] @staticmethod def _split_filter_chain(filter_string: str) -> list[str]: """Split a -vf filter chain on commas, respecting single-quoted substrings.""" parts: list[str] = [] start = 0 in_quote = False for i, ch in enumerate(filter_string): if ch == "'": in_quote = not in_quote elif ch == "," and not in_quote: parts.append(filter_string[start:i]) start = i + 1 parts.append(filter_string[start:]) return parts @classmethod def _enforce_zoompan_output_size(cls, filter_string: str, width: int, height: int) -> str: """Force any zoompan filter to output the same geometry as the input clip.""" parts = cls._split_filter_chain(filter_string) out_parts: list[str] = [] for part in parts: if "zoompan=" in part: # Force s=WxH inside zoompan options (digitsxdigits only). if re.search(r":s=\d+x\d+", part): part = re.sub(r":s=\d+x\d+", f":s={width}x{height}", part) else: part = f"{part}:s={width}x{height}" out_parts.append(part) return ",".join(out_parts) @staticmethod def _sanitize_filter_string(filter_string: str) -> str: """ Best-effort sanitizer for Gemini-generated FFmpeg expressions. Converts comparison operators (t<3, on>=75, etc.) into FFmpeg expr functions (lt(), gte(), ...), which are far more reliably parsed across FFmpeg builds. """ s = filter_string # Order matters: handle >= / <= before > / < patterns: list[tuple[re.Pattern[str], str]] = [ (re.compile(r"(?=\s*(-?\d+(?:\.\d+)?)"), r"gte(\1,\2)"), (re.compile(r"(?\s*(-?\d+(?:\.\d+)?)"), r"gt(\1,\2)"), (re.compile(r"(?=75) before executing FFmpeg. sanitized = self._sanitize_filter_string(filter_string) if sanitized != filter_string: print("๐Ÿงผ Sanitized AI Filter (converted comparisons to lt/lte/gt/gte functions)") print(f"๐Ÿงผ Before: {filter_string}") print(f"๐Ÿงผ After: {sanitized}") filter_string = sanitized # Enforce zoompan output size to preserve aspect ratio / resolution. if w and h: enforced = self._enforce_zoompan_output_size(filter_string, w, h) if enforced != filter_string: print(f"๐Ÿ“ Enforced zoompan output size to {w}x{h}") filter_string = enforced # Ensure square pixels (avoid weird display stretching in some players). if "setsar=" not in filter_string: filter_string = f"{filter_string},setsar=1" # Use explicit environment with UTF-8 to avoid ascii errors in subprocess env = os.environ.copy() # On some minimal docker images, we need to ensure we use a UTF-8 locale # Try C.UTF-8 first, fallback to en_US.UTF-8 if available, but C.UTF-8 is usually safer for minimal env["LANG"] = "C.UTF-8" env["LC_ALL"] = "C.UTF-8" # Dry-run the filter on 2 seconds before committing to a full encode; # on failure, give Gemini one self-repair attempt with the real error. ok, error_text = self._test_filter(input_path, filter_string, env) if not ok: print(f"โš ๏ธ AI filter failed dry-run: {error_text}") repaired = self._repair_filter(filter_string, error_text, w or 1080, h or 1920) if repaired: repaired = self._sanitize_filter_string(repaired) if w and h: repaired = self._enforce_zoompan_output_size(repaired, w, h) if "setsar=" not in repaired: repaired = f"{repaired},setsar=1" ok, error_text = self._test_filter(input_path, repaired, env) if ok: print("๐Ÿ”ง Self-repaired AI filter passed dry-run.") filter_string = repaired if not ok: raise RuntimeError(f"AI filter failed validation even after self-repair: {error_text}") print(f"๐ŸŽฌ Executing AI Filter: {filter_string}") cmd = [ 'ffmpeg', '-y', '-i', input_path, '-vf', filter_string, *video_encode_args(QUALITY), '-c:a', 'copy', *METADATA_SCRUB, '-movflags', '+faststart', output_path ] try: # We must encode arguments if filesystem is ascii but we have unicode chars # But subprocess in Python 3 handles unicode args by encoding them with os.fsencode(). # If sys.getfilesystemencoding() is ascii, this fails. # We can't change fs encoding at runtime easily. # Workaround: pass bytes directly? subprocess allows bytes in args. # Convert command elements to bytes assuming utf-8 if they are strings cmd_bytes = [] for arg in cmd: if isinstance(arg, str): cmd_bytes.append(arg.encode('utf-8')) else: cmd_bytes.append(arg) subprocess.run(cmd_bytes, check=True, env=env, timeout=1800) except subprocess.TimeoutExpired: print("โŒ FFmpeg filter timed out after 1800s.") raise RuntimeError("FFmpeg filter timed out after 1800s.") except subprocess.CalledProcessError as e: print(f"โŒ FFmpeg failed: {e}") raise e if __name__ == "__main__": pass