From 49b02c2d4f5f4aea568c8081ff8928c13161de6a Mon Sep 17 00:00:00 2001 From: Accusys Date: Thu, 19 Nov 2026 21:53:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20scene=20frame=20=E8=B6=8A=E7=95=8C?= =?UTF-8?q?=E5=B0=8E=E8=87=B4=20jpg=20=E6=8F=90=E5=8F=96=E5=A4=B1=E6=95=97?= =?UTF-8?q?=E3=80=81job=20=E5=8D=A1=20ingestion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因 (090.mp4): - 影片 total_frames=300 (frame 0~299),但最後 moment 的 end=300 被選為 scene_004 的 frame_number → 越界 → ffmpeg 提取失敗 → scene_004.jpg 不存在 - extract_scene_frames 跳過失敗的 frame,但 profile.scenes 仍保留 → scenes.json 有 5 scene 但只有 4 張 jpg → scene_vlm_done 判定 4≠5 → false → job 永遠卡在 ingestion 等待 修復: 1. generate_scene_profile: clamp 所有 frame_number 到 total_frames-1 2. process_scene_vlm: extract 後若 jpg 缺失,按 filename 比對移除對應 scene, 確保 scenes.json 與磁碟 jpg 一致 驗證: 090.mp4 re-run → scenes.json 4 scene 全 completed, 4 張 jpg, ingestion all_ok=true, job completed --- src/core/processor/scene_vlm.rs | 10 +++++++++- src/worker/job_worker.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) 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.