83 lines
3.0 KiB
Rust
83 lines
3.0 KiB
Rust
use crate::audio_converter::convert_audio_to_aac;
|
|
use crate::dry_run::{print_shell_script_header, ShellScript};
|
|
use crate::file_finder::{find_audio_file, find_ass_file, find_largest_video_file, get_output_name_from_ass};
|
|
use crate::video_merger::merge_video_and_audio;
|
|
use std::io::Result;
|
|
use std::path::Path;
|
|
|
|
pub fn merge_video_from_path(path: &Path, dry_run: bool, overwrite: bool) -> Result<()> {
|
|
if dry_run {
|
|
println!("=== DRY RUN MODE ===");
|
|
println!("This is a dry run. Below is the shell script that would be executed:");
|
|
println!();
|
|
print_shell_script_header();
|
|
}
|
|
|
|
println!("Step 1: Searching for video file in: {}", path.display());
|
|
let video_path = find_largest_video_file(path)?;
|
|
println!("Found video file: {}", video_path.display());
|
|
|
|
if dry_run {
|
|
let mut script = ShellScript::new();
|
|
script.add_comment("File paths");
|
|
script.add_variable("VIDEO_FILE", &video_path);
|
|
crate::dry_run::print_shell_script(&script);
|
|
}
|
|
|
|
println!("Step 2: Looking for corresponding audio file...");
|
|
let audio_path = find_audio_file(&video_path)?;
|
|
println!("Found audio file: {}", audio_path.display());
|
|
|
|
if dry_run {
|
|
let mut script = ShellScript::new();
|
|
script.add_variable("AUDIO_FILE", &audio_path);
|
|
crate::dry_run::print_shell_script(&script);
|
|
}
|
|
|
|
println!("Step 3: Looking for ASS subtitle file to determine output name...");
|
|
let output_name = match find_ass_file(path) {
|
|
Ok(ass_path) => {
|
|
println!("Found ASS file: {}", ass_path.display());
|
|
let name = get_output_name_from_ass(&ass_path);
|
|
println!("Output name will be: {}", name);
|
|
if dry_run {
|
|
let mut script = ShellScript::new();
|
|
script.add_variable("ASS_FILE", &ass_path);
|
|
script.add_variable_str("OUTPUT_NAME", &name);
|
|
crate::dry_run::print_shell_script(&script);
|
|
}
|
|
name
|
|
}
|
|
Err(_) => {
|
|
println!("No ASS file found, using video file name as output name");
|
|
let name = video_path.file_stem()
|
|
.and_then(|n| n.to_str())
|
|
.unwrap_or("output")
|
|
.to_string();
|
|
println!("Output name will be: {}", name);
|
|
name
|
|
}
|
|
};
|
|
|
|
println!();
|
|
println!("Step 4: Converting audio to AAC format...");
|
|
let aac_path = convert_audio_to_aac(&audio_path, dry_run, overwrite)?;
|
|
println!();
|
|
|
|
println!("Step 5: Merging video and audio...");
|
|
let output_path = merge_video_and_audio(&video_path, &aac_path, &output_name, path, dry_run, overwrite)?;
|
|
|
|
if dry_run {
|
|
println!();
|
|
println!("=== DRY RUN COMPLETE ===");
|
|
println!("To actually perform these operations, run without -n flag.");
|
|
println!("You can also copy the shell script above and execute it manually.");
|
|
} else {
|
|
println!();
|
|
println!("=== MERGE COMPLETE ===");
|
|
println!("Video file: {}", output_path.display());
|
|
}
|
|
|
|
Ok(())
|
|
}
|