This commit is contained in:
		
							parent
							
								
									26eb7b7ec5
								
							
						
					
					
						commit
						67ef015561
					
				| 
						 | 
					@ -13,6 +13,8 @@ async function downloadVideo(url, ws, id) {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (video.basic_info.duration >= 900) quality = '360p' // 15 minutes
 | 
					        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}/${quality}`)
 | 
				
			||||||
        downloader.on('message', async function message(data) {
 | 
					        downloader.on('message', async function message(data) {
 | 
				
			||||||
| 
						 | 
					@ -38,4 +40,21 @@ async function downloadVideo(url, ws, id) {
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function getVideoQuality(json, quality) {
 | 
				
			||||||
 | 
					    const adaptiveFormats = json['streaming_data']['adaptive_formats'];
 | 
				
			||||||
 | 
					    let video = adaptiveFormats.find(f => f.quality_label === quality && !f.has_audio);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // If the specified quality isn't available, find the lowest quality video
 | 
				
			||||||
 | 
					    if (!video) {
 | 
				
			||||||
 | 
					        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 video ? video.quality_label : null;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = { downloadVideo }
 | 
					module.exports = { downloadVideo }
 | 
				
			||||||
		Loading…
	
		Reference in New Issue