use crate::dry_run::ShellScript; use crate::ffmpeg::FFmpegCommand; use std::io::Result; use std::path::{Path, PathBuf}; use std::fs; pub fn merge_video_and_audio(video_path: &Path, audio_path: &Path, dry_run: bool, overwrite: bool) -> Result { let origin_video_extension = video_path.extension().unwrap().to_str().unwrap(); let origin_filename = format!("original.{}", &origin_video_extension); let origin_path = video_path.parent().unwrap().join(origin_filename); let output_path = video_path.with_extension("mp4"); if dry_run { let mut script = ShellScript::new(); script.add_comment("Rename original video file"); script.add_variable("VIDEO_BACKUP", &origin_path); script.add_variable("VIDEO_OUTPUT", &output_path); script.add_command("mv \"$VIDEO_FILE\" \"$VIDEO_BACKUP\""); script.add_empty_line(); script.add_comment("Merge video and audio"); crate::dry_run::print_shell_script(&script); FFmpegCommand::new() .overwrite(overwrite) .input_var("VIDEO_BACKUP") .input_var("AUDIO_AAC") .codec("copy") .output_var("VIDEO_OUTPUT") .execute(true)?; return Ok(output_path); } println!("Renaming original video: {} -> {}", video_path.display(), origin_path.display()); fs::rename(video_path, &origin_path)?; println!("Merging video and audio:"); println!(" Video: {}", origin_path.display()); println!(" Audio: {}", audio_path.display()); println!(" Output: {}", output_path.display()); FFmpegCommand::new() .overwrite(overwrite) .input(&origin_path) .input(audio_path) .codec("copy") .output(&output_path) .execute(false)?; println!("Merge completed: {}", output_path.display()); Ok(output_path) }