improve video preview

This commit is contained in:
juancarlos.cavero 2025-12-21 01:59:15 +01:00
parent 5be810d606
commit 3c92d6ce67
8 changed files with 574 additions and 156 deletions

View file

@ -0,0 +1,3 @@
{
"cpu_info": "aarch64"
}

View file

@ -0,0 +1,21 @@
{
"settings_version": "0.0.6",
"datasets_dir": "/app/datasets",
"weights_dir": "weights",
"runs_dir": "runs",
"uuid": "e9f2075fc19916239a376a302c9631266f63b2fead1a3a29759f6048a7455a2e",
"sync": true,
"api_key": "",
"openai_api_key": "",
"clearml": true,
"comet": true,
"dvc": true,
"hub": true,
"mlflow": true,
"neptune": true,
"raytune": true,
"tensorboard": false,
"wandb": false,
"vscode_msg": true,
"openvino_msg": true
}

View file

@ -22,3 +22,5 @@ dist-ssr
*.njsproj
*.sln
*.sw?
.config
.config/*

View file

@ -143,6 +143,21 @@ function App() {
const [logsVisible, setLogsVisible] = useState(true);
const [processingMedia, setProcessingMedia] = useState(null);
const [activeTab, setActiveTab] = useState('dashboard'); // dashboard, settings
// Sync state for original video playback
const [syncedTime, setSyncedTime] = useState(0);
const [isSyncedPlaying, setIsSyncedPlaying] = useState(false);
const [syncTrigger, setSyncTrigger] = useState(0);
const handleClipPlay = (startTime) => {
setSyncedTime(startTime);
setIsSyncedPlaying(true);
setSyncTrigger(prev => prev + 1);
};
const handleClipPause = () => {
setIsSyncedPlaying(false);
};
useEffect(() => {
// Encrypt Gemini Key too for consistency if desired, but user asked specifically about Social integration not saving well.
@ -461,7 +476,15 @@ function App() {
</div>
{/* Video Preview */}
{processingMedia && <ProcessingAnimation media={processingMedia} isComplete={status === 'complete'} />}
{processingMedia && (
<ProcessingAnimation
media={processingMedia}
isComplete={status === 'complete'}
syncedTime={syncedTime}
isSyncedPlaying={isSyncedPlaying}
syncTrigger={syncTrigger}
/>
)}
{/* Logs Terminal */}
<div className={`bg-[#0c0c0e] rounded-xl border border-white/10 overflow-hidden flex flex-col transition-all duration-500 ${status === 'complete' ? 'h-32 min-h-0 opacity-50 hover:opacity-100' : 'flex-1 min-h-[200px]'}`}>
@ -517,6 +540,8 @@ function App() {
jobId={jobId}
uploadPostKey={uploadPostKey}
uploadUserId={uploadUserId}
onPlay={(time) => handleClipPlay(time)}
onPause={handleClipPause}
/>
))}
</div>

View file

@ -1,9 +1,11 @@
import React, { useEffect, useState } from 'react';
import { Scan, Scissors, Activity, Radio, CheckCircle } from 'lucide-react';
import React, { useEffect, useState, useRef } from 'react';
import { Scan, Scissors, Activity, Radio, CheckCircle, Play } from 'lucide-react';
const ProcessingAnimation = ({ media, isComplete }) => {
const ProcessingAnimation = ({ media, isComplete, syncedTime, isSyncedPlaying, syncTrigger }) => {
const [videoSrc, setVideoSrc] = useState(null);
const [isYouTube, setIsYouTube] = useState(false);
const videoRef = useRef(null);
const iframeRef = useRef(null);
useEffect(() => {
if (!media) return;
@ -19,26 +21,141 @@ const ProcessingAnimation = ({ media, isComplete }) => {
}
}, [media]);
// Handle Sync Playback for Local Video
useEffect(() => {
if (!isYouTube && videoRef.current) {
if (isSyncedPlaying) {
// Sync Mode: Seek to time and Play
videoRef.current.currentTime = syncedTime;
videoRef.current.play().catch(e => console.log("Auto-play prevented", e));
videoRef.current.loop = false;
videoRef.current.muted = true; // Keep muted to avoid double audio with clip
} else {
// Stop Sync: Pause
videoRef.current.pause();
// If Analysis Complete and we just stopped syncing (paused clip), we might want to return to ambient loop?
// User issue: "ahora el video loop original que sale oscurito ahora bien pero es una imagen estatica no se está reproduciendo el video en bucle como antes"
// This means when NOT synced, it should loop.
// HOWEVER, previously user asked: "si pauso el video preview y lo reanudo el video original se vuelve al princpio en vez de contnuar igual"
// This implies:
// 1. If I PAUSE the clip -> Left video should PAUSE (static image) so it can resume.
// 2. If I STOP (or it finishes? or just idle state?) -> It should LOOP.
// The problem is we only have onPause from the clip.
// Maybe we need to distinguish "Pause" vs "Idle/Stop".
// But currently we just get `isSyncedPlaying = false`.
// If the user wants "resume from where left off", it MUST be static (paused).
// If the user wants "loop when not playing", it MUST play.
// These are contradictory for the "Paused" state.
// BUT, maybe the "loop original" refers to the initial state BEFORE any clip is played?
// OR when the clip finishes?
// Let's look at the logic:
// isSyncedPlaying is true ONLY when a clip is playing.
// When clip pauses, isSyncedPlaying becomes false.
// If we want it to loop "como antes", we should set it to loop.
// But then we lose the "resume" position because it starts looping.
// Unless... we only loop if we haven't started syncing yet? Or if explicitly reset?
// Wait, the user said: "ahora el video loop original que sale oscurito ahora bien pero es una imagen estatica"
// This likely refers to the state AFTER analysis is complete but BEFORE (or after) playing a clip.
// If I haven't touched a clip yet, `isSyncedPlaying` is false.
// In that case, it SHOULD be looping.
// My previous change removed the "else { play loop }" block entirely.
// I need to restore the loop for the IDLE state, but keep the PAUSE for the "paused clip" state?
// That requires knowing WHY `isSyncedPlaying` is false.
// Actually, if `isSyncedPlaying` is false, it means no clip is controlling it.
// If I want it to loop in the background, I can just let it loop.
// BUT if I play a clip later, it will jump to the sync time anyway (handled by the `if (isSyncedPlaying)` block).
// The only issue is if I PAUSE the clip, `isSyncedPlaying` becomes false, and if I immediately start looping,
// visually it might jump or start moving when it should be "paused".
// Let's try this:
// If `syncedTime` is 0 (or we track if we ever started syncing?), we loop.
// But `syncedTime` updates on play.
// Alternative interpretation: The user sees it static because I removed `videoRef.current.play()` in the else block.
// If I put it back, it fixes the "loop" issue.
// Does it break the "resume" issue?
// "si pauso el video preview y lo reanudo el video original se vuelve al princpio en vez de contnuar igual"
// If I pause the clip -> `isSyncedPlaying` = false.
// If logic says -> Loop from 0.
// Then I resume -> `isSyncedPlaying` = true -> Jump to `syncedTime`.
// This actually SHOULD work fine for "resume", because `syncedTime` comes from the clip's current time.
// The only visual glitch is that while paused, the left video is looping instead of frozen on the frame.
// If the user accepts that "Paused Clip" = "Background Loop", then we are good.
// If the user wants "Paused Clip" = "Frozen Frame" AND "Idle" = "Background Loop", we need more state.
// But typically "Idle" implies we aren't focusing on a clip.
// Let's restore the loop behavior because "video loop original... es una imagen estatica" sounds like a bug to them.
if (isComplete) {
videoRef.current.loop = true;
videoRef.current.play().catch(e => console.log("Ambient play prevented", e));
}
}
}
}, [syncedTime, isSyncedPlaying, isYouTube, isComplete, syncTrigger]);
// Handle Sync Playback for YouTube (Basic Iframe Control via PostMessage)
useEffect(() => {
if (isYouTube && iframeRef.current && videoSrc) {
const iframeWindow = iframeRef.current.contentWindow;
if (isSyncedPlaying) {
// Seek and Play
iframeWindow.postMessage(JSON.stringify({ event: 'command', func: 'seekTo', args: [syncedTime, true] }), '*');
iframeWindow.postMessage(JSON.stringify({ event: 'command', func: 'playVideo', args: [] }), '*');
} else {
// Pause
// iframeWindow.postMessage(JSON.stringify({ event: 'command', func: 'pauseVideo', args: [] }), '*'); // Removed pause to allow loop if needed, but YT embeds are tricky with custom loops via API.
// For now, let's just pause YouTube as complex looping is harder without state.
iframeWindow.postMessage(JSON.stringify({ event: 'command', func: 'pauseVideo', args: [] }), '*');
}
}
}, [syncedTime, isSyncedPlaying, isYouTube, videoSrc, syncTrigger]);
const getYouTubeId = (url) => {
const regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/;
const match = url.match(regExp);
return (match && match[2].length === 11) ? match[2] : null;
};
const containerClasses = `relative w-full aspect-video rounded-xl overflow-hidden bg-black border border-white/10 shadow-2xl mb-8 group animate-[fadeIn_0.5s_ease-out] transition-all duration-500
${isComplete && !isSyncedPlaying ? 'grayscale brightness-50' : ''}
${isSyncedPlaying ? 'ring-2 ring-primary ring-offset-2 ring-offset-black shadow-primary/20' : ''}`;
const getVideoOpacityClass = () => {
if (isSyncedPlaying) return 'opacity-100'; // Playing: Full visibility
if (isComplete) return 'opacity-30'; // Idle Result: Darker
return 'opacity-40 grayscale group-hover:grayscale-0'; // Processing: Dark + Grayscale effect
};
return (
<div className={`relative w-full aspect-video rounded-xl overflow-hidden bg-black/40 border border-white/10 shadow-2xl mb-8 group animate-[fadeIn_0.5s_ease-out] transition-all duration-500 ${isComplete ? 'grayscale brightness-50' : ''}`}>
<div className={containerClasses}>
{/* Video Layer */}
<div className={`absolute inset-0 transition-all duration-700 ${isComplete ? 'opacity-30' : 'opacity-40 grayscale group-hover:grayscale-0'}`}>
<div className={`absolute inset-0 transition-all duration-700 ${getVideoOpacityClass()}`}>
{isYouTube && videoSrc ? (
<iframe
className="w-full h-full pointer-events-none scale-110"
src={`https://www.youtube.com/embed/${videoSrc}?autoplay=1&mute=1&controls=0&loop=1&playlist=${videoSrc}&modestbranding=1&showinfo=0&rel=0`}
ref={iframeRef}
className={`w-full h-full ${isSyncedPlaying ? '' : 'pointer-events-none scale-110'}`}
// Add enablejsapi=1 for postMessage control
src={`https://www.youtube.com/embed/${videoSrc}?autoplay=1&mute=1&controls=0&loop=1&playlist=${videoSrc}&modestbranding=1&showinfo=0&rel=0&enablejsapi=1`}
title="Processing Video"
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
/>
) : videoSrc ? (
<video
ref={videoRef}
src={videoSrc}
className="w-full h-full object-cover"
autoPlay
@ -53,58 +170,59 @@ const ProcessingAnimation = ({ media, isComplete }) => {
)}
</div>
{!isComplete && (
{/* Overlays - Hide when synced playing so user sees clean video */}
{!isSyncedPlaying && !isComplete && (
<>
{/* Grid Overlay */}
<div className="absolute inset-0 bg-[linear-gradient(rgba(255,255,255,0.03)_1px,transparent_1px),linear-gradient(90deg,rgba(255,255,255,0.03)_1px,transparent_1px)] bg-[size:40px_40px] z-10 pointer-events-none"></div>
{/* Scanner Animation Line */}
<div className="absolute left-0 w-full h-[2px] bg-primary shadow-[0_0_15px_2px_rgba(59,130,246,0.5)] animate-[scan_2.5s_linear_infinite] z-20 pointer-events-none"></div>
{/* Scanner Gradient Band */}
<div className="absolute left-0 w-full h-[15%] bg-gradient-to-b from-primary/0 via-primary/5 to-primary/0 animate-[scan-overlay_2.5s_linear_infinite] z-10 pointer-events-none"></div>
</>
)}
{/* HUD Elements */}
<div className={`absolute top-4 left-4 z-30 flex items-center gap-2 px-3 py-1.5 backdrop-blur-md rounded-lg border text-xs font-mono font-bold uppercase transition-all duration-500 ${isComplete ? 'bg-green-500/10 border-green-500/20 text-green-400' : 'bg-black/60 border-primary/30 text-primary animate-pulse'}`}>
{isComplete ? (
<>
<CheckCircle size={14} /> Analysis Complete
</>
) : (
<>
<Scan size={14} /> Scanning Content...
</>
)}
</div>
{/* HUD Elements - Hide when synced playing */}
{!isSyncedPlaying && (
<div className={`absolute top-4 left-4 z-30 flex items-center gap-2 px-3 py-1.5 backdrop-blur-md rounded-lg border text-xs font-mono font-bold uppercase transition-all duration-500 ${isComplete ? 'bg-green-500/10 border-green-500/20 text-green-400' : 'bg-black/60 border-primary/30 text-primary animate-pulse'}`}>
{isComplete ? (
<>
<CheckCircle size={14} /> Analysis Complete
</>
) : (
<>
<Scan size={14} /> Scanning Content...
</>
)}
</div>
)}
{!isComplete && (
{!isSyncedPlaying && !isComplete && (
<div className="absolute top-4 right-4 z-30 flex items-center gap-2 px-3 py-1.5 bg-black/60 backdrop-blur-md rounded-lg border border-white/10 text-white/50 text-[10px] font-mono">
AI_MODEL: GEMINI-2.5-PRO
</div>
)}
{/* Visual Flair: Simulated Crop Marks */}
{!isComplete && (
{/* Visual Flair */}
{!isSyncedPlaying && !isComplete && (
<div className="absolute inset-0 pointer-events-none z-20 overflow-hidden">
{/* Vertical crop lines showing "potential vertical format" */}
<div className="absolute top-0 bottom-0 left-[35%] w-[1px] bg-yellow-500/20 border-r border-dashed border-yellow-500/40"></div>
<div className="absolute top-0 bottom-0 right-[35%] w-[1px] bg-yellow-500/20 border-l border-dashed border-yellow-500/40"></div>
{/* Center focus indicator */}
<div className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-12 h-12 border border-white/20 rounded-full flex items-center justify-center">
<div className="w-1 h-1 bg-red-500 rounded-full animate-ping"></div>
</div>
<div className="absolute bottom-1/3 left-1/2 -translate-x-1/2 flex flex-col items-center justify-center gap-2 opacity-60">
<Scissors size={24} className="text-white/20" />
</div>
</div>
)}
{/* Synced Playing Indicator */}
{isSyncedPlaying && (
<div className="absolute top-4 right-4 z-30 flex items-center gap-2 px-3 py-1.5 bg-red-600/90 backdrop-blur text-white rounded-lg shadow-lg animate-pulse font-bold text-[10px] uppercase tracking-wider border border-white/20">
<Activity size={12} /> Live Sync
</div>
)}
{/* Bottom Info Bar */}
{!isComplete && (
{!isSyncedPlaying && !isComplete && (
<div className="absolute bottom-0 left-0 right-0 p-4 bg-gradient-to-t from-black/90 to-transparent z-30 flex justify-between items-end border-t border-white/5">
<div className="font-mono text-[10px] text-primary/80 space-y-1">
<div className="flex items-center gap-2"><Activity size={10} className="animate-bounce" /> > ANALYSIS_THREAD_01: ACTIVE</div>
@ -123,4 +241,4 @@ const ProcessingAnimation = ({ media, isComplete }) => {
);
};
export default ProcessingAnimation;
export default ProcessingAnimation;

View file

@ -2,8 +2,10 @@ import React, { useState } from 'react';
import { Download, Share2, Instagram, Youtube, Video, CheckCircle, AlertCircle, X, Loader2, Copy } from 'lucide-react';
import { getApiUrl } from '../config';
export default function ResultCard({ clip, index, jobId, uploadPostKey, uploadUserId }) {
export default function ResultCard({ clip, index, jobId, uploadPostKey, uploadUserId, onPlay, onPause }) {
const [showModal, setShowModal] = useState(false);
const videoRef = React.useRef(null);
const [platforms, setPlatforms] = useState({
tiktok: true,
instagram: true,
@ -70,11 +72,22 @@ export default function ResultCard({ clip, index, jobId, uploadPostKey, uploadUs
{/* Left: Video Preview (Responsive Width) */}
<div className="w-full md:w-[180px] lg:w-[200px] bg-black relative shrink-0 aspect-[9/16] md:aspect-auto">
<video
ref={videoRef}
src={videoUrl}
controls
className="w-full h-full object-cover"
loop
playsInline
onPlay={() => {
const currentTime = videoRef.current ? videoRef.current.currentTime : 0;
onPlay && onPlay(clip.start + currentTime);
}}
onPause={() => onPause && onPause()}
onEnded={() => {
if (videoRef.current) {
videoRef.current.currentTime = 0;
videoRef.current.play();
}
}}
/>
<div className="absolute top-3 left-3 flex gap-2">
<span className="bg-black/60 backdrop-blur-md text-white text-[10px] font-bold px-2 py-1 rounded-md border border-white/10 uppercase tracking-wide">

473
main.py
View file

@ -12,13 +12,15 @@ import os
import numpy as np
from tqdm import tqdm
import yt_dlp
import yt_dlp
import mediapipe as mp
# import whisper (replaced by faster_whisper inside function)
from google import genai
from google import genai
from dotenv import load_dotenv
import json
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module='google.protobuf')
# Load environment variables
load_dotenv()
@ -63,58 +65,314 @@ OUTPUT — RETURN ONLY VALID JSON (no markdown, no comments). Order clips by pre
}}
"""
# Load the YOLO model once
# Load the YOLO model once (Keep for backup or scene analysis if needed)
model = YOLO('yolov8n.pt')
# Load the Haar Cascade for face detection once
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# --- MediaPipe Setup ---
# Use Face Mesh for more accurate detection and mouth movement tracking
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(
max_num_faces=5,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
def analyze_scene_content(video_path, scene_start_time, scene_end_time):
class SmoothedCameraman:
"""
Analyzes the middle frame of a scene to detect people and faces.
Handles smooth camera movement to track a subject without jitter.
Includes deadzone and dynamic smoothing.
"""
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"Error: Could not open video {video_path}")
def __init__(self, output_width, output_height, video_width, video_height, smoothing_factor=0.1):
self.output_width = output_width
self.output_height = output_height
self.video_width = video_width
self.video_height = video_height
self.smoothing_factor = smoothing_factor
self.current_center_x = video_width / 2
self.target_center_x = video_width / 2
# Deadzone: Ignore movements smaller than this (relative to video width)
self.deadzone = video_width * 0.03
# Calculate crop dimensions once
self.crop_height = video_height
self.crop_width = int(self.crop_height * ASPECT_RATIO)
if self.crop_width > video_width:
# Handle case where original video is already narrow/vertical
self.crop_width = video_width
self.crop_height = int(self.crop_width / ASPECT_RATIO)
def update_target(self, face_box):
"""
Updates the target center based on detected face/person.
face_box: [x, y, w, h]
"""
if face_box:
x, y, w, h = face_box
new_target = x + w / 2
# Deadzone logic: Only update if the target has moved significantly
# This prevents jitter when the person is stationary but detection wobbles
if abs(new_target - self.target_center_x) > self.deadzone:
self.target_center_x = new_target
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:
# Dynamic Smoothing
diff = self.target_center_x - self.current_center_x
distance = abs(diff)
# Adaptive speed:
# - If very far (scene change or fast move), move FAST.
# - If close (minor adjustment), move SLOW to be smooth.
if distance > self.video_width * 0.15:
speed = 0.15 # Catch up moderately fast, but not instant (reduced from 0.3)
elif distance < self.video_width * 0.05:
speed = 0.05 # Very smooth for small adjustments
else:
speed = self.smoothing_factor
self.current_center_x += diff * speed
# Clamp center to keep crop within bounds
half_crop = self.crop_width / 2
# Ensure we don't go out of left/right bounds
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)
# Final safety check
x1 = max(0, x1)
x2 = min(self.video_width, x2)
# For height, we usually take the full height unless we want to zoom
# Current logic: full height
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, 'is_speaking': bool}
"""
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.2 # Max movement distance
# Try to match with known faces seen recently
for kf in self.known_faces:
if frame_number - kf['last_frame'] > 60: # Forgot faces older than 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
# Remove old entry for this ID if exists
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'],
'is_speaking': face['is_speaking']
})
# 2. Update Scores with decay
# Decay old scores
for pid in list(self.speaker_scores.keys()):
self.speaker_scores[pid] *= 0.9 # Fast decay
if self.speaker_scores[pid] < 0.1:
del self.speaker_scores[pid]
# Add new scores
for cand in current_candidates:
pid = cand['id']
# Boost score if speaking
score_boost = 20.0 if cand['is_speaking'] else 1.0
# Boost score if large (main subject)
# Normalize area (assuming 720p or 1080p, say max area ~ 1M pixels)
# Actually score is already area-based in previous function, so just use it
raw_score = cand['score'] / (width * width * 0.1) # Normalize roughly
self.speaker_scores[pid] = self.speaker_scores.get(pid, 0) + (raw_score * score_boost)
# 3. Determine Best Speaker
if not current_candidates:
return None # No one found
# Sort candidates by accumulated score
best_candidate = None
max_score = -1
for cand in current_candidates:
pid = cand['id']
total_score = self.speaker_scores.get(pid, 0)
# Hysteresis: Bonus for current active speaker
if pid == self.active_speaker_id:
total_score *= 2.0 # Hard stickiness
if total_score > max_score:
max_score = total_score
best_candidate = cand
# 4. Decide Switch
if best_candidate:
target_id = best_candidate['id']
# If it's the same person, just update
if target_id == self.active_speaker_id:
self.locked_counter += 1
return best_candidate['box']
# If it's a NEW person
# Check cooldown
if frame_number - self.last_switch_frame < self.switch_cooldown:
# Too soon to switch, stay with old one if possible
# Find old one in current candidates
old_cand = next((c for c in current_candidates if c['id'] == self.active_speaker_id), None)
if old_cand:
return old_cand['box']
# Check stabilization (do we really want to switch?)
# If the new score is overwhelmingly high (someone started shouting/talking clearly), switch faster
# But normally wait a bit?
# For now, let's just trust the accumulated score buffer + hysteresis
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 with their raw scores and speaking status.
"""
height, width, _ = frame.shape
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = face_mesh.process(rgb_frame)
candidates = []
if not results.multi_face_landmarks:
return []
for face_landmarks in results.multi_face_landmarks:
# Bounding Box
x_min, y_min = width, height
x_max, y_max = 0, 0
# Optimization: Use only silhouette landmarks (every 10th or specific indices)
# Full mesh is 468 points.
for i in range(0, 468, 10):
lm = face_landmarks.landmark[i]
x, y = int(lm.x * width), int(lm.y * height)
if x < x_min: x_min = x
if x > x_max: x_max = x
if y < y_min: y_min = y
if y > y_max: y_max = y
w = x_max - x_min
h = y_max - y_min
# Mouth Openness
upper_lip = face_landmarks.landmark[13]
lower_lip = face_landmarks.landmark[14]
mouth_dist = abs(upper_lip.y - lower_lip.y) * height
mouth_ratio = mouth_dist / h if h > 0 else 0
# Heuristic: Threshold for speaking
is_speaking = mouth_ratio > 0.05 # Increased threshold to reduce false positives
candidates.append({
'box': [x_min, y_min, w, h],
'score': w * h, # Area as base score
'is_speaking': is_speaking
})
return candidates
fps = cap.get(cv2.CAP_PROP_FPS)
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
start_frame = scene_start_time.get_frames()
end_frame = scene_end_time.get_frames()
middle_frame_number = int(start_frame + (end_frame - start_frame) / 2)
if not results:
return None
best_box = None
max_area = 0
cap.set(cv2.CAP_PROP_POS_FRAMES, middle_frame_number)
ret, frame = cap.read()
if not ret:
cap.release()
return []
results = model([frame], verbose=False)
detected_objects = []
for result in results:
boxes = result.boxes
for box in boxes:
if box.cls[0] == 0:
x1, y1, x2, y2 = [int(i) for i in box.xyxy[0]]
person_box = [x1, y1, x2, y2]
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]
person_roi_gray = cv2.cvtColor(frame[y1:y2, x1:x2], cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(person_roi_gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
face_box = None
if len(faces) > 0:
fx, fy, fw, fh = faces[0]
face_box = [x1 + fx, y1 + fy, x1 + fx + fw, y1 + fy + fh]
detected_objects.append({'person_box': person_box, 'face_box': face_box})
cap.release()
return detected_objects
return best_box
def detect_scenes(video_path):
video_manager = VideoManager([video_path])
@ -128,47 +386,6 @@ def detect_scenes(video_path):
video_manager.release()
return scene_list, fps
def get_enclosing_box(boxes):
if not boxes:
return None
min_x = min(box[0] for box in boxes)
min_y = min(box[1] for box in boxes)
max_x = max(box[2] for box in boxes)
max_y = max(box[3] for box in boxes)
return [min_x, min_y, max_x, max_y]
def decide_cropping_strategy(scene_analysis, frame_height):
num_people = len(scene_analysis)
if num_people == 0:
return 'LETTERBOX', None
if num_people == 1:
target_box = scene_analysis[0]['face_box'] or scene_analysis[0]['person_box']
return 'TRACK', target_box
person_boxes = [obj['person_box'] for obj in scene_analysis]
group_box = get_enclosing_box(person_boxes)
group_width = group_box[2] - group_box[0]
max_width_for_crop = frame_height * ASPECT_RATIO
if group_width < max_width_for_crop:
return 'TRACK', group_box
else:
return 'LETTERBOX', None
def calculate_crop_box(target_box, frame_width, frame_height):
target_center_x = (target_box[0] + target_box[2]) / 2
crop_height = frame_height
crop_width = int(crop_height * ASPECT_RATIO)
x1 = int(target_center_x - crop_width / 2)
y1 = 0
x2 = int(target_center_x + crop_width / 2)
y2 = frame_height
if x1 < 0:
x1 = 0
x2 = crop_width
if x2 > frame_width:
x2 = frame_width
x1 = frame_width - crop_width
return x1, y1, x2, y2
def get_video_resolution(video_path):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
@ -198,7 +415,6 @@ def download_youtube_video(url, output_dir="."):
cookies_env = os.environ.get("YOUTUBE_COOKIES")
if cookies_env:
print("🍪 Found YOUTUBE_COOKIES env var, creating cookies file inside container...")
#print(f"📄 YOUTUBE_COOKIES raw value:\n{cookies_env}")
try:
with open(cookies_path, 'w') as f:
f.write(cookies_env)
@ -255,7 +471,7 @@ def download_youtube_video(url, output_dir="."):
def process_video_to_vertical(input_video, final_output_video):
"""
Core logic to convert horizontal video to vertical using scene detection.
Core logic to convert horizontal video to vertical using scene detection and Active Speaker Tracking (MediaPipe).
"""
script_start_time = time.time()
@ -284,7 +500,7 @@ def process_video_to_vertical(input_video, final_output_video):
print(f" ✅ Found {len(scenes)} scenes.")
print("\n 🧠 Step 2: Analyzing scene content...")
print("\n 🧠 Step 2: Preparing Active Tracking...")
original_width, original_height = get_video_resolution(input_video)
OUTPUT_HEIGHT = original_height
@ -292,19 +508,13 @@ def process_video_to_vertical(input_video, final_output_video):
if OUTPUT_WIDTH % 2 != 0:
OUTPUT_WIDTH += 1
scenes_analysis = []
for i, (start_time, end_time) in enumerate(scenes):
analysis = analyze_scene_content(input_video, start_time, end_time)
strategy, target_box = decide_cropping_strategy(analysis, original_height)
scenes_analysis.append({
'start_frame': start_time.get_frames(),
'end_frame': end_time.get_frames(),
'analysis': analysis,
'strategy': strategy,
'target_box': target_box
})
# Initialize Cameraman
cameraman = SmoothedCameraman(OUTPUT_WIDTH, OUTPUT_HEIGHT, original_width, original_height, smoothing_factor=0.08) # Slower smoothing
# Initialize Speaker Tracker
speaker_tracker = SpeakerTracker(cooldown_frames=45) # 1.5s cooldown before switching again
print("\n ✂️ Step 4: Processing video frames...")
print("\n ✂️ Step 4: Processing video frames (Tracking & Cropping)...")
command = [
'ffmpeg', '-y', '-f', 'rawvideo', '-vcodec', 'rawvideo',
@ -321,33 +531,59 @@ def process_video_to_vertical(input_video, final_output_video):
frame_number = 0
current_scene_index = 0
with tqdm(total=total_frames, desc=" Applying Plan") as pbar:
# Pre-calculate scene boundaries for fast lookup
scene_boundaries = []
for s_start, s_end in scenes:
scene_boundaries.append((s_start.get_frames(), s_end.get_frames()))
with tqdm(total=total_frames, desc=" Tracking & Cropping") as pbar:
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if current_scene_index < len(scenes_analysis) - 1 and \
frame_number >= scenes_analysis[current_scene_index + 1]['start_frame']:
current_scene_index += 1
scene_data = scenes_analysis[current_scene_index]
strategy = scene_data['strategy']
target_box = scene_data['target_box']
if strategy == 'TRACK':
crop_box = calculate_crop_box(target_box, original_width, original_height)
processed_frame = frame[crop_box[1]:crop_box[3], crop_box[0]:crop_box[2]]
output_frame = cv2.resize(processed_frame, (OUTPUT_WIDTH, OUTPUT_HEIGHT))
else: # LETTERBOX
scale_factor = OUTPUT_WIDTH / original_width
scaled_height = int(original_height * scale_factor)
scaled_frame = cv2.resize(frame, (OUTPUT_WIDTH, scaled_height))
output_frame = np.zeros((OUTPUT_HEIGHT, OUTPUT_WIDTH, 3), dtype=np.uint8)
y_offset = (OUTPUT_HEIGHT - scaled_height) // 2
output_frame[y_offset:y_offset + scaled_height, :] = scaled_frame
# 1. Check for Scene Change to Snap Camera
is_new_scene = False
# Check if current frame is a start frame of any scene
for s_start, _ in scene_boundaries:
if frame_number == s_start:
is_new_scene = True
break
# Reset tracker on new scene
if is_new_scene:
speaker_tracker = SpeakerTracker(cooldown_frames=45)
# 2. Detect Candidates
candidates = detect_face_candidates(frame)
# 3. Get Target from Tracker
# Tracker handles ID persistence, hysteresis, and cooldown
target_box = speaker_tracker.get_target(candidates, frame_number, original_width)
if target_box:
cameraman.update_target(target_box)
else:
# Fallback: Use YOLO to find the person if face detection fails
person_box = detect_person_yolo(frame)
if person_box:
cameraman.update_target(person_box)
# 4. Get Crop Box (with smoothing, unless new scene)
x1, y1, x2, y2 = cameraman.get_crop_box(force_snap=is_new_scene)
# 4. Crop & Resize
# Ensure coordinates are valid
crop_h = y2 - y1
crop_w = x2 - x1
if crop_h > 0 and crop_w > 0:
cropped_frame = frame[y1:y2, x1:x2]
output_frame = cv2.resize(cropped_frame, (OUTPUT_WIDTH, OUTPUT_HEIGHT))
else:
# Fallback if crop is invalid (shouldn't happen)
output_frame = cv2.resize(frame, (OUTPUT_WIDTH, OUTPUT_HEIGHT))
ffmpeg_process.stdin.write(output_frame.tobytes())
frame_number += 1
pbar.update(1)
@ -370,7 +606,6 @@ def process_video_to_vertical(input_video, final_output_video):
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.")
# Create silent audio? Or just skip audio merge
pass
print("\n ✨ Step 6: Merging...")

View file

@ -8,6 +8,7 @@ yt-dlp
faster-whisper
google-genai
python-dotenv
mediapipe
fastapi
uvicorn