From cc05f6efba251d3e8abda1c2c95f4d9dd96bd1e6 Mon Sep 17 00:00:00 2001 From: nukeop <12746779+nukeop@users.noreply.github.com> Date: Tue, 3 Dec 2024 03:08:56 +0100 Subject: [PATCH] Always get the largest stream thumbnail --- packages/core/src/rest/Youtube.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/core/src/rest/Youtube.ts b/packages/core/src/rest/Youtube.ts index c883c9879a..913aed3c16 100644 --- a/packages/core/src/rest/Youtube.ts +++ b/packages/core/src/rest/Youtube.ts @@ -51,7 +51,7 @@ function formatPlaylistTrack(track: ytpl.Item) { return { streams: [{ source: 'Youtube', id: track.id }], name: track.title, - thumbnail: track.thumbnails[0].url, + thumbnail: getLargestThumbnail(track.thumbnails), artist: track.author.name }; } @@ -93,7 +93,7 @@ async function handleYoutubeVideo(url: string): Promise { return [{ streams: [{ source: 'Youtube', id: videoDetails.videoId }], name: videoDetails.title, - thumbnail: videoDetails.thumbnails[0].url, + thumbnail: getLargestThumbnail(videoDetails.thumbnails), artist: { name: videoDetails.ownerChannelName } }]; } @@ -177,14 +177,14 @@ export const getStreamForId = async (id: string, sourceName: string, useSponsorB stream: formatInfo.url, duration: parseInt(trackInfo.videoDetails.lengthSeconds), title: trackInfo.videoDetails.title, - thumbnail: trackInfo.thumbnail_url, + thumbnail: getLargestThumbnail(trackInfo.videoDetails.thumbnails), format: formatInfo.container, skipSegments: segments, originalUrl: videoUrl, isLive: formatInfo.isLive, author: { name: trackInfo.videoDetails.author.name, - thumbnail: trackInfo.videoDetails.author.thumbnails[0].url + thumbnail: getLargestThumbnail(trackInfo.videoDetails.author.thumbnails) } }; } catch (e) { @@ -201,11 +201,20 @@ function videoToStreamData(video: SearchVideo, source: string): StreamData { stream: undefined, duration: parseInt(video.duration.text), title: video.title, - thumbnail: video.thumbnails[0].url, + thumbnail: getLargestThumbnail(video.thumbnails), originalUrl: video.url, author: { name: video.channel.name, - thumbnail: video.thumbnails[0].url + thumbnail: getLargestThumbnail(video.thumbnails) } }; } + +const getLargestThumbnail = (thumbnails: ytdl.thumbnail[]): string => { + const isNotEmpty = thumbnails.length > 0; + const largestThumbnail = isNotEmpty && thumbnails.reduce((prev, current) => { + return (prev.height * prev.width) > (current.height * current.width) ? prev : current; + }); + + return largestThumbnail?.url; +};