mirror of
https://github.com/wukko/cobalt.git
synced 2025-02-02 08:26:22 +01:00
api/youtube: improve video quality normalization once again
This commit is contained in:
parent
8fc9ca2916
commit
b6e827c6f9
1 changed files with 9 additions and 7 deletions
|
@ -42,6 +42,8 @@ const hlsCodecList = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const videoQualities = [144, 240, 360, 480, 720, 1080, 1440, 2160, 4320];
|
||||||
|
|
||||||
const transformSessionData = (cookie) => {
|
const transformSessionData = (cookie) => {
|
||||||
if (!cookie)
|
if (!cookie)
|
||||||
return;
|
return;
|
||||||
|
@ -200,9 +202,9 @@ export default async function(o) {
|
||||||
|
|
||||||
const quality = o.quality === "max" ? 9000 : Number(o.quality);
|
const quality = o.quality === "max" ? 9000 : Number(o.quality);
|
||||||
|
|
||||||
const matchQuality = res => {
|
const normalizeQuality = res => {
|
||||||
const qual = res.height > res.width ? res.width : res.height;
|
const shortestSide = res.height > res.width ? res.width : res.height;
|
||||||
return Math.ceil(qual / 24) * 24;
|
return videoQualities.find(quality => quality >= shortestSide);
|
||||||
}
|
}
|
||||||
|
|
||||||
let video, audio, dubbedLanguage,
|
let video, audio, dubbedLanguage,
|
||||||
|
@ -249,7 +251,7 @@ export default async function(o) {
|
||||||
const best = variants.find(i => matchHlsCodec(i.codecs));
|
const best = variants.find(i => matchHlsCodec(i.codecs));
|
||||||
|
|
||||||
const preferred = variants.find(i =>
|
const preferred = variants.find(i =>
|
||||||
matchHlsCodec(i.codecs) && matchQuality(i.resolution) === quality
|
matchHlsCodec(i.codecs) && normalizeQuality(i.resolution) === quality
|
||||||
);
|
);
|
||||||
|
|
||||||
let selected = preferred || best;
|
let selected = preferred || best;
|
||||||
|
@ -340,7 +342,7 @@ export default async function(o) {
|
||||||
|
|
||||||
if (!o.isAudioOnly) {
|
if (!o.isAudioOnly) {
|
||||||
const qual = (i) => {
|
const qual = (i) => {
|
||||||
return matchQuality({
|
return normalizeQuality({
|
||||||
width: i.width,
|
width: i.width,
|
||||||
height: i.height,
|
height: i.height,
|
||||||
})
|
})
|
||||||
|
@ -406,14 +408,14 @@ export default async function(o) {
|
||||||
let resolution;
|
let resolution;
|
||||||
|
|
||||||
if (o.youtubeHLS) {
|
if (o.youtubeHLS) {
|
||||||
resolution = matchQuality(video.resolution);
|
resolution = normalizeQuality(video.resolution);
|
||||||
filenameAttributes.resolution = `${video.resolution.width}x${video.resolution.height}`;
|
filenameAttributes.resolution = `${video.resolution.width}x${video.resolution.height}`;
|
||||||
filenameAttributes.extension = hlsCodecList[format].container;
|
filenameAttributes.extension = hlsCodecList[format].container;
|
||||||
|
|
||||||
video = video.uri;
|
video = video.uri;
|
||||||
audio = audio.uri;
|
audio = audio.uri;
|
||||||
} else {
|
} else {
|
||||||
resolution = matchQuality({
|
resolution = normalizeQuality({
|
||||||
width: video.width,
|
width: video.width,
|
||||||
height: video.height,
|
height: video.height,
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue