Skip to content

Commit

Permalink
test: audio convertion
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidsonGomes committed Jun 10, 2024
1 parent ed6e287 commit c1cbc0d
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions src/api/services/channels/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import P from 'pino';
import qrcode, { QRCodeToDataURLOptions } from 'qrcode';
import qrcodeTerminal from 'qrcode-terminal';
import sharp from 'sharp';
import { PassThrough } from 'stream';

import { CacheEngine } from '../../../cache/cacheengine';
import {
Expand Down Expand Up @@ -2361,17 +2362,14 @@ export class BaileysStartupService extends ChannelStartupService {
});
}

public async processAudio(audio: string, number: string) {
let tempAudioPath: string;
let outputAudio: string;

public async processAudio(audio: string, number: string): Promise<string> {
number = number.replace(/\D/g, '');
const hash = `${number}-${new Date().getTime()}`;
const outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`;

if (isURL(audio)) {
outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`;
tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`;
let inputAudioStream: PassThrough;

if (isURL(audio)) {
const timestamp = new Date().getTime();
const url = `${audio}?timestamp=${timestamp}`;

Expand All @@ -2380,41 +2378,28 @@ export class BaileysStartupService extends ChannelStartupService {
};

const response = await axios.get(url, config);

const writer = fs.createWriteStream(tempAudioPath);

response.data.pipe(writer);

await new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
inputAudioStream = response.data.pipe(new PassThrough());
} else {
outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`;
tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`;

const audioBuffer = Buffer.from(audio, 'base64');
await fs.promises.writeFile(tempAudioPath, audioBuffer);
inputAudioStream = new PassThrough();
inputAudioStream.end(audioBuffer);
}

return new Promise((resolve, reject) => {
ffmpeg.setFfmpegPath(ffmpegPath.path);

ffmpeg()
.input(tempAudioPath)
ffmpeg(inputAudioStream)
.outputFormat('ogg')
.noVideo()
.audioCodec('libopus')
.saveToFile(outputAudio)
.addOutputOptions('-avoid_negative_ts make_zero')
.audioChannels(1)
.on('error', async function (error) {
.on('error', function (error) {
console.log('error', error);
// await fs.promises.unlink(tempAudioPath);
if (error) reject(error);
reject(error);
})
.on('end', async function () {
// await fs.promises.unlink(tempAudioPath);
.on('end', function () {
resolve(outputAudio);
})
.run();
Expand Down

0 comments on commit c1cbc0d

Please sign in to comment.