MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MDX / forceMiniSticker

Function forceMiniSticker

plugins/igs.ts:173–216  ·  view source on GitHub ↗
(inputBuffer: Buffer, isVideo: boolean, cropSquare: boolean)

Source from the content-addressed store, hash-verified

171}
172
173async function forceMiniSticker(inputBuffer: Buffer, isVideo: boolean, cropSquare: boolean) {
174 const tmpDir = path.join(process.cwd(), 'temp');
175 if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir, { recursive: true });
176
177 const tempInput = path.join(tmpDir, `mini_${Date.now()}.${isVideo ? 'mp4' : 'jpg'}`);
178 const tempOutput = path.join(tmpDir, `mini_out_${Date.now()}.webp`);
179 fs.writeFileSync(tempInput, inputBuffer);
180
181 const vf = cropSquare
182 ? `crop=min(iw\\,ih):min(iw\\,ih),scale=256:256${isVideo ? ',fps=6' : ''}`
183 : `scale=256:256:force_original_aspect_ratio=decrease,pad=256:256:(ow-iw)/2:(oh-ih)/2:color=#00000000${isVideo ? ',fps=6' : ''}`;
184
185 const cmd = `ffmpeg -y -i "${tempInput}" ${isVideo ? '-t 2' : ''} -vf "${vf}" -c:v libwebp -preset default -loop 0 -pix_fmt yuva420p -quality 10 -compression_level 6 -b:v 40k "${tempOutput}"`;
186
187 await new Promise<void>((resolve, reject) => {
188 exec(cmd, (error) => error ? reject(error) : resolve(undefined));
189 });
190
191 if (!fs.existsSync(tempOutput)) {
192 try { fs.unlinkSync(tempInput); } catch {}
193 return null;
194 }
195
196 const smallWebp = fs.readFileSync(tempOutput);
197
198 const img = new webp.Image();
199 await img.load(smallWebp);
200 const json = {
201 'sticker-pack-id': crypto.randomBytes(32).toString('hex'),
202 'sticker-pack-name': config.packname || 'MegaBot',
203 'emojis': ['📸']
204 };
205 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]);
206 const jsonBuffer = Buffer.from(JSON.stringify(json), 'utf8');
207 const exif = Buffer.concat([exifAttr, jsonBuffer]);
208 exif.writeUIntLE(jsonBuffer.length, 14, 4);
209 img.exif = exif;
210 const finalBuffer = await img.save(null);
211
212 try { fs.unlinkSync(tempInput); } catch {}
213 try { fs.unlinkSync(tempOutput); } catch {}
214
215 return finalBuffer;
216}
217
218export default {
219 command: 'igs',

Callers 1

handlerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected