mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-24 17:38:40 -06:00
test: audio convertion
This commit is contained in:
parent
ed6e28744e
commit
c1cbc0d30c
@ -54,6 +54,7 @@ import P from 'pino';
|
|||||||
import qrcode, { QRCodeToDataURLOptions } from 'qrcode';
|
import qrcode, { QRCodeToDataURLOptions } from 'qrcode';
|
||||||
import qrcodeTerminal from 'qrcode-terminal';
|
import qrcodeTerminal from 'qrcode-terminal';
|
||||||
import sharp from 'sharp';
|
import sharp from 'sharp';
|
||||||
|
import { PassThrough } from 'stream';
|
||||||
|
|
||||||
import { CacheEngine } from '../../../cache/cacheengine';
|
import { CacheEngine } from '../../../cache/cacheengine';
|
||||||
import {
|
import {
|
||||||
@ -2361,17 +2362,14 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async processAudio(audio: string, number: string) {
|
public async processAudio(audio: string, number: string): Promise<string> {
|
||||||
let tempAudioPath: string;
|
|
||||||
let outputAudio: string;
|
|
||||||
|
|
||||||
number = number.replace(/\D/g, '');
|
number = number.replace(/\D/g, '');
|
||||||
const hash = `${number}-${new Date().getTime()}`;
|
const hash = `${number}-${new Date().getTime()}`;
|
||||||
|
const outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`;
|
||||||
|
|
||||||
|
let inputAudioStream: PassThrough;
|
||||||
|
|
||||||
if (isURL(audio)) {
|
if (isURL(audio)) {
|
||||||
outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`;
|
|
||||||
tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`;
|
|
||||||
|
|
||||||
const timestamp = new Date().getTime();
|
const timestamp = new Date().getTime();
|
||||||
const url = `${audio}?timestamp=${timestamp}`;
|
const url = `${audio}?timestamp=${timestamp}`;
|
||||||
|
|
||||||
@ -2380,41 +2378,28 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const response = await axios.get(url, config);
|
const response = await axios.get(url, config);
|
||||||
|
inputAudioStream = response.data.pipe(new PassThrough());
|
||||||
const writer = fs.createWriteStream(tempAudioPath);
|
|
||||||
|
|
||||||
response.data.pipe(writer);
|
|
||||||
|
|
||||||
await new Promise((resolve, reject) => {
|
|
||||||
writer.on('finish', resolve);
|
|
||||||
writer.on('error', reject);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`;
|
|
||||||
tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`;
|
|
||||||
|
|
||||||
const audioBuffer = Buffer.from(audio, 'base64');
|
const audioBuffer = Buffer.from(audio, 'base64');
|
||||||
await fs.promises.writeFile(tempAudioPath, audioBuffer);
|
inputAudioStream = new PassThrough();
|
||||||
|
inputAudioStream.end(audioBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
ffmpeg.setFfmpegPath(ffmpegPath.path);
|
ffmpeg.setFfmpegPath(ffmpegPath.path);
|
||||||
|
|
||||||
ffmpeg()
|
ffmpeg(inputAudioStream)
|
||||||
.input(tempAudioPath)
|
|
||||||
.outputFormat('ogg')
|
.outputFormat('ogg')
|
||||||
.noVideo()
|
.noVideo()
|
||||||
.audioCodec('libopus')
|
.audioCodec('libopus')
|
||||||
.saveToFile(outputAudio)
|
.saveToFile(outputAudio)
|
||||||
.addOutputOptions('-avoid_negative_ts make_zero')
|
.addOutputOptions('-avoid_negative_ts make_zero')
|
||||||
.audioChannels(1)
|
.audioChannels(1)
|
||||||
.on('error', async function (error) {
|
.on('error', function (error) {
|
||||||
console.log('error', error);
|
console.log('error', error);
|
||||||
// await fs.promises.unlink(tempAudioPath);
|
reject(error);
|
||||||
if (error) reject(error);
|
|
||||||
})
|
})
|
||||||
.on('end', async function () {
|
.on('end', function () {
|
||||||
// await fs.promises.unlink(tempAudioPath);
|
|
||||||
resolve(outputAudio);
|
resolve(outputAudio);
|
||||||
})
|
})
|
||||||
.run();
|
.run();
|
||||||
|
Loading…
Reference in New Issue
Block a user