mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-13 15:14:49 -06:00
feat: convert audio with api
This commit is contained in:
parent
34769e2293
commit
e986768716
@ -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
|
||||
|
@ -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';
|
||||
@ -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(),
|
||||
},
|
||||
});
|
||||
|
||||
const response = await axios.get(url, config);
|
||||
inputAudioStream = response.data.pipe(new PassThrough());
|
||||
if (!data.audio) {
|
||||
throw new InternalServerErrorException('Failed to convert audio');
|
||||
}
|
||||
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user