Add progress indicator for subtitle download

This commit is contained in:
bytedream 2023-07-12 21:15:01 +02:00
parent 751735477c
commit 49de7bbba9
2 changed files with 49 additions and 20 deletions

View file

@ -204,22 +204,51 @@ impl Downloader {
}, },
}) })
} }
if !format.subtitles.is_empty() {
#[cfg(not(windows))]
let pb = ProgressBar::new_spinner()
.with_style(
ProgressStyle::with_template(
format!(
":: {:<1$} {{msg}} {{spinner}}",
"Downloading subtitles", fmt_space
)
.as_str(),
)
.unwrap()
.tick_strings(&["", "\\", "|", "/", ""]),
)
.with_finish(ProgressFinish::Abandon);
pb.enable_steady_tick(Duration::from_millis(100));
let len = get_video_length(&video_path)?; let len = get_video_length(&video_path)?;
for (subtitle, not_cc) in format.subtitles.iter() { for (subtitle, not_cc) in format.subtitles.iter() {
let subtitle_path = self.download_subtitle(subtitle.clone(), len).await?; let mut progress_message = pb.message();
if !progress_message.is_empty() {
progress_message += ", "
}
progress_message += &subtitle.locale.to_string();
let mut subtitle_title = subtitle.locale.to_human_readable(); let mut subtitle_title = subtitle.locale.to_human_readable();
if !not_cc { if !not_cc {
progress_message += " (CC)";
subtitle_title += " (CC)" subtitle_title += " (CC)"
} }
if i != 0 { if i != 0 {
progress_message += &format!(" [Video: #{}]", i + 1);
subtitle_title += &format!(" [Video: #{}]", i + 1) subtitle_title += &format!(" [Video: #{}]", i + 1)
} }
pb.set_message(progress_message);
let subtitle_path = self.download_subtitle(subtitle.clone(), len).await?;
subtitles.push(FFmpegMeta { subtitles.push(FFmpegMeta {
path: subtitle_path, path: subtitle_path,
language: subtitle.locale.clone(), language: subtitle.locale.clone(),
title: subtitle_title, title: subtitle_title,
}) })
} }
}
videos.push(FFmpegMeta { videos.push(FFmpegMeta {
path: video_path, path: video_path,
language: format.video.1.clone(), language: format.video.1.clone(),
@ -454,7 +483,7 @@ impl Downloader {
let tempfile = tempfile(".mp4")?; let tempfile = tempfile(".mp4")?;
let (mut file, path) = tempfile.into_parts(); let (mut file, path) = tempfile.into_parts();
download_segments(ctx, &mut file, Some(message), variant_data).await?; download_segments(ctx, &mut file, message, variant_data).await?;
Ok(path) Ok(path)
} }
@ -468,7 +497,7 @@ impl Downloader {
let tempfile = tempfile(".m4a")?; let tempfile = tempfile(".m4a")?;
let (mut file, path) = tempfile.into_parts(); let (mut file, path) = tempfile.into_parts();
download_segments(ctx, &mut file, Some(message), variant_data).await?; download_segments(ctx, &mut file, message, variant_data).await?;
Ok(path) Ok(path)
} }
@ -494,7 +523,7 @@ impl Downloader {
pub async fn download_segments( pub async fn download_segments(
ctx: &Context, ctx: &Context,
writer: &mut impl Write, writer: &mut impl Write,
message: Option<String>, message: String,
variant_data: &VariantData, variant_data: &VariantData,
) -> Result<()> { ) -> Result<()> {
let segments = variant_data.segments().await?; let segments = variant_data.segments().await?;
@ -514,7 +543,7 @@ pub async fn download_segments(
.unwrap() .unwrap()
.progress_chars("##-"), .progress_chars("##-"),
) )
.with_message(message.map(|m| m + " ").unwrap_or_default()) .with_message(message)
.with_finish(ProgressFinish::Abandon); .with_finish(ProgressFinish::Abandon);
Some(progress) Some(progress)
} else { } else {

View file

@ -169,7 +169,7 @@ impl CliLogger {
let finish_str = ""; let finish_str = "";
#[cfg(windows)] #[cfg(windows)]
// windows does not support all unicode characters by default in their consoles, so // windows does not support all unicode characters by default in their consoles, so
// we're using this (square root?) symbol instead. microsoft. // we're using this (square root) symbol instead. microsoft.
let finish_str = ""; let finish_str = "";
let pb = ProgressBar::new_spinner(); let pb = ProgressBar::new_spinner();