Add --threads (-t) option to downloading commands (#256)

* Add `single-threaded` option to downloading commands

* Replace `--single-threaded` boolean option with `--threads` optional `usize` option

* Simplify `threads` field unwrapping

* Make `--threads` `usize` with a default value
This commit is contained in:
Valentine Briese 2023-10-15 11:52:53 -07:00 committed by GitHub
parent 13335c020b
commit bbb5a78765
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 166 additions and 149 deletions

View file

@ -98,6 +98,10 @@ pub struct Archive {
#[arg(short, long, default_value_t = false)]
pub(crate) yes: bool,
#[arg(help = "The number of threads used to download")]
#[arg(short, long, default_value_t = num_cpus::get())]
pub(crate) threads: usize,
#[arg(help = "Crunchyroll series url(s)")]
#[arg(required = true)]
pub(crate) urls: Vec<String>,
@ -158,7 +162,8 @@ impl Execute for Archive {
.ffmpeg_preset(self.ffmpeg_preset.clone().unwrap_or_default())
.output_format(Some("matroska".to_string()))
.audio_sort(Some(self.audio.clone()))
.subtitle_sort(Some(self.subtitle.clone()));
.subtitle_sort(Some(self.subtitle.clone()))
.threads(self.threads);
for single_formats in single_format_collection.into_iter() {
let (download_formats, mut format) = get_format(&self, &single_formats).await?;

View file

@ -80,6 +80,10 @@ pub struct Download {
#[arg(long, default_value_t = false)]
pub(crate) force_hardsub: bool,
#[arg(help = "The number of threads used to download")]
#[arg(short, long, default_value_t = num_cpus::get())]
pub(crate) threads: usize,
#[arg(help = "Url(s) to Crunchyroll episodes or series")]
#[arg(required = true)]
pub(crate) urls: Vec<String>,
@ -149,7 +153,8 @@ impl Execute for Download {
} else {
None
})
.ffmpeg_preset(self.ffmpeg_preset.clone().unwrap_or_default());
.ffmpeg_preset(self.ffmpeg_preset.clone().unwrap_or_default())
.threads(self.threads);
for mut single_formats in single_format_collection.into_iter() {
// the vec contains always only one item

View file

@ -50,6 +50,7 @@ pub struct DownloadBuilder {
audio_sort: Option<Vec<Locale>>,
subtitle_sort: Option<Vec<Locale>>,
force_hardsub: bool,
threads: usize,
}
impl DownloadBuilder {
@ -61,6 +62,7 @@ impl DownloadBuilder {
audio_sort: None,
subtitle_sort: None,
force_hardsub: false,
threads: num_cpus::get(),
}
}
@ -73,6 +75,7 @@ impl DownloadBuilder {
subtitle_sort: self.subtitle_sort,
force_hardsub: self.force_hardsub,
threads: self.threads,
formats: vec![],
}
@ -99,6 +102,7 @@ pub struct Downloader {
subtitle_sort: Option<Vec<Locale>>,
force_hardsub: bool,
threads: usize,
formats: Vec<DownloadFormat>,
}
@ -502,7 +506,8 @@ impl Downloader {
let tempfile = tempfile(".mp4")?;
let (mut file, path) = tempfile.into_parts();
download_segments(ctx, &mut file, message, variant_data).await?;
self.download_segments(ctx, &mut file, message, variant_data)
.await?;
Ok(path)
}
@ -516,7 +521,8 @@ impl Downloader {
let tempfile = tempfile(".m4a")?;
let (mut file, path) = tempfile.into_parts();
download_segments(ctx, &mut file, message, variant_data).await?;
self.download_segments(ctx, &mut file, message, variant_data)
.await?;
Ok(path)
}
@ -537,14 +543,14 @@ impl Downloader {
Ok(path)
}
}
pub async fn download_segments(
async fn download_segments(
&self,
ctx: &Context,
writer: &mut impl Write,
message: String,
variant_data: &VariantData,
) -> Result<()> {
) -> Result<()> {
let segments = variant_data.segments().await?;
let total_segments = segments.len();
@ -569,7 +575,7 @@ pub async fn download_segments(
None
};
let cpus = num_cpus::get();
let cpus = self.threads;
let mut segs: Vec<Vec<VariantSegment>> = Vec::with_capacity(cpus);
for _ in 0..cpus {
segs.push(vec![])
@ -673,8 +679,8 @@ pub async fn download_segments(
if let Some(p) = &progress {
let progress_len = p.length().unwrap();
let estimated_segment_len =
(variant_data.bandwidth / 8) * segments.get(pos as usize).unwrap().length.as_secs();
let estimated_segment_len = (variant_data.bandwidth / 8)
* segments.get(pos as usize).unwrap().length.as_secs();
let bytes_len = bytes.len() as u64;
p.set_length(progress_len - estimated_segment_len + bytes_len);
@ -719,6 +725,7 @@ pub async fn download_segments(
}
Ok(())
}
}
fn estimate_variant_file_size(variant_data: &VariantData, segments: &Vec<VariantSegment>) -> u64 {