From 3f7004cc060727c98bd81eca61af71e60c771c9f Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Wed, 27 Dec 2023 23:26:09 -0500 Subject: [PATCH] show drops woo --- src/src/main.rs | 123 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 90 insertions(+), 33 deletions(-) diff --git a/src/src/main.rs b/src/src/main.rs index 3f084ad..8b3eac2 100644 --- a/src/src/main.rs +++ b/src/src/main.rs @@ -13,7 +13,7 @@ fn main() { // define a component that renders a div with the text "Hello, world!" fn App(cx: Scope) -> Element { let file = use_state(cx, String::new); - let analysis = use_state(cx, Vec::::new); + let analysis = use_state(cx, Analysis::new); let status = use_state(cx, String::new); cx.render(rsx! { header { @@ -41,8 +41,19 @@ fn App(cx: Scope) -> Element { input { r#type: "button", value: "analyze", disabled: file.get().len() == 0, onclick: move |evt| { to_owned![file]; to_owned![analysis]; + to_owned![status]; async move { - analysis.set(analyze(file.get().clone())); + let analyzed = analyze(file.get().clone()); + if analyzed.err.len() > 0 { + status.set(analyzed.err.clone()); + } else { + status.set(format!( + "found {} clips to keep and {} clips to drop", + analyzed.with_content.len(), + analyzed.without_content.len(), + )); + } + analysis.set(analyzed); } }} input { r#type: "button", value: "clipify", disabled: file.get().len() == 0, onclick: move |evt| { @@ -59,54 +70,100 @@ fn App(cx: Scope) -> Element { br {} } div { - analysis.get().iter().map(|a| { - rsx! { - p { - "{a.start}..{a.stop}: " - br {} - img { src: "data:image/png;base64, {a.screenshot}" } + h3 { "Clips to Keep" } + div { + analysis.get().with_content.iter().map(|a| { + rsx! { + p { + "{a.start}..{a.stop}: " + br {} + img { src: "data:image/png;base64, {a.screenshot}" } + } } - } - }) + }) + } + } + div { + h3 { "Clips to Remove" } + div { + analysis.get().without_content.iter().map(|a| { + rsx! { + p { + "{a.start}..{a.stop}: " + br {} + img { src: "data:image/png;base64, {a.screenshot}" } + } + } + }) + } } } } }) } +struct Analysis { + with_content: Vec, + without_content: Vec, + err: String, +} + +impl Analysis { + fn new() -> Self { + Self{ + with_content: vec![], + without_content: vec![], + err: "".to_string(), + } + } +} + struct Analyzed { start: String, stop: String, screenshot: String, } -fn analyze(file: String) -> Vec { +fn analyze(file: String) -> Analysis { let content_spans = lib::video::inspect(&file); // TODO desktop //let content_spans: Result, String> = Ok(vec![lib::video::ContentSpan{start: 0.5, stop: 20.2}]); if content_spans.is_err() { - return vec![Analyzed{ - start: format!("<>", content_spans.err().unwrap()), - screenshot: a_png.to_string(), - }]; + return Analysis{ + with_content: vec![], + without_content: vec![], + err: format!("failed to analyze {}: {}", file, content_spans.err().unwrap()), + }; + } + let content_spans = content_spans.unwrap(); + + let mut non_content_spans = vec![]; + if let Some(first_content_span) = content_spans.iter().nth(0) { + non_content_spans.push(lib::video::ContentSpan{start: 0.0, stop: first_content_span.start}); + } + for i in 1..content_spans.len() { + non_content_spans.push(lib::video::ContentSpan{ + start: content_spans[i-1].stop, + stop: content_spans[i].start, + }); + } + + let screenshot = |content_span: &lib::video::ContentSpan| -> Analyzed { + let ts = (content_span.start + content_span.stop) / 2.0; + Analyzed { + start: content_span.start.to_string(), + stop: content_span.stop.to_string(), + screenshot: match lib::video::screenshot_png(&file, ts) { + Ok(png) => general_purpose::STANDARD.encode(&png), + Err(_) => a_png.to_string(), + } + } + }; + + Analysis{ + with_content: content_spans.iter().map(screenshot).collect(), + without_content: non_content_spans.iter().map(screenshot).collect(), + err: "".to_string(), } - content_spans.unwrap().iter() - .map(|content_span| { - let mut result = Analyzed{ - start: content_span.start.to_string(), - stop: content_span.stop.to_string(), - screenshot: a_png.to_string(), - }; - match lib::video::screenshot_png( - &file, - (content_span.start + content_span.stop) / 2.0, - ) { - Ok(png) => { result.screenshot = general_purpose::STANDARD.encode(&png); }, - Err(msg) => { result.stop = format!("{} <>", result.stop, file, msg); }, - }; - result - }) - .collect() } fn clipify(file: String) -> String {