mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 04:02:54 -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_USE_SSL=true
|
||||||
# S3_REGION=eu-south
|
# 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.
|
# Define a global apikey to access all instances.
|
||||||
# OBS: This key must be inserted in the request header to create an instance.
|
# OBS: This key must be inserted in the request header to create an instance.
|
||||||
AUTHENTICATION_API_KEY=429683C4C977415CAAFCCE10F7D57E11
|
AUTHENTICATION_API_KEY=429683C4C977415CAAFCCE10F7D57E11
|
||||||
|
@ -125,6 +125,7 @@ import { isArray, isBase64, isURL } from 'class-validator';
|
|||||||
import { randomBytes } from 'crypto';
|
import { randomBytes } from 'crypto';
|
||||||
import EventEmitter2 from 'eventemitter2';
|
import EventEmitter2 from 'eventemitter2';
|
||||||
import ffmpeg from 'fluent-ffmpeg';
|
import ffmpeg from 'fluent-ffmpeg';
|
||||||
|
import FormData from 'form-data';
|
||||||
import { readFileSync } from 'fs';
|
import { readFileSync } from 'fs';
|
||||||
import Long from 'long';
|
import Long from 'long';
|
||||||
import mime from 'mime';
|
import mime from 'mime';
|
||||||
@ -2631,53 +2632,75 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async processAudio(audio: string): Promise<Buffer> {
|
public async processAudio(audio: string): Promise<Buffer> {
|
||||||
let inputAudioStream: PassThrough;
|
if (process.env.API_AUDIO_CONVERTER) {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
if (isURL(audio)) {
|
if (isURL(audio)) {
|
||||||
const timestamp = new Date().getTime();
|
formData.append('url', audio);
|
||||||
const url = `${audio}?timestamp=${timestamp}`;
|
} else {
|
||||||
|
formData.append('base64', audio);
|
||||||
|
}
|
||||||
|
|
||||||
const config: any = {
|
const { data } = await axios.post(process.env.API_AUDIO_CONVERTER, formData, {
|
||||||
responseType: 'stream',
|
headers: {
|
||||||
};
|
...formData.getHeaders(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const response = await axios.get(url, config);
|
if (!data.audio) {
|
||||||
inputAudioStream = response.data.pipe(new PassThrough());
|
throw new InternalServerErrorException('Failed to convert audio');
|
||||||
|
}
|
||||||
|
|
||||||
|
return Buffer.from(data.audio, 'base64');
|
||||||
} else {
|
} else {
|
||||||
const audioBuffer = Buffer.from(audio, 'base64');
|
let inputAudioStream: PassThrough;
|
||||||
inputAudioStream = new PassThrough();
|
|
||||||
inputAudioStream.end(audioBuffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
if (isURL(audio)) {
|
||||||
const outputAudioStream = new PassThrough();
|
const timestamp = new Date().getTime();
|
||||||
const chunks: Buffer[] = [];
|
const url = `${audio}?timestamp=${timestamp}`;
|
||||||
|
|
||||||
outputAudioStream.on('data', (chunk) => chunks.push(chunk));
|
const config: any = {
|
||||||
outputAudioStream.on('end', () => {
|
responseType: 'stream',
|
||||||
const outputBuffer = Buffer.concat(chunks);
|
};
|
||||||
resolve(outputBuffer);
|
|
||||||
});
|
|
||||||
|
|
||||||
outputAudioStream.on('error', (error) => {
|
const response = await axios.get(url, config);
|
||||||
console.log('error', error);
|
inputAudioStream = response.data.pipe(new PassThrough());
|
||||||
reject(error);
|
} 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)
|
outputAudioStream.on('data', (chunk) => chunks.push(chunk));
|
||||||
.outputFormat('ogg')
|
outputAudioStream.on('end', () => {
|
||||||
.noVideo()
|
const outputBuffer = Buffer.concat(chunks);
|
||||||
.audioCodec('libopus')
|
resolve(outputBuffer);
|
||||||
.addOutputOptions('-avoid_negative_ts make_zero')
|
});
|
||||||
.audioChannels(1)
|
|
||||||
.pipe(outputAudioStream, { end: true })
|
outputAudioStream.on('error', (error) => {
|
||||||
.on('error', function (error) {
|
|
||||||
console.log('error', error);
|
console.log('error', error);
|
||||||
reject(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) {
|
public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
|
||||||
|
Loading…
Reference in New Issue
Block a user