Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { Module } = require('../main');
- const ffmpeg = require('fluent-ffmpeg');
- const fs = require('fs');
- const { getTempPath } = require('../core/helpers');
- Module({
- pattern: 'cmp3',
- desc: 'Compress large audio file to smaller size',
- use: 'media',
- usage: 'compressaudio (reply to audio)'
- }, async (message) => {
- if (!message.reply_message || !message.reply_message.audio) {
- return await message.sendReply('_Reply to an audio file to compress it._');
- }
- const processing = await message.sendReply('_Compressing audio, please wait..._');
- let inputPath;
- let outputPath;
- try {
- inputPath = await message.reply_message.download();
- outputPath = getTempPath('compressed.mp3');
- await new Promise((resolve, reject) => {
- ffmpeg(inputPath)
- .audioCodec('libmp3lame')
- .audioBitrate('64k')
- .format('mp3')
- .save(outputPath)
- .on('end', resolve)
- .on('error', reject);
- });
- await message.sendMessage(
- { stream: fs.createReadStream(outputPath) },
- 'audio',
- { mimetype: 'audio/mpeg' }
- );
- await message.edit('_Audio compressed successfully._', message.jid, processing.key);
- } catch (err) {
- await message.edit('_Failed to compress audio._', message.jid, processing.key);
- } finally {
- if (inputPath && fs.existsSync(inputPath)) fs.unlinkSync(inputPath);
- if (outputPath && fs.existsSync(outputPath)) fs.unlinkSync(outputPath);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment