deepshrink_ffmpeg/
progress.rs1#[derive(Debug, Clone, PartialEq)]
9pub enum Progress {
10 OutTimeUs(u64),
12 Continue,
14 End,
16}
17
18pub fn parse_line(line: &str) -> Option<Progress> {
20 let (key, value) = line.split_once('=')?;
21 match key.trim() {
22 "out_time_us" | "out_time_ms" => {
23 value.trim().parse().ok().map(Progress::OutTimeUs)
26 }
27 "progress" => match value.trim() {
28 "end" => Some(Progress::End),
29 "continue" => Some(Progress::Continue),
30 _ => None,
31 },
32 _ => None,
33 }
34}
35
36pub fn fraction(out_time_us: u64, total_secs: f64) -> f64 {
38 if total_secs <= 0.0 {
39 return 0.0;
40 }
41 let elapsed_secs = out_time_us as f64 / 1_000_000.0;
42 (elapsed_secs / total_secs).clamp(0.0, 1.0)
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn parses_out_time() {
51 assert_eq!(
52 parse_line("out_time_us=1500000"),
53 Some(Progress::OutTimeUs(1_500_000))
54 );
55 assert_eq!(
56 parse_line("out_time_ms=2000000"),
57 Some(Progress::OutTimeUs(2_000_000))
58 );
59 }
60
61 #[test]
62 fn parses_progress_markers() {
63 assert_eq!(parse_line("progress=continue"), Some(Progress::Continue));
64 assert_eq!(parse_line("progress=end"), Some(Progress::End));
65 }
66
67 #[test]
68 fn ignores_other_keys() {
69 assert_eq!(parse_line("frame=42"), None);
70 assert_eq!(parse_line("bitrate= 456.7kbits/s"), None);
71 assert_eq!(parse_line("no-equals-sign"), None);
72 }
73
74 #[test]
75 fn fraction_clamps() {
76 assert_eq!(fraction(0, 10.0), 0.0);
77 assert_eq!(fraction(5_000_000, 10.0), 0.5);
78 assert_eq!(fraction(20_000_000, 10.0), 1.0);
79 assert_eq!(fraction(1_000_000, 0.0), 0.0);
80 }
81}