Skip to content

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

Download video: an address that says manifest but answers JSON isn't a stream (#377) (#397)

Ahmed Nefzaouia0c27a7a8710
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2fdbfeb4..1b7f2034 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
### Fixed
+- **Download video** finds the video on pages whose player loads a `.mpd` or `.m3u8` address that answers something else first, as SVT Play does ([#377](https://github.com/anefzaoui/misterdownloadmanager/issues/377)).
- RSS feeds: the boxes on the **Download rules** tab (**Episodes**, **Must contain** and the others) keep the keyboard while you type, instead of losing it after each key ([#353](https://github.com/anefzaoui/misterdownloadmanager/issues/353)).
- A download refused by a site's bot check (Cloudflare's challenge) says so and how to get the file, instead of "the server answered 403 Forbidden" ([#376](https://github.com/anefzaoui/misterdownloadmanager/issues/376)).
- **Open folder** works on a torrent that is still downloading ([#354](https://github.com/anefzaoui/misterdownloadmanager/issues/354)).
diff --git a/ROADMAP.md b/ROADMAP.md
index 387830d0..8d291d68 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -13,6 +13,9 @@ Everything below ships together, in version 0.5.0.
### Done
+- ✅ Download video on pages whose player first loads a manifest's address answering something else
+ (SVT Play): the real manifest goes to MDM, not that first answer
+ ([#377](https://github.com/anefzaoui/misterdownloadmanager/issues/377))
- ✅ Menus on compositors that animate windows (Hyprland): no border and no slide-in, and going from
title to title moves one window, so nothing fades; the reporter confirmed it
([#277](https://github.com/anefzaoui/misterdownloadmanager/issues/277))
diff --git a/extension/background.js b/extension/background.js
index 3c849b39..0d3af933 100644
--- a/extension/background.js
+++ b/extension/background.js
@@ -501,6 +501,8 @@ const AUDIO_EXT = /^(mp3|m4a|aac|oga|ogg|opus|flac|wav|wma)$/;
// file's, XML. Such an answer is only noted (`maybe`, MAYBE_MAX a tab), never listed unread.
const MAYBE_TYPE = /^(text\/plain|(application|binary)\/octet-stream|(application|text)\/xml|)$/;
const MAYBE_MAX = 20;
+// A manifest's own types: HLS's (Apple's and the x- ones), DASH's (and its older video/ name).
+const MANIFEST_TYPE = /mpegurl|dash\+xml|dash\.mpd/;
const MANIFEST_MAX = 2 << 20;
const pathOf = (url) => url.split(/[?#]/)[0];
@@ -513,6 +515,12 @@ function mediaKind(d) {
if (d.statusCode && (d.statusCode < 200 || d.statusCode > 299)) return null;
const mime = headerOf(d, "content-type").split(";")[0].trim().toLowerCase();
const path = pathOf(d.url).toLowerCase();
+ // The address says manifest, the type says it isn't: a page or JSON that names one (SVT Play's
+ // switcher answers .../dash-full.mpd with JSON naming the real manifest; loaded first, it went
+ // to the app and yt-dlp found no video in it, #377). Read before believed, like any `maybe`.
+ if (/\.(m3u8|mpd)$/.test(path) && !MANIFEST_TYPE.test(mime) && !MAYBE_TYPE.test(mime)) {
+ return Number(headerOf(d, "content-length")) > MANIFEST_MAX ? null : { kind: "maybe", mime, size: 0 };
+ }
if (/\.m3u8$/.test(path) || /mpegurl/.test(mime)) return { kind: "hls", mime, size: 0 };
if (/\.mpd$/.test(path) || /dash\+xml/.test(mime)) return { kind: "dash", mime, size: 0 };
if (SEGMENT_TYPE.test(mime) || SEGMENT_EXT.test(path)) return null;
diff --git a/tools/extension/test.mjs b/tools/extension/test.mjs
index bdd380a1..da0a691a 100644
--- a/tools/extension/test.mjs
+++ b/tools/extension/test.mjs
@@ -948,6 +948,69 @@ const MPD = (protect) => `<?xml version="1.0"?>
assert.equal(fetched.length, reads, "each read once");
ok("#171: manifests without .m3u8/.mpd or their type: noted, read only when asked, listed");
}
+{
+ // SVT Play (#377): the player first loads .../dash-full.mpd, a JSON answer naming the manifest,
+ // then the manifest itself from an address without .mpd. The button must send the manifest.
+ const SWITCHER = "https://switcher.test/resolve/1/dash-full.mpd";
+ const REAL = "https://api.test/manifest?manifestUrl=x";
+ served = { [SWITCHER]: { body: '{"location":"https://cdn.test/dash-full.mpd"}' } };
+ const blob = { 0: { href: "https://watch.test/v/5", players: [{ src: "blob:https://watch.test/a", w: 0, h: 0, duration: 0, drm: false }] } };
+ const tabs = { 9: { id: 9, url: "https://watch.test/v/5", title: "Clip", incognito: false } };
+ const sender = { id: "ours", url: "https://watch.test/v/5", frameId: 0, tab: { id: 9, url: "https://watch.test/v/5" } };
+ {
+ const { api, calls, listeners } = fakeApi({ firefox: false, frames: blob, tabs });
+ load(api, false);
+ listeners.tabUpdated(9, { url: "https://watch.test/v/5" });
+ await settle();
+ fetched.length = 0;
+ listeners.responseStarted(res(SWITCHER, "xmlhttprequest", H("application/json")));
+ listeners.responseStarted(res(REAL, "xmlhttprequest", H("application/dash+xml")));
+ await settle();
+ await new Promise((r) => listeners.message({ type: "video", src: "" }, sender, r));
+ assert.equal(natives(calls).find((x) => x.video).src, REAL, "the manifest, not the JSON that names it");
+ assert.ok(!fetched.some(([u]) => u === SWITCHER), "a real stream known: the other isn't even read");
+ }
+ {
+ // Alone, it is read when a stream is asked for: JSON is no stream, so nothing goes as one.
+ const { api, calls, listeners } = fakeApi({ firefox: false, frames: blob, tabs });
+ load(api, false);
+ listeners.tabUpdated(9, { url: "https://watch.test/v/5" });
+ await settle();
+ fetched.length = 0;
+ listeners.responseStarted(res(SWITCHER, "xmlhttprequest", H("application/json")));
+ await settle();
+ await new Promise((r) => listeners.message({ type: "video", src: "" }, sender, r));
+ const m = natives(calls).find((x) => x.video);
+ assert.equal(m.url, "https://watch.test/v/5", "the page goes");
+ assert.equal(m.src, undefined, "no stream");
+ assert.ok(fetched.some(([u]) => u === SWITCHER), "read before believed");
+ }
+ {
+ // What keeps working: a .m3u8 or .mpd served as its own type, as text or as a file, is a stream
+ // on sight; one served as something else is a stream too once read and found to be one.
+ const OK_HLS = "https://cdn.test/a/index.m3u8";
+ const TXT_MPD = "https://cdn.test/b/stream.mpd";
+ const ODD = "https://cdn.test/c/odd.m3u8";
+ served = { [ODD]: { body: "#EXTM3U"+String.fromCharCode(10)+"#EXT-X-ENDLIST"+String.fromCharCode(10) } };
+ const { api, calls, listeners } = fakeApi({ firefox: false, frames: blob, tabs });
+ load(api, false);
+ listeners.tabUpdated(9, { url: "https://watch.test/v/5" });
+ await settle();
+ fetched.length = 0;
+ listeners.responseStarted(res(OK_HLS, "xmlhttprequest", H("application/vnd.apple.mpegurl")));
+ listeners.responseStarted(res(TXT_MPD, "xmlhttprequest", H("text/plain")));
+ await settle();
+ assert.equal(fetched.length, 0, "not read to be believed");
+ assert.deepEqual((await popupAsk(listeners, { type: "media", tab: 9 })).media.map((m) => m.url).sort(), [OK_HLS, TXT_MPD].sort());
+ listeners.tabUpdated(9, { url: "https://watch.test/v/6" });
+ await settle();
+ listeners.responseStarted(res(ODD, "xmlhttprequest", H("video/mp2t")));
+ await settle();
+ await new Promise((r) => listeners.message({ type: "video", src: "" }, { ...sender, url: "https://watch.test/v/6", tab: { id: 9, url: "https://watch.test/v/6" } }, r));
+ assert.equal(natives(calls).filter((x) => x.video).pop().src, ODD, "read, and it is one");
+ }
+ ok("#377: a .mpd or .m3u8 address answering JSON is no stream: read first, the real manifest goes");
+}
{
// The popup reads nothing for a frame whose players have files of their own (no MediaSource).
served = { "https://cdn.test/p/playlist?id=8": { body: "#EXTM3U\n#EXT-X-ENDLIST\n" } };