mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-17 04:32:53 -06:00
fix: audio convertion with stream
This commit is contained in:
parent
c1cbc0d30c
commit
9cd50833cf
@ -2362,11 +2362,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async processAudio(audio: string, number: string): Promise<string> {
|
public async processAudio(audio: string): Promise<Buffer> {
|
||||||
number = number.replace(/\D/g, '');
|
|
||||||
const hash = `${number}-${new Date().getTime()}`;
|
|
||||||
const outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`;
|
|
||||||
|
|
||||||
let inputAudioStream: PassThrough;
|
let inputAudioStream: PassThrough;
|
||||||
|
|
||||||
if (isURL(audio)) {
|
if (isURL(audio)) {
|
||||||
@ -2386,23 +2382,33 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
const outputAudioStream = new PassThrough();
|
||||||
|
const chunks: Buffer[] = [];
|
||||||
|
|
||||||
|
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.setFfmpegPath(ffmpegPath.path);
|
||||||
|
|
||||||
ffmpeg(inputAudioStream)
|
ffmpeg(inputAudioStream)
|
||||||
.outputFormat('ogg')
|
.outputFormat('ogg')
|
||||||
.noVideo()
|
.noVideo()
|
||||||
.audioCodec('libopus')
|
.audioCodec('libopus')
|
||||||
.saveToFile(outputAudio)
|
|
||||||
.addOutputOptions('-avoid_negative_ts make_zero')
|
.addOutputOptions('-avoid_negative_ts make_zero')
|
||||||
.audioChannels(1)
|
.audioChannels(1)
|
||||||
|
.pipe(outputAudioStream, { end: true })
|
||||||
.on('error', function (error) {
|
.on('error', function (error) {
|
||||||
console.log('error', error);
|
console.log('error', error);
|
||||||
reject(error);
|
reject(error);
|
||||||
})
|
});
|
||||||
.on('end', function () {
|
|
||||||
resolve(outputAudio);
|
|
||||||
})
|
|
||||||
.run();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2412,13 +2418,13 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data?.encoding) {
|
if (data?.encoding) {
|
||||||
const convert = await this.processAudio(data.audio, data.number);
|
const convert = await this.processAudio(data.audio);
|
||||||
if (typeof convert === 'string') {
|
|
||||||
const audio = fs.readFileSync(convert).toString('base64');
|
if (Buffer.isBuffer(convert)) {
|
||||||
const result = this.sendMessageWithTyping<AnyMessageContent>(
|
const result = this.sendMessageWithTyping<AnyMessageContent>(
|
||||||
data.number,
|
data.number,
|
||||||
{
|
{
|
||||||
audio: Buffer.from(audio, 'base64'),
|
audio: convert,
|
||||||
ptt: true,
|
ptt: true,
|
||||||
mimetype: 'audio/ogg; codecs=opus',
|
mimetype: 'audio/ogg; codecs=opus',
|
||||||
},
|
},
|
||||||
@ -2426,11 +2432,9 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
isIntegration,
|
isIntegration,
|
||||||
);
|
);
|
||||||
|
|
||||||
fs.unlinkSync(convert);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} else {
|
} else {
|
||||||
throw new InternalServerErrorException(convert);
|
throw new InternalServerErrorException('Failed to convert audio');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user