closes #62, #66, #75
This commit is contained in:
wukko
2023-02-26 22:49:25 +06:00
parent 9b17300492
commit 6465ac8d6f
21 changed files with 388 additions and 425 deletions

View File

@@ -1,45 +1,50 @@
import { xml2json } from "xml-js";
import { genericUserAgent, maxVideoDuration, services } from "../../config.js";
import selectQuality from "../../stream/selectQuality.js";
import { genericUserAgent, maxVideoDuration } from "../../config.js";
export default async function(obj) {
const representationMatch = {
"2160": 7,
"1440": 6,
"1080": 5,
"720": 4,
"480": 3,
"360": 2,
"240": 1,
"144": 0
}
const resolutionMatch = {
"3840": "2160",
"2560": "1440",
"1920": "1080",
"1280": "720",
"852": "480",
"640": "360",
"426": "240",
// "256": "144"
}
export default async function(o) {
let html;
html = await fetch(`https://vk.com/video-${obj.userId}_${obj.videoId}`, {
html = await fetch(`https://vk.com/video-${o.userId}_${o.videoId}`, {
headers: { "user-agent": genericUserAgent }
}).then((r) => { return r.text() }).catch(() => { return false });
if (!html) return { error: 'ErrorCouldntFetch' };
if (!html.includes(`{"lang":`)) return { error: 'ErrorEmptyDownload' };
let quality = o.quality === "max" ? 7 : representationMatch[o.quality];
let js = JSON.parse('{"lang":' + html.split(`{"lang":`)[1].split(']);')[0]);
if (Number(js["mvData"]["is_active_live"]) !== 0) return { error: 'ErrorLiveVideo' };
if (js["mvData"]["duration"] > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
if (Number(js.mvData.is_active_live) !== 0) return { error: 'ErrorLiveVideo' };
if (js.mvData.duration > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
let mpd = JSON.parse(xml2json(js["player"]["params"][0]["manifest"], { compact: true, spaces: 4 }));
let repr = mpd["MPD"]["Period"]["AdaptationSet"]["Representation"];
if (!mpd["MPD"]["Period"]["AdaptationSet"]["Representation"]) repr = mpd["MPD"]["Period"]["AdaptationSet"][0]["Representation"];
let mpd = JSON.parse(xml2json(js.player.params[0]["manifest"], { compact: true, spaces: 4 }));
let repr = mpd.MPD.Period.AdaptationSet.Representation ? mpd.MPD.Period.AdaptationSet.Representation : mpd.MPD.Period.AdaptationSet[0]["Representation"];
let bestQuality = repr[repr.length - 1];
let resolutionPick = Number(bestQuality._attributes.width) > Number(bestQuality._attributes.height) ? 'width': 'height'
if (Number(bestQuality._attributes.id) > Number(quality)) bestQuality = repr[quality];
let selectedQuality,
attr = repr[repr.length - 1]["_attributes"],
qualities = Object.keys(services.vk.quality_match);
for (let i in qualities) {
if (qualities[i] === attr["height"]) {
selectedQuality = `url${attr["height"]}`;
break
}
if (qualities[i] === attr["width"]) {
selectedQuality = `url${attr["width"]}`;
break
}
}
let maxQuality = js["player"]["params"][0][selectedQuality].split('type=')[1].slice(0, 1);
let userQuality = selectQuality('vk', obj.quality, Object.entries(services.vk.quality_match).reduce((r, [k, v]) => { r[v] = k; return r; })[maxQuality]);
let userRepr = repr[services.vk.representation_match[userQuality]]["_attributes"];
if (!(selectedQuality in js["player"]["params"][0])) return { error: 'ErrorEmptyDownload' };
return {
urls: js["player"]["params"][0][`url${userQuality}`],
filename: `vk_${obj.userId}_${obj.videoId}_${userRepr["width"]}x${userRepr['height']}.mp4`
}
if (bestQuality) return {
urls: js.player.params[0][`url${resolutionMatch[bestQuality._attributes[resolutionPick]]}`],
filename: `vk_${o.userId}_${o.videoId}_${bestQuality._attributes.width}x${bestQuality._attributes.height}.mp4`
};
return { error: 'ErrorEmptyDownload' }
}