Skip to content

Commit

Permalink
feat: convert audio with api
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidsonGomes committed Oct 21, 2024
1 parent 34769e2 commit e986768
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ S3_USE_SSL=true
# S3_USE_SSL=true
# S3_REGION=eu-south

# Evolution Audio Converter - Environment variables - https://github.com/EvolutionAPI/evolution-audio-converter
API_AUDIO_CONVERTER=http://localhost:4040/process-audio

# Define a global apikey to access all instances.
# OBS: This key must be inserted in the request header to create an instance.
AUTHENTICATION_API_KEY=429683C4C977415CAAFCCE10F7D57E11
Expand Down
93 changes: 58 additions & 35 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import { isArray, isBase64, isURL } from 'class-validator';
import { randomBytes } from 'crypto';
import EventEmitter2 from 'eventemitter2';
import ffmpeg from 'fluent-ffmpeg';
import FormData from 'form-data';
import { readFileSync } from 'fs';
import Long from 'long';
import mime from 'mime';
Expand Down Expand Up @@ -2631,53 +2632,75 @@ export class BaileysStartupService extends ChannelStartupService {
}

public async processAudio(audio: string): Promise<Buffer> {
let inputAudioStream: PassThrough;
if (process.env.API_AUDIO_CONVERTER) {
const formData = new FormData();

if (isURL(audio)) {
const timestamp = new Date().getTime();
const url = `${audio}?timestamp=${timestamp}`;
if (isURL(audio)) {
formData.append('url', audio);
} else {
formData.append('base64', audio);
}

const config: any = {
responseType: 'stream',
};
const { data } = await axios.post(process.env.API_AUDIO_CONVERTER, formData, {
headers: {
...formData.getHeaders(),
},
});

if (!data.audio) {
throw new InternalServerErrorException('Failed to convert audio');
}

const response = await axios.get(url, config);
inputAudioStream = response.data.pipe(new PassThrough());
return Buffer.from(data.audio, 'base64');
} else {
const audioBuffer = Buffer.from(audio, 'base64');
inputAudioStream = new PassThrough();
inputAudioStream.end(audioBuffer);
}
let inputAudioStream: PassThrough;

return new Promise((resolve, reject) => {
const outputAudioStream = new PassThrough();
const chunks: Buffer[] = [];
if (isURL(audio)) {
const timestamp = new Date().getTime();
const url = `${audio}?timestamp=${timestamp}`;

outputAudioStream.on('data', (chunk) => chunks.push(chunk));
outputAudioStream.on('end', () => {
const outputBuffer = Buffer.concat(chunks);
resolve(outputBuffer);
});
const config: any = {
responseType: 'stream',
};

outputAudioStream.on('error', (error) => {
console.log('error', error);
reject(error);
});
const response = await axios.get(url, config);
inputAudioStream = response.data.pipe(new PassThrough());
} else {
const audioBuffer = Buffer.from(audio, 'base64');
inputAudioStream = new PassThrough();
inputAudioStream.end(audioBuffer);
}

ffmpeg.setFfmpegPath(ffmpegPath.path);
return new Promise((resolve, reject) => {
const outputAudioStream = new PassThrough();
const chunks: Buffer[] = [];

ffmpeg(inputAudioStream)
.outputFormat('ogg')
.noVideo()
.audioCodec('libopus')
.addOutputOptions('-avoid_negative_ts make_zero')
.audioChannels(1)
.pipe(outputAudioStream, { end: true })
.on('error', function (error) {
outputAudioStream.on('data', (chunk) => chunks.push(chunk));
outputAudioStream.on('end', () => {
const outputBuffer = Buffer.concat(chunks);
resolve(outputBuffer);
});

outputAudioStream.on('error', (error) => {
console.log('error', error);
reject(error);
});
});

ffmpeg.setFfmpegPath(ffmpegPath.path);

ffmpeg(inputAudioStream)
.outputFormat('ogg')
.noVideo()
.audioCodec('libopus')
.addOutputOptions('-avoid_negative_ts make_zero')
.audioChannels(1)
.pipe(outputAudioStream, { end: true })
.on('error', function (error) {
console.log('error', error);
reject(error);
});
});
}
}

public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
Expand Down

0 comments on commit e986768

Please sign in to comment.