From 7a0089d3e671dc77e267a6d1a14b9bc66ef07396 Mon Sep 17 00:00:00 2001 From: localhost Date: Sat, 29 Jun 2024 12:16:36 +0200 Subject: [PATCH] removing need for cobalt --- utils/metadata.js | 43 +++++++++++++++++++------------------------ utils/ytdlp.js | 9 +-------- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/utils/metadata.js b/utils/metadata.js index 08a86d2..cccd6ab 100644 --- a/utils/metadata.js +++ b/utils/metadata.js @@ -1,5 +1,4 @@ const fetch = require('node-fetch') -const maxRetries = 5 async function getPipedInstance() { const instances = await (await fetch('https://piped-instances.kavin.rocks/', { @@ -32,31 +31,27 @@ async function getPlaylistVideos(id) { return json } -async function getVideoDownload(url, quality) { - let json - - for (let retries = 0; retries < maxRetries; retries++) { - try { - json = await (await fetch('http://gluetun:9000/api/json', { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - 'url': url, - 'vQuality': quality - }) - })).json() - - if (json.error) continue - return json - } catch (error) { - continue - } +async function getVideoDownload(json, quality) { + const adaptiveFormats = json['streaming_data']['adaptive_formats']; + let video = adaptiveFormats.find(f => f.quality_label === `${quality}p` && !f.has_audio); + if (!video) { // stupid bullshit. basically finding the lowest quality if the quality specified isnt there + video = adaptiveFormats.filter(f => !f.has_audio).reduce((prev, current) => { + if (!prev || parseInt(current.quality_label) < parseInt(prev.quality_label)) { + return current; + } + return prev; + }, null); } - return json + const audio = adaptiveFormats.find(f => f.has_audio); + + return { + url: [ + video.url, + audio.url + ] + }; } + module.exports = { getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos, getVideoDownload } \ No newline at end of file diff --git a/utils/ytdlp.js b/utils/ytdlp.js index 215a526..488147e 100644 --- a/utils/ytdlp.js +++ b/utils/ytdlp.js @@ -5,7 +5,6 @@ const hr = require('@tsmx/human-readable') const ffmpeg = require('fluent-ffmpeg') const ffmpegStatic = require('ffmpeg-static') -const path = require('node:path') const fs = require('node:fs') ffmpeg.setFfmpegPath(ffmpegStatic) @@ -22,13 +21,7 @@ async function downloadVideo(url, ws, id) { } if (video.basic_info.duration >= 900) quality = '360' // 15 minutes - const downloadJson = await metadata.getVideoDownload(url, quality) - if (downloadJson.status == 'error') { - return resolve({ - message: `Failed to request Youtube with error ${downloadJson.text}. Please retry...`, - fail: true - }) - } + const downloadJson = await metadata.getVideoDownload(video, quality) let size = '' let startTime = Date.now()