rutube: pick closest quality to requested quality

This commit is contained in:
dumbmoron 2024-07-07 15:14:12 +00:00
parent 2f4a013a2a
commit ab1b07fe44
No known key found for this signature in database

View file

@ -10,6 +10,8 @@ async function requestJSON(url) {
} catch {} } catch {}
} }
const delta = (a, b) => Math.abs(a - b);
export default async function(obj) { export default async function(obj) {
if (obj.yappyId) { if (obj.yappyId) {
const yappy = await requestJSON( const yappy = await requestJSON(
@ -25,7 +27,7 @@ export default async function(obj) {
} }
} }
const quality = obj.quality === "max" ? "9000" : obj.quality; const quality = Number(obj.quality) || 9000;
const requestURL = new URL(`https://rutube.ru/api/play/options/${obj.id}/?no_404=true&referer&pver=v2`); const requestURL = new URL(`https://rutube.ru/api/play/options/${obj.id}/?no_404=true&referer&pver=v2`);
if (obj.key) requestURL.searchParams.set('p', obj.key); if (obj.key) requestURL.searchParams.set('p', obj.key);
@ -45,12 +47,16 @@ export default async function(obj) {
if (!m3u8) return { error: 'ErrorCouldntFetch' }; if (!m3u8) return { error: 'ErrorCouldntFetch' };
m3u8 = HLS.parse(m3u8).variants.sort((a, b) => Number(b.bandwidth) - Number(a.bandwidth)); m3u8 = HLS.parse(m3u8).variants;
let bestQuality = m3u8[0]; const matchingQuality = m3u8.reduce((prev, next) => {
if (Number(quality) < bestQuality.resolution.height) { const diff = {
bestQuality = m3u8.find((i) => (Number(quality) === i.resolution.height)); prev: delta(quality, prev.resolution.height),
} next: delta(quality, next.resolution.height)
};
return diff.prev < diff.next ? prev : next;
});
const fileMetadata = { const fileMetadata = {
title: cleanString(play.title.trim()), title: cleanString(play.title.trim()),
@ -58,15 +64,15 @@ export default async function(obj) {
} }
return { return {
urls: bestQuality.uri, urls: matchingQuality.uri,
isM3U8: true, isM3U8: true,
filenameAttributes: { filenameAttributes: {
service: "rutube", service: "rutube",
id: obj.id, id: obj.id,
title: fileMetadata.title, title: fileMetadata.title,
author: fileMetadata.artist, author: fileMetadata.artist,
resolution: `${bestQuality.resolution.width}x${bestQuality.resolution.height}`, resolution: `${matchingQuality.resolution.width}x${matchingQuality.resolution.height}`,
qualityLabel: `${bestQuality.resolution.height}p`, qualityLabel: `${matchingQuality.resolution.height}p`,
extension: "mp4" extension: "mp4"
}, },
fileMetadata: fileMetadata fileMetadata: fileMetadata