Skip to content

Commit bf9a528

Browse files
committed
improved script with more resolution fallbacks, fix fallback order since sddefault is actually higher res than hqdefault
1 parent be2ccbe commit bf9a528

File tree

58 files changed

+25
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+25
-10
lines changed
14.5 KB
14.9 KB
11.2 KB
12.4 KB
24.8 KB
14.6 KB
17.9 KB
17.3 KB
17.6 KB
12.1 KB
20.2 KB

node-scripts/download-non-challenge-thumbnails.mjs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,49 @@ for (const jsonPath of paths.videos) {
2828
fs.rmSync(path.join(baseDir, filename), { force: true });
2929
});
3030

31-
const { videoId } = JSON.parse(fs.readFileSync(jsonPath));
32-
33-
console.log(videoId);
31+
const video = JSON.parse(fs.readFileSync(jsonPath));
32+
const isMultiPart = !!video.parts;
33+
let videoId = isMultiPart ? video.parts[0].videoId : video.videoId;
3434

3535
// download thumbnail from YouTube CDN
36-
const thumbnailPaths = [
37-
`https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`, // highest resolution (might not be available for 720p videos)
38-
`https://img.youtube.com/vi/${videoId}/hqdefault.jpg` // fallback
36+
const thumbnailResolutions = [
37+
'maxresdefault', // 1280x720 - may not be available for 720p videos
38+
'sddefault', // 640x480
39+
'hqdefault', // 480x360
40+
'mqdefault', // 320x180
41+
'default' // 120x90
3942
];
4043

4144
let buffer;
42-
for (const thumbnailPath of thumbnailPaths) {
45+
let resolution;
46+
for (const thumbnailResolution of thumbnailResolutions) {
47+
resolution = thumbnailResolution;
48+
49+
// const thumbnailPath = `https://img.youtube.com/vi_webp/${videoId}/${resolution}.webp`;
50+
const thumbnailPath = `https://img.youtube.com/vi/${videoId}/${resolution}.jpg`;
4351
buffer = await getImage(thumbnailPath);
4452
if (buffer) break;
4553

46-
console.warn(`Failed to download ${thumbnailPath}`);
54+
console.warn(
55+
videoId,
56+
`🟠`,
57+
resolution,
58+
`| missing, trying lower resolution...`
59+
);
4760
}
4861

4962
if (!buffer) {
5063
console.error(
51-
`Failed to locate a thumbnail for ${videoId} - process aborted.`
64+
`🔴 Failed to locate a thumbnail for ${videoId} - process aborted.`
5265
);
5366
process.exit(1);
5467
}
5568

69+
console.log(videoId, '🟢', resolution, '\n');
70+
5671
// write file to disk
5772
fs.writeFileSync(path.join(baseDir, 'index.jpg'), buffer);
5873

5974
// sleep for a bit to avoid spamming / getting blocked
60-
await sleep(100 + Math.random() * 500);
75+
await sleep(100 + Math.random() * 300);
6176
}

0 commit comments

Comments
 (0)