fix: audio encoding

This commit is contained in:
Davidson Gomes 2024-04-12 11:20:18 -03:00
parent 589dec52a3
commit 6e2a3a410a
2 changed files with 23 additions and 12 deletions

View File

@ -8,6 +8,7 @@
* Adjusts in redis * Adjusts in redis
* Send global event in websocket * Send global event in websocket
* Fix audio encoding
# 1.7.1 (2024-04-03 10:19) # 1.7.1 (2024-04-03 10:19)

View File

@ -39,9 +39,9 @@ import makeWASocket, {
import { Label } from '@whiskeysockets/baileys/lib/Types/Label'; import { Label } from '@whiskeysockets/baileys/lib/Types/Label';
import { LabelAssociation } from '@whiskeysockets/baileys/lib/Types/LabelAssociation'; import { LabelAssociation } from '@whiskeysockets/baileys/lib/Types/LabelAssociation';
import axios from 'axios'; import axios from 'axios';
import { exec } from 'child_process';
import { arrayUnique, isBase64, isURL } from 'class-validator'; import { arrayUnique, isBase64, isURL } from 'class-validator';
import EventEmitter2 from 'eventemitter2'; import EventEmitter2 from 'eventemitter2';
import ffmpeg from 'fluent-ffmpeg';
import fs, { existsSync, readFileSync } from 'fs'; import fs, { existsSync, readFileSync } from 'fs';
import { parsePhoneNumber } from 'libphonenumber-js'; import { parsePhoneNumber } from 'libphonenumber-js';
import Long from 'long'; import Long from 'long';
@ -2311,7 +2311,7 @@ export class BaileysStartupService extends WAStartupService {
if (isURL(audio)) { if (isURL(audio)) {
this.logger.verbose('Audio is url'); this.logger.verbose('Audio is url');
outputAudio = `${join(this.storePath, 'temp', `${hash}.mp4`)}`; outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`;
tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`; tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`;
this.logger.verbose('Output audio path: ' + outputAudio); this.logger.verbose('Output audio path: ' + outputAudio);
@ -2340,7 +2340,7 @@ export class BaileysStartupService extends WAStartupService {
} else { } else {
this.logger.verbose('Audio is base64'); this.logger.verbose('Audio is base64');
outputAudio = `${join(this.storePath, 'temp', `${hash}.mp4`)}`; outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`;
tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`; tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`;
this.logger.verbose('Output audio path: ' + outputAudio); this.logger.verbose('Output audio path: ' + outputAudio);
@ -2353,15 +2353,25 @@ export class BaileysStartupService extends WAStartupService {
this.logger.verbose('Converting audio to mp4'); this.logger.verbose('Converting audio to mp4');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(`${ffmpegPath.path} -i ${tempAudioPath} -vn -ab 128k -ar 44100 -f ipod ${outputAudio} -y`, (error) => { // This fix was suggested by @PurpShell
fs.unlinkSync(tempAudioPath); ffmpeg.setFfmpegPath(ffmpegPath.path);
this.logger.verbose('Temp audio deleted');
if (error) reject(error); ffmpeg()
.input(tempAudioPath)
this.logger.verbose('Audio converted to mp4'); .outputFormat('ogg')
resolve(outputAudio); .noVideo()
}); .audioCodec('libopus')
.save(outputAudio)
.on('error', function (error) {
console.log('error', error);
fs.unlinkSync(tempAudioPath);
if (error) reject(error);
})
.on('end', async function () {
fs.unlinkSync(tempAudioPath);
resolve(outputAudio);
})
.run();
}); });
} }
@ -2381,7 +2391,7 @@ export class BaileysStartupService extends WAStartupService {
{ {
audio: Buffer.from(audio, 'base64'), audio: Buffer.from(audio, 'base64'),
ptt: true, ptt: true,
mimetype: 'audio/mp4', mimetype: 'audio/ogg; codecs=opus',
}, },
{ presence: 'recording', delay: data?.options?.delay }, { presence: 'recording', delay: data?.options?.delay },
isChatwoot, isChatwoot,