fix: scene frame 越界導致 jpg 提取失敗、job 卡 ingestion
Core Test / lint-and-test (push) Has been cancelled
Cross-Project Integration / integration (push) Has been cancelled

根因 (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
This commit is contained in:
Accusys
2026-11-19 21:53:56 +08:00
parent 532a2287c5
commit 49b02c2d4f
2 changed files with 40 additions and 1 deletions
+9 -1
View File
@@ -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;
+31
View File
@@ -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<String> = 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.