UNPKG

@seaavey/bot

Version:

The Library for Seaavey Bot

80 lines (79 loc) 5.04 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.videoToWebp = exports.imageToWebp = void 0; exports.writeExif = writeExif; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const fluent_ffmpeg_1 = __importDefault(require("fluent-ffmpeg")); const node_webpmux_1 = __importDefault(require("node-webpmux")); const Utils_1 = require("@seaavey/tools/lib/Utils"); const temp = path_1.default.join(process.cwd(), 'temp'); const imageToWebp = async (media) => { const tmpFileIn = path_1.default.join(temp, await (0, Utils_1.getRandom)((media === null || media === void 0 ? void 0 : media.ext) || 'png')); const tmpFileOut = path_1.default.join(temp, await (0, Utils_1.getRandom)('webp')); fs_1.default.writeFileSync(tmpFileIn, media.data); try { await new Promise((resolve, reject) => { (0, fluent_ffmpeg_1.default)(tmpFileIn) .on('error', reject) .on('end', () => resolve(true)) //.addOutputOptions([`-vcodec`,`libwebp`,`-vf`,`scale=512:512:force_original_aspect_ratio=increase,fps=15,crop=512:512`]).toFormat('webp').save(tmpFileOut) .addOutputOptions(['-vcodec', 'libwebp', '-vf', "scale='min(320,iw)':min'(320,ih)':force_original_aspect_ratio=decrease,fps=15,split[a][b];[a]palettegen=reserve_transparent=on[p];[b][p]paletteuse"]) .toFormat('webp') .saveToFile(tmpFileOut); }); const buff = fs_1.default.readFileSync(tmpFileOut); return buff; } catch (e) { throw e; } }; exports.imageToWebp = imageToWebp; const videoToWebp = async (media) => { const tmpFileIn = path_1.default.join(temp, await (0, Utils_1.getRandom)((media === null || media === void 0 ? void 0 : media.ext) || 'mp4')); const tmpFileOut = path_1.default.join(temp, await (0, Utils_1.getRandom)('webp')); fs_1.default.writeFileSync(tmpFileIn, media.data); try { await new Promise((resolve, reject) => { (0, fluent_ffmpeg_1.default)(tmpFileIn) .on('error', reject) .on('end', () => resolve(true)) //.addOutputOptions([`-vcodec`,`libwebp`,`-vf`,`scale=512:512:force_original_aspect_ratio=increase,fps=15,crop=512:512`]).toFormat('webp').save(tmpFileOut) .addOutputOptions(['-vcodec', 'libwebp', '-vf', "scale='min(320,iw)':min'(320,ih)':force_original_aspect_ratio=decrease,fps=15, pad=320:320:-1:-1:color=white../..0.0, split [a][b]; [a] palettegen=reserve_transparent=on:transparency_color=000000 [p]; [b][p] paletteuse", '-loop', '0', '-ss', '00:00:00', '-t', '00:00:05', '-preset', 'default', '-an', '-vsync', '0']) .toFormat('webp') .saveToFile(tmpFileOut); }); const buff = fs_1.default.readFileSync(tmpFileOut); return buff; } catch (e) { throw e; } }; exports.videoToWebp = videoToWebp; async function writeExif(media, metadata) { let wMedia = /webp/.test(media.mimetype) ? media.data : /image/.test(media.mimetype) ? await (0, exports.imageToWebp)(media) : /video/.test(media.mimetype) ? await (0, exports.videoToWebp)(media) : ''; if (metadata && (Object === null || Object === void 0 ? void 0 : Object.keys(metadata).length) !== 0) { const img = new node_webpmux_1.default.Image(); const json = { 'sticker-pack-id': (metadata === null || metadata === void 0 ? void 0 : metadata.packId) || `Seaavey-${Date.now()}`, 'sticker-pack-name': (metadata === null || metadata === void 0 ? void 0 : metadata.packName) || 'WhatsApp Sticker By', 'sticker-pack-publisher': (metadata === null || metadata === void 0 ? void 0 : metadata.packPublish) || 'Seaavey-BOT', 'android-app-store-link': (metadata === null || metadata === void 0 ? void 0 : metadata.androidApp) || 'https://play.google.com/store/apps/details?id=com.bitsmedia.android.muslimpro', 'ios-app-store-link': (metadata === null || metadata === void 0 ? void 0 : metadata.iOSApp) || 'https://apps.apple.com/id/app/muslim-pro-al-quran-adzan/id388389451?|=id', emojis: (metadata === null || metadata === void 0 ? void 0 : metadata.emojis) || ['😋', '😎', '🤣', '😂', '😁'], 'is-avatar-sticker': (metadata === null || metadata === void 0 ? void 0 : metadata.isAvatar) || 0, }; 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]); const jsonBuff = Buffer.from(JSON.stringify(json), 'utf-8'); const exif = Buffer.concat([exifAttr, jsonBuff]); exif.writeUIntLE(jsonBuff.length, 14, 4); await img.load(wMedia); img.exif = exif; return await img.save(null); } }