lekker thumbnailer jongen

This commit is contained in:
Flummi 2022-01-04 21:03:48 +01:00
parent da6b310449
commit 081df808d9
4 changed files with 119 additions and 26 deletions

73
debug/thumbnailer.mjs Normal file
View File

@ -0,0 +1,73 @@
import cfg from "../src/inc/config.mjs";
import sql from "../src/inc/sql.mjs";
import { promises as fs } from "fs";
import { exec as _exec } from "child_process";
import fetch from "flumm-fetch-cookies";
const exec = cmd => new Promise((resolve, reject) => {
_exec(cmd, { maxBuffer: 5e3 * 1024 }, (err, stdout, stderr) => {
if(err)
return reject(err);
if(stderr)
console.error(stderr);
resolve({ stdout: stdout });
});
});
const _args = process.argv.slice(2);
const _itemid = +_args[0] || 1;
const items = await sql('items').where('id', _itemid);//.where('mime', 'like', 'audio/%');
let count = 1;
let total = items.length;
for(let item of items) {
const itemid = item.id;
const filename = item.dest;
const mime = item.mime;
try {
if(mime.startsWith('video/') || mime == 'image/gif')
await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./tmp/${itemid}.png`);
else if(mime.startsWith('image/') && mime != 'image/gif')
await exec(`convert ./public/b/${filename} ./tmp/${itemid}.png`);
else if(mime.startsWith('audio/')) {
if(item.src.match(/soundcloud/)) {
const cover = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' --get-thumbnail "${item.src}"`)).stdout.trim();
if(!cover.match(/default_avatar/)) {
try {
await exec(`wget "${cover}" -O ./tmp/${itemid}.jpg`);
const size = (await fs.stat(`./tmp/${itemid}.jpg`)).size;
if(size >= 0)
await exec(`convert ./tmp/${itemid}.jpg ./tmp/${itemid}.png`);
await fs.unlink(`./tmp/${itemid}.jpg`);
} catch(err) {}
}
else
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
}
else
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
await fs.copyFile(`./tmp/${itemid}.png`, `./public/ca/${itemid}.png`);
}
await exec(`convert "./tmp/${itemid}.png" -resize "128x128^" -gravity center -crop 128x128+0+0 +repage ./public/t/${itemid}.png`);
await fs.unlink(`./tmp/${itemid}.png`);
} catch(err) {
await fs.copyFile('./mugge.png', `./public/t/${itemid}.png`);
}
console.log(`current: ${itemid} (${count} / ${total})`);
count++;
}
/*for(let item of items) {
try {
const itemid = item.id;
const filename = item.dest;
await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./debug/tmp/${itemid}`);
await exec(`convert "./debug/tmp/${itemid}" -resize "128x128^" -gravity center -crop 128x128+0+0 +repage ./public/t/${itemid}.png`);
await fs.unlink(`./debug/tmp/${itemid}`);
console.log(`current: ${itemid} (${count} / ${total})`);
} catch(err) {}
count++;
};*/

BIN
mugge.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

View File

@ -7,6 +7,8 @@
"start": "node --experimental-json-modules src/index.mjs",
"trigger": "node --experimental-json-modules debug/trigger.mjs",
"autotagger": "node --experimental-json-modules debug/autotagger.mjs",
"thumbnailer": "node --experimental-json-modules debug/thumbnailer.mjs",
"test": "node --experimental-json-modules debug/test.mjs",
"clean": "node --experimental-json-modules debug/clean.mjs"
},
"author": "Flummi",

View File

@ -127,52 +127,70 @@ export default async bot => {
stamp: ~~(new Date() / 1000),
active: 1
});
const insertq = (await sql('items').where('dest', filename).limit(1))[0].id;
const itemid = (await sql('items').where('dest', filename).limit(1))[0].id;
// generate thumbnail
try {
let thumb_orig = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' --get-thumbnail "${link}"`)).stdout.trim();
if(!thumb_orig.startsWith("http")) {
if(mime.startsWith("image") && mime !== "image/gif")
thumb_orig = `./public/b/${filename}`;
else if(!mime.startsWith("audio")) {
await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./tmp/${insertq}`);
thumb_orig = `./tmp/${insertq}`;
if(mime.startsWith('video/') || mime == 'image/gif')
await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./tmp/${itemid}.png`);
else if(mime.startsWith('image/') && mime != 'image/gif')
await exec(`convert ./public/b/${filename} ./tmp/${itemid}.png`);
else if(mime.startsWith('audio/')) {
if(link.match(/soundcloud/)) {
const cover = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' --get-thumbnail "${link}"`)).stdout.trim();
if(!cover.match(/default_avatar/)) {
try {
await exec(`wget "${cover}" -O ./tmp/${itemid}.jpg`);
const size = (await fs.promises.stat(`./tmp/${itemid}.jpg`)).size;
if(size >= 0)
await exec(`convert ./tmp/${itemid}.jpg ./tmp/${itemid}.png`);
await fs.promises.unlink(`./tmp/${itemid}.jpg`);
} catch(err) {}
}
else if(mime.startsWith("audio")) {
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${insertq}.png`)
await exec(`cp ./tmp/${insertq}.png ./public/ca/${insertq}.png`)
thumb_orig = `./tmp/${insertq}.png`;
else
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
}
else
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
await fs.promises.copyFile(`./tmp/${itemid}.png`, `./public/ca/${itemid}.png`);
}
await exec(`convert "${thumb_orig}" -resize "128x128^" -gravity center -crop 128x128+0+0 +repage ./public/t/${insertq}.png`);
await fs.promises.unlink(`./tmp/${insertq}`).catch(_=>{});
await exec(`convert "./tmp/${itemid}.png" -resize "128x128^" -gravity center -crop 128x128+0+0 +repage ./public/t/${itemid}.png`);
await fs.promises.unlink(`./tmp/${itemid}.png`);
} catch(err) {
e.reply("\x033>no thumb lol");
console.error(err);
await fs.promises.copyFile('./mugge.png', `./public/t/${itemid}.png`);
}
let speed = lib.calcSpeed(size, end);
speed = !Number.isFinite(speed) ? "yes" : `${speed.toFixed(2)} Mbit/s`;
// tagger
// autotagger
let tag;
if(mime.startsWith("image")) {
try {
if(mime.startsWith('image') && mime != 'image/gif') {
const body = { headers: { Authorization: `Basic ${cfg.tagger.btoa}` } };
const res = await (await fetch(`${cfg.tagger.endpoint}/categories/nsfw_beta?image_url=${cfg.main.url}/b/${filename}`, body)).json();
if(res) {
tag = (res.result.categories[0].name.en === "safe") ? "sfw" : "nsfw";
if(res)
tag = (res.result.categories[0].name.en === 'safe') ? 'sfw' : 'nsfw';
}
else if(mime.startsWith('audio')) {
tag = 'sfw';
}
if(tag === 'sfw' || tag === 'nsfw') {
await sql("tags_assign").insert({
tag_id: tag === "sfw" ? 1 : 2,
item_id: insertq,
user_id: 7 // user: autotagger
item_id: itemid,
user_id: 7 // user: autotagger (ID: 7)
});
}
} catch(err) {
console.error(err);
}
e.reply([
`[f0cked] link: ${cfg.main.url}/${insertq} | size: ${lib.formatSize(size)} | speed: ${speed}` + (tag ? ` | ${tag}` : "")
`[f0cked] link: ${cfg.main.url}/${itemid} | size: ${lib.formatSize(size)} | speed: ${speed}` + (tag ? ` | ${tag}` : "")
]);
});