fallback of formats
This commit is contained in:
@@ -565,18 +565,40 @@ App.videos = App.videos || {};
|
||||
return 0;
|
||||
};
|
||||
|
||||
App.videos.pickBestFormat = function(formats, preferredHeight) {
|
||||
if (!Array.isArray(formats) || formats.length === 0) return null;
|
||||
const candidates = formats.filter((fmt) => fmt && fmt.url);
|
||||
if (!candidates.length) return null;
|
||||
const videoCandidates = candidates.filter((fmt) => {
|
||||
const videoExt = String(fmt.video_ext || '').toLowerCase();
|
||||
const vcodec = String(fmt.vcodec || '').toLowerCase();
|
||||
const headerValue = function(headers, name) {
|
||||
if (!headers) return '';
|
||||
return headers[name] || headers[name.toLowerCase()] || '';
|
||||
};
|
||||
|
||||
const deriveReferer = function(url) {
|
||||
if (!url) return '';
|
||||
try {
|
||||
return `${new URL(url).origin}/`;
|
||||
} catch (err) {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
// Ranks the playable formats best-first so callers can fall back to the
|
||||
// next candidate when a URL fails. Quality is the primary key; when several
|
||||
// formats share the same quality the one that appears later in the source
|
||||
// list is preferred (start with the last one). When a preferred height is
|
||||
// set, formats at or below it come first (best of those first), followed by
|
||||
// anything above it ordered closest-to-preferred first as a last resort.
|
||||
App.videos.rankFormats = function(formats, preferredHeight) {
|
||||
if (!Array.isArray(formats) || formats.length === 0) return [];
|
||||
const candidates = formats
|
||||
.map((fmt, index) => ({ fmt, index }))
|
||||
.filter((entry) => entry.fmt && entry.fmt.url);
|
||||
if (!candidates.length) return [];
|
||||
const videoCandidates = candidates.filter((entry) => {
|
||||
const videoExt = String(entry.fmt.video_ext || '').toLowerCase();
|
||||
const vcodec = String(entry.fmt.vcodec || '').toLowerCase();
|
||||
if (videoExt && videoExt !== 'none') return true;
|
||||
if (vcodec && vcodec !== 'none') return true;
|
||||
return false;
|
||||
});
|
||||
let pool = videoCandidates.length ? videoCandidates : candidates;
|
||||
const pool = videoCandidates.length ? videoCandidates : candidates;
|
||||
const score = (fmt) => {
|
||||
const height = App.videos.coerceNumber(fmt.height || fmt.quality);
|
||||
const width = App.videos.coerceNumber(fmt.width);
|
||||
@@ -585,89 +607,98 @@ App.videos = App.videos || {};
|
||||
const fps = App.videos.coerceNumber(fmt.fps);
|
||||
return [size, bitrate, fps];
|
||||
};
|
||||
// Tie-break on the original index so equal-quality formats start with
|
||||
// the last one in the source list.
|
||||
const compare = (a, b, descending) => {
|
||||
const sa = score(a.fmt);
|
||||
const sb = score(b.fmt);
|
||||
for (let i = 0; i < sa.length; i++) {
|
||||
if (sa[i] !== sb[i]) return descending ? sb[i] - sa[i] : sa[i] - sb[i];
|
||||
}
|
||||
return b.index - a.index;
|
||||
};
|
||||
if (preferredHeight) {
|
||||
const atOrBelow = pool.filter((fmt) => {
|
||||
const size = score(fmt)[0];
|
||||
return size > 0 && size <= preferredHeight;
|
||||
const atOrBelow = [];
|
||||
const above = [];
|
||||
pool.forEach((entry) => {
|
||||
const size = score(entry.fmt)[0];
|
||||
if (size > 0 && size <= preferredHeight) {
|
||||
atOrBelow.push(entry);
|
||||
} else {
|
||||
above.push(entry);
|
||||
}
|
||||
});
|
||||
if (atOrBelow.length) {
|
||||
pool = atOrBelow;
|
||||
} else {
|
||||
// Nothing at or below the preferred quality, fall back to the lowest available.
|
||||
const lowest = pool.reduce((min, fmt) => {
|
||||
if (!min) return fmt;
|
||||
return score(fmt)[0] < score(min)[0] ? fmt : min;
|
||||
}, null);
|
||||
return lowest;
|
||||
}
|
||||
atOrBelow.sort((a, b) => compare(a, b, true));
|
||||
above.sort((a, b) => compare(a, b, false));
|
||||
return atOrBelow.concat(above).map((entry) => entry.fmt);
|
||||
}
|
||||
return pool.reduce((best, fmt) => {
|
||||
if (!best) return fmt;
|
||||
const bestScore = score(best);
|
||||
const curScore = score(fmt);
|
||||
for (let i = 0; i < curScore.length; i++) {
|
||||
if (curScore[i] > bestScore[i]) return fmt;
|
||||
if (curScore[i] < bestScore[i]) return best;
|
||||
}
|
||||
return best;
|
||||
}, null);
|
||||
return pool.slice().sort((a, b) => compare(a, b, true)).map((entry) => entry.fmt);
|
||||
};
|
||||
|
||||
App.videos.resolveStreamSource = function(videoOrUrl, options) {
|
||||
App.videos.pickBestFormat = function(formats, preferredHeight) {
|
||||
const ranked = App.videos.rankFormats(formats, preferredHeight);
|
||||
return ranked.length ? ranked[0] : null;
|
||||
};
|
||||
|
||||
// Resolves an ordered list of stream source candidates (best first). The
|
||||
// player walks this list and falls back to the next entry when a URL fails.
|
||||
App.videos.resolveStreamSources = function(videoOrUrl, options) {
|
||||
const applyPreferredQuality = !options || options.applyPreferredQuality !== false;
|
||||
let sourceUrl = '';
|
||||
let referer = '';
|
||||
let userAgent = '';
|
||||
const isLive = !!(videoOrUrl && typeof videoOrUrl === 'object' &&
|
||||
(videoOrUrl.isLive || (videoOrUrl.meta && videoOrUrl.meta.isLive)));
|
||||
if (typeof videoOrUrl === 'string') {
|
||||
sourceUrl = videoOrUrl;
|
||||
} else if (videoOrUrl && typeof videoOrUrl === 'object') {
|
||||
const meta = videoOrUrl.meta || videoOrUrl;
|
||||
sourceUrl = meta.url || videoOrUrl.url || '';
|
||||
let preferredHeight = null;
|
||||
if (applyPreferredQuality) {
|
||||
const preferredQuality = App.storage.getPreferredQuality();
|
||||
preferredHeight = preferredQuality === 'auto' ? null : App.videos.coerceNumber(preferredQuality);
|
||||
}
|
||||
const best = App.videos.pickBestFormat(meta.formats, preferredHeight);
|
||||
if (best && best.url) {
|
||||
sourceUrl = best.url;
|
||||
if (best.http_headers && (best.http_headers.Referer || best.http_headers.referer)) {
|
||||
referer = best.http_headers.Referer || best.http_headers.referer;
|
||||
}
|
||||
if (best.http_headers && (best.http_headers['User-Agent'] || best.http_headers['user-agent'])) {
|
||||
userAgent = best.http_headers['User-Agent'] || best.http_headers['user-agent'];
|
||||
}
|
||||
}
|
||||
if (!referer && meta.http_headers && (meta.http_headers.Referer || meta.http_headers.referer)) {
|
||||
referer = meta.http_headers.Referer || meta.http_headers.referer;
|
||||
}
|
||||
if (!userAgent && meta.http_headers && (meta.http_headers['User-Agent'] || meta.http_headers['user-agent'])) {
|
||||
userAgent = meta.http_headers['User-Agent'] || meta.http_headers['user-agent'];
|
||||
return videoOrUrl ? [{ url: videoOrUrl, referer: deriveReferer(videoOrUrl), userAgent: '', isLive }] : [];
|
||||
}
|
||||
if (!videoOrUrl || typeof videoOrUrl !== 'object') return [];
|
||||
|
||||
const meta = videoOrUrl.meta || videoOrUrl;
|
||||
const metaReferer = headerValue(meta.http_headers, 'Referer');
|
||||
const metaUserAgent = headerValue(meta.http_headers, 'User-Agent');
|
||||
let preferredHeight = null;
|
||||
if (applyPreferredQuality) {
|
||||
const preferredQuality = App.storage.getPreferredQuality();
|
||||
preferredHeight = preferredQuality === 'auto' ? null : App.videos.coerceNumber(preferredQuality);
|
||||
}
|
||||
|
||||
const sources = App.videos.rankFormats(meta.formats, preferredHeight).map((fmt) => {
|
||||
const referer = headerValue(fmt.http_headers, 'Referer') || metaReferer || deriveReferer(fmt.url);
|
||||
const userAgent = headerValue(fmt.http_headers, 'User-Agent') || metaUserAgent;
|
||||
return { url: fmt.url, referer, userAgent, isLive };
|
||||
});
|
||||
|
||||
if (!sources.length) {
|
||||
const fallbackUrl = meta.url || videoOrUrl.url || '';
|
||||
if (fallbackUrl) {
|
||||
sources.push({
|
||||
url: fallbackUrl,
|
||||
referer: metaReferer || deriveReferer(fallbackUrl),
|
||||
userAgent: metaUserAgent,
|
||||
isLive
|
||||
});
|
||||
}
|
||||
}
|
||||
if (!referer && sourceUrl) {
|
||||
try {
|
||||
referer = `${new URL(sourceUrl).origin}/`;
|
||||
} catch (err) {
|
||||
referer = '';
|
||||
}
|
||||
}
|
||||
return { url: sourceUrl, referer, userAgent, isLive };
|
||||
return sources;
|
||||
};
|
||||
|
||||
App.videos.resolveStreamSource = function(videoOrUrl, options) {
|
||||
const sources = App.videos.resolveStreamSources(videoOrUrl, options);
|
||||
return sources.length ? sources[0] : { url: '', referer: '', userAgent: '', isLive: false };
|
||||
};
|
||||
|
||||
// Builds a proxied stream URL. Extra params other than `url` are forwarded
|
||||
// by the backend as request headers, so use real header names here.
|
||||
App.videos.buildStreamUrl = function(videoOrUrl, options) {
|
||||
const resolved = App.videos.resolveStreamSource(videoOrUrl, options);
|
||||
if (!resolved.url) return '';
|
||||
App.videos.buildStreamUrlFromSource = function(resolved) {
|
||||
if (!resolved || !resolved.url) return '';
|
||||
const refererParam = resolved.referer ? `&referer=${encodeURIComponent(resolved.referer)}` : '';
|
||||
const userAgentParam = resolved.userAgent ? `&User-Agent=${encodeURIComponent(resolved.userAgent)}` : '';
|
||||
const liveParam = resolved.isLive ? '&live=1' : '';
|
||||
return `/api/stream?url=${encodeURIComponent(resolved.url)}${refererParam}${userAgentParam}${liveParam}`;
|
||||
};
|
||||
|
||||
App.videos.buildStreamUrl = function(videoOrUrl, options) {
|
||||
return App.videos.buildStreamUrlFromSource(App.videos.resolveStreamSource(videoOrUrl, options));
|
||||
};
|
||||
|
||||
App.videos.downloadVideo = function(video) {
|
||||
if (!video) return;
|
||||
const streamUrl = App.videos.buildStreamUrl(video, { applyPreferredQuality: false });
|
||||
|
||||
Reference in New Issue
Block a user