Compare commits
3 commits
ae9a6f19ba
...
b5d8842ccc
Author | SHA1 | Date | |
---|---|---|---|
b5d8842ccc | |||
fd9e7f6d68 | |||
cb708d17fd |
1 changed files with 56 additions and 4 deletions
60
src/main.rs
60
src/main.rs
|
@ -37,7 +37,7 @@ async fn main() {
|
|||
// build our application with a single route
|
||||
let app = Router::new()
|
||||
.route("/", post(upload_file))
|
||||
.layer(axum::extract::DefaultBodyLimit::max(512 * 1024 * 1024));
|
||||
.layer(axum::extract::DefaultBodyLimit::max(5 * 1024 * 1024 * 1024));
|
||||
|
||||
let upstream_runner = get_upstream_runner();
|
||||
|
||||
|
@ -245,6 +245,52 @@ fn calculate_frame_count(
|
|||
})
|
||||
}
|
||||
|
||||
fn calculate_frame_rate(
|
||||
temp_path: &std::path::Path,
|
||||
frame_rate_str: String,
|
||||
first_run: bool,
|
||||
) -> anyhow::Result<f64> {
|
||||
let parts = frame_rate_str.split("/").into_iter().collect::<Vec<_>>();
|
||||
let frames_per = parts.get(0).unwrap().parse::<f64>()?;
|
||||
let per_second = parts.get(1).unwrap().parse::<f64>()?;
|
||||
if frames_per == 0.0f64 && per_second == 0.0f64 {
|
||||
log::warn!("got incorrect frame rate, calling ffmpeg again...");
|
||||
if !first_run {
|
||||
std::panic!("couldnt get frame rate shit");
|
||||
}
|
||||
|
||||
// call ffprobe directly
|
||||
let mut cmd = std::process::Command::new("ffprobe");
|
||||
let cmd = cmd.args(&[
|
||||
"-v",
|
||||
"error",
|
||||
"-select_streams",
|
||||
"v",
|
||||
"-of",
|
||||
"default=noprint_wrappers=1:nokey=1",
|
||||
"-show_entries",
|
||||
"stream=r_frame_rate",
|
||||
]);
|
||||
let cmd = cmd.arg(temp_path.to_str().unwrap());
|
||||
|
||||
cmd.get_args().for_each(|arg| {
|
||||
log::debug!("arg {:?}", arg);
|
||||
});
|
||||
|
||||
let output = cmd.output()?;
|
||||
let possibly_new_frame_rate = String::from_utf8(output.stdout)?
|
||||
.strip_suffix("\n")
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
log::debug!("raw ffprobe gave {:?}", possibly_new_frame_rate);
|
||||
|
||||
calculate_frame_rate(temp_path.clone(), possibly_new_frame_rate, false)
|
||||
} else {
|
||||
Ok(frames_per / per_second)
|
||||
}
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn upload_file(
|
||||
options: Query<Options>,
|
||||
|
@ -271,6 +317,7 @@ async fn upload_file(
|
|||
if let Some(file_contents) = maybe_file_contents {
|
||||
let file_type = maybe_file_type.unwrap();
|
||||
let file_name = maybe_file_name.unwrap();
|
||||
log::info!("file {} {}", file_type, file_name);
|
||||
let is_video = file_type.starts_with("video/")
|
||||
|| file_name.ends_with(".mp4")
|
||||
|| file_name.ends_with(".gif")
|
||||
|
@ -289,14 +336,15 @@ async fn upload_file(
|
|||
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::<Vec<_>>();
|
||||
let frame_rate: f64 =
|
||||
parts.get(0).unwrap().parse::<f64>()? / parts.get(1).unwrap().parse::<f64>()?;
|
||||
calculate_frame_rate(temp_file.path(), stream.r_frame_rate.clone(), true)?;
|
||||
|
||||
let total_frame_count =
|
||||
calculate_frame_count(temp_file.path(), &stream, &info.format, frame_rate)?;
|
||||
|
||||
log::debug!("total frame count = {}", total_frame_count);
|
||||
log::debug!("frame rate = {}", 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,
|
||||
|
@ -348,6 +396,10 @@ async fn upload_file(
|
|||
let response = WrappedResponse::Tags(final_tag_set.into_iter().collect::<Vec<_>>());
|
||||
Ok((StatusCode::OK, Json(response)))
|
||||
} else {
|
||||
if !file_type.starts_with("image/") {
|
||||
log::warn!("warning: mimetype {} is not image/", file_type);
|
||||
}
|
||||
|
||||
let json_response =
|
||||
send_image_to_dd(file_contents.to_vec(), file_name, &file_type, &options).await?;
|
||||
Ok((StatusCode::OK, Json(json_response)))
|
||||
|
|
Loading…
Reference in a new issue