fix: VFR frame extraction - use select=eq(n) instead of -ss frame/fps

- tkg.rs key_frame: select=eq(n\,best_frame) instead of -ss frame/fps
- tkg.rs retry: same fix for need_retry block
- tools.rs agent frame: select=eq(n\,frame_number)
- media_api.rs segment video: select=between(n\,sf\,ef-1) for frame mode

VFR videos (r_frame_rate != actual fps) now get correct frames.
CFR videos unaffected: select=eq(n) produces same result as -ss frame/fps.

Fixes: face card / face-detail thumbnail mismatch for VFR files.
This commit is contained in:
Accusys
2026-08-05 12:36:25 +08:00
parent 2117ca60b8
commit a6d705abf1
3 changed files with 36 additions and 21 deletions
+21 -11
View File
@@ -1236,25 +1236,35 @@ async fn video_clip(
return Err(StatusCode::BAD_REQUEST); return Err(StatusCode::BAD_REQUEST);
} }
let is_frame_mode = q.start_frame.is_some();
let mode = q.mode.as_deref().unwrap_or("normal").to_string(); let mode = q.mode.as_deref().unwrap_or("normal").to_string();
let audio = q.audio.as_deref().unwrap_or("on"); let audio = q.audio.as_deref().unwrap_or("on");
let mut cmd = ffmpeg_cmd(); let mut cmd = ffmpeg_cmd();
cmd.args(["-ss", &s.to_string(), "-i", &file_path]); if is_frame_mode {
if q.start_frame.is_some() { // Use select=between(n\,sf\,ef-1) for VFR compatibility (decoded index, not time-based)
let frame_count = ((e - s) * fps) as i64; let sf = q.start_frame.unwrap() as i64;
cmd.args(["-vframes", &frame_count.to_string()]); let ef = q.end_frame.unwrap() as i64;
} else { let select = format!("select=between(n\\,{}\\,{})", sf, ef - 1);
cmd.args(["-t", &(e - s).to_string()]); let frame_count = (ef - sf) as i64;
}
cmd.args(["-i", &file_path]);
if mode == "debug" { if mode == "debug" {
let debug_text = if let (Some(sf), Some(ef)) = (q.start_frame, q.end_frame) { let debug_text = format!("drawtext=text='Frame %{{n}} FRAMES {}-{}':fontsize=28:fontcolor=white:box=1:boxcolor=black@0.6:x=10:y=10", sf, ef);
format!("drawtext=text='Frame %{{n}} FRAMES {}-{}':fontsize=28:fontcolor=white:box=1:boxcolor=black@0.6:x=10:y=10", sf, ef) let combined_vf = format!("{},{}", select, debug_text);
cmd.args(["-vf", &combined_vf]);
} else { } else {
"drawtext=text='Frame %{n} CLIP':fontsize=28:fontcolor=white:box=1:boxcolor=black@0.6:x=10:y=10".to_string() cmd.args(["-vf", &select]);
}; }
cmd.args(["-frames:v", &frame_count.to_string()]);
} else {
cmd.args(["-ss", &s.to_string(), "-i", &file_path]);
cmd.args(["-t", &(e - s).to_string()]);
if mode == "debug" {
let debug_text = "drawtext=text='Frame %{n} CLIP':fontsize=28:fontcolor=white:box=1:boxcolor=black@0.6:x=10:y=10".to_string();
cmd.args(["-vf", &debug_text]); cmd.args(["-vf", &debug_text]);
} }
}
if audio == "off" { if audio == "off" {
cmd.args(["-an"]); cmd.args(["-an"]);
} }
+6 -2
View File
@@ -928,12 +928,16 @@ pub async fn exec_analyze_frame(
} }
}); });
// Use select=eq(n\,frame) for VFR compatibility (decoded index, not time-based)
let select = format!("select=eq(n\\,{})", frame_number);
let output = tokio::process::Command::new(&ffmpeg_path) let output = tokio::process::Command::new(&ffmpeg_path)
.args([ .args([
"-ss",
&format!("{:.3}", timestamp_secs),
"-i", "-i",
&video_path, &video_path,
])
.args([
"-vf",
&select,
"-vframes", "-vframes",
"1", "1",
"-f", "-f",
+7 -6
View File
@@ -1267,19 +1267,20 @@ async fn build_face_track_nodes_from_qdrant(
); );
// Extract key_frame JPEG bytes via ffmpeg pipe // Extract key_frame JPEG bytes via ffmpeg pipe
// Use select=eq(n\,frame) for VFR compatibility (decoded index, not time-based)
let key_frame_jpeg: Option<Vec<u8>> = video_path.as_ref().and_then(|vp| { let key_frame_jpeg: Option<Vec<u8>> = video_path.as_ref().and_then(|vp| {
let timestamp = agg.best_frame as f64 / actual_fps; let select = format!("select=eq(n\\,{})", agg.best_frame);
let output = std::process::Command::new("ffmpeg") let output = std::process::Command::new("ffmpeg")
.args([ .args([
"-y", "-y",
"-v", "-v",
"quiet", "quiet",
"-ss",
&format!("{:.3}", timestamp),
"-i", "-i",
]) ])
.arg(vp) .arg(vp)
.args([ .args([
"-vf",
&select,
"-frames:v", "-frames:v",
"1", "1",
"-q:v", "-q:v",
@@ -1404,7 +1405,7 @@ async fn build_face_track_nodes_from_qdrant(
if need_retry { if need_retry {
if let Some(ref vp) = video_path { if let Some(ref vp) = video_path {
let timestamp = agg.best_frame as f64 / actual_fps; let select = format!("select=eq(n\\,{})", agg.best_frame);
// Retry key_frame extraction // Retry key_frame extraction
if !kf_path.exists() { if !kf_path.exists() {
if let Ok(output) = std::process::Command::new("ffmpeg") if let Ok(output) = std::process::Command::new("ffmpeg")
@@ -1412,12 +1413,12 @@ async fn build_face_track_nodes_from_qdrant(
"-y", "-y",
"-v", "-v",
"quiet", "quiet",
"-ss",
&format!("{:.3}", timestamp),
"-i", "-i",
vp, vp,
]) ])
.args([ .args([
"-vf",
&select,
"-frames:v", "-frames:v",
"1", "1",
"-q:v", "-q:v",