From a41a98162219e8940727a842baa0e6eb51e86be5 Mon Sep 17 00:00:00 2001 From: Thomas Gotwig Date: Sun, 26 May 2024 11:47:12 +0200 Subject: [PATCH] =?UTF-8?q?=E2=8F=B3=20Add=20fps=5Fchanger=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commanders/fps_changer.rs | 98 +++++++++++++++++++++++++++++++++++ src/commanders/mod.rs | 1 + src/main.rs | 94 +-------------------------------- 3 files changed, 100 insertions(+), 93 deletions(-) create mode 100644 src/commanders/fps_changer.rs diff --git a/src/commanders/fps_changer.rs b/src/commanders/fps_changer.rs new file mode 100644 index 0000000..9742a6d --- /dev/null +++ b/src/commanders/fps_changer.rs @@ -0,0 +1,98 @@ +use super::fps_reader::get_fps; +use crate::{ + commanders, + helpers::{ + io_helper::path_bufs_to_sorted_strings, str_helper::gen_input_file_content_for_ffmpeg, + }, +}; +use std::{ + collections::{HashMap, HashSet}, + path::{Path, PathBuf}, +}; + +pub fn change_fps( + files_to_merge: Vec, + tmp_dir: &Path, + fps_from_cli: f32, +) -> (Vec, Vec, std::string::String) { + let mut new_files_to_merge = Vec::new(); + let mut map: HashMap<&PathBuf, f32> = HashMap::new(); + + for file_to_merge in &files_to_merge { + map.insert(file_to_merge, get_fps(file_to_merge)); + } + + let fps_goal = if fps_from_cli != 0. { + fps_from_cli + } else { + *map.values() + .min_by(|x, y| x.partial_cmp(y).unwrap()) + .unwrap() + }; + + let set: HashSet = map.values().map(|value| value.to_string()).collect(); + let files_to_merge = if set.len() > 1 { + println!("----------------------------------------------------------------"); + println!("🔎 FPS mismatches detected"); + println!(); + println!("Will be merged directly: \n"); + let mut output = Vec::new(); + for (key, value) in &map { + if value == &fps_goal { + output.push(format!( + "- {} ({} fps)", + key.file_name().unwrap().to_string_lossy(), + value + )); + } + } + output.sort(); + for line in output { + println!("{}", line); + } + println!(); + println!("Will be merged indirectly, generating new files from listed below with {} fps and merges with listed above:", fps_goal); + println!(); + let mut output = Vec::new(); + for (key, value) in &map { + if value != &fps_goal { + output.push(format!( + "- {} ({} fps)", + key.file_name().unwrap().to_string_lossy(), + value + )); + } + } + output.sort(); + for line in output { + println!("{}", line); + } + println!("----------------------------------------------------------------"); + println!("🚀 Start FPS Changer, calling:"); + println!(); + for file_to_merge in files_to_merge { + let fps = get_fps(&file_to_merge); + + if fps != fps_goal { + let new_file_to_merge = + commanders::fps_adjuster::adjust_fps(file_to_merge, &fps_goal, tmp_dir); + new_files_to_merge.push(new_file_to_merge); + } else { + new_files_to_merge.push(file_to_merge); + } + } + + new_files_to_merge + } else { + files_to_merge + }; + + let files_to_merge_as_strings = path_bufs_to_sorted_strings(&files_to_merge); + let ffmpeg_input_content = gen_input_file_content_for_ffmpeg(&files_to_merge_as_strings); + + ( + files_to_merge, + files_to_merge_as_strings, + ffmpeg_input_content, + ) +} diff --git a/src/commanders/mod.rs b/src/commanders/mod.rs index aae0edd..70938cb 100644 --- a/src/commanders/mod.rs +++ b/src/commanders/mod.rs @@ -2,6 +2,7 @@ mod _cmd; pub mod chapterer; pub mod fps_adjuster; +pub mod fps_changer; pub mod fps_reader; pub mod merger; pub mod selector; diff --git a/src/main.rs b/src/main.rs index 8e26243..bc1e042 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ mod cli; mod commanders; mod helpers; -use crate::commanders::fps_reader::get_fps; +use crate::commanders::fps_changer::change_fps; use crate::commanders::selector::select; use crate::helpers::str_helper::create_order_of_merging; use cli::Cli; @@ -10,16 +10,11 @@ use core::time; use helpers::io_helper::create; use helpers::io_helper::create_tmp_dir; use helpers::io_helper::exit_when_ffmpeg_not_available; -use helpers::io_helper::path_bufs_to_sorted_strings; use helpers::io_helper::remove_file; -use helpers::str_helper::gen_input_file_content_for_ffmpeg; use helpers::str_helper::split; use path_slash::PathExt; -use std::collections::HashMap; -use std::collections::HashSet; use std::io::Error; use std::path::Path; -use std::path::PathBuf; use std::thread; use system_shutdown::shutdown; @@ -98,90 +93,3 @@ fn main() -> Result<(), Error> { Ok(()) } } - -pub fn change_fps( - files_to_merge: Vec, - tmp_dir: &Path, - fps_from_cli: f32, -) -> (Vec, Vec, std::string::String) { - let mut new_files_to_merge = Vec::new(); - let mut map: HashMap<&PathBuf, f32> = HashMap::new(); - - for file_to_merge in &files_to_merge { - map.insert(file_to_merge, get_fps(file_to_merge)); - } - - let fps_goal = if fps_from_cli != 0. { - fps_from_cli - } else { - *map.values() - .min_by(|x, y| x.partial_cmp(y).unwrap()) - .unwrap() - }; - - let set: HashSet = map.values().map(|value| value.to_string()).collect(); - let files_to_merge = if set.len() > 1 { - println!("----------------------------------------------------------------"); - println!("🔎 FPS mismatches detected"); - println!(); - println!("Will be merged directly: \n"); - let mut output = Vec::new(); - for (key, value) in &map { - if value == &fps_goal { - output.push(format!( - "- {} ({} fps)", - key.file_name().unwrap().to_string_lossy(), - value - )); - } - } - output.sort(); - for line in output { - println!("{}", line); - } - println!(); - println!("Will be merged indirectly, generating new files from listed below with {} fps and merges with listed above:", fps_goal); - println!(); - let mut output = Vec::new(); - for (key, value) in &map { - if value != &fps_goal { - output.push(format!( - "- {} ({} fps)", - key.file_name().unwrap().to_string_lossy(), - value - )); - } - } - output.sort(); - for line in output { - println!("{}", line); - } - println!("----------------------------------------------------------------"); - println!("🚀 Start FPS Changer, calling:"); - println!(); - for file_to_merge in files_to_merge { - let fps = get_fps(&file_to_merge); - - if fps != fps_goal { - let new_file_to_merge = - commanders::fps_adjuster::adjust_fps(file_to_merge, &fps_goal, tmp_dir); - new_files_to_merge.push(new_file_to_merge); - } else { - new_files_to_merge.push(file_to_merge); - } - } - - new_files_to_merge - } else { - files_to_merge - }; - - let files_to_merge_as_strings = path_bufs_to_sorted_strings(&files_to_merge); - let ffmpeg_input_content = gen_input_file_content_for_ffmpeg(&files_to_merge_as_strings); - - ( - files_to_merge, - files_to_merge_as_strings, - ffmpeg_input_content, - ) -}