Skip to content

Commit

Permalink
rutube: pick closest quality to requested quality
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbmoron committed Jul 7, 2024
1 parent 2f4a013 commit ab1b07f
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/modules/processing/services/rutube.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ async function requestJSON(url) {
} catch {}
}

const delta = (a, b) => Math.abs(a - b);

export default async function(obj) {
if (obj.yappyId) {
const yappy = await requestJSON(
Expand All @@ -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`);
if (obj.key) requestURL.searchParams.set('p', obj.key);
Expand All @@ -45,28 +47,32 @@ export default async function(obj) {

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];
if (Number(quality) < bestQuality.resolution.height) {
bestQuality = m3u8.find((i) => (Number(quality) === i.resolution.height));
}
const matchingQuality = m3u8.reduce((prev, next) => {
const diff = {
prev: delta(quality, prev.resolution.height),
next: delta(quality, next.resolution.height)
};

return diff.prev < diff.next ? prev : next;
});

const fileMetadata = {
title: cleanString(play.title.trim()),
artist: cleanString(play.author.name.trim()),
}

return {
urls: bestQuality.uri,
urls: matchingQuality.uri,
isM3U8: true,
filenameAttributes: {
service: "rutube",
id: obj.id,
title: fileMetadata.title,
author: fileMetadata.artist,
resolution: `${bestQuality.resolution.width}x${bestQuality.resolution.height}`,
qualityLabel: `${bestQuality.resolution.height}p`,
resolution: `${matchingQuality.resolution.width}x${matchingQuality.resolution.height}`,
qualityLabel: `${matchingQuality.resolution.height}p`,
extension: "mp4"
},
fileMetadata: fileMetadata
Expand Down

0 comments on commit ab1b07f

Please sign in to comment.