Skip to content

Commit

Permalink
merge/vimeo: use HLS instead of dash & clean up (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
wukko committed Jun 23, 2024
2 parents 33c3c39 + b51bcc2 commit 558b6a9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 69 deletions.
14 changes: 10 additions & 4 deletions src/modules/processing/matchActionDecider.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di
else if (r.isGif && toGif) action = "gif";
else if (isAudioMuted) action = "muteVideo";
else if (isAudioOnly) action = "audio";
else if (r.isM3U8) action = "singleM3U8";
else if (r.isM3U8) action = "m3u8";
else action = "video";

if (action === "picker" || action === "audio") {
Expand All @@ -48,13 +48,19 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di
params = { type: "gif" }
break;

case "singleM3U8":
params = { type: "remux" }
case "m3u8":
params = {
type: Array.isArray(r.urls) ? "render" : "remux"
}
break;

case "muteVideo":
let muteType = "mute";
if (Array.isArray(r.urls) && !r.isM3U8) {
muteType = "bridge";
}
params = {
type: Array.isArray(r.urls) ? "bridge" : "mute",
type: muteType,
u: Array.isArray(r.urls) ? r.urls[0] : r.urls,
mute: true
}
Expand Down
119 changes: 54 additions & 65 deletions src/modules/processing/services/vimeo.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,97 @@
import { env } from "../../config.js";
import { cleanString } from '../../sub/utils.js';

const resolutionMatch = {
"3840": "2160",
"2732": "1440",
"2560": "1440",
"2048": "1080",
"1920": "1080",
"1366": "720",
"1280": "720",
"960": "480",
"640": "360",
"426": "240"
}

const qualityMatch = {
"2160": "4K",
"1440": "2K",
"480": "540",
import HLS from "hls-parser";

"4K": "2160",
"2K": "1440",
"540": "480"
const resolutionMatch = {
"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) {
let quality = obj.quality === "max" ? "9000" : obj.quality;
if (!quality || obj.isAudioOnly) quality = "9000";
let quality = obj.quality === "max" ? 9000 : Number(obj.quality);
if (quality < 240) quality = 240;
if (!quality || obj.isAudioOnly) quality = 9000;

const url = new URL(`https://player.vimeo.com/video/${obj.id}/config`);
if (obj.password) {
url.searchParams.set('h', obj.password);
}

let api = await fetch(url)
const api = await fetch(url)
.then(r => r.json())
.catch(() => {});
if (!api) return { error: 'ErrorCouldntFetch' };

let downloadType = "dash";

if (!obj.isAudioOnly && JSON.stringify(api).includes('"progressive":[{'))
downloadType = "progressive";
if (!api) return { error: 'ErrorCouldntFetch' };

let fileMetadata = {
const fileMetadata = {
title: cleanString(api.video.title.trim()),
artist: cleanString(api.video.owner.name.trim()),
}

if (downloadType !== "dash") {
if (qualityMatch[quality]) quality = qualityMatch[quality];
let all = api.request.files.progressive.sort((a, b) => Number(b.width) - Number(a.width));
let best = all[0];
if (api.video?.duration > env.durationLimit)
return { error: ['ErrorLengthLimit', env.durationLimit / 60] };

let bestQuality = all[0].quality.split('p')[0];
if (qualityMatch[bestQuality]) {
bestQuality = qualityMatch[bestQuality]
}
const urlMasterHLS = api.request?.files?.hls?.cdns?.akfire_interconnect_quic?.url;

if (Number(quality) < Number(bestQuality)) {
best = all.find(i => i.quality.split('p')[0] === quality);
}
if (!urlMasterHLS) return { error: 'ErrorCouldntFetch' }

if (!best) return { error: 'ErrorEmptyDownload' };
const masterHLS = await fetch(urlMasterHLS)
.then(r => r.text())
.catch(() => {});

return {
urls: best.url,
audioFilename: `vimeo_${obj.id}_audio`,
filename: `vimeo_${obj.id}_${best.width}x${best.height}.mp4`
}
}
if (!masterHLS) return { error: 'ErrorCouldntFetch' };

if (api.video.duration > env.durationLimit)
return { error: ['ErrorLengthLimit', env.durationLimit / 60] };
const variants = HLS.parse(masterHLS)?.variants?.sort(
(a, b) => Number(b.bandwidth) - Number(a.bandwidth)
);
if (!variants || variants.length === 0) return { error: 'ErrorEmptyDownload' };

let masterJSONURL = api.request.files.dash.cdns.akfire_interconnect_quic.url;
let masterJSON = await fetch(masterJSONURL).then(r => r.json()).catch(() => {});
let bestQuality;

if (!masterJSON) return { error: 'ErrorCouldntFetch' };
if (!masterJSON.video) return { error: 'ErrorEmptyDownload' };
if (quality < resolutionMatch[variants[0]?.resolution?.width]) {
bestQuality = variants.find(v =>
(quality === resolutionMatch[v.resolution.width])
);
}

let masterJSON_Video = masterJSON.video
.sort((a, b) => Number(b.width) - Number(a.width))
.filter(a => ["dash", "mp42"].includes(a.format));
if (!bestQuality) bestQuality = variants[0];

let bestVideo = masterJSON_Video[0];
if (Number(quality) < Number(resolutionMatch[bestVideo.width])) {
bestVideo = masterJSON_Video.find(i => resolutionMatch[i.width] === quality)
}
const expandLink = (path) => {
return new URL(path, urlMasterHLS).toString();
};

let masterM3U8 = `${masterJSONURL.split("/sep/")[0]}/sep/video/${bestVideo.id}/master.m3u8`;
const fallbackResolution = bestVideo.height > bestVideo.width ? bestVideo.width : bestVideo.height;
let urls = expandLink(bestQuality.uri);

const audioPath = bestQuality?.audio[0]?.uri;
if (audioPath) {
urls = [
urls,
expandLink(audioPath)
]
} else if (obj.isAudioOnly) {
return { error: 'ErrorEmptyDownload' };
}

return {
urls: masterM3U8,
urls,
isM3U8: true,
fileMetadata: fileMetadata,
filenameAttributes: {
service: "vimeo",
id: obj.id,
title: fileMetadata.title,
author: fileMetadata.artist,
resolution: `${bestVideo.width}x${bestVideo.height}`,
qualityLabel: `${resolutionMatch[bestVideo.width] || fallbackResolution}p`,
resolution: `${bestQuality.resolution.width}x${bestQuality.resolution.height}`,
qualityLabel: `${resolutionMatch[bestQuality.resolution.width]}p`,
extension: "mp4"
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/modules/stream/internal-hls.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function transformObject(streamInfo, hlsObject) {

hlsObject.uri = createInternalStream(fullUrl.toString(), streamInfo);

if (hlsObject.map) {
hlsObject.map = transformObject(streamInfo, hlsObject.map);
}

return hlsObject;
}

Expand Down
4 changes: 4 additions & 0 deletions src/modules/stream/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export function streamLiveRender(streamInfo, res) {

args = args.concat(ffmpegArgs[format]);

if (hlsExceptions.includes(streamInfo.service)) {
args.push('-bsf:a', 'aac_adtstoasc')
}

if (streamInfo.metadata) {
args = args.concat(metadataManager(streamInfo.metadata))
}
Expand Down

0 comments on commit 558b6a9

Please sign in to comment.