Skip to content

Commit cc05f6e

Browse files
committed
Always get the largest stream thumbnail
1 parent f8e315a commit cc05f6e

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

packages/core/src/rest/Youtube.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function formatPlaylistTrack(track: ytpl.Item) {
5151
return {
5252
streams: [{ source: 'Youtube', id: track.id }],
5353
name: track.title,
54-
thumbnail: track.thumbnails[0].url,
54+
thumbnail: getLargestThumbnail(track.thumbnails),
5555
artist: track.author.name
5656
};
5757
}
@@ -93,7 +93,7 @@ async function handleYoutubeVideo(url: string): Promise<YoutubeResult[]> {
9393
return [{
9494
streams: [{ source: 'Youtube', id: videoDetails.videoId }],
9595
name: videoDetails.title,
96-
thumbnail: videoDetails.thumbnails[0].url,
96+
thumbnail: getLargestThumbnail(videoDetails.thumbnails),
9797
artist: { name: videoDetails.ownerChannelName }
9898
}];
9999
}
@@ -177,14 +177,14 @@ export const getStreamForId = async (id: string, sourceName: string, useSponsorB
177177
stream: formatInfo.url,
178178
duration: parseInt(trackInfo.videoDetails.lengthSeconds),
179179
title: trackInfo.videoDetails.title,
180-
thumbnail: trackInfo.thumbnail_url,
180+
thumbnail: getLargestThumbnail(trackInfo.videoDetails.thumbnails),
181181
format: formatInfo.container,
182182
skipSegments: segments,
183183
originalUrl: videoUrl,
184184
isLive: formatInfo.isLive,
185185
author: {
186186
name: trackInfo.videoDetails.author.name,
187-
thumbnail: trackInfo.videoDetails.author.thumbnails[0].url
187+
thumbnail: getLargestThumbnail(trackInfo.videoDetails.author.thumbnails)
188188
}
189189
};
190190
} catch (e) {
@@ -201,11 +201,20 @@ function videoToStreamData(video: SearchVideo, source: string): StreamData {
201201
stream: undefined,
202202
duration: parseInt(video.duration.text),
203203
title: video.title,
204-
thumbnail: video.thumbnails[0].url,
204+
thumbnail: getLargestThumbnail(video.thumbnails),
205205
originalUrl: video.url,
206206
author: {
207207
name: video.channel.name,
208-
thumbnail: video.thumbnails[0].url
208+
thumbnail: getLargestThumbnail(video.thumbnails)
209209
}
210210
};
211211
}
212+
213+
const getLargestThumbnail = (thumbnails: ytdl.thumbnail[]): string => {
214+
const isNotEmpty = thumbnails.length > 0;
215+
const largestThumbnail = isNotEmpty && thumbnails.reduce((prev, current) => {
216+
return (prev.height * prev.width) > (current.height * current.width) ? prev : current;
217+
});
218+
219+
return largestThumbnail?.url;
220+
};

0 commit comments

Comments
 (0)