Skip to content

Instantly share code, notes, and snippets.

@hi-ogawa
Created January 25, 2025 07:22
Show Gist options
  • Save hi-ogawa/cff0ca65fed3783a6831f72cf5da9508 to your computer and use it in GitHub Desktop.
Save hi-ogawa/cff0ca65fed3783a6831f72cf5da9508 to your computer and use it in GitHub Desktop.
A little script I used to fix up thumbnails of old videos
/*
Fix up old thumbnails https://www.youtube.com/@hiroshi18181/videos
- dump all videos data
yt-dlp -j https://www.youtube.com/@hiroshi18181/videos > videos.ndjson
jq -s '[.[] | {id, title, description}]' videos.ndjson > videos-slim.json
node youtube-thumbnail.js process-json videos-slim.json > videos-slim2.json5
- download thumbnails
node youtube-thumbnail.js download-thumbnail videos-slim2.json5
- merge thumbnails
do it manually on https://www.photopea.com/
*/
import fs from "node:fs";
import { Readable } from "node:stream";
async function main() {
const [command, ...args] = process.argv.slice(2);
if (command === 'process-json') {
/** @type {{ id: string, title: string, description: string }[]} */
const entries = JSON.parse(fs.readFileSync(args[0], "utf-8"));
const entries2 = entries.map((e) => {
const matches = e.description.matchAll(
/https:\/\/www\.youtube\.com\/watch\?v=[\w-]{11}/g,
);
return [
e.title,
`https://www.youtube.com/watch?v=${e.id}`,
[...matches].map((m) => m[0]),
];
});
console.log(entries2)
}
if (command === 'download-thumbnail') {
/**
* @param {string} url
* @param {string} destFile
*/
async function download(url, destFile) {
const response = await fetch(url);
await fs.promises.writeFile(destFile, Readable.fromWeb(response.body));
}
function toThumbnailUrl(url) {
return `https://i.ytimg.com/vi/${url.match(/v=([\w-]{11})/)[1]}/maxresdefault.jpg`;
}
/** @type {[string, string, string[]][]} */
const entries = (0, eval)(fs.readFileSync(args[0], "utf-8"));
for (let j = 0; j < entries.length; j++) {
const [title, base, others] = entries[j];
const dir = `.tmp/${String(j).padStart(2, '0')}-${title}`;
fs.mkdirSync(dir, { recursive: true });
// download
await download(toThumbnailUrl(base), `${dir}/base.jpg`);
for (let i = 0; i < others.length; i++) {
await download(toThumbnailUrl(others[i]), `${dir}/other-${i}.jpg`)
}
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment