Mark CC subtitle track as CC & grab normal subtitles and CC when using -m audio (#141)

This commit is contained in:
ByteDream 2023-04-17 18:14:54 +02:00
parent c0e2df4804
commit 847c6a1abc
4 changed files with 38 additions and 26 deletions

View file

@ -242,10 +242,16 @@ async fn get_format(
}
};
let subtitles: Vec<Subtitle> = 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::<HashMap<Locale, Subtitle>>()
.into_values()
.collect(),
}),
MergeBehavior::Auto => {

View file

@ -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))

View file

@ -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<Subtitle>,
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 {

View file

@ -301,7 +301,7 @@ pub struct Format {
impl Format {
pub fn from_single_formats(
mut single_formats: Vec<(SingleFormat, VariantData, Vec<Subtitle>)>,
mut single_formats: Vec<(SingleFormat, VariantData, Vec<(Subtitle, bool)>)>,
) -> Self {
let locales: Vec<(Locale, Vec<Locale>)> = 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::<Vec<Locale>>(),
)
})