From 0f55c2011032a6ada8a66d2743efc88e229ff5a6 Mon Sep 17 00:00:00 2001 From: s-ol Date: Fri, 22 May 2026 15:11:04 +0200 Subject: tsv-video-*: detect video length using format duration --- src/ffmpeg.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/ffmpeg.rs') diff --git a/src/ffmpeg.rs b/src/ffmpeg.rs index fbfdc94..3c385c4 100644 --- a/src/ffmpeg.rs +++ b/src/ffmpeg.rs @@ -90,6 +90,7 @@ fn filter_probe_args(ff_args: &[String]) -> Vec { out } +#[derive(Debug)] pub struct VideoInfo { pub width: u32, pub height: u32, @@ -106,7 +107,7 @@ pub fn probe_video(ff_args: &[String], max_frames: Option) -> VideoInfo { .args(["-select_streams", "v:0"]) .args([ "-show_entries", - "stream=codec_tag_string,width,height,pix_fmt,r_frame_rate,nb_frames", + "stream=codec_tag_string,width,height,pix_fmt,r_frame_rate,nb_frames:format=duration", ]) .args(["-of", "csv=p=0"]) .args(&probe_args) @@ -120,8 +121,8 @@ pub fn probe_video(ff_args: &[String], max_frames: Option) -> VideoInfo { let stdout = String::from_utf8_lossy(&output.stdout); let line = stdout.trim(); - let parts: Vec<&str> = line.split(',').collect(); - if parts.len() != 6 { + let parts: Vec<&str> = line.split(&[',', '\n']).collect(); + if parts.len() != 7 { panic!("unexpected ffprobe output: {line}"); } @@ -137,7 +138,10 @@ pub fn probe_video(ff_args: &[String], max_frames: Option) -> VideoInfo { } else { parts[4].parse().expect("parse fps") }; - let num_frames = parts[5].parse().ok().or(max_frames); + let num_frames = parts[5].parse::().ok().or(max_frames).or_else(|| { + let duration: f64 = parts[6].parse().ok()?; + Some((duration * fps) as u32) + }); VideoInfo { width, -- cgit v1.2.3