import time import cv2 import scenedetect import subprocess import argparse import re import sys from scenedetect import open_video, SceneManager from scenedetect.detectors import ContentDetector from ultralytics import YOLO import torch import os import numpy as np from tqdm import tqdm import yt_dlp import mediapipe as mp # import whisper (replaced by faster_whisper inside function) from google import genai from google.genai import types as genai_types from dotenv import load_dotenv import json import warnings warnings.filterwarnings("ignore", category=UserWarning, module='google.protobuf') # Load environment variables load_dotenv() # --- Constants --- ASPECT_RATIO = 9 / 16 GEMINI_PROMPT_TEMPLATE = """ You are a senior short-form video editor. Read the ENTIRE transcript and word-level timestamps to choose the 3–15 MOST VIRAL moments for TikTok/IG Reels/YouTube Shorts. Each clip must be between 15 and 60 seconds long. ⚠️ FFMPEG TIME CONTRACT — STRICT REQUIREMENTS: - Return timestamps in ABSOLUTE SECONDS from the start of the video (usable in: ffmpeg -ss -to -i ...). - Only NUMBERS with decimal point, up to 3 decimals (examples: 0, 1.250, 17.350). - Ensure 0 ≤ start < end ≤ VIDEO_DURATION_SECONDS. - Each clip between 15 and 60 s (inclusive). - Prefer starting 0.2–0.4 s BEFORE the hook and ending 0.2–0.4 s AFTER the payoff. - Use silence moments for natural cuts; never cut in the middle of a word or phrase. - STRICTLY FORBIDDEN to use time formats other than absolute seconds. VIDEO_DURATION_SECONDS: {video_duration} TRANSCRIPT_TEXT (raw): {transcript_text} WORDS_JSON (array of {{w, s, e}} where s/e are seconds): {words_json} STRICT EXCLUSIONS: - No generic intros/outros or purely sponsorship segments unless they contain the hook. - No clips < 15 s or > 60 s. OUTPUT — RETURN ONLY VALID JSON (no markdown, no comments). Order clips by predicted performance (best to worst). In the descriptions, ALWAYS include a CTA like "Follow me and comment X and I'll send you the workflow" (especially if discussing an n8n workflow): {{ "shorts": [ {{ "start": , "end": , "video_description_for_tiktok": "", "video_description_for_instagram": "", "video_title_for_youtube_short": "", "viral_hook_text": "<SHORT punchy text overlay (max 10 words). MUST BE IN THE SAME LANGUAGE AS THE VIDEO TRANSCRIPT. Examples: 'POV: You realized...', 'Did you know?', 'Stop doing this!'>" }} ] }} """ # Load the YOLO model once (Keep for backup or scene analysis if needed) model = YOLO('yolov8n.pt') # --- MediaPipe Setup --- # Use standard Face Detection (BlazeFace) for speed mp_face_detection = mp.solutions.face_detection face_detection = mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5) class SmoothedCameraman: """ Handles smooth camera movement. Simplified Logic: "Heavy Tripod" Only moves if the subject leaves the center safe zone. Moves slowly and linearly. """ def __init__(self, output_width, output_height, video_width, video_height, aspect_ratio=ASPECT_RATIO): self.output_width = output_width self.output_height = output_height self.video_width = video_width self.video_height = video_height self.aspect_ratio = aspect_ratio # Initial State self.current_center_x = video_width / 2 self.target_center_x = video_width / 2 # Calculate crop dimensions once self.crop_height = video_height self.crop_width = int(self.crop_height * aspect_ratio) if self.crop_width > video_width: self.crop_width = video_width self.crop_height = int(self.crop_width / aspect_ratio) # Safe Zone: 20% of the video width # As long as the target is within this zone relative to current center, DO NOT MOVE. self.safe_zone_radius = self.crop_width * 0.25 def update_target(self, face_box): """ Updates the target center based on detected face/person. """ if face_box: x, y, w, h = face_box self.target_center_x = x + w / 2 def get_crop_box(self, force_snap=False): """ Returns the (x1, y1, x2, y2) for the current frame. """ if force_snap: self.current_center_x = self.target_center_x else: diff = self.target_center_x - self.current_center_x # SIMPLIFIED LOGIC: # 1. Is the target outside the safe zone? if abs(diff) > self.safe_zone_radius: # 2. If yes, move towards it slowly (Linear Speed) # Determine direction direction = 1 if diff > 0 else -1 # Speed: 2 pixels per frame (Slow pan) # If the distance is HUGE (scene change or fast movement), speed up slightly if abs(diff) > self.crop_width * 0.5: speed = 15.0 # Fast re-frame else: speed = 3.0 # Slow, steady pan self.current_center_x += direction * speed # Check if we overshot (prevent oscillation) new_diff = self.target_center_x - self.current_center_x if (direction == 1 and new_diff < 0) or (direction == -1 and new_diff > 0): self.current_center_x = self.target_center_x # If inside safe zone, DO NOTHING (Stationary Camera) # Clamp center half_crop = self.crop_width / 2 if self.current_center_x - half_crop < 0: self.current_center_x = half_crop if self.current_center_x + half_crop > self.video_width: self.current_center_x = self.video_width - half_crop x1 = int(self.current_center_x - half_crop) x2 = int(self.current_center_x + half_crop) x1 = max(0, x1) x2 = min(self.video_width, x2) y1 = 0 y2 = self.video_height return x1, y1, x2, y2 class SpeakerTracker: """ Tracks speakers over time to prevent rapid switching and handle temporary obstructions. """ def __init__(self, stabilization_frames=15, cooldown_frames=30): self.active_speaker_id = None self.speaker_scores = {} # {id: score} self.last_seen = {} # {id: frame_number} self.locked_counter = 0 # How long we've been locked on current speaker # Hyperparameters self.stabilization_threshold = stabilization_frames # Frames needed to confirm a new speaker self.switch_cooldown = cooldown_frames # Minimum frames before switching again self.last_switch_frame = -1000 # ID tracking self.next_id = 0 self.known_faces = [] # [{'id': 0, 'center': x, 'last_frame': 123}] def get_target(self, face_candidates, frame_number, width): """ Decides which face to focus on. face_candidates: list of {'box': [x,y,w,h], 'score': float} """ current_candidates = [] # 1. Match faces to known IDs (simple distance tracking) for face in face_candidates: x, y, w, h = face['box'] center_x = x + w / 2 best_match_id = -1 min_dist = width * 0.15 # Reduced matching radius to avoid jumping in groups # Try to match with known faces seen recently for kf in self.known_faces: if frame_number - kf['last_frame'] > 30: # Forgot faces older than 1s (was 2s) continue dist = abs(center_x - kf['center']) if dist < min_dist: min_dist = dist best_match_id = kf['id'] # If no match, assign new ID if best_match_id == -1: best_match_id = self.next_id self.next_id += 1 # Update known face self.known_faces = [kf for kf in self.known_faces if kf['id'] != best_match_id] self.known_faces.append({'id': best_match_id, 'center': center_x, 'last_frame': frame_number}) current_candidates.append({ 'id': best_match_id, 'box': face['box'], 'score': face['score'] }) # 2. Update Scores with decay for pid in list(self.speaker_scores.keys()): self.speaker_scores[pid] *= 0.85 # Faster decay (was 0.9) if self.speaker_scores[pid] < 0.1: del self.speaker_scores[pid] # Add new scores for cand in current_candidates: pid = cand['id'] # Score is purely based on size (proximity) now that we don't have mouth raw_score = cand['score'] / (width * width * 0.05) self.speaker_scores[pid] = self.speaker_scores.get(pid, 0) + raw_score # 3. Determine Best Speaker if not current_candidates: # If no one found, maintain last active speaker if cooldown allows # to avoid black screen or jump to 0,0 return None best_candidate = None max_score = -1 for cand in current_candidates: pid = cand['id'] total_score = self.speaker_scores.get(pid, 0) # Hysteresis: HUGE Bonus for current active speaker if pid == self.active_speaker_id: total_score *= 3.0 # Sticky factor if total_score > max_score: max_score = total_score best_candidate = cand # 4. Decide Switch if best_candidate: target_id = best_candidate['id'] if target_id == self.active_speaker_id: self.locked_counter += 1 return best_candidate['box'] # New person if frame_number - self.last_switch_frame < self.switch_cooldown: old_cand = next((c for c in current_candidates if c['id'] == self.active_speaker_id), None) if old_cand: return old_cand['box'] self.active_speaker_id = target_id self.last_switch_frame = frame_number self.locked_counter = 0 return best_candidate['box'] return None def detect_face_candidates(frame): """ Returns list of all detected faces using lightweight FaceDetection. """ height, width, _ = frame.shape rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = face_detection.process(rgb_frame) candidates = [] if not results.detections: return [] for detection in results.detections: bboxC = detection.location_data.relative_bounding_box x = int(bboxC.xmin * width) y = int(bboxC.ymin * height) w = int(bboxC.width * width) h = int(bboxC.height * height) candidates.append({ 'box': [x, y, w, h], 'score': w * h # Area as score }) return candidates def detect_person_yolo(frame): """ Fallback: Detect largest person using YOLO when face detection fails. Returns [x, y, w, h] of the person's 'upper body' approximation. """ # Use the globally loaded model results = model(frame, verbose=False, classes=[0]) # class 0 is person if not results: return None best_box = None max_area = 0 for result in results: boxes = result.boxes for box in boxes: x1, y1, x2, y2 = [int(i) for i in box.xyxy[0]] w = x2 - x1 h = y2 - y1 area = w * h if area > max_area: max_area = area # Focus on the top 40% of the person (head/chest) for framing # This approximates where the face is if we can't detect it directly face_h = int(h * 0.4) best_box = [x1, y1, w, face_h] return best_box def create_general_frame(frame, output_width, output_height): """ Creates a 'General Shot' frame: - Background: Blurred zoom of original - Foreground: Original video scaled to fit width, centered vertically. """ orig_h, orig_w = frame.shape[:2] # 1. Background (Fill Height) # Crop center to aspect ratio bg_scale = output_height / orig_h bg_w = int(orig_w * bg_scale) bg_resized = cv2.resize(frame, (bg_w, output_height), interpolation=cv2.INTER_LANCZOS4) # Crop center of background start_x = (bg_w - output_width) // 2 if start_x < 0: start_x = 0 background = bg_resized[:, start_x:start_x+output_width] if background.shape[1] != output_width: background = cv2.resize(background, (output_width, output_height), interpolation=cv2.INTER_LANCZOS4) # Blur background background = cv2.GaussianBlur(background, (51, 51), 0) # 2. Foreground (Fit Width) scale = output_width / orig_w fg_h = int(orig_h * scale) foreground = cv2.resize(frame, (output_width, fg_h), interpolation=cv2.INTER_LANCZOS4) # 3. Overlay y_offset = (output_height - fg_h) // 2 # Clone background to avoid modifying it final_frame = background.copy() final_frame[y_offset:y_offset+fg_h, :] = foreground return final_frame def analyze_scenes_strategy(video_path, scenes): """ Analyzes each scene to determine if it should be TRACK (Single person) or GENERAL (Group/Wide). Returns list of strategies corresponding to scenes. """ cap = cv2.VideoCapture(video_path) strategies = [] if not cap.isOpened(): return ['TRACK'] * len(scenes) for start, end in tqdm(scenes, desc=" Analyzing Scenes"): # Sample 3 frames (start, middle, end) frames_to_check = [ start.get_frames() + 5, int((start.get_frames() + end.get_frames()) / 2), end.get_frames() - 5 ] face_counts = [] for f_idx in frames_to_check: cap.set(cv2.CAP_PROP_POS_FRAMES, f_idx) ret, frame = cap.read() if not ret: continue # Detect faces candidates = detect_face_candidates(frame) face_counts.append(len(candidates)) # Decision Logic if not face_counts: avg_faces = 0 else: avg_faces = sum(face_counts) / len(face_counts) # Strategy: # 0 faces -> GENERAL (Landscape/B-roll) # 1 face -> TRACK # > 1.2 faces -> GENERAL (Group) if avg_faces > 1.2 or avg_faces < 0.5: strategies.append('GENERAL') else: strategies.append('TRACK') cap.release() return strategies def detect_scenes(video_path): video = open_video(video_path) scene_manager = SceneManager() scene_manager.add_detector(ContentDetector()) scene_manager.detect_scenes(video=video) scene_list = scene_manager.get_scene_list() fps = video.frame_rate return scene_list, fps def get_video_resolution(video_path): cap = cv2.VideoCapture(video_path) if not cap.isOpened(): raise IOError(f"Could not open video file {video_path}") width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) cap.release() return width, height def sanitize_filename(filename): """Remove invalid characters from filename.""" filename = re.sub(r'[<>:"/\\|?*#]', '', filename) filename = filename.replace(' ', '_') return filename[:100] def download_youtube_video(url, output_dir="."): """ Downloads a YouTube video using yt-dlp. Returns the path to the downloaded video and the video title. """ # SSRF guard: block non-http(s) schemes and private/loopback/metadata hosts # before handing the URL to yt-dlp. from security_utils import assert_public_url assert_public_url(url) print(f"🔍 Debug: yt-dlp version: {yt_dlp.version.__version__}") print("📥 Downloading video from YouTube...") step_start_time = time.time() cookies_path = '/app/cookies.txt' cookies_env = os.environ.get("YOUTUBE_COOKIES") if cookies_env: print("🍪 Found YOUTUBE_COOKIES env var, creating cookies file inside container...") try: with open(cookies_path, 'w') as f: f.write(cookies_env) if os.path.exists(cookies_path): print(f" Debug: Cookies file created. Size: {os.path.getsize(cookies_path)} bytes") with open(cookies_path, 'r') as f: content = f.read(100) print(f" Debug: First 100 chars of cookie file: {content}") except Exception as e: print(f"⚠️ Failed to write cookies file: {e}") cookies_path = None else: cookies_path = None print("⚠️ YOUTUBE_COOKIES env var not found.") # Optional HTTP proxy. Set PROXY_URL to route downloads through it; unset # (self-host) goes direct as before. _proxy = os.environ.get("PROXY_URL", "").strip() or None if _proxy: print("🌐 Using proxy for download.") # Two download strategies, tried in order so a break in the HD path degrades # gracefully instead of failing the whole job: an HD attempt first, then a # conservative fallback (also the only strategy for self-host). _bgutil_http = os.environ.get("BGUTIL_BASE_URL", "").strip() _bgutil_script = os.environ.get("BGUTIL_SCRIPT_PATH", "").strip() if _bgutil_http: hd_args = {'youtubepot-bgutilhttp': {'base_url': [_bgutil_http]}} elif _bgutil_script: hd_args = {'youtubepot-bgutilscript': {'script_path': [_bgutil_script]}} else: hd_args = None fallback_args = { 'youtube': { 'player_client': ['tv_embed', 'android', 'mweb', 'web'], 'player_skip': ['webpage', 'configs'], } } # Cap at 720p when using a paid proxy (bandwidth cost); direct keeps best. if _proxy: hd_fmt = ('bestvideo[vcodec^=avc1][height<=720][ext=mp4]+bestaudio[ext=m4a]/' 'bestvideo[vcodec^=avc1][height<=720]+bestaudio/best[height<=720][ext=mp4]/best[height<=720]/best') else: hd_fmt = 'bestvideo[vcodec^=avc1][ext=mp4]+bestaudio[ext=m4a]/bestvideo[vcodec^=avc1]+bestaudio/best[ext=mp4]/best' fallback_fmt = 'best[ext=mp4]/best' def _base_opts(extractor_args): return { 'quiet': False, 'verbose': True, 'no_warnings': False, 'cookiefile': cookies_path if cookies_path else None, 'proxy': _proxy, 'socket_timeout': 30, 'retries': 10, 'fragment_retries': 10, 'nocheckcertificate': True, 'cachedir': False, 'extractor_args': extractor_args, 'http_headers': { 'User-Agent': ( 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' ), }, } def _attempt(extractor_args, fmt): with yt_dlp.YoutubeDL(_base_opts(extractor_args)) as ydl: info = ydl.extract_info(url, download=False) sanitized = sanitize_filename(info.get('title', 'youtube_video')) expected = os.path.join(output_dir, f'{sanitized}.mp4') if os.path.exists(expected): os.remove(expected) dl_opts = { **_base_opts(extractor_args), 'format': fmt, 'outtmpl': os.path.join(output_dir, f'{sanitized}.%(ext)s'), 'merge_output_format': 'mp4', 'overwrites': True, } with yt_dlp.YoutubeDL(dl_opts) as ydl: ydl.download([url]) return sanitized attempts = ([('HD', hd_args, hd_fmt)] if hd_args else []) + [('fallback', fallback_args, fallback_fmt)] sanitized_title = None last_err = None for label, ea, fmt in attempts: try: print(f"📥 Download attempt: {label}") sanitized_title = _attempt(ea, fmt) print(f"✅ Download succeeded ({label}).") break except Exception as e: last_err = e print(f"⚠️ Download attempt '{label}' failed: {str(e)[:200]}") if sanitized_title is None: import sys error_msg = f""" ❌ ================================================================= ❌ ❌ FATAL ERROR: YOUTUBE DOWNLOAD FAILED (all strategies) ❌ ================================================================= ❌ REASON: YouTube blocked the request or the download tooling is out of date. 👇 SOLUTION FOR USER: download the video manually and use the 'Upload Video' tab. Technical Details: {str(last_err)} """ print(error_msg, file=sys.stdout) print(error_msg, file=sys.stderr) sys.stdout.flush(); sys.stderr.flush() time.sleep(0.5) raise last_err downloaded_file = os.path.join(output_dir, f'{sanitized_title}.mp4') if not os.path.exists(downloaded_file): for f in os.listdir(output_dir): if f.startswith(sanitized_title) and f.endswith('.mp4'): downloaded_file = os.path.join(output_dir, f) break print(f"✅ Video downloaded in {time.time() - step_start_time:.2f}s: {downloaded_file}") return downloaded_file, sanitized_title def finalize_clip_passthrough(input_video, final_output_video): """Keep the clip's native framing (for horizontal/16:9 output): just re-encode with faststart, no reframing/cropping.""" if os.path.exists(final_output_video): os.remove(final_output_video) print(f"🎬 Passthrough (native framing): {input_video}") cmd = [ 'ffmpeg', '-y', '-i', input_video, '-c:v', 'libx264', '-preset', 'medium', '-crf', '18', '-c:a', 'aac', '-movflags', '+faststart', final_output_video, ] subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=1800) print(f"✅ Clip saved to {final_output_video}") return True def render_clip(input_video, final_output_video, output_format="auto"): """Route a cut clip through the right renderer for the chosen output format. vertical/auto -> 9:16 reframe, square -> 1:1 reframe, horizontal -> keep.""" if output_format == "horizontal": return finalize_clip_passthrough(input_video, final_output_video) aspect = 1.0 if output_format == "square" else ASPECT_RATIO return process_video_to_vertical(input_video, final_output_video, aspect_ratio=aspect) def process_video_to_vertical(input_video, final_output_video, aspect_ratio=ASPECT_RATIO): """ Core logic to reframe a horizontal video to a target aspect ratio using scene detection and Active Speaker Tracking (MediaPipe). aspect_ratio: width/height of the output (9/16 vertical, 1.0 square). """ script_start_time = time.time() # Define temporary file paths based on the output name base_name = os.path.splitext(final_output_video)[0] temp_video_output = f"{base_name}_temp_video.mp4" temp_audio_output = f"{base_name}_temp_audio.aac" # Clean up previous temp files if they exist if os.path.exists(temp_video_output): os.remove(temp_video_output) if os.path.exists(temp_audio_output): os.remove(temp_audio_output) if os.path.exists(final_output_video): os.remove(final_output_video) print(f"🎬 Processing clip: {input_video}") print(" Step 1: Detecting scenes...") scenes, fps = detect_scenes(input_video) if not scenes: print(" ❌ No scenes were detected. Using full video as one scene.") # If scene detection fails or finds nothing, treat whole video as one scene cap = cv2.VideoCapture(input_video) total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) cap.release() from scenedetect import FrameTimecode scenes = [(FrameTimecode(0, fps), FrameTimecode(total_frames, fps))] print(f" ✅ Found {len(scenes)} scenes.") print("\n 🧠 Step 2: Preparing Active Tracking...") original_width, original_height = get_video_resolution(input_video) OUTPUT_HEIGHT = original_height OUTPUT_WIDTH = int(OUTPUT_HEIGHT * aspect_ratio) # Never ask for a crop wider than the source; shrink height to fit instead. if OUTPUT_WIDTH > original_width: OUTPUT_WIDTH = original_width OUTPUT_HEIGHT = int(OUTPUT_WIDTH / aspect_ratio) if OUTPUT_WIDTH % 2 != 0: OUTPUT_WIDTH += 1 if OUTPUT_HEIGHT % 2 != 0: OUTPUT_HEIGHT += 1 # Initialize Cameraman cameraman = SmoothedCameraman(OUTPUT_WIDTH, OUTPUT_HEIGHT, original_width, original_height, aspect_ratio=aspect_ratio) # --- New Strategy: Per-Scene Analysis --- print("\n 🤖 Step 3: Analyzing Scenes for Strategy (Single vs Group)...") scene_strategies = analyze_scenes_strategy(input_video, scenes) # scene_strategies is a list of 'TRACK' or 'General' corresponding to scenes print("\n ✂️ Step 4: Processing video frames...") command = [ 'ffmpeg', '-y', '-f', 'rawvideo', '-vcodec', 'rawvideo', '-s', f'{OUTPUT_WIDTH}x{OUTPUT_HEIGHT}', '-pix_fmt', 'bgr24', '-r', str(fps), '-i', '-', '-c:v', 'libx264', '-preset', 'fast', '-crf', '18', '-an', temp_video_output ] ffmpeg_process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) cap = cv2.VideoCapture(input_video) total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) frame_number = 0 current_scene_index = 0 # Pre-calculate scene boundaries scene_boundaries = [] for s_start, s_end in scenes: scene_boundaries.append((s_start.get_frames(), s_end.get_frames())) # Global tracker for single-person shots speaker_tracker = SpeakerTracker(cooldown_frames=30) with tqdm(total=total_frames, desc=" Processing", file=sys.stdout) as pbar: while cap.isOpened(): ret, frame = cap.read() if not ret: break # Update Scene Index if current_scene_index < len(scene_boundaries): start_f, end_f = scene_boundaries[current_scene_index] if frame_number >= end_f and current_scene_index < len(scene_boundaries) - 1: current_scene_index += 1 # Determine Strategy for current frame based on scene current_strategy = scene_strategies[current_scene_index] if current_scene_index < len(scene_strategies) else 'TRACK' # Apply Strategy if current_strategy == 'GENERAL': # "Plano General" -> Blur Background + Fit Width output_frame = create_general_frame(frame, OUTPUT_WIDTH, OUTPUT_HEIGHT) # Reset cameraman/tracker so they don't drift while inactive cameraman.current_center_x = original_width / 2 cameraman.target_center_x = original_width / 2 else: # "Single Speaker" -> Track & Crop # Detect every 2nd frame for performance if frame_number % 2 == 0: candidates = detect_face_candidates(frame) target_box = speaker_tracker.get_target(candidates, frame_number, original_width) if target_box: cameraman.update_target(target_box) else: person_box = detect_person_yolo(frame) if person_box: cameraman.update_target(person_box) # Snap camera on scene change to avoid panning from previous scene position is_scene_start = (frame_number == scene_boundaries[current_scene_index][0]) x1, y1, x2, y2 = cameraman.get_crop_box(force_snap=is_scene_start) # Crop if y2 > y1 and x2 > x1: cropped = frame[y1:y2, x1:x2] output_frame = cv2.resize(cropped, (OUTPUT_WIDTH, OUTPUT_HEIGHT), interpolation=cv2.INTER_LANCZOS4) else: output_frame = cv2.resize(frame, (OUTPUT_WIDTH, OUTPUT_HEIGHT), interpolation=cv2.INTER_LANCZOS4) ffmpeg_process.stdin.write(output_frame.tobytes()) frame_number += 1 pbar.update(1) ffmpeg_process.stdin.close() stderr_output = ffmpeg_process.stderr.read().decode() ffmpeg_process.wait() cap.release() if ffmpeg_process.returncode != 0: print("\n ❌ FFmpeg frame processing failed.") print(" Stderr:", stderr_output) return False print("\n 🔊 Step 5: Extracting audio...") audio_extract_command = [ 'ffmpeg', '-y', '-i', input_video, '-vn', '-acodec', 'copy', temp_audio_output ] try: subprocess.run(audio_extract_command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) except subprocess.CalledProcessError: print("\n ❌ Audio extraction failed (maybe no audio?). Proceeding without audio.") pass print("\n ✨ Step 6: Merging...") if os.path.exists(temp_audio_output): merge_command = [ 'ffmpeg', '-y', '-i', temp_video_output, '-i', temp_audio_output, '-c:v', 'copy', '-c:a', 'copy', final_output_video ] else: merge_command = [ 'ffmpeg', '-y', '-i', temp_video_output, '-c:v', 'copy', final_output_video ] try: subprocess.run(merge_command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) print(f" ✅ Clip saved to {final_output_video}") except subprocess.CalledProcessError as e: print("\n ❌ Final merge failed.") print(" Stderr:", e.stderr.decode()) return False # Clean up temp files if os.path.exists(temp_video_output): os.remove(temp_video_output) if os.path.exists(temp_audio_output): os.remove(temp_audio_output) return True def transcribe_video(video_path): print("🎙️ Transcribing video with Faster-Whisper (CPU Optimized)...") from faster_whisper import WhisperModel # "small" is noticeably better than "base" on non-English audio (e.g. German # compound words) at a modest speed cost. Overridable via env for tuning. model_size = os.environ.get("WHISPER_MODEL", "small") device = os.environ.get("WHISPER_DEVICE", "cpu") compute = os.environ.get("WHISPER_COMPUTE", "int8") model = WhisperModel(model_size, device=device, compute_type=compute) # vad_filter drops silence; condition_on_previous_text off avoids repetition # loops / hallucinations across segments. segments, info = model.transcribe( video_path, word_timestamps=True, beam_size=5, vad_filter=True, condition_on_previous_text=False, ) print(f" Detected language '{info.language}' with probability {info.language_probability:.2f}") # Convert to openai-whisper compatible format transcript_segments = [] full_text = "" for segment in segments: # Print progress to keep user informed (and prevent timeouts feeling) print(f" [{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}") seg_dict = { 'text': segment.text, 'start': segment.start, 'end': segment.end, 'words': [] } if segment.words: for word in segment.words: seg_dict['words'].append({ 'word': word.word, 'start': word.start, 'end': word.end, 'probability': word.probability }) transcript_segments.append(seg_dict) full_text += segment.text + " " return { 'text': full_text.strip(), 'segments': transcript_segments, 'language': info.language } def get_viral_clips(transcript_result, video_duration): print("🤖 Analyzing with Gemini...") api_key = os.getenv("GEMINI_API_KEY") if not api_key: print("❌ Error: GEMINI_API_KEY not found in environment variables.") return None client = genai.Client(api_key=api_key) model_name = os.environ.get("GEMINI_MODEL") or 'gemini-3.1-flash-lite' print(f"🤖 Initializing Gemini with model: {model_name}") # Extract words words = [] for segment in transcript_result['segments']: for word in segment.get('words', []): words.append({ 'w': word['word'], 's': word['start'], 'e': word['end'] }) prompt = GEMINI_PROMPT_TEMPLATE.format( video_duration=video_duration, transcript_text=json.dumps(transcript_result['text']), words_json=json.dumps(words) ) try: # Retry transient Gemini failures (503 high demand, 429 quota, 500) # with exponential backoff before giving up (issue #27). max_attempts = 3 response = None # Force valid JSON output. Manual fence-stripping + json.loads breaks on # models that emit trailing text or truncate (e.g. flash-lite), so let # the API guarantee a JSON body. Fall back to a plain call if the SDK # or model rejects the option. try: gen_config = genai_types.GenerateContentConfig(response_mime_type="application/json") except Exception: gen_config = None for attempt in range(1, max_attempts + 1): try: response = client.models.generate_content( model=model_name, contents=prompt, config=gen_config, ) if gen_config is not None else client.models.generate_content( model=model_name, contents=prompt, ) break except Exception as e: msg = str(e) transient = any(tok in msg for tok in ( '503', 'UNAVAILABLE', '429', 'RESOURCE_EXHAUSTED', '500', 'INTERNAL', 'overloaded', 'Deadline' )) if attempt == max_attempts or not transient: raise wait = 5 * (2 ** (attempt - 1)) print(f"⚠️ Gemini transient error (attempt {attempt}/{max_attempts}), retrying in {wait}s: {msg[:150]}") time.sleep(wait) # --- Cost Calculation --- try: usage = response.usage_metadata if usage: # USD per 1M tokens, longest-prefix match on the model name so # env-overridden models still price correctly (ai.google.dev). MODEL_PRICES = { "gemini-3.5-flash": (1.50, 9.00), "gemini-3.1-flash-lite": (0.25, 1.50), "gemini-3-flash-preview": (0.50, 3.00), "gemini-2.5-flash-lite": (0.10, 0.40), "gemini-2.5-flash": (0.30, 2.50), } match = None for key in MODEL_PRICES: if model_name.startswith(key) and (match is None or len(key) > len(match)): match = key input_price_per_million, output_price_per_million = ( MODEL_PRICES[match] if match else (0.50, 3.00) # conservative default ) prompt_tokens = usage.prompt_token_count output_tokens = usage.candidates_token_count input_cost = (prompt_tokens / 1_000_000) * input_price_per_million output_cost = (output_tokens / 1_000_000) * output_price_per_million total_cost = input_cost + output_cost cost_analysis = { "input_tokens": prompt_tokens, "output_tokens": output_tokens, "input_cost": input_cost, "output_cost": output_cost, "total_cost": total_cost, "model": model_name } print(f"💰 Token Usage ({model_name}):") print(f" - Input Tokens: {prompt_tokens} (${input_cost:.6f})") print(f" - Output Tokens: {output_tokens} (${output_cost:.6f})") print(f" - Total Estimated Cost: ${total_cost:.6f}") except Exception as e: print(f"⚠️ Could not calculate cost: {e}") cost_analysis = None # ------------------------ # Clean response if it contains markdown code blocks text = (response.text or "").strip() if text.startswith("```json"): text = text[7:] elif text.startswith("```"): text = text[3:] if text.endswith("```"): text = text[:-3] text = text.strip() try: result_json = json.loads(text) except json.JSONDecodeError: # Rescue: extract the outermost {...} object if the model wrapped # the JSON in prose or appended stray tokens. first, last = text.find('{'), text.rfind('}') if first == -1 or last <= first: raise result_json = json.loads(text[first:last + 1]) if cost_analysis: result_json['cost_analysis'] = cost_analysis return result_json except Exception as e: print(f"❌ Gemini Error: {e}") return None if __name__ == '__main__': parser = argparse.ArgumentParser(description="AutoCrop-Vertical with Viral Clip Detection.") input_group = parser.add_mutually_exclusive_group(required=True) input_group.add_argument('-i', '--input', type=str, help="Path to the input video file.") input_group.add_argument('-u', '--url', type=str, help="YouTube URL to download and process.") parser.add_argument('-o', '--output', type=str, help="Output directory or file (if processing whole video).") parser.add_argument('--keep-original', action='store_true', help="Keep the downloaded YouTube video.") parser.add_argument('--skip-analysis', action='store_true', help="Skip AI analysis and convert the whole video.") parser.add_argument('--format', type=str, default="auto", choices=["auto", "vertical", "horizontal", "square"], help="Output aspect: vertical/auto (9:16), horizontal (keep 16:9), square (1:1).") args = parser.parse_args() output_format = args.format script_start_time = time.time() def _ensure_dir(path: str) -> str: """Create directory if missing and return the same path.""" if path: os.makedirs(path, exist_ok=True) return path # 1. Get Input Video if args.url: # For multi-clip runs, treat --output as an OUTPUT DIRECTORY (create it if needed). # For whole-video runs (--skip-analysis), --output can be a file path. if args.output and not args.skip_analysis: output_dir = _ensure_dir(args.output) else: # If output is a directory, use it; if it's a filename, use its directory; else default "." if args.output and os.path.isdir(args.output): output_dir = args.output elif args.output and not os.path.isdir(args.output): output_dir = os.path.dirname(args.output) or "." else: output_dir = "." input_video, video_title = download_youtube_video(args.url, output_dir) else: input_video = args.input video_title = os.path.splitext(os.path.basename(input_video))[0] if args.output and not args.skip_analysis: # For multi-clip runs, treat --output as an OUTPUT DIRECTORY (create it if needed). output_dir = _ensure_dir(args.output) else: # If output is a directory, use it; if it's a filename, use its directory; else default to input dir. if args.output and os.path.isdir(args.output): output_dir = args.output elif args.output and not os.path.isdir(args.output): output_dir = os.path.dirname(args.output) or os.path.dirname(input_video) else: output_dir = os.path.dirname(input_video) if not os.path.exists(input_video): print(f"❌ Input file not found: {input_video}") exit(1) # 2. Decision: Analyze clips or process whole? if args.skip_analysis: print("⏩ Skipping analysis, processing entire video...") output_file = args.output if args.output else os.path.join(output_dir, f"{video_title}_vertical.mp4") render_clip(input_video, output_file, output_format) else: # 3. Transcribe transcript = transcribe_video(input_video) # Get duration cap = cv2.VideoCapture(input_video) fps = cap.get(cv2.CAP_PROP_FPS) frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) duration = frame_count / fps cap.release() # 4. Gemini Analysis clips_data = get_viral_clips(transcript, duration) if not clips_data or 'shorts' not in clips_data: print("❌ Failed to identify clips. Converting whole video as fallback.") output_file = os.path.join(output_dir, f"{video_title}_vertical.mp4") render_clip(input_video, output_file, output_format) else: print(f"🔥 Found {len(clips_data['shorts'])} viral clips!") # Save metadata clips_data['transcript'] = transcript # Save full transcript for subtitles metadata_file = os.path.join(output_dir, f"{video_title}_metadata.json") with open(metadata_file, 'w') as f: json.dump(clips_data, f, indent=2) print(f" Saved metadata to {metadata_file}") # 5. Process each clip for i, clip in enumerate(clips_data['shorts']): start = clip['start'] end = clip['end'] print(f"\n🎬 Processing Clip {i+1}: {start}s - {end}s") print(f" Title: {clip.get('video_title_for_youtube_short', 'No Title')}") # Cut clip clip_filename = f"{video_title}_clip_{i+1}.mp4" clip_temp_path = os.path.join(output_dir, f"temp_{clip_filename}") clip_final_path = os.path.join(output_dir, clip_filename) # ffmpeg cut # Using re-encoding for precision as requested by strict seconds cut_command = [ 'ffmpeg', '-y', '-ss', str(start), '-to', str(end), '-i', input_video, '-c:v', 'libx264', '-crf', '18', '-preset', 'fast', '-c:a', 'aac', clip_temp_path ] subprocess.run(cut_command, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) # Process vertical success = render_clip(clip_temp_path, clip_final_path, output_format) if success: print(f" ✅ Clip {i+1} ready: {clip_final_path}") # Clean up temp cut if os.path.exists(clip_temp_path): os.remove(clip_temp_path) # Clean up original if requested if args.url and not args.keep_original and os.path.exists(input_video): os.remove(input_video) print(f"🗑️ Cleaned up downloaded video.") total_time = time.time() - script_start_time print(f"\n⏱️ Total execution time: {total_time:.2f}s")