From ef64ba589be83c9a337025b4bd64277f54f4475f Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 12 Jun 2023 16:33:34 -0300 Subject: [PATCH 1/4] bump max body size --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 3b6035a..79324d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ async fn main() { // build our application with a single route let app = Router::new() .route("/", post(upload_file)) - .layer(axum::extract::DefaultBodyLimit::max(300 * 1024 * 1024)); + .layer(axum::extract::DefaultBodyLimit::max(512 * 1024 * 1024)); let upstream_runner = get_upstream_runner(); From 0f29ba76c28e7832d896d7b8eea4232329389c35 Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 12 Jun 2023 16:33:44 -0300 Subject: [PATCH 2/4] add webm support --- src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 79324d4..e681544 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,7 +117,7 @@ async fn send_image_to_dd( .await?; let body = resp.text().await?; - log::info!("body: {}", &body); + log::info!("sd body: {}", &body); let json_response: WrappedResponse = serde_json::from_str(&body)?; log::debug!("called!"); @@ -134,6 +134,8 @@ async fn send_image_to_dd( map.insert("image", &file_base64); let serialized_map = serde_json::to_vec(&map).unwrap(); + let len = serialized_map.len(); + log::info!("wd14 request length {} bytes", len); let resp = reqwest::Client::new() .post(format!("{}/tagger/v1/interrogate", url)) @@ -142,7 +144,7 @@ async fn send_image_to_dd( .await?; let body = resp.text().await?; - log::info!("body: {}", &body); + log::info!("wd14 body: {}", &body); let json_response: WD14Response = serde_json::from_str(&body)?; // turn WD14Response into WrappedResponse @@ -235,7 +237,8 @@ async fn upload_file( let file_name = maybe_file_name.unwrap(); let is_video = file_type.starts_with("video/") || file_name.ends_with(".mp4") - || file_name.ends_with(".gif"); + || file_name.ends_with(".gif") + || file_name.ends_with(".webm"); if is_video { let mut final_tag_set = HashSet::new(); From ad268357bda6d78c9aa5d1e2a85e4c3914be9ccb Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 12 Jun 2023 16:33:47 -0300 Subject: [PATCH 3/4] tweak values for larger videos --- src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index e681544..6e5365d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -263,8 +263,10 @@ async fn upload_file( 11..=60 => 10, 61..=120 => 15, 121..=300 => 20, - 301.. => 30, - _ => 33, + 301..=1000 => 30, + 1001..=1200 => 40, + 1201.. => 60, + _ => 63, } as f64; let wanted_frame_skip = wanted_frame_skip_seconds * frame_rate; From ae9a6f19ba6b604f5ff4e330fa5e388446e4f758 Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 12 Jun 2023 17:08:28 -0300 Subject: [PATCH 4/4] more robust logic to fetch video frame count --- src/main.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6e5365d..c4998f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use axum_macros::debug_handler; use base64::{engine::general_purpose, Engine as _}; use core::panic; use ffmpeg_cli::Parameter; +use ffprobe::{Format, Stream}; use futures_util::{future::ready, StreamExt}; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, HashSet}; @@ -209,6 +210,41 @@ async fn fetch_frame_as_image( Ok(()) } +fn fetch_frame_count_full_decode(path: &std::path::Path) -> anyhow::Result { + let config = ffprobe::ConfigBuilder::new().count_frames(true).build(); + let new_info = ffprobe::ffprobe_config(config, path)?; + let res = new_info + .streams + .get(0) + .unwrap() + .nb_read_frames + .clone() + .unwrap() + .parse::()?; + Ok(res) +} + +fn calculate_frame_count( + path: &std::path::Path, + stream: &Stream, + format: &Format, + frame_rate: f64, +) -> anyhow::Result { + Ok(if let Some(parseable_data) = stream.nb_frames.clone() { + // if we can get it from the stream metadata, use it + parseable_data.parse::()? + } else if let Some(parseable_data) = format.try_get_duration() { + // this is a std::time::duration + // multiply that by frame rate and we get total frame count (approximate) + log::warn!("fetching duration from format metadata..."); + let seconds = parseable_data?.as_secs_f64(); + (seconds * frame_rate) as u64 + } else { + log::warn!("file didn't provide frame metadata, calculating it ourselves..."); + fetch_frame_count_full_decode(path)? + }) +} + #[debug_handler] async fn upload_file( options: Query, @@ -250,13 +286,17 @@ async fn upload_file( let info = ffprobe::ffprobe(temp_file.path())?; let stream = info.streams.get(0).unwrap(); - let total_frame_count = stream.nb_frames.clone().unwrap().parse::()?; - let frame_rate_str = stream.r_frame_rate.clone(); + log::debug!("stream = {:?}", stream); + log::debug!("format = {:?}", info.format); + let frame_rate_str = stream.r_frame_rate.clone(); let parts = frame_rate_str.split("/").into_iter().collect::>(); let frame_rate: f64 = parts.get(0).unwrap().parse::()? / parts.get(1).unwrap().parse::()?; + let total_frame_count = + calculate_frame_count(temp_file.path(), &stream, &info.format, frame_rate)?; + let total_length_in_seconds = total_frame_count as f64 / frame_rate; let wanted_frame_skip_seconds = match total_length_in_seconds as usize { 0..=10 => 2,