use std::io::Result; use std::path::{Path, PathBuf}; pub fn find_largest_video_file(path: &Path) -> Result { let mut largest_size = 0; let mut largest_file = PathBuf::from(path); for entry in std::fs::read_dir(path)? { let entry_path = entry?.path().canonicalize()?; if entry_path.is_dir() { continue; } let size = entry_path.metadata()?.len(); if size >= largest_size { largest_size = size; largest_file = entry_path.into(); } } Ok(largest_file) } pub fn find_audio_file(video_path: &Path) -> Result { let parent = video_path.parent().unwrap(); let video_extension = video_path.extension().and_then(|e| e.to_str()); // Try to find .m4a file first (old format) let video_name = video_path.file_stem().and_then(|n| n.to_str()).unwrap_or(""); let audio_m4a_path = parent.join(format!("{}.m4a", video_name)); if audio_m4a_path.exists() { return Ok(audio_m4a_path); } // Try to find another .m4s file (new format) if video_extension == Some("m4s") { let mut audio_candidates = Vec::new(); for entry in std::fs::read_dir(parent)? { let entry_path = entry?.path(); if entry_path.is_file() { if let Some(ext) = entry_path.extension().and_then(|e| e.to_str()) { if ext == "m4s" && entry_path != video_path { let size = entry_path.metadata()?.len(); audio_candidates.push((entry_path, size)); } } } } if !audio_candidates.is_empty() { // Return the largest .m4s file that's not the video file audio_candidates.sort_by(|a, b| b.1.cmp(&a.1)); return Ok(audio_candidates[0].0.clone()); } } Err(std::io::Error::new( std::io::ErrorKind::NotFound, format!("Audio file does not exist. Looked for: {}", audio_m4a_path.display()) )) } pub fn find_ass_file(path: &Path) -> Result { for entry in std::fs::read_dir(path)? { let entry_path = entry?.path(); if entry_path.is_file() { if let Some(ext) = entry_path.extension().and_then(|e| e.to_str()) { if ext == "ass" { return Ok(entry_path); } } } } Err(std::io::Error::new( std::io::ErrorKind::NotFound, "ASS subtitle file not found in directory" )) } pub fn get_output_name_from_ass(ass_path: &Path) -> String { ass_path.file_stem() .and_then(|n| n.to_str()) .unwrap_or("output") .to_string() }