MCPcopy
hub / github.com/mruniquehacker/Knightbot-MD / forceMiniSticker

Function forceMiniSticker

commands/igs.js:273–316  ·  view source on GitHub ↗
(inputBuffer, isVideo, cropSquare)

Source from the content-addressed store, hash-verified

271
272// Extreme fallback to force very small stickers when needed
273async function forceMiniSticker(inputBuffer, isVideo, cropSquare) {
274 const tmpDir = path.join(process.cwd(), 'tmp');
275 if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir, { recursive: true });
276
277 const tempInput = path.join(tmpDir, `mini_${Date.now()}.${isVideo ? 'mp4' : 'jpg'}`);
278 const tempOutput = path.join(tmpDir, `mini_out_${Date.now()}.webp`);
279 fs.writeFileSync(tempInput, inputBuffer);
280
281 const vf = cropSquare
282 ? `crop=min(iw\\,ih):min(iw\\,ih),scale=256:256${isVideo ? ',fps=6' : ''}`
283 : `scale=256:256:force_original_aspect_ratio=decrease,pad=256:256:(ow-iw)/2:(oh-ih)/2:color=#00000000${isVideo ? ',fps=6' : ''}`;
284
285 const cmd = `ffmpeg -y -i "${tempInput}" ${isVideo ? '-t 2' : ''} -vf "${vf}" -c:v libwebp -preset default -loop 0 -pix_fmt yuva420p -quality 25 -compression_level 6 -b:v 60k "${tempOutput}"`;
286
287 await new Promise((resolve, reject) => {
288 exec(cmd, (error) => error ? reject(error) : resolve());
289 });
290
291 if (!fs.existsSync(tempOutput)) {
292 try { fs.unlinkSync(tempInput); } catch {}
293 return null;
294 }
295 const smallWebp = fs.readFileSync(tempOutput);
296
297 // Re-apply EXIF
298 const img = new webp.Image();
299 await img.load(smallWebp);
300 const json = {
301 'sticker-pack-id': crypto.randomBytes(32).toString('hex'),
302 'sticker-pack-name': settings.packname || 'KnightBot',
303 'emojis': ['📸']
304 };
305 const exifAttr = Buffer.from([0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00]);
306 const jsonBuffer = Buffer.from(JSON.stringify(json), 'utf8');
307 const exif = Buffer.concat([exifAttr, jsonBuffer]);
308 exif.writeUIntLE(jsonBuffer.length, 14, 4);
309 img.exif = exif;
310 const finalBuffer = await img.save(null);
311
312 try { fs.unlinkSync(tempInput); } catch {}
313 try { fs.unlinkSync(tempOutput); } catch {}
314
315 return finalBuffer;
316}
317
318module.exports = { igsCommand };
319

Callers 1

igsCommandFunction · 0.85

Calls 1

execFunction · 0.85

Tested by

no test coverage detected