remove useless video call

This commit is contained in:
localhost 2025-09-24 19:32:30 +02:00
parent aff62f48ec
commit 5ac3540277
1 changed files with 1 additions and 31 deletions

View File

@ -3,20 +3,8 @@ import { getVideo } from '@/utils/metadata';
async function downloadVideo(ws: any, id: string): Promise<{ fail: boolean, message: string }> { async function downloadVideo(ws: any, id: string): Promise<{ fail: boolean, message: string }> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let quality = '480p'
const video = await getVideo(id)
if (video.error) {
return resolve({
message: `Failed to request Youtube with error ${video.error}. Please retry...`,
fail: true
})
}
if (video.basic_info.duration >= 900) quality = '360p' // 15 minutes
quality = await getVideoQuality(video, quality)
let isDownloading = true let isDownloading = true
const downloader = new WebSocket(`ws://${(process.env.METADATA!).replace('http://', '')}/download/${id}/${quality}`) const downloader = new WebSocket(`ws://${(process.env.METADATA!).replace('http://', '')}/download/${id}`)
downloader.on('message', async function message(data: any) { downloader.on('message', async function message(data: any) {
const text = data.toString() const text = data.toString()
@ -42,22 +30,4 @@ async function downloadVideo(ws: any, id: string): Promise<{ fail: boolean, mess
}) })
} }
async function getVideoQuality(json: any, quality: string) {
const adaptiveFormats = json['streaming_data']['adaptive_formats'];
let video = adaptiveFormats.find((f: any) => f.quality_label === quality && !f.has_audio);
if (!video) {
const target = parseInt(quality);
video = adaptiveFormats // find the quality thats closest to the one we wanted
.filter((f: any) => !f.has_audio && f.quality_label)
.reduce((prev: any, curr: any) => {
const currDiff = Math.abs(parseInt(curr.quality_label) - target);
const prevDiff = prev ? Math.abs(parseInt(prev.quality_label) - target) : Infinity;
return currDiff < prevDiff ? curr : prev;
}, null);
}
return video ? video.quality_label : null;
}
export { downloadVideo } export { downloadVideo }