2022-01-04 20:03:48 +00:00
|
|
|
import sql from "../src/inc/sql.mjs";
|
2022-01-05 19:21:50 +00:00
|
|
|
import fs from "fs";
|
2022-01-04 20:03:48 +00:00
|
|
|
import { exec as _exec } from "child_process";
|
|
|
|
|
|
|
|
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);
|
2022-01-05 19:21:50 +00:00
|
|
|
const _itemid = +_args[0] || 0;
|
2022-01-04 20:03:48 +00:00
|
|
|
|
2022-01-05 19:21:50 +00:00
|
|
|
let items;
|
|
|
|
if(_itemid > 0)
|
|
|
|
items = await sql('items').where('id', _itemid);
|
|
|
|
else
|
2022-01-06 03:10:44 +00:00
|
|
|
items = await sql('items').orderBy('id', 'asc').where('src', 'like', '%soundcloud%').where('mime', 'like', 'audio/%');
|
2022-01-04 20:03:48 +00:00
|
|
|
let count = 1;
|
|
|
|
let total = items.length;
|
|
|
|
|
|
|
|
for(let item of items) {
|
|
|
|
const itemid = item.id;
|
|
|
|
const filename = item.dest;
|
|
|
|
const mime = item.mime;
|
2022-01-05 19:21:50 +00:00
|
|
|
const link = item.src;
|
2022-01-04 20:03:48 +00:00
|
|
|
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/')) {
|
2022-01-05 19:21:50 +00:00
|
|
|
if(link.match(/soundcloud/)) {
|
2022-01-06 03:10:44 +00:00
|
|
|
let cover = (await exec(`yt-dlp --get-thumbnail "${link}"`)).stdout.trim();
|
2022-01-04 20:03:48 +00:00
|
|
|
if(!cover.match(/default_avatar/)) {
|
2022-01-06 03:10:44 +00:00
|
|
|
cover = cover.replace(/-(large|original)\./, '-t500x500.');
|
2022-01-04 20:03:48 +00:00
|
|
|
try {
|
|
|
|
await exec(`wget "${cover}" -O ./tmp/${itemid}.jpg`);
|
2022-01-05 19:21:50 +00:00
|
|
|
const size = (await fs.promises.stat(`./tmp/${itemid}.jpg`)).size;
|
|
|
|
if(size >= 0) {
|
2022-01-04 20:03:48 +00:00
|
|
|
await exec(`convert ./tmp/${itemid}.jpg ./tmp/${itemid}.png`);
|
2022-01-06 03:10:44 +00:00
|
|
|
await exec(`convert ./tmp/${itemid}.jpg ./public/ca/${itemid}.webp`);
|
2022-01-05 19:21:50 +00:00
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
//console.log(err);
|
|
|
|
}
|
2022-01-04 20:03:48 +00:00
|
|
|
}
|
2022-01-05 19:21:50 +00:00
|
|
|
else {
|
2022-01-04 20:03:48 +00:00
|
|
|
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
|
2022-01-05 19:21:50 +00:00
|
|
|
await exec(`convert ./tmp/${itemid}.png ./public/ca/${itemid}.webp`);
|
|
|
|
}
|
2022-01-04 20:03:48 +00:00
|
|
|
}
|
2022-01-05 19:21:50 +00:00
|
|
|
else {
|
2022-01-04 20:03:48 +00:00
|
|
|
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
|
2022-01-05 19:21:50 +00:00
|
|
|
await exec(`convert ./tmp/${itemid}.png ./public/ca/${itemid}.webp`);
|
|
|
|
}
|
2022-01-04 20:03:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 19:21:50 +00:00
|
|
|
await exec(`convert "./tmp/${itemid}.png" -resize "128x128^" -gravity center -crop 128x128+0+0 +repage ./public/t/${itemid}.webp`);
|
|
|
|
await fs.promises.unlink(`./tmp/${itemid}.png`).catch(err => {});
|
|
|
|
await fs.promises.unlink(`./tmp/${itemid}.jpg`).catch(err => {});
|
2022-01-04 20:03:48 +00:00
|
|
|
} catch(err) {
|
2022-01-05 19:21:50 +00:00
|
|
|
//console.log(err);
|
|
|
|
await exec(`convert ./mugge.png ./public/t/${itemid}.webp`);
|
2022-01-04 20:03:48 +00:00
|
|
|
}
|
|
|
|
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++;
|
|
|
|
};*/
|