From 9e38ce17c17f237b0a44b893d299b7c4d1baf161 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 29 Oct 2026 10:55:40 -0700 Subject: [PATCH] feat(cli): a measure command and its schema --- komorebi-recorder/src/analysis/schemas.rs | 6 ++ komorebi-recorder/src/cli/mod.rs | 5 ++ komorebi-recorder/src/cli/video.rs | 72 +++++++++++++++++++++++ komorebi-recorder/tests/video.rs | 59 +++++++++++++++++++ 4 files changed, 142 insertions(+) diff --git a/komorebi-recorder/src/analysis/schemas.rs b/komorebi-recorder/src/analysis/schemas.rs index e6738ddf..75c00ea6 100644 --- a/komorebi-recorder/src/analysis/schemas.rs +++ b/komorebi-recorder/src/analysis/schemas.rs @@ -24,6 +24,7 @@ use super::envelope::Envelope; use super::framediff::FrameDiffData; use super::history::HistoryData; use super::logq::LogqData; +use super::measure::MeasureData; use super::motion::MotionData; use super::overview::OverviewData; use super::search::SearchData; @@ -129,6 +130,11 @@ pub const ALL: &[Entry] = &[ about: "`blank`: stretches where a region was blank (one colour) or frozen", schema: of::, }, + Entry { + name: "measure/1", + about: "`measure`: mean or peak brightness or drawn width of a region, on a fixed-rate timeline", + schema: of::, + }, Entry { name: "compare/1", about: "`compare`: two recordings side by side with the difference of each measure", diff --git a/komorebi-recorder/src/cli/mod.rs b/komorebi-recorder/src/cli/mod.rs index 9963e30f..2030cf8b 100644 --- a/komorebi-recorder/src/cli/mod.rs +++ b/komorebi-recorder/src/cli/mod.rs @@ -61,6 +61,7 @@ pub use video::BlankArgs; pub use video::ChangesArgs; pub use video::CompareArgs; pub use video::FramediffArgs; +pub use video::MeasureArgs; pub use video::MotionArgs; pub use video::SheetArgs; pub use viewer_command::GuiArgs; @@ -141,6 +142,8 @@ pub enum Command { Framediff(FramediffArgs), /// When a region was blank (one colour, such as white) or frozen Blank(BlankArgs), + /// A statistic of a region (mean or peak brightness, drawn width) on a fixed-rate timeline + Measure(MeasureArgs), /// Two recordings side by side: frames, motion, findings, annotations Compare(CompareArgs), /// A contact sheet image of frames, with the times, events and bug region marked @@ -231,6 +234,7 @@ impl Command { Self::Motion(args) => args.json, Self::Framediff(args) => args.json, Self::Blank(args) => args.json, + Self::Measure(args) => args.json, Self::Compare(args) => args.json, Self::Annotate(args) => args.json(), Self::Annotations(args) => args.json, @@ -314,6 +318,7 @@ pub fn run(command: Command) -> eyre::Result<()> { Command::Motion(args) => video::motion(&args), Command::Framediff(args) => video::framediff(&args), Command::Blank(args) => video::blank(&args), + Command::Measure(args) => video::measure(&args), Command::Compare(args) => video::compare(&args), Command::Findings(args) => findings::findings(&args), Command::Annotate(args) => annotate::annotate(&args), diff --git a/komorebi-recorder/src/cli/video.rs b/komorebi-recorder/src/cli/video.rs index f530f8d3..a43ae348 100644 --- a/komorebi-recorder/src/cli/video.rs +++ b/komorebi-recorder/src/cli/video.rs @@ -7,6 +7,7 @@ //! | `motion` | [`Analysis::motion`] | `motion/1` | //! | `framediff` | [`framediff`](crate::analysis::framediff::framediff) | `framediff/1` | //! | `blank` | [`Analysis::blank`] | `blank/1` | +//! | `measure` | [`Analysis::measure`] | `measure/1` | //! | `compare` | [`Analysis::compare`] | `compare/1` | //! //! Both work on a range of video time, given as `--from` and `--to` (any time `state --at` takes; @@ -33,6 +34,10 @@ use crate::analysis::framediff::DEFAULT_WIDTH; use crate::analysis::framediff::FrameAt; use crate::analysis::framediff::FrameDiffOptions; use crate::analysis::framediff::SCHEMA_FRAMEDIFF; +use crate::analysis::measure::DEFAULT_HZ; +use crate::analysis::measure::MeasureOptions; +use crate::analysis::measure::SCHEMA_MEASURE; +use crate::analysis::measure::Statistic; use crate::analysis::motion::SCHEMA_MOTION; use crate::analysis::motion::ScanOptions; use crate::analysis::pixels::Crop; @@ -199,6 +204,38 @@ pub struct BlankArgs { pub json: bool, } +/// The arguments of `measure`. +#[derive(Args, Debug)] +pub struct MeasureArgs { + /// Session directory + pub session: PathBuf, + /// The region to measure, in display points: x,y,w,h [default: the whole display] + #[arg(long, value_name = "X,Y,W,H")] + pub region: Option, + /// What to measure, repeatable: mean (brightness), peak (brightness), width (drawn width, in + /// points) [default: all three] + #[arg(long = "stat", value_name = "STAT")] + pub stats: Vec, + /// Samples per second of the fixed-rate timeline + #[arg(long, default_value_t = DEFAULT_HZ)] + pub hz: f64, + /// The brightness (0 to 255) that is background for `width` [default: the most common one in the region] + #[arg(long, value_name = "0-255")] + pub background: Option, + /// Only this display (repeatable) [default: every display with video] + #[arg(long = "display", value_name = "ID")] + pub displays: Vec, + /// From this moment [default: the start of the video] + #[arg(long, value_name = "TIME")] + pub from: Option, + /// To this moment [default: the end of the video] + #[arg(long, value_name = "TIME")] + pub to: Option, + /// Machine-readable JSON output + #[arg(long)] + pub json: bool, +} + /// The arguments of `compare`. #[derive(Args, Debug)] pub struct CompareArgs { @@ -489,6 +526,41 @@ pub(super) fn blank(args: &BlankArgs) -> eyre::Result<()> { Ok(()) } +pub(super) fn measure(args: &MeasureArgs) -> eyre::Result<()> { + let analysis = Analysis::open(&args.session)?; + let video = SessionVideo::new(&analysis.manifest, analysis.guard()); + let (from, to) = range(&analysis, args.from.as_deref(), args.to.as_deref(), None)?; + let shown = displays(&video, &args.displays)?; + let mut statistics = if args.stats.is_empty() { + Statistic::ALL.to_vec() + } else { + args.stats.clone() + }; + statistics.dedup(); + + let data = analysis.measure( + &video, + &MeasureOptions { + scan: ScanOptions { + displays: &shown, + from_s: from, + to_s: to, + crop: region(args.region.as_deref())?, + cancel: None, + }, + statistics: &statistics, + hz: args.hz, + background: args.background, + }, + )?; + if args.json { + return print_json(&Envelope::ok(SCHEMA_MEASURE, data).warnings(analysis.warnings())); + } + + print!("{}", data.to_text()); + Ok(()) +} + pub(super) fn compare(args: &CompareArgs) -> eyre::Result<()> { let a = Analysis::open(&args.session_a)?; let b = Analysis::open(&args.session_b)?; diff --git a/komorebi-recorder/tests/video.rs b/komorebi-recorder/tests/video.rs index 08b9e989..0c4a651e 100644 --- a/komorebi-recorder/tests/video.rs +++ b/komorebi-recorder/tests/video.rs @@ -555,3 +555,62 @@ fn a_sheet_draws_the_windows_of_the_moment_and_says_so_when_there_is_no_track() assert!(warnings.contains("no window track"), "{warnings}"); assert!(answer["data"]["frames"][0].get("windows").is_none()); } + +#[test] +fn measure_puts_a_region_on_a_fixed_rate_timeline() { + let Some(dir) = session("video-measure") else { + return; + }; + + let measured = json(&[ + "measure", + &dir, + "--display", + "1", + "--from", + "0", + "--to", + "1", + "--hz", + "10", + "--json", + ]); + assert_eq!(measured["schema"], "measure/1"); + let data = &measured["data"]; + assert_eq!(data["statistics"], json!(["mean", "peak", "width"])); + let samples = data["series"][0]["samples"].as_array().expect("samples"); + assert_eq!(samples.len(), 11); + for (index, sample) in samples.iter().enumerate() { + assert!((sample["video_s"].as_f64().expect("time") - index as f64 / 10.0).abs() < 1e-9); + let shown = sample["shown_s"].as_f64().expect("a frame is on screen"); + assert!(shown <= sample["video_s"].as_f64().expect("time") + 1e-9); + } + + // display 2 has no video before 1 s + let late = json(&[ + "measure", + &dir, + "--display", + "2", + "--from", + "0", + "--to", + "1.5", + "--hz", + "2", + "--stat", + "peak", + "--json", + ]); + let samples = late["data"]["series"][0]["samples"] + .as_array() + .expect("samples"); + assert!(samples[0]["values"][0].is_null()); + assert!(!samples[2]["values"][0].is_null()); + + // a rate that is not a rate + assert_eq!( + recorder(&["measure", &dir, "--hz", "0"]).status.code(), + Some(2) + ); +}