Skip to content

misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux

Videos and galleries: the speed limits reach yt-dlp and gallery-dl, and a change starts them again (#708) (#775)

Ahmed Nefzaoui34ac35952677
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2cf1bb8e..a2c79593 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
- **Sizes for a stream's qualities:** HLS qualities showed no size; each is now estimated from its bitrate and the video's length, in the Download video window and in the download list.
- **A video already in the list is no longer downloaded again without a word:** picking the same quality of the same video, from the menu over a video or the Download video window, now follows Options → Downloads ("If a duplicate download link is added": ask, a numbered name, overwrite, or open / resume the one there). Another quality of the same video is not a duplicate.
- The progress window's bar glides as the download goes, instead of moving in steps four times a second.
+- Video downloads (and image galleries) keep to the speed limits you set: a video's own limit (its progress window's **Speed Limiter** tab) and the limit for all downloads together now reach yt-dlp and gallery-dl too, not only the limit for one file, and turning the limiter on or off, changing its settings or a video's own limit applies to the videos under way: yt-dlp takes a limit only as it starts, so they stop and start again with the new one, from where they were. Several videos under the limit for all downloads share it evenly: when another starts, the ones running with more than their share start again with it. A video or a gallery started while the Scheduler's download limit is reached now waits, as other downloads do ([#708](https://github.com/anefzaoui/misterdownloadmanager/issues/708)).
- File names whose stem is a Windows reserved device name followed by spaces before the dot (`CON .txt`, `LPT1 .log`) are now prefixed with `_` (`_CON .txt`), as `CON.txt` already was ([#655](https://github.com/anefzaoui/misterdownloadmanager/pull/655)).
- The browser's **video button** over a video in a feed (a site's home page, a timeline, search results) sends that video's own address instead of the page's, which yt-dlp read as no video at all ("No video found on this page"). It finds the post the video sits in, as you would: on LinkedIn its `/feed/update/urn:li:activity:…/` (or `urn:li:ugcPost:…`) address, read from the post in LinkedIn's 2026 feed too, on Facebook the reel's, the video's or the post's own link, on X the post's (`/status/…`), on Instagram `/p/…` or `/reel/…`, on Reddit the post's comments page, on TikTok `/@…/video/…`, on Vimeo and Dailymotion the clip's page, and elsewhere a post's own link, such as its timestamp. Only a post that names that one video counts. When nothing does, the video's own file goes (when it plays one, not pieces), and otherwise nothing goes and the button says to open the post and use the button there ([#689](https://github.com/anefzaoui/misterdownloadmanager/issues/689), [#643](https://github.com/anefzaoui/misterdownloadmanager/issues/643)).
- Torrents: pausing a torrent just as one of its pieces was being checked no longer leaves it one piece short for good. That piece was neither counted as downloaded nor asked for again, so the torrent stopped just before the end, and pausing and starting it again didn't help, only restarting MDM did; a pause now puts that piece back to be downloaded again ([#706](https://github.com/anefzaoui/misterdownloadmanager/issues/706)).
diff --git a/ROADMAP.md b/ROADMAP.md
index f31b7200..0d5faa74 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -503,6 +503,10 @@ Bug fixes from the first days of 0.5.0, a Safari extension, and more file types.
These ship in the next version.
+- ✅ Videos and image galleries keep to the speed limits (for one file, their own, and all
+ downloads together), and start again with a new one when it changes; one started while the
+ Scheduler's download limit is reached waits
+ ([#708](https://github.com/anefzaoui/misterdownloadmanager/issues/708))
- ✅ The browser's Download video button in right-to-left languages: its corners and divider follow the text direction, so its outline stays whole
- ✅ **Deferred Rename** on a download's **Download status** tab: save a new name while downloading to rename the finished file's stem (and video subtitles) or an exclusive finished folder on completion, leaving shared/save or pre-existing folders, split-archive volumes and single-file torrents under their original names ([#655](https://github.com/anefzaoui/misterdownloadmanager/pull/655))
- ✅ Links added by hand go with the User-Agent of your default browser, at the version you have
diff --git a/crates/app/src/app.rs b/crates/app/src/app.rs
index f7424b8e..c9193c47 100644
--- a/crates/app/src/app.rs
+++ b/crates/app/src/app.rs
@@ -940,6 +940,17 @@ pub struct App {
debrid_from: u64,
route: mdm_engine::Route,
adapter_paused: Vec<u64>,
+ /// Videos and galleries (#708): yt-dlp and gallery-dl take a speed limit only as they start
+ /// (`--limit-rate`). The limits each running one was started under (`helper_limits`), the limit
+ /// set in its progress window without Remember (it lasts until it is stopped), and the ones
+ /// stopped to start again at a new limit, started once their jobs have ended
+ /// (`limit_restart_tick`). #769 moves their downloads to the engine, whose limits follow a
+ /// change by themselves.
+ helper_rates: HashMap<u64, (Option<u64>, Option<u64>)>,
+ /// The rate each running video and gallery was started with (`helper_rate`).
+ helper_started: HashMap<u64, Option<u64>>,
+ helper_own: HashMap<u64, Option<u64>>,
+ limit_restarts: Vec<u64>,
find: Option<crate::find::Find>,
/// The last search, for Find Next.
last_find: Option<crate::find::Query>,
@@ -1573,6 +1584,10 @@ impl App {
debrid_from: 0,
route: mdm_engine::Route::Any,
adapter_paused: Vec::new(),
+ helper_rates: HashMap::new(),
+ helper_started: HashMap::new(),
+ helper_own: HashMap::new(),
+ limit_restarts: Vec::new(),
find: None,
last_find: None,
notice: None,
@@ -2975,17 +2990,8 @@ impl App {
if item.live.is_some() || (item.status == Status::Complete && item.gallery.is_none()) {
return;
}
- // A video from a page: yt-dlp, not the engine.
- if self.item(id).is_some_and(|i| i.video.is_some()) {
- self.start_video(id, show);
- return;
- }
- // An image gallery: gallery-dl.
- if self.item(id).is_some_and(|i| i.gallery.is_some()) {
- self.start_gallery(id, show);
- return;
- }
- // The download limit is reached: it starts when the window has room again.
+ // The download limit is reached: it starts when the window has room again (a video or a
+ // gallery too, #708: their bytes count toward it).
if self.settings.limits_on && self.quota.blocked {
if !self.quota.held.contains(&id) {
self.quota.held.push(id);
@@ -2999,6 +3005,18 @@ impl App {
self.dirty = true;
return;
}
+ // A video from a page: yt-dlp, not the engine.
+ if self.item(id).is_some_and(|i| i.video.is_some()) {
+ self.start_video(id, show);
+ self.share_with_new_helper(id);
+ return;
+ }
+ // An image gallery: gallery-dl.
+ if self.item(id).is_some_and(|i| i.gallery.is_some()) {
+ self.start_gallery(id, show);
+ self.share_with_new_helper(id);
+ return;
+ }
if self.item(id).is_some_and(|i| i.torrent.is_some()) {
self.start_torrent(id, show);
return;
@@ -3095,14 +3113,127 @@ impl App {
}
/// Apply the Speed Limiter's "for one file" setting to every running download, file or
- /// torrent; one with a remembered limit of its own keeps it, as when it starts.
- fn apply_speed_limit(&self) {
+ /// torrent; one with a remembered limit of its own keeps it, as when it starts. Videos and
+ /// galleries start again at their new limit (`reapply_helper_limits`).
+ fn apply_speed_limit(&mut self) {
let limit = self.settings.speed_limit();
for i in &self.items {
if let Some(l) = &i.live {
l.dl.set_speed_limit(i.own_limit.or(limit));
}
}
+ self.reapply_helper_limits();
+ }
+
+ /// The limits a video or a gallery (#708) is under, as a file download's are set: its own
+ /// limit (set in its progress window, else remembered), else the Speed Limiter's for one
+ /// file; and the limit for all downloads together, when on. A change of these starts it
+ /// again (`reapply_helper_limits`).
+ fn helper_limits(&self, id: u64) -> (Option<u64>, Option<u64>) {
+ let own = self
+ .helper_own
+ .get(&id)
+ .copied()
+ .unwrap_or_else(|| self.items.iter().find(|i| i.id == id)?.own_limit);
+ let s = &self.settings;
+ (
+ own.or(s.speed_limit()),
+ s.global_limit_on.then(|| s.global_limit_kb.max(1) * 1024),
+ )
+ }
+
+ /// The speed limit a video or a gallery (#708) starts with, bytes/s: the lower of its limit
+ /// for one file (`helper_limits`) and an even share, with the other videos and galleries
+ /// running, of the limit for all downloads together as file downloads have it now
+ /// (`apply_global_limit`: what torrents leave of it).
+ fn helper_rate(&self, id: u64) -> Option<u64> {
+ let (one, _) = self.helper_limits(id);
+ let others = self
+ .items
+ .iter()
+ .filter(|i| i.id != id && i.live.as_ref().is_some_and(|l| l.dl.is_helper()))
+ .count() as u64;
+ helper_rate(one, self.global.get(), others)
+ }
+
+ /// The speed limits changed (the Speed Limiter turned on or off, its settings, a download's
+ /// own limit): every running video and gallery whose limit is now another is stopped and
+ /// started again with it (#708). yt-dlp continues its partial file, gallery-dl skips the
+ /// files already saved, as when the network adapter changes.
+ fn reapply_helper_limits(&mut self) {
+ let changed: Vec<u64> = self
+ .items
+ .iter()
+ .filter(|i| i.live.as_ref().is_some_and(|l| l.dl.is_helper()))
+ .filter(|i| self.helper_rates.get(&i.id).copied() != Some(self.helper_limits(i.id)))
+ .map(|i| i.id)
+ .collect();
+ self.restart_helpers(changed);
+ }
+
+ /// A video or a gallery `id` has started under the limit for all downloads (#708): the
+ /// others running that were started with more than their even share now are started again
+ /// with it, so together they keep to the limit. Starting several at once (a queue, a
+ /// playlist) restarts only the ones started a moment before, still looking their page up;
+ /// one that ends leaves the others as they are (under the limit).
+ fn share_with_new_helper(&mut self, id: u64) {
+ if self.global.get().is_none() || !self.item(id).is_some_and(|i| i.live.is_some()) {
+ return;
+ }
+ let over: Vec<u64> = self
+ .items
+ .iter()
+ .filter(|i| i.id != id && i.live.as_ref().is_some_and(|l| l.dl.is_helper()))
+ .filter(|i| {
+ let now = self.helper_rate(i.id);
+ match (self.helper_started.get(&i.id).copied().flatten(), now) {
+ (Some(had), Some(now)) => had > now,
+ (None, Some(_)) => true,
+ _ => false,
+ }
+ })
+ .map(|i| i.id)
+ .collect();
+ self.restart_helpers(over);
+ }
+
+ /// Stop the videos and galleries `ids` to start them again with their speed limit now
+ /// (`limit_restart_tick`).
+ fn restart_helpers(&mut self, ids: Vec<u64>) {
+ for id in ids {
+ if let Some(i) = self.items.iter_mut().find(|i| i.id == id)
+ && let Some(l) = &i.live
+ {
+ l.dl.cancel();
+ i.note(
+ // i18n: event log entry: a video or gallery download stopped to start again at the new speed limit (its helper program takes a limit only as it starts)
+ crate::i18n::t("Stopped: the speed limit changed; starting again with it")
+ .to_owned(),
+ );
+ }
+ if !self.limit_restarts.contains(&id) {
+ self.limit_restarts.push(id);
+ }
+ }
+ }
+
+ /// Videos and galleries stopped for a new speed limit, started again once their jobs have
+ /// ended (#708).
+ fn limit_restart_tick(&mut self) {
+ if self.limit_restarts.is_empty() {
+ return;
+ }
+ let ready: Vec<u64> = self
+ .limit_restarts
+ .iter()
+ .copied()
+ .filter(|id| self.items.iter().any(|i| i.id == *id && i.live.is_none()))
+ .collect();
+ for &id in &ready {
+ self.start(id, false);
+ }
+ self.limit_restarts
+ .retain(|id| self.items.iter().any(|i| i.id == *id) && !ready.contains(id));
}
fn stop(&mut self, id: u64) {
@@ -4368,13 +4499,29 @@ impl App {
); // not translated: a log line
}
+ /// The speed limit a video or gallery starts with now (`helper_rate`); the limits it is under
+ /// are kept to tell a change (`reapply_helper_limits`). A limit set in its progress window lasts until it is stopped:
+ /// a start other than for a new limit forgets it.
+ fn helper_starts(&mut self, id: u64) -> Option<u64> {
+ // Started again for a new limit (by `limit_restart_tick`, or its queue first): done.
+ if let Some(at) = self.limit_restarts.iter().position(|x| *x == id) {
+ self.limit_restarts.remove(at);
+ } else {
+ self.helper_own.remove(&id);
+ }
+ self.helper_rates.insert(id, self.helper_limits(id));
+ let rate = self.helper_rate(id);
+ self.helper_started.insert(id, rate);
+ rate
+ }
+
/// Start a video item: yt-dlp (installed and verified) downloads it.
fn start_video(&mut self, id: u64, show: bool) {
self.claim_place(id);
let tools = mdm_tools::tools_dir();
let tool = tools.as_deref().and_then(mdm_tools::installed);
let ffmpeg = tools.as_deref().and_then(mdm_tools::ffmpeg::installed);
- let rate = self.settings.speed_limit();
+ let rate = self.helper_starts(id);
// A stream in pieces (HLS, DASH): as many at once as a file's connections (#531).
let fragments = self
.items
@@ -4481,7 +4628,7 @@ impl App {
let tool = mdm_tools::tools_dir()
.as_deref()
.and_then(mdm_tools::gallery::installed);
- let rate = self.settings.speed_limit();
+ let rate = self.helper_starts(id);
let Some(item) = self.item(id) else { return };
let Some(spec) = item.gallery.clone() else {
return;
@@ -9735,14 +9882,21 @@ impl App {
}
}
progress::Action::Limit(limit, remember) => {
+ let mut helper = false;
if let Some(item) = self.item(id) {
if let Some(l) = item.live.as_ref() {
l.dl.set_speed_limit(limit);
+ helper = l.dl.is_helper();
}
// Remembered, it applies again after a stop or a restart.
item.own_limit = if remember { limit } else { None };
self.dirty = true;
}
+ // A video or a gallery starts again with it (#708).
+ if helper {
+ self.helper_own.insert(id, limit);
+ self.reapply_helper_limits();
+ }
}
progress::Action::UploadLimit(limit) => {
if let Some(item) = self.item(id)
@@ -9858,6 +10012,7 @@ impl App {
self.follow_assistive_technology(ctx);
}
self.network_adapter_tick(ctx);
+ self.limit_restart_tick();
self.debrid_tick();
self.poll(ctx);
self.poll_remote_torrents();
@@ -10096,6 +10251,17 @@ fn sharing_stopped_note(why: crate::torrents::Reached) -> String {
/// other uses `other`: half of it at least, and what the other leaves when that is more; never
/// under 1 KB/s (the limiter takes 0 as no limit).
///
+/// A video's or a gallery's speed limit as it starts (#708): the lower of `one` (its own limit,
+/// else the one for one file) and an even share of `global` (the limit for all downloads as file
+/// downloads have it now) with the `others` running; never under 1 KB/s.
+fn helper_rate(one: Option<u64>, global: Option<u64>, others: u64) -> Option<u64> {
+ let share = global.map(|g| (g / (others + 1)).max(1024));
+ match (one, share) {
+ (Some(a), Some(b)) => Some(a.min(b)),
+ (a, b) => a.or(b),
+ }
+}
+
/// An other side within 15% of its half counts as using its half: measured speeds wobble, and
/// taking every dip as room to grow kept both sides over their halves (measured: 463 + 398
/// KB/s under an 800 KB/s cap).
@@ -10330,7 +10496,29 @@ mod tray_toggle_tests {
#[cfg(test)]
mod global_cap_tests {
- use super::share;
+ use super::{helper_rate, share};
+
+ /// #708: a video's or a gallery's limit as it starts: its own (or the one for one file), and
+ /// an even share of the limit for all downloads with the other helpers running.
+ #[test]
+ fn a_helper_starts_under_both_limits() {
+ const K: u64 = 1024;
+ assert_eq!(helper_rate(None, None, 0), None);
+ assert_eq!(helper_rate(Some(500 * K), None, 3), Some(500 * K));
+ assert_eq!(helper_rate(None, Some(500 * K), 0), Some(500 * K));
+ assert_eq!(
+ helper_rate(None, Some(500 * K), 1),
+ Some(250 * K),
+ "shared by two"
+ );
+ assert_eq!(helper_rate(Some(200 * K), Some(500 * K), 1), Some(200 * K));
+ assert_eq!(helper_rate(Some(400 * K), Some(500 * K), 1), Some(250 * K));
+ assert_eq!(
+ helper_rate(None, Some(2 * K), 9),
+ Some(K),
+ "never under 1 KB/s"
+ );
+ }
#[test]
fn each_side_gets_half_and_what_the_other_leaves() {
diff --git a/crates/app/src/model.rs b/crates/app/src/model.rs
index 59996741..054e0e53 100644
--- a/crates/app/src/model.rs
+++ b/crates/app/src/model.rs
@@ -581,8 +581,15 @@ impl Job {
}
}
- /// The speed limit can change while a file downloads; yt-dlp takes it at the start; a
- /// torrent's applies to the whole torrent session.
+ /// A helper program downloads it (yt-dlp, gallery-dl): it takes a speed limit only as it
+ /// starts, so a new limit starts it again (#708, `App::reapply_helper_limits`).
+ pub fn is_helper(&self) -> bool {
+ matches!(self, Job::Video { .. } | Job::Gallery { .. })
+ }
+
+ /// The speed limit can change while a file downloads; yt-dlp and gallery-dl take it at the
+ /// start (the app starts them again, `is_helper`); a torrent's applies to the whole torrent
+ /// session.
pub fn set_speed_limit(&self, rate: Option<u64>) {
match self {
Job::File(d) => d.set_speed_limit(rate),
diff --git a/docs/connections-and-speed.md b/docs/connections-and-speed.md
index 36f2e1c8..b6dc8d72 100644
--- a/docs/connections-and-speed.md
+++ b/docs/connections-and-speed.md
@@ -118,7 +118,7 @@ The Speed Limiter keeps MDM from using all of your connection.
**Always turn on Speed Limiter on Mister Download Manager startup** turns it on each time MDM starts.
-A torrent counts as one file here: each torrent gets the limit of its own. A download with a limit of its own (see [For one download](#for-one-download)) keeps that one.
+A torrent counts as one file here: each torrent gets the limit of its own. So do a video and an image gallery ([Videos](video.md#speed-limits)). A download with a limit of its own (see [For one download](#for-one-download)) keeps that one.
### For all downloads together
@@ -136,7 +136,7 @@ In the download's progress window, open the **Speed Limiter** tab:
2. Set **Maximum download speed** in KBytes/sec. The new limit applies at once.
3. Tick **Remember Speed Limiter settings for this file on download stop/resume** to keep this limit for the file: it applies again when you resume it later, even after restarting MDM.
-For a torrent, the limit is that torrent's alone; the others keep theirs. A torrent can't be held under 16 KBytes/sec: a lower limit gives it 16.
+A video or a gallery takes a limit of its own the same way. For a torrent, the limit is that torrent's alone; the others keep theirs. A torrent can't be held under 16 KBytes/sec: a lower limit gives it 16.
> Some servers drop the connection when the speed is very low. Avoid a tight limit for servers that can't resume (**Resume capability: No**).
diff --git a/docs/video.md b/docs/video.md
index 5c229388..0c9efdbb 100644
--- a/docs/video.md
+++ b/docs/video.md
@@ -88,6 +88,10 @@ MDM lists at most 20,000 videos of one list.
A page address that points at one video inside a playlist or a mix (for example `watch?v=…&list=…`) downloads that one video, never the whole list.
+## Speed limits
+
+Videos keep to the [Speed Limiter](connections-and-speed.md#speed-limiter): the limit for one file, a video's own limit (the **Speed Limiter** tab of its progress window), and the limit for all downloads together, which the videos running share evenly. yt-dlp takes a limit only as it starts, so when you turn the limiter on or off, change its settings or a video's own limit, MDM stops the videos under way and starts them again with the new limit; they go on from where they were (the log says "Stopped: the speed limit changed; starting again with it"). When another video starts under the limit for all downloads, the ones running with more than their share now start again with it; when one ends, the others keep theirs. Image galleries do the same with gallery-dl, which skips the files already saved. Other changes (torrents taking more or less of the limit for all downloads, **Only while I'm using the computer**) apply to the next video that starts. File downloads change speed at once instead; [#769](https://github.com/anefzaoui/misterdownloadmanager/issues/769) moves video downloads to MDM's own downloader, where they will too.
+
## Through a VPN's adapter
With **Options → Proxy / Socks → [Network adapter](options.md#proxy--socks)** set, video downloads go through that adapter too, with the same kill switch as other downloads: MDM hands yt-dlp (and the ffmpeg it starts) a proxy of its own on this computer, and opens every connection through the adapter, looking the names up with the DNS you chose in **Options → Connection** (without an adapter, yt-dlp uses the system's DNS). When the adapter disconnects, the video download stops, and it starts again when the adapter is back. Formats sent over RTMP, RTSP, MMS or FTP can't go through it and are refused.
diff --git a/i18n/en.tsv b/i18n/en.tsv
index 5788f081..2a6a46a2 100644
--- a/i18n/en.tsv
+++ b/i18n/en.tsv
@@ -3060,6 +3060,9 @@
#. event log entry: the download stopped to start again through the network adapter as it is now (a VPN reconnected, another adapter chosen)
Stopped: the network adapter changed; starting again Stopped: the network adapter changed; starting again
#. message or event log entry in the main window
+#. event log entry: a video or gallery download stopped to start again at the new speed limit (its helper program takes a limit only as it starts)
+ Stopped: the speed limit changed; starting again with it Stopped: the speed limit changed; starting again with it
+#. message or event log entry in the main window
#. event log entry: the download stopped because the chosen network adapter (a VPN's, usually) went away; {adapter} its name
Stopped: {adapter} is not connected; it goes on when it is back Stopped: {adapter} is not connected; it goes on when it is back
#. text in a download's progress window