cobalt/src/modules/processing/services/vimeo.js

94 lines
2.6 KiB
JavaScript
Raw Normal View History

import { env } from "../../config.js";
import { cleanString } from '../../sub/utils.js';
2023-02-26 17:49:25 +01:00
import HLS from "hls-parser";
2023-02-26 17:49:25 +01:00
const resolutionMatch = {
2024-06-23 19:22:58 +02:00
"3840": 2160,
"2732": 1440,
"2560": 1440,
"2048": 1080,
"1920": 1080,
"1366": 720,
"1280": 720,
"960": 480,
"640": 360,
"426": 240
}
export default async function(obj) {
2024-06-23 19:22:58 +02:00
let quality = obj.quality === "max" ? 9000 : Number(obj.quality);
if (quality < 240) quality = 240;
if (!quality || obj.isAudioOnly) quality = 9000;
2024-03-05 00:26:14 +01:00
const url = new URL(`https://player.vimeo.com/video/${obj.id}/config`);
if (obj.password) {
url.searchParams.set('h', obj.password);
}
const api = await fetch(url)
2024-03-05 00:26:14 +01:00
.then(r => r.json())
.catch(() => {});
if (!api) return { error: 'ErrorCouldntFetch' };
const fileMetadata = {
title: cleanString(api.video.title.trim()),
artist: cleanString(api.video.owner.name.trim()),
}
if (api.video.duration > env.durationLimit)
return { error: ['ErrorLengthLimit', env.durationLimit / 60] };
const urlMasterHLS = api.request.files.hls.cdns.akfire_interconnect_quic.url;
const masterHLS = await fetch(urlMasterHLS)
.then(r => r.text())
.catch(() => {});
if (!masterHLS) return { error: 'ErrorCouldntFetch' };
const variants = HLS.parse(masterHLS).variants.sort(
(a, b) => Number(b.bandwidth) - Number(a.bandwidth)
);
if (!variants) return { error: 'ErrorEmptyDownload' };
let bestQuality;
2024-06-23 19:22:58 +02:00
if (quality < resolutionMatch[variants[0].resolution.width]) {
bestQuality = variants.find(v =>
2024-06-23 19:22:58 +02:00
(quality === resolutionMatch[v.resolution.width])
);
}
if (!bestQuality) bestQuality = variants[0];
const expandLink = (path) => {
return new URL(path, urlMasterHLS).toString();
};
let urls = expandLink(bestQuality.uri);
const audioPath = bestQuality?.audio[0]?.uri;
if (audioPath) {
urls = [
urls,
expandLink(audioPath)
]
2024-06-23 19:22:58 +02:00
} else if (obj.isAudioOnly) {
return { error: 'ErrorEmptyDownload' };
}
return {
urls,
isM3U8: true,
fileMetadata: fileMetadata,
filenameAttributes: {
service: "vimeo",
id: obj.id,
title: fileMetadata.title,
author: fileMetadata.artist,
resolution: `${bestQuality.resolution.width}x${bestQuality.resolution.height}`,
qualityLabel: `${resolutionMatch[bestQuality.resolution.width]}p`,
extension: "mp4"
}
}
}