more robust frame rate calculation

This commit is contained in:
Luna 2023-06-13 23:46:57 -03:00
parent cb708d17fd
commit fd9e7f6d68

View file

@ -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,