Don't re-resolve a favorite whose URL is already the media file
Some channels hand back the media URL itself as an item's url. Since the favorites fix, opening one of those sent it to /api/resolve first, so yt-dlp fetched the media just to report the URL we already had. On a signed link (`?secure=<ts>-<token>`) that is a second request against something that may be single-use or IP-bound, and the request that matters -- the playback fetch -- is then refused. Such URLs now play directly, with no resolve round trip, as they did before. Alongside that, three things that make expiry survivable: /api/stream, after its existing referer-less retry, now retries a 403 completely bare (Range only). Signed CDN links are routinely served to a plain browser request and refused when it carries extras -- a `Sec-Fetch-Mode: navigate` on a media subresource, say, which is what yt-dlp's generic extractor hands back and no real player would send. When every source fails, the player re-resolves once and retries instead of giving up, since the likeliest cause is that signed URLs went stale in a long-open tab rather than the video being gone. A manual quality pick is dropped for that retry, as it names one of the URLs that just failed. Favorites stored by older versions still carry a `meta` blob of resolved formats, long expired; it's now stripped on read so nothing can reach for one. Verified: a favorite whose url is a .mp4 plays with zero /api/resolve calls, straight from that URL; playback, prefetch, feed paging, HUD, rotation, momentum and the version check all still pass. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QBDkEXP4htyXTCZUwMLphd
This commit is contained in:
@@ -9,7 +9,19 @@ App.favorites = App.favorites || {};
|
||||
try {
|
||||
const raw = localStorage.getItem(FAVORITES_KEY);
|
||||
const parsed = raw ? JSON.parse(raw) : [];
|
||||
return Array.isArray(parsed) ? parsed : [];
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
// Favorites saved by older versions carry a `meta` blob of resolved
|
||||
// formats whose URLs are signed and long expired. Drop it on the way
|
||||
// in so no code path can reach for one; everything re-resolves from
|
||||
// `url` at play time, and normalize() no longer stores it.
|
||||
return parsed.map((item) => {
|
||||
if (item && typeof item === 'object' && item.meta) {
|
||||
const clean = Object.assign({}, item);
|
||||
delete clean.meta;
|
||||
return clean;
|
||||
}
|
||||
return item;
|
||||
});
|
||||
} catch (err) {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -698,6 +698,27 @@ App.player = App.player || {};
|
||||
if (settled || token !== cp.attemptToken) return;
|
||||
settled = true;
|
||||
if (hasNext) attempt(index + 1);
|
||||
else if (!(opts && opts.refreshed) && App.videos &&
|
||||
typeof App.videos.refreshFormats === 'function') {
|
||||
// Every candidate failed. Media URLs are signed with an
|
||||
// expiry, so the most likely cause is that these ones went
|
||||
// stale (a tab left open, or formats resolved a while ago),
|
||||
// not that the video is gone. Re-resolve and try once more
|
||||
// before telling the viewer it can't be played.
|
||||
App.videos.refreshFormats(videoData).then((meta) => {
|
||||
if (token !== cp.attemptToken) return;
|
||||
// A manually picked format points at one of the URLs
|
||||
// that just failed, so the retry goes back to automatic
|
||||
// selection over the freshly resolved list.
|
||||
cp.formatOverride = null;
|
||||
const retryOpts = Object.assign({}, opts, { refreshed: true, resumeAt });
|
||||
if (meta) playSources(videoData, retryOpts);
|
||||
else {
|
||||
clearLoading();
|
||||
showError(message, () => playSources(videoData, retryOpts), sourceUrl);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
clearLoading();
|
||||
showError(message, () => playSources(videoData, opts), sourceUrl);
|
||||
|
||||
@@ -345,6 +345,19 @@ App.videos = App.videos || {};
|
||||
return prefetch.inFlight;
|
||||
};
|
||||
|
||||
// Throws away a video's resolved formats and asks the server again. Media
|
||||
// URLs are commonly signed with an expiry (`?secure=<unix ts>-<token>`), so
|
||||
// formats resolved earlier -- in a long-open tab, or held in the session
|
||||
// cache -- eventually start returning 403 even though nothing is wrong with
|
||||
// the video itself. The player calls this once before giving up.
|
||||
App.videos.refreshFormats = function(video) {
|
||||
if (!video || typeof video !== 'object') return Promise.resolve(null);
|
||||
const cacheKey = video.id || video.url;
|
||||
if (cacheKey) metaCache.delete(cacheKey);
|
||||
video.meta = null;
|
||||
return App.videos.ensureFormats(video);
|
||||
};
|
||||
|
||||
// Called by the virtualizer once the reader is within two rows of the end.
|
||||
App.videos.releasePrefetched = function() {
|
||||
if (!prefetch.batch || state.isLoading) return false;
|
||||
@@ -1415,12 +1428,36 @@ App.videos = App.videos || {};
|
||||
|
||||
// Resolves a video's real media formats (once per session) and attaches them
|
||||
// as `video.meta`. Returns the resolved meta, or null when it can't be had.
|
||||
// Mirrors the backend's is_direct_media(): a URL whose path (query ignored,
|
||||
// trailing slash tolerated) already names a media file.
|
||||
const DIRECT_MEDIA_RE = /\.(mp4|m4v|m4s|webm|mov|ts|m3u8|mpd)$/i;
|
||||
|
||||
const isDirectMediaUrl = function(url) {
|
||||
if (!url) return false;
|
||||
try {
|
||||
return DIRECT_MEDIA_RE.test(new URL(url, window.location.href).pathname.replace(/\/+$/, ''));
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
App.videos.ensureFormats = function(video) {
|
||||
if (!video || typeof video !== 'object') return Promise.resolve(null);
|
||||
if (hasFormats(video.meta)) return Promise.resolve(video.meta);
|
||||
const cacheKey = video.id || video.url;
|
||||
if (!cacheKey || !video.url) return Promise.resolve(null);
|
||||
|
||||
// Some channels hand back the media URL itself as the item URL. Sending
|
||||
// that to /api/resolve makes yt-dlp fetch the media just to tell us what
|
||||
// we already know: slow, and on a signed URL it's a second request
|
||||
// against a link that may be single-use or IP-bound -- after which the
|
||||
// one that matters, the actual playback fetch, is refused. Play it as-is.
|
||||
if (isDirectMediaUrl(video.url)) {
|
||||
const meta = { url: video.url, formats: [{ url: video.url }], http_headers: {} };
|
||||
video.meta = meta;
|
||||
return Promise.resolve(meta);
|
||||
}
|
||||
|
||||
let promise = metaCache.get(cacheKey);
|
||||
if (!promise) {
|
||||
promise = (async () => {
|
||||
|
||||
Reference in New Issue
Block a user