Skip to content

Commit

Permalink
πŸŽ‰ Add --verbose (major logging changes)
Browse files Browse the repository at this point in the history
closes #26
  • Loading branch information
tgotwig committed May 26, 2024
1 parent 5538033 commit 85e4719
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 61 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## πŸŽ‰ [Unreleased]

### Added

- **--verbose** which prints detailed logs.

## πŸŽ‰ [0.3.2] - 2024-01-14

### Added
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ OPTIONS:
--skip-chapterer Skips the chapterer
--skip-fps-changer Skips the fps changer
--skip-wait Skips the wait time for reading
-V, --version Print version information```
-V, --version Print version information
--verbose Prints detailed logs
```

## ✨ Installing / Getting started
Expand Down
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl Cli {
.long("skip-wait")
.help("Skips the wait time for reading")
)
.arg(Arg::new("verbose")
.long("verbose")
.help("Prints detailed logs")
)
.arg_required_else_help(true)
.get_matches();

Expand Down
74 changes: 61 additions & 13 deletions src/commanders/_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
use crate::cli::Cli;
use path_slash::PathBufExt;
use std::{
io::Error,
path::PathBuf,
process::{Child, Command, Output, Stdio},
};
use term_painter::Color::BrightBlue;
use term_painter::ToStyle;

use path_slash::PathBufExt;
pub fn merge(input: String, output: &String) -> Result<Child, std::io::Error> {
let matches = Cli::init().get_matches();
let verbose: bool = matches.is_present("verbose");

pub fn merge(input: String, output: String) -> Result<Child, std::io::Error> {
let cmd = format!(
"ffmpeg -y -f concat -safe 0 -i {} -map 0 -c copy {}",
input, output
);

println!("πŸš€ Start Merger, calling: `{}`\n", cmd);
execute_cmd(cmd)
println!("πŸš€ Run Merger, calling: {}", BrightBlue.paint(&cmd));
if verbose {
execute_cmd(cmd)
} else {
execute_cmd_silently(cmd)
}
}

pub fn merge_with_chapters(
input_file_for_chapterer: &str,
file_path: PathBuf,
output_file_for_chapterer: &str,
) -> Result<Child, std::io::Error> {
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,
file_path.to_str().unwrap(),
output_file_for_chapterer
);

println!("πŸš€ Calling:\n");
println!("- {}\n", cmd);

execute_cmd(cmd)
println!("πŸ“– Run Chapterer, calling: {}", BrightBlue.paint(&cmd));
if verbose {
execute_cmd(cmd)
} else {
execute_cmd_silently(cmd)
}
}

pub fn run_ffmpeg_info_command(file_to_merge: &PathBuf) -> Result<Output, Error> {
Expand All @@ -45,27 +59,47 @@ 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(),
fps_goal,
new_file_location.to_str().unwrap()
);
println!("- {}", cmd);
println!("πŸš€ Start FPS Changer, calling: {}", BrightBlue.paint(&cmd));

// let res = execute_cmd(cmd).unwrap().wait_with_output();
// println!("{:?}", res);

let res = execute_cmd(cmd).unwrap().wait_with_output();
println!("{:?}", res);
if verbose {
let res = execute_cmd(cmd).unwrap().wait_with_output();
println!("{:?}", res);
} else {
execute_cmd_silently(cmd)
.unwrap()
.wait_with_output()
.unwrap();
}
new_file_location
}

pub fn get_media_seconds(media_path: &str) -> Result<f64, Box<Error>> {
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
);

println!("πŸš€ Calling:\n");
println!("- {}\n", cmd);
if verbose {
println!(
"πŸ“– Getting media seconds, calling: {}",
BrightBlue.paint(&cmd)
);
}
let res = execute_cmd(cmd);

let output = res.unwrap().wait_with_output().unwrap();
Expand All @@ -86,3 +120,17 @@ fn execute_cmd(cmd: String) -> Result<Child, std::io::Error> {
.stdout(Stdio::piped())
.spawn()
}

fn execute_cmd_silently(cmd: String) -> Result<Child, std::io::Error> {
let (interpreter, arg) = if cfg!(target_os = "windows") {
("powershell", "/c")
} else {
("sh", "-c")
};
Command::new(interpreter)
.arg(arg)
.arg(cmd)
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
}
26 changes: 19 additions & 7 deletions src/commanders/chapterer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::str;
use term_painter::Color::BrightBlue;
use term_painter::ToStyle;

pub fn execute(
files_to_merge_as_strings: Vec<String>,
tmp_dir: PathBuf,
ffmpeg_output_file: PathBuf,
file_format: &String,
) {
println!("----------------------------------------------------------------");
println!("πŸ“– Start Chapterer\n");

let mut start_time = 0;
let mut metadata_string = String::from(";FFMETADATA1\n");

Expand All @@ -40,7 +39,8 @@ pub fn execute(

let input_file_for_chapterer: String = ffmpeg_output_file.to_slash().unwrap().to_string();
let mut output_with_chapters = ffmpeg_output_file.clone();
output_with_chapters.set_file_name(format!("output_with_chapters.{}", file_format));
let output_file_name = format!("output_with_chapters.{}", file_format);
output_with_chapters.set_file_name(&output_file_name);
let output_file_for_chapterer: String = output_with_chapters.to_slash().unwrap().to_string();

_cmd::merge_with_chapters(
Expand All @@ -51,11 +51,23 @@ pub fn execute(
.unwrap()
.wait_with_output()
.unwrap();
println!(
"βœ… Successfully generated: {}",
BrightBlue.paint(&output_file_for_chapterer)
);

fs::remove_file(Path::new(&input_file_for_chapterer)).unwrap();
fs::rename(output_file_for_chapterer, ffmpeg_output_file).unwrap();

println!("βœ… Video with chapters created successfully.");
println!(
"βœ… Successfully deleted: {}",
BrightBlue.paint(&input_file_for_chapterer)
);

fs::rename(&output_file_for_chapterer, ffmpeg_output_file).unwrap();
println!(
"βœ… Successfully renamed: {} to {}",
BrightBlue.paint(output_file_for_chapterer),
BrightBlue.paint(input_file_for_chapterer)
);
}

fn extract_title(path: &str, file_format: &str) -> String {
Expand Down
55 changes: 34 additions & 21 deletions src/commanders/fps_changer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::fps_reader::get_fps;
use crate::{
cli::Cli,
commanders,
helpers::{
io_helper::path_bufs_to_sorted_strings, str_helper::gen_input_file_content_for_ffmpeg,
Expand All @@ -15,6 +16,9 @@ pub fn change_fps(
tmp_dir: &Path,
fps_from_cli: f32,
) -> (Vec<PathBuf>, Vec<std::string::String>, std::string::String) {
let matches = Cli::init().get_matches();
let verbose: bool = matches.is_present("verbose");

let mut new_files_to_merge = Vec::new();
let mut map: HashMap<&PathBuf, f32> = HashMap::new();

Expand All @@ -32,44 +36,53 @@ pub fn change_fps(

let set: HashSet<String> = 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();
let mut output_directly = Vec::new();
for (key, value) in &map {
if value == &fps_goal {
output.push(format!(
output_directly.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();
output_directly.sort();

let mut output_indirectly = Vec::new();
for (key, value) in &map {
if value != &fps_goal {
output.push(format!(
output_indirectly.push(format!(
"- {} ({} fps)",
key.file_name().unwrap().to_string_lossy(),
value
));
}
}
output.sort();
for line in output {
println!("{}", line);
output_indirectly.sort();

println!(
"πŸ”Ž FPS mismatches detected ({:?}), scaling to {} fps: {}/{}",
set,
fps_goal,
output_indirectly.len(),
set.len()
);

if verbose {
println!();
println!("Will be merged directly: \n");
for line in output_directly {
println!("{}", line);
}
println!();
println!("Will be merged indirectly, generating new files from listed below with {} fps and merges with listed above:", fps_goal);
println!();
for line in output_indirectly {
println!("{}", line);
}
println!();
}
println!("----------------------------------------------------------------");
println!("πŸš€ Start FPS Changer, calling:");
println!();

for file_to_merge in files_to_merge {
let fps = get_fps(&file_to_merge);

Expand Down
12 changes: 5 additions & 7 deletions src/commanders/merger.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::commanders::_cmd;
use term_painter::Color::BrightBlue;
use term_painter::ToStyle;

pub fn merge(input: String, output: String, file_format: &String) {
let child = _cmd::merge(input, output);
pub fn merge(input: String, output: String) {
let child = _cmd::merge(input, &output);

let res = child.unwrap().wait_with_output();
println!("{:?}", res);

println!("----------------------------------------------------------------");
if res.is_ok() {
println!("βœ… Successfully generated:");
println!();
println!("- output.{}", file_format);
println!("βœ… Successfully generated: {}", BrightBlue.paint(output));
} else {
panic!("❌ Something went wrong: \n\n{}", res.unwrap_err());
}
Expand Down
16 changes: 11 additions & 5 deletions src/helpers/io_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::io::{Result, Write};
use std::path::{Path, PathBuf};
use std::process::exit;

use crate::cli::Cli;

pub fn exit_when_ffmpeg_not_available() {
if which::which("ffmpeg").is_err() {
eprintln!("❌ ffmpeg is not available. Please install it first.");
Expand All @@ -13,12 +15,16 @@ 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() {
println!("----------------------------------------------------------------");
print!(
"πŸ—‘οΈ Removing old data:\n\n- {}",
path.file_name().unwrap().to_string_lossy()
);
if verbose {
print!(
"πŸ—‘οΈ Removing old data: `{}`",
path.file_name().unwrap().to_string_lossy()
);
}
fs::remove_file(path)?;
}
Ok(())
Expand Down
Loading

0 comments on commit 85e4719

Please sign in to comment.