diff --git a/crunchy-cli-core/src/archive/command.rs b/crunchy-cli-core/src/archive/command.rs index 580276b..a048685 100644 --- a/crunchy-cli-core/src/archive/command.rs +++ b/crunchy-cli-core/src/archive/command.rs @@ -242,10 +242,16 @@ async fn get_format( } }; - let subtitles: Vec = archive + let subtitles: Vec<(Subtitle, bool)> = archive .subtitle .iter() - .filter_map(|s| stream.subtitles.get(s).cloned()) + .filter_map(|s| { + stream + .subtitles + .get(s) + .cloned() + .map(|l| (l, single_format.audio == Locale::ja_JP)) + }) .collect(); format_pairs.push((single_format, video.clone(), audio, subtitles.clone())); @@ -278,9 +284,6 @@ async fn get_format( subtitles: format_pairs .iter() .flat_map(|(_, _, _, subtitles)| subtitles.clone()) - .map(|s| (s.locale.clone(), s)) - .collect::>() - .into_values() .collect(), }), MergeBehavior::Auto => { diff --git a/crunchy-cli-core/src/download/command.rs b/crunchy-cli-core/src/download/command.rs index d18320a..142bf9c 100644 --- a/crunchy-cli-core/src/download/command.rs +++ b/crunchy-cli-core/src/download/command.rs @@ -203,12 +203,14 @@ async fn get_format( let download_format = DownloadFormat { video: (video.clone(), single_format.audio.clone()), audios: vec![(audio, single_format.audio.clone())], - subtitles: subtitle.clone().map_or(vec![], |s| vec![s]), + subtitles: subtitle + .clone() + .map_or(vec![], |s| vec![(s, single_format.audio == Locale::ja_JP)]), }; let format = Format::from_single_formats(vec![( single_format.clone(), video, - subtitle.map_or(vec![], |s| vec![s]), + subtitle.map_or(vec![], |s| vec![(s, single_format.audio == Locale::ja_JP)]), )]); Ok((download_format, format)) diff --git a/crunchy-cli-core/src/utils/download.rs b/crunchy-cli-core/src/utils/download.rs index f0f51c1..57baf83 100644 --- a/crunchy-cli-core/src/utils/download.rs +++ b/crunchy-cli-core/src/utils/download.rs @@ -11,6 +11,7 @@ use log::{debug, warn, LevelFilter}; use regex::Regex; use std::borrow::Borrow; use std::borrow::BorrowMut; +use std::cmp::Ordering; use std::collections::BTreeMap; use std::env; use std::io::Write; @@ -81,7 +82,7 @@ struct FFmpegMeta { pub struct DownloadFormat { pub video: (VariantData, Locale), pub audios: Vec<(VariantData, Locale)>, - pub subtitles: Vec, + pub subtitles: Vec<(Subtitle, bool)>, } pub struct Downloader { @@ -144,12 +145,19 @@ impl Downloader { }) } if let Some(subtitle_sort) = &self.subtitle_sort { - format.subtitles.sort_by(|a, b| { - subtitle_sort - .iter() - .position(|l| l == &a.locale) - .cmp(&subtitle_sort.iter().position(|l| l == &b.locale)) - }) + format + .subtitles + .sort_by(|(a_subtitle, a_not_cc), (b_subtitle, b_not_cc)| { + let ordering = subtitle_sort + .iter() + .position(|l| l == &a_subtitle.locale) + .cmp(&subtitle_sort.iter().position(|l| l == &b_subtitle.locale)); + if matches!(ordering, Ordering::Equal) { + a_not_cc.cmp(b_not_cc).reverse() + } else { + ordering + } + }) } } @@ -191,20 +199,19 @@ impl Downloader { }) } let len = get_video_length(&video_path)?; - for subtitle in format.subtitles.iter() { + for (subtitle, not_cc) in format.subtitles.iter() { let subtitle_path = self.download_subtitle(subtitle.clone(), len).await?; + let mut subtitle_title = subtitle.locale.to_human_readable(); + if !not_cc { + subtitle_title += " (CC)" + } + if i != 0 { + subtitle_title += &format!(" [Video: #{}]", i + 1) + } subtitles.push(FFmpegMeta { path: subtitle_path, language: subtitle.locale.clone(), - title: if i == 0 { - subtitle.locale.to_human_readable() - } else { - format!( - "{} [Video: #{}]", - subtitle.locale.to_human_readable(), - i + 1 - ) - }, + title: subtitle_title, }) } videos.push(FFmpegMeta { diff --git a/crunchy-cli-core/src/utils/format.rs b/crunchy-cli-core/src/utils/format.rs index c6f3168..2f546df 100644 --- a/crunchy-cli-core/src/utils/format.rs +++ b/crunchy-cli-core/src/utils/format.rs @@ -301,7 +301,7 @@ pub struct Format { impl Format { pub fn from_single_formats( - mut single_formats: Vec<(SingleFormat, VariantData, Vec)>, + mut single_formats: Vec<(SingleFormat, VariantData, Vec<(Subtitle, bool)>)>, ) -> Self { let locales: Vec<(Locale, Vec)> = single_formats .iter() @@ -310,7 +310,7 @@ impl Format { single_format.audio.clone(), subtitles .into_iter() - .map(|s| s.locale.clone()) + .map(|(s, _)| s.locale.clone()) .collect::>(), ) })