diff --git a/src/commanders/_cmd.rs b/src/commanders/_cmd.rs index db0f0bb..421fc76 100644 --- a/src/commanders/_cmd.rs +++ b/src/commanders/_cmd.rs @@ -1,4 +1,6 @@ use crate::cli::Cli; +use clap::ArgMatches; +use lazy_static::lazy_static; use path_slash::PathBufExt; use std::{ io::Error, @@ -8,17 +10,19 @@ use std::{ use term_painter::Color::BrightBlue; use term_painter::ToStyle; -pub fn merge(input: String, output: &String) -> Result { - let matches = Cli::init().get_matches(); - let verbose: bool = matches.is_present("verbose"); +lazy_static! { + static ref MATCHES: ArgMatches = Cli::init().get_matches(); + static ref VERBOSE: bool = MATCHES.is_present("verbose"); +} +pub fn merge(input: String, output: &String) -> Result { let cmd = format!( "ffmpeg -y -f concat -safe 0 -i {} -map 0 -c copy {}", input, output ); println!("šŸš€ Run Merger, calling: {}", BrightBlue.paint(&cmd)); - if verbose { + if *VERBOSE { execute_cmd(cmd) } else { execute_cmd_silently(cmd) @@ -30,9 +34,6 @@ pub fn merge_with_chapters( file_path: PathBuf, output_file_for_chapterer: &str, ) -> Result { - let matches = Cli::init().get_matches(); - let verbose: bool = matches.is_present("verbose"); - let cmd = format!( "ffmpeg -y -i {} -i {} -map 0 -map_metadata 1 -codec copy {}", &input_file_for_chapterer, @@ -41,7 +42,7 @@ pub fn merge_with_chapters( ); println!("šŸ“– Run Chapterer, calling: {}", BrightBlue.paint(&cmd)); - if verbose { + if *VERBOSE { execute_cmd(cmd) } else { execute_cmd_silently(cmd) @@ -59,9 +60,6 @@ pub fn adjust_fps_by_ffmpeg( fps_goal: &f32, new_file_location: PathBuf, ) -> PathBuf { - let matches = Cli::init().get_matches(); - let verbose: bool = matches.is_present("verbose"); - let cmd = format!( "ffmpeg -i {} -r {} {}", file_to_merge.to_str().unwrap(), @@ -73,7 +71,7 @@ pub fn adjust_fps_by_ffmpeg( // let res = execute_cmd(cmd).unwrap().wait_with_output(); // println!("{:?}", res); - if verbose { + if *VERBOSE { let res = execute_cmd(cmd).unwrap().wait_with_output(); println!("{:?}", res); } else { @@ -86,15 +84,12 @@ pub fn adjust_fps_by_ffmpeg( } pub fn get_media_seconds(media_path: &str) -> Result> { - let matches = Cli::init().get_matches(); - let verbose: bool = matches.is_present("verbose"); - let cmd = format!( "ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 '{}'", media_path ); - if verbose { + if *VERBOSE { println!( "šŸ“– Getting media seconds, calling: {}", BrightBlue.paint(&cmd) diff --git a/src/commanders/fps_changer.rs b/src/commanders/fps_changer.rs index bfb0057..377b382 100644 --- a/src/commanders/fps_changer.rs +++ b/src/commanders/fps_changer.rs @@ -7,19 +7,23 @@ use crate::{ str_helper::gen_input_file_content_for_ffmpeg, }, }; +use clap::ArgMatches; +use lazy_static::lazy_static; use std::{ collections::{HashMap, HashSet}, path::PathBuf, }; +lazy_static! { + static ref MATCHES: ArgMatches = Cli::init().get_matches(); + static ref VERBOSE: bool = MATCHES.is_present("verbose"); +} + pub fn change_fps( files_to_merge: Vec, tmp_dir: &PathBuf, fps_from_cli: f32, ) -> (Vec, Vec, std::string::String) { - let matches = Cli::init().get_matches(); - let verbose: bool = matches.is_present("verbose"); - let tmp_dir_for_fps_changer = create_dir_for_fps_changer(&tmp_dir).unwrap(); let mut new_files_to_merge = Vec::new(); @@ -71,7 +75,7 @@ pub fn change_fps( set.len() ); - if verbose { + if *VERBOSE { println!(); println!("Will be merged directly: \n"); for line in output_directly { diff --git a/src/helpers/io_helper.rs b/src/helpers/io_helper.rs index 1efb47c..3805731 100644 --- a/src/helpers/io_helper.rs +++ b/src/helpers/io_helper.rs @@ -1,3 +1,6 @@ +use crate::cli::Cli; +use clap::ArgMatches; +use lazy_static::lazy_static; use nanoid::nanoid; use std::env::temp_dir; use std::fs::{self, canonicalize, File}; @@ -5,7 +8,10 @@ use std::io::{self, Result, Write}; use std::path::{Path, PathBuf}; use std::process::exit; -use crate::cli::Cli; +lazy_static! { + static ref MATCHES: ArgMatches = Cli::init().get_matches(); + static ref VERBOSE: bool = MATCHES.is_present("verbose"); +} pub fn exit_when_ffmpeg_not_available() { if which::which("ffmpeg").is_err() { @@ -15,11 +21,8 @@ pub fn exit_when_ffmpeg_not_available() { } pub fn remove_file(path: &Path) -> Result<()> { - let matches = Cli::init().get_matches(); - let verbose: bool = matches.is_present("verbose"); - if Path::new(path).exists() { - if verbose { + if *VERBOSE { print!( "šŸ—‘ļø Removing old data: `{}`", path.file_name().unwrap().to_string_lossy() diff --git a/src/main.rs b/src/main.rs index e905a3d..591d39b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod helpers; use crate::commanders::fps_changer::change_fps; use crate::commanders::selector::select; use crate::helpers::str_helper::create_order_of_merging; +use clap::ArgMatches; use cli::Cli; use core::time; use helpers::io_helper::create; @@ -12,6 +13,7 @@ use helpers::io_helper::create_tmp_dir; use helpers::io_helper::exit_when_ffmpeg_not_available; use helpers::io_helper::remove_file; use helpers::str_helper::split; +use lazy_static::lazy_static; use path_slash::PathExt; use std::io::Error; use std::path::Path; @@ -20,6 +22,11 @@ use system_shutdown::shutdown; use term_painter::Color::BrightBlue; use term_painter::ToStyle; +lazy_static! { + static ref MATCHES: ArgMatches = Cli::init().get_matches(); + static ref VERBOSE: bool = MATCHES.is_present("verbose"); +} + fn main() -> Result<(), Error> { let matches = Cli::init().get_matches(); exit_when_ffmpeg_not_available(); @@ -33,7 +40,6 @@ fn main() -> Result<(), Error> { let skip_fps_changer = matches.is_present("skip-fps-changer"); let skip_chapterer = matches.is_present("skip-chapterer"); let skip_wait = matches.is_present("skip-wait"); - let verbose: bool = matches.is_present("verbose"); let fps_from_cli = matches .value_of("fps") .unwrap_or("0") @@ -49,7 +55,7 @@ fn main() -> Result<(), Error> { select(&file_format); if !ffmpeg_input_content.is_empty() { - if verbose { + if *VERBOSE { println!("\n\nšŸ“œ Order of merging:\n"); println!("{}\n", create_order_of_merging(&ffmpeg_input_content)); if !skip_wait {