diff --git a/src/core/processor/scene_vlm.rs b/src/core/processor/scene_vlm.rs index 0f4aa0b1..e9e7d06a 100644 --- a/src/core/processor/scene_vlm.rs +++ b/src/core/processor/scene_vlm.rs @@ -376,10 +376,18 @@ impl SceneVlmProcessor { all_frames.extend(frames); } - // Fix indices + // Fix indices and clamp frame numbers to the valid range. + // The last interval's end can equal total_frames (exclusive bound); + // sampling it would pick frame == total_frames, which is out of range + // and makes ffmpeg extraction fail → missing jpg → permanent failed scene. + let max_frame = total_frames.saturating_sub(1) as u32; for (i, frame) in all_frames.iter_mut().enumerate() { frame.index = i; frame.filename = format!("scene_{:03}.jpg", i); + if frame.frame_number > max_frame { + frame.frame_number = max_frame; + frame.timestamp_sec = frame.frame_number as f64 / fps.max(0.001); + } } profile.scenes = all_frames; diff --git a/src/worker/job_worker.rs b/src/worker/job_worker.rs index 1d45eafa..2c630971 100644 --- a/src/worker/job_worker.rs +++ b/src/worker/job_worker.rs @@ -5736,6 +5736,37 @@ async fn process_job(&self, job: crate::core::db::MonitorJob) -> Result<()> { SceneVlmProcessor::extract_scene_frames(&video_path, output_dir, uuid, &profile.scenes) .await?; + // Guard: extract_scene_frames skips frames it cannot decode, which can + // desync extracted[] from profile.scenes[]. Drop any scene whose jpg is + // missing so scenes.json always matches the on-disk frames. + if extracted.len() != profile.scenes.len() { + // Match by filename so a failed extraction in the middle cannot + // shift the alignment of subsequent scenes. + let existing: std::collections::HashSet = extracted + .iter() + .filter(|p| p.exists()) + .filter_map(|p| p.file_name().map(|n| n.to_string_lossy().to_string())) + .collect(); + let old_scenes = std::mem::take(&mut profile.scenes); + let mut aligned = Vec::new(); + let mut paths = Vec::new(); + for scene in old_scenes { + if existing.contains(&scene.filename) { + if let Some(p) = extracted.iter().find(|p| { + p.file_name().map(|n| n.to_string_lossy() == scene.filename).unwrap_or(false) + }) { + paths.push(p.clone()); + } + aligned.push(scene); + } else { + warn!("[Scene VLM] dropping scene with missing jpg: {}", scene.filename); + } + } + profile.scenes = aligned; + profile.total_scenes = profile.scenes.len(); + extracted = paths; + } + // Second-pass dedup: pHash + time-gap over the extracted jpgs. // Drop a scene when it is visually near-identical to a previously kept // one within the time-gap window. Resume-carried scenes are kept as-is.