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
+23 -13
View File
@@ -1236,24 +1236,34 @@ async fn video_clip(
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 audio = q.audio.as_deref().unwrap_or("on");
let mut cmd = ffmpeg_cmd();
cmd.args(["-ss", &s.to_string(), "-i", &file_path]);
if q.start_frame.is_some() {
let frame_count = ((e - s) * fps) as i64;
cmd.args(["-vframes", &frame_count.to_string()]);
} else {
cmd.args(["-t", &(e - s).to_string()]);
}
if mode == "debug" {
let debug_text = if let (Some(sf), Some(ef)) = (q.start_frame, q.end_frame) {
format!("drawtext=text='Frame %{{n}} FRAMES {}-{}':fontsize=28:fontcolor=white:box=1:boxcolor=black@0.6:x=10:y=10", sf, ef)
if is_frame_mode {
// Use select=between(n\,sf\,ef-1) for VFR compatibility (decoded index, not time-based)
let sf = q.start_frame.unwrap() as i64;
let ef = q.end_frame.unwrap() as i64;
let select = format!("select=between(n\\,{}\\,{})", sf, ef - 1);
let frame_count = (ef - sf) as i64;
cmd.args(["-i", &file_path]);
if mode == "debug" {
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);
let combined_vf = format!("{},{}", select, debug_text);
cmd.args(["-vf", &combined_vf]);
} 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", &debug_text]);
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]);
}
}
if audio == "off" {
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)
.args([
"-ss",
&format!("{:.3}", timestamp_secs),
"-i",
&video_path,
])
.args([
"-vf",
&select,
"-vframes",
"1",
"-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
// 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 timestamp = agg.best_frame as f64 / actual_fps;
let select = format!("select=eq(n\\,{})", agg.best_frame);
let output = std::process::Command::new("ffmpeg")
.args([
"-y",
"-v",
"quiet",
"-ss",
&format!("{:.3}", timestamp),
"-i",
])
.arg(vp)
.args([
"-vf",
&select,
"-frames:v",
"1",
"-q:v",
@@ -1404,7 +1405,7 @@ async fn build_face_track_nodes_from_qdrant(
if need_retry {
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
if !kf_path.exists() {
if let Ok(output) = std::process::Command::new("ffmpeg")
@@ -1412,12 +1413,12 @@ async fn build_face_track_nodes_from_qdrant(
"-y",
"-v",
"quiet",
"-ss",
&format!("{:.3}", timestamp),
"-i",
vp,
])
.args([
"-vf",
&select,
"-frames:v",
"1",
"-q:v",