| const fs = require('fs') | |
| const path = require('path') | |
| const { spawn } = require('child_process') | |
| function ffmpeg(buffer, args = [], ext = '', ext2 = '') { | |
| return new Promise(async (resolve, reject) => { | |
| try { | |
| let tmp = path.join(__dirname, '../tmp', + new Date + '.' + ext) | |
| let out = tmp + '.' + ext2 | |
| await fs.promises.writeFile(tmp, buffer) | |
| spawn('ffmpeg', [ | |
| '-y', | |
| '-i', tmp, | |
| ...args, | |
| out | |
| ]) | |
| .on('error', reject) | |
| .on('close', async (code) => { | |
| try { | |
| await fs.promises.unlink(tmp) | |
| if (code !== 0) return reject(code) | |
| resolve({ data: await fs.promises.readFile(out), filename: out }) | |
| // await fs.promises.unlink(out) | |
| } catch (e) { | |
| reject(e) | |
| } | |
| }) | |
| } catch (e) { | |
| reject(e) | |
| } | |
| }) | |
| } | |
| /** | |
| * Convert Audio to Playable WhatsApp Audio | |
| * @param {Buffer} buffer Audio Buffer | |
| * @param {String} ext File Extension | |
| */ | |
| function toPTT(buffer, ext) { | |
| return ffmpeg(buffer, [ | |
| '-vn', | |
| '-c:a', 'libopus', | |
| '-b:a', '128k', | |
| '-vbr', 'on', | |
| ], ext, 'ogg') | |
| } | |
| /** | |
| * Convert Audio to Playable WhatsApp PTT | |
| * @param {Buffer} buffer Audio Buffer | |
| * @param {String} ext File Extension | |
| */ | |
| function toAudio(buffer, ext) { | |
| return ffmpeg(buffer, [ | |
| '-vn', | |
| '-c:a', 'libopus', | |
| '-b:a', '128k', | |
| '-vbr', 'on', | |
| '-compression_level', '10' | |
| ], ext, 'opus') | |
| } | |
| /** | |
| * Convert Audio to Playable WhatsApp Video | |
| * @param {Buffer} buffer Video Buffer | |
| * @param {String} ext File Extension | |
| */ | |
| function toVideo(buffer, ext) { | |
| return ffmpeg(buffer, [ | |
| '-c:v', 'libx264', | |
| '-c:a', 'aac', | |
| '-ab', '128k', | |
| '-ar', '44100', | |
| '-crf', '32', | |
| '-preset', 'slow' | |
| ], ext, 'mp4') | |
| } | |
| module.exports = { | |
| toAudio, | |
| toPTT, | |
| toVideo, | |
| ffmpeg, | |
| } |