JerryCoderr

comprez.js

Mar 11th, 2026
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Module } = require('../main');
  2. const ffmpeg = require('fluent-ffmpeg');
  3. const fs = require('fs');
  4. const { getTempPath } = require('../core/helpers');
  5.  
  6. Module({
  7.     pattern: 'cmp3',
  8.     desc: 'Compress large audio file to smaller size',
  9.     use: 'media',
  10.     usage: 'compressaudio (reply to audio)'
  11. }, async (message) => {
  12.     if (!message.reply_message || !message.reply_message.audio) {
  13.         return await message.sendReply('_Reply to an audio file to compress it._');
  14.     }
  15.  
  16.     const processing = await message.sendReply('_Compressing audio, please wait..._');
  17.  
  18.     let inputPath;
  19.     let outputPath;
  20.  
  21.     try {
  22.         inputPath = await message.reply_message.download();
  23.         outputPath = getTempPath('compressed.mp3');
  24.  
  25.         await new Promise((resolve, reject) => {
  26.             ffmpeg(inputPath)
  27.                 .audioCodec('libmp3lame')
  28.                 .audioBitrate('64k')
  29.                 .format('mp3')
  30.                 .save(outputPath)
  31.                 .on('end', resolve)
  32.                 .on('error', reject);
  33.         });
  34.  
  35.         await message.sendMessage(
  36.             { stream: fs.createReadStream(outputPath) },
  37.             'audio',
  38.             { mimetype: 'audio/mpeg' }
  39.         );
  40.  
  41.         await message.edit('_Audio compressed successfully._', message.jid, processing.key);
  42.  
  43.     } catch (err) {
  44.         await message.edit('_Failed to compress audio._', message.jid, processing.key);
  45.     } finally {
  46.         if (inputPath && fs.existsSync(inputPath)) fs.unlinkSync(inputPath);
  47.         if (outputPath && fs.existsSync(outputPath)) fs.unlinkSync(outputPath);
  48.     }
  49. });
Advertisement
Add Comment
Please, Sign In to add comment