aboutsummaryrefslogtreecommitdiffstats
path: root/crates
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-11-07 15:09:37 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-11-07 15:09:37 +0100
commit780dd251d8f550fb461b7cdaeeee4ec6589609d8 (patch)
tree2c7736f58cbbe84aab2059c13bd115eabe3cbb46 /crates
parentfix(yt/cmds/watch/external_run): Use new config cmd line options for re-exec (diff)
downloadyt-780dd251d8f550fb461b7cdaeeee4ec6589609d8.zip
feat(yt/cmds/download): Don't construct multiple yt_dlp instances
This change also switches to the new yt_dlp option format and the new config macro.
Diffstat (limited to '')
-rw-r--r--crates/yt/src/commands/download/implm/download/mod.rs92
-rw-r--r--crates/yt/src/commands/download/implm/mod.rs6
-rw-r--r--crates/yt/src/commands/download/mod.rs3
-rw-r--r--crates/yt/src/yt_dlp/download_options.rs (renamed from crates/yt/src/commands/download/implm/download/download_options.rs)40
-rw-r--r--crates/yt/src/yt_dlp/hooks.rs (renamed from crates/yt/src/commands/download/implm/download/hooks.rs)0
5 files changed, 90 insertions, 51 deletions
diff --git a/crates/yt/src/commands/download/implm/download/mod.rs b/crates/yt/src/commands/download/implm/download/mod.rs
index 39f7960..fd28a63 100644
--- a/crates/yt/src/commands/download/implm/download/mod.rs
+++ b/crates/yt/src/commands/download/implm/download/mod.rs
@@ -13,24 +13,20 @@ use std::{collections::HashMap, path::PathBuf, sync::Arc, time::Duration};
use crate::{
app::App,
- commands::download::implm::download::download_options::download_opts,
shared::bytes::Bytes,
storage::{
db::{extractor_hash::ExtractorHash, insert::Operations, video::Video},
notify::{wait_for_cache_reduction, wait_for_db_write},
},
- yt_dlp::get_current_cache_allocation,
+ yt_dlp::{download_options::download_opts, get_current_cache_allocation},
};
use anyhow::{Context, Result, bail};
use log::{debug, error, info, warn};
+use serde_json::{Value, json};
use tokio::{select, task::JoinHandle, time};
use yt_dlp::YoutubeDL;
-#[allow(clippy::module_name_repetitions)]
-pub(crate) mod download_options;
-pub(crate) mod hooks;
-
#[derive(Debug)]
#[allow(clippy::module_name_repetitions)]
pub(crate) struct CurrentDownload {
@@ -40,12 +36,23 @@ pub(crate) struct CurrentDownload {
}
impl CurrentDownload {
- fn new_from_video(app: &App, video: Video) -> Result<Self> {
+ fn new_from_video(mut yt_dlp: YoutubeDL, video: Video) -> Self {
let extractor_hash = video.extractor_hash;
- debug!("Download started: {}", &video.title);
- let yt_dlp = Arc::new(download_opts(app, video.subtitle_langs.as_deref())?);
+ debug!("Download started: {}", video.title);
+ if let Some(langs) = video.subtitle_langs.as_ref() {
+ yt_dlp.set_option(
+ "subtitleslangs",
+ Value::Array(
+ langs
+ .split(',')
+ .map(|val| Value::String(val.to_owned()))
+ .collect::<Vec<_>>(),
+ ),
+ );
+ }
+ let yt_dlp = Arc::new(yt_dlp);
let local_yt_dlp = Arc::clone(&yt_dlp);
let task_handle = tokio::task::spawn_blocking(move || {
@@ -57,25 +64,26 @@ impl CurrentDownload {
Ok((result.remove(0), video))
});
- Ok(Self {
+ Self {
task_handle,
yt_dlp,
extractor_hash,
- })
+ }
}
- fn abort(self) -> Result<()> {
+ fn abort(self) -> Result<YoutubeDL> {
debug!("Cancelling download.");
+
self.yt_dlp.close()?;
- Ok(())
+ Ok(Arc::into_inner(self.yt_dlp).expect("No other references should be life"))
}
fn is_finished(&self) -> bool {
self.task_handle.is_finished()
}
- async fn finalize(self, app: &App) -> Result<()> {
+ async fn finalize(self, app: &App) -> Result<YoutubeDL> {
let (result, mut video) = self.task_handle.await??;
let mut ops = Operations::new("Downloader: Set download path");
@@ -90,7 +98,7 @@ impl CurrentDownload {
result.display()
);
- Ok(())
+ Ok(Arc::into_inner(self.yt_dlp).expect("No other references should be life"))
}
}
@@ -111,23 +119,20 @@ pub(crate) struct Downloader {
video_size_cache: HashMap<ExtractorHash, u64>,
printed_warning: bool,
cached_cache_allocation: Option<Bytes>,
-}
-
-impl Default for Downloader {
- fn default() -> Self {
- Self::new()
- }
+ yt_dlp: Option<YoutubeDL>,
}
impl Downloader {
- #[must_use]
- pub(crate) fn new() -> Self {
- Self {
+ pub(crate) fn new(app: &App) -> Result<Self> {
+ let yt_dlp = download_opts(app, None)?;
+
+ Ok(Self {
current_download: None,
video_size_cache: HashMap::new(),
printed_warning: false,
cached_cache_allocation: None,
- }
+ yt_dlp: Some(yt_dlp),
+ })
}
/// Check if enough cache is available.
@@ -168,7 +173,7 @@ impl Downloader {
Waiting for cache size reduction..",
next_video.title,
Bytes::new(video_size),
- &cache_allocation,
+ cache_allocation,
Bytes::new(max_cache_size)
);
self.printed_warning = true;
@@ -228,7 +233,7 @@ impl Downloader {
if current_download.is_finished() {
// The download is done, finalize it and leave it removed.
- current_download.finalize(&app).await?;
+ self.yt_dlp = Some(current_download.finalize(&app).await?);
continue;
}
@@ -244,11 +249,16 @@ impl Downloader {
);
// Replace the currently downloading video
- current_download
- .abort()
- .context("Failed to abort last download")?;
+ self.yt_dlp = Some(
+ current_download
+ .abort()
+ .context("Failed to abort last download")?,
+ );
- let new_current_download = CurrentDownload::new_from_video(&app, next_video)?;
+ let new_current_download = CurrentDownload::new_from_video(
+ self.yt_dlp.take().expect("Should be some here"),
+ next_video,
+ );
self.current_download = Some(new_current_download);
}
@@ -257,7 +267,10 @@ impl Downloader {
"No video is being downloaded right now, setting it to '{}'",
next_video.title
);
- let new_current_download = CurrentDownload::new_from_video(&app, next_video)?;
+ let new_current_download = CurrentDownload::new_from_video(
+ self.yt_dlp.take().expect("Should be some here"),
+ next_video,
+ );
self.current_download = Some(new_current_download);
}
@@ -277,7 +290,20 @@ impl Downloader {
if let Some(value) = self.video_size_cache.get(&video.extractor_hash) {
Ok(*value)
} else {
- let size = video.get_approx_size()?;
+ let size = {
+ let mut yt_dlp = self.yt_dlp.take().expect("Should be some here");
+ let size = yt_dlp.with_reset_option("getcomments", false, |yt_dlp| {
+ yt_dlp.with_reset_option("writeinfojson", false, |yt_dlp| {
+ yt_dlp.with_reset_option("extractor_args", json! {{}}, |yt_dlp| {
+ video.get_approx_size(yt_dlp)
+ })
+ })
+ })?;
+
+ self.yt_dlp = Some(yt_dlp);
+
+ size
+ };
assert_eq!(
self.video_size_cache.insert(video.extractor_hash, size),
diff --git a/crates/yt/src/commands/download/implm/mod.rs b/crates/yt/src/commands/download/implm/mod.rs
index c74a909..8647a54 100644
--- a/crates/yt/src/commands/download/implm/mod.rs
+++ b/crates/yt/src/commands/download/implm/mod.rs
@@ -32,7 +32,7 @@ impl DownloadCommand {
max_cache_size,
} = self;
- let max_cache_size = max_cache_size.unwrap_or(app.config.download.max_cache_size.as_u64());
+ let max_cache_size = app.config.download.max_cache_size.as_u64();
info!("Max cache size: '{}'", Bytes::new(max_cache_size));
clear_stale_downloaded_paths(&app).await?;
@@ -46,8 +46,8 @@ impl DownloadCommand {
ops.commit(&app).await?;
}
- download::Downloader::new()
- .consume(app, max_cache_size)
+ download::Downloader::new(&app)?
+ .consume(app, max_cache_size, count)
.await?;
Ok(())
diff --git a/crates/yt/src/commands/download/mod.rs b/crates/yt/src/commands/download/mod.rs
index 15026ba..f765803 100644
--- a/crates/yt/src/commands/download/mod.rs
+++ b/crates/yt/src/commands/download/mod.rs
@@ -21,9 +21,6 @@ pub(super) struct DownloadCommand {
#[arg(short, long)]
force: bool,
- /// The maximum size the download dir should have.
- #[arg(short, long, value_parser = byte_parser)]
- max_cache_size: Option<u64>,
}
fn byte_parser(input: &str) -> Result<u64, anyhow::Error> {
diff --git a/crates/yt/src/commands/download/implm/download/download_options.rs b/crates/yt/src/yt_dlp/download_options.rs
index 4174266..d81f595 100644
--- a/crates/yt/src/commands/download/implm/download/download_options.rs
+++ b/crates/yt/src/yt_dlp/download_options.rs
@@ -10,17 +10,15 @@
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
use anyhow::Context;
+use log::info;
use serde_json::{Value, json};
use yt_dlp::{YoutubeDL, options::YoutubeDLOptions};
-use crate::app::App;
+use crate::{app::App, yt_dlp::match_filters::ignore_member_only_wrapped};
-use super::hooks::{wrapped_progress_hook, wrapped_post_processor_hook};
+use super::hooks::{wrapped_post_processor_hook, wrapped_progress_hook};
-pub(crate) fn download_opts(
- app: &App,
- subtitle_langs: Option<&str>,
-) -> anyhow::Result<YoutubeDL> {
+pub(crate) fn download_opts(app: &App, subtitle_langs: Option<&str>) -> anyhow::Result<YoutubeDL> {
let base = YoutubeDLOptions::new()
.with_progress_hook(wrapped_progress_hook)
.with_post_processor_hook(wrapped_post_processor_hook)
@@ -115,13 +113,31 @@ pub(crate) fn download_opts(
.map(|val| Value::String(val.to_owned()))
.collect::<Vec<_>>(),
),
- );
+ )
+ .add_match_filter(ignore_member_only_wrapped);
- if let Some(cookies) = &app.config.global.cookies {
- base.set("cookies", cookies.to_str())
+ let base = if let Some(cookies) = &app.config.download.cookies {
+ base.set("cookiefile", cookies.to_str())
+ } else if let Some(cookies_from_browser) = &app.config.download.cookies_from_browser {
+ base.set(
+ "cookiesfrombrowser",
+ Value::Array(vec![cookies_from_browser.to_owned().into()]),
+ )
} else {
base
- }
- .build()
- .context("Failed to instanciate download yt_dlp")
+ };
+
+ let base = {
+ let mut final_base = base;
+
+ for (key, value) in &app.config.update.yt_dlp_options {
+ info!("Adding additional yt-dlp freeform option: {key} to {value}");
+ final_base = final_base.set(key, value.to_owned());
+ }
+
+ final_base
+ };
+
+ base.build()
+ .context("Failed to instanciate download yt_dlp")
}
diff --git a/crates/yt/src/commands/download/implm/download/hooks.rs b/crates/yt/src/yt_dlp/hooks.rs
index de52b98..de52b98 100644
--- a/crates/yt/src/commands/download/implm/download/hooks.rs
+++ b/crates/yt/src/yt_dlp/hooks.rs