add minimum subtitle size

This commit is contained in:
kralverde 2024-01-28 13:38:02 -05:00
parent 7cf7a8e71c
commit a7bef039fe
3 changed files with 29 additions and 5 deletions

View file

@ -114,6 +114,12 @@ pub struct Archive {
#[arg(long)] #[arg(long)]
pub(crate) include_fonts: bool, pub(crate) include_fonts: bool,
#[arg(
help = "If a subtitle byte size is less than this amount, it is not included in the downloaded file."
)]
#[arg(long, default_value_t = 0)]
pub(crate) meaningful_subtitle_size: u64,
#[arg(help = "Skip files which are already existing")] #[arg(help = "Skip files which are already existing")]
#[arg(long, default_value_t = false)] #[arg(long, default_value_t = false)]
pub(crate) skip_existing: bool, pub(crate) skip_existing: bool,
@ -213,6 +219,7 @@ impl Execute for Archive {
.output_format(Some("matroska".to_string())) .output_format(Some("matroska".to_string()))
.audio_sort(Some(self.audio.clone())) .audio_sort(Some(self.audio.clone()))
.subtitle_sort(Some(self.subtitle.clone())) .subtitle_sort(Some(self.subtitle.clone()))
.meaningful_subtitle_min_size(self.meaningful_subtitle_size)
.threads(self.threads); .threads(self.threads);
for single_formats in single_format_collection.into_iter() { for single_formats in single_format_collection.into_iter() {

View file

@ -104,6 +104,12 @@ pub struct Download {
#[arg(long, default_value_t = false)] #[arg(long, default_value_t = false)]
pub(crate) force_hardsub: bool, pub(crate) force_hardsub: bool,
#[arg(
help = "If a subtitle byte size is less than this amount, it is not included in the downloaded file."
)]
#[arg(long, default_value_t = 0)]
pub(crate) meaningful_subtitle_size: u64,
#[arg(help = "The number of threads used to download")] #[arg(help = "The number of threads used to download")]
#[arg(short, long, default_value_t = num_cpus::get())] #[arg(short, long, default_value_t = num_cpus::get())]
pub(crate) threads: usize, pub(crate) threads: usize,
@ -220,6 +226,7 @@ impl Execute for Download {
DownloadBuilder::new(ctx.client.clone(), ctx.rate_limiter.clone()) DownloadBuilder::new(ctx.client.clone(), ctx.rate_limiter.clone())
.default_subtitle(self.subtitle.clone()) .default_subtitle(self.subtitle.clone())
.force_hardsub(self.force_hardsub) .force_hardsub(self.force_hardsub)
.meaningful_subtitle_min_size(self.meaningful_subtitle_size)
.output_format(if is_special_file(&self.output) || self.output == "-" { .output_format(if is_special_file(&self.output) || self.output == "-" {
Some("mpegts".to_string()) Some("mpegts".to_string())
} else { } else {

View file

@ -14,6 +14,7 @@ use std::borrow::Borrow;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::io::Write; use std::io::Write;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::str::FromStr; use std::str::FromStr;
@ -56,6 +57,7 @@ pub struct DownloadBuilder {
output_format: Option<String>, output_format: Option<String>,
audio_sort: Option<Vec<Locale>>, audio_sort: Option<Vec<Locale>>,
subtitle_sort: Option<Vec<Locale>>, subtitle_sort: Option<Vec<Locale>>,
meaningful_subtitle_min_size: u64,
force_hardsub: bool, force_hardsub: bool,
download_fonts: bool, download_fonts: bool,
threads: usize, threads: usize,
@ -72,6 +74,7 @@ impl DownloadBuilder {
output_format: None, output_format: None,
audio_sort: None, audio_sort: None,
subtitle_sort: None, subtitle_sort: None,
meaningful_subtitle_min_size: 0,
force_hardsub: false, force_hardsub: false,
download_fonts: false, download_fonts: false,
threads: num_cpus::get(), threads: num_cpus::get(),
@ -88,6 +91,7 @@ impl DownloadBuilder {
output_format: self.output_format, output_format: self.output_format,
audio_sort: self.audio_sort, audio_sort: self.audio_sort,
subtitle_sort: self.subtitle_sort, subtitle_sort: self.subtitle_sort,
meaningful_subtitle_min_size: self.meaningful_subtitle_min_size,
force_hardsub: self.force_hardsub, force_hardsub: self.force_hardsub,
download_fonts: self.download_fonts, download_fonts: self.download_fonts,
@ -121,6 +125,7 @@ pub struct Downloader {
output_format: Option<String>, output_format: Option<String>,
audio_sort: Option<Vec<Locale>>, audio_sort: Option<Vec<Locale>>,
subtitle_sort: Option<Vec<Locale>>, subtitle_sort: Option<Vec<Locale>>,
meaningful_subtitle_min_size: u64,
force_hardsub: bool, force_hardsub: bool,
download_fonts: bool, download_fonts: bool,
@ -298,11 +303,16 @@ impl Downloader {
.then_some(format!(" for video {}", i)) .then_some(format!(" for video {}", i))
.unwrap_or_default() .unwrap_or_default()
); );
subtitles.push(FFmpegMeta {
path: subtitle_path, let subtitle_size = subtitle_path.metadata()?.size();
language: subtitle.locale.clone(), if subtitle_size > self.meaningful_subtitle_min_size {
title: subtitle_title, // Only add subtitles with meaningful information
}) subtitles.push(FFmpegMeta {
path: subtitle_path,
language: subtitle.locale.clone(),
title: subtitle_title,
});
}
} }
} }
videos.push(FFmpegMeta { videos.push(FFmpegMeta {