feat: origin filename support.
This commit is contained in:
+58
-10
@@ -21,17 +21,65 @@ pub fn find_largest_video_file(path: &Path) -> Result<PathBuf> {
|
||||
}
|
||||
|
||||
pub fn find_audio_file(video_path: &Path) -> Result<PathBuf> {
|
||||
let video_name = video_path.file_name().unwrap().to_str().unwrap();
|
||||
let video_extension = video_path.extension().unwrap();
|
||||
let audio_name = format!("{}.m4a", &video_name[..video_name.len() - video_extension.len() - 1]);
|
||||
let audio_path = video_path.parent().unwrap().join(audio_name);
|
||||
let parent = video_path.parent().unwrap();
|
||||
let video_extension = video_path.extension().and_then(|e| e.to_str());
|
||||
|
||||
if !audio_path.exists() {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
format!("Audio file does not exist: {}", audio_path.display())
|
||||
));
|
||||
// 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);
|
||||
}
|
||||
|
||||
Ok(audio_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<PathBuf> {
|
||||
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user