mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 04:02:54 -06:00
fix: Fixed getBase64FromMediaMessage with convertToMp4
This commit is contained in:
parent
97f633a69a
commit
01ae55f02a
@ -25,6 +25,7 @@
|
|||||||
* Fixed send message to group without no cache (local or redis)
|
* Fixed send message to group without no cache (local or redis)
|
||||||
* Fixed startTypebot with startSession = true
|
* Fixed startTypebot with startSession = true
|
||||||
* Fixed issue of always creating a new label when saving chatwoot
|
* Fixed issue of always creating a new label when saving chatwoot
|
||||||
|
* Fixed getBase64FromMediaMessage with convertToMp4
|
||||||
|
|
||||||
# 2.1.1 (2024-09-22 10:31)
|
# 2.1.1 (2024-09-22 10:31)
|
||||||
|
|
||||||
|
@ -113,6 +113,7 @@ import makeWASocket, {
|
|||||||
} from 'baileys';
|
} from 'baileys';
|
||||||
import { Label } from 'baileys/lib/Types/Label';
|
import { Label } from 'baileys/lib/Types/Label';
|
||||||
import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
|
import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
|
||||||
|
import { spawn } from 'child_process';
|
||||||
import { isBase64, isURL } from 'class-validator';
|
import { isBase64, isURL } from 'class-validator';
|
||||||
import { randomBytes } from 'crypto';
|
import { randomBytes } from 'crypto';
|
||||||
import EventEmitter2 from 'eventemitter2';
|
import EventEmitter2 from 'eventemitter2';
|
||||||
@ -2454,53 +2455,69 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async processAudioMp4(audio: string) {
|
public async processAudioMp4(audio: string) {
|
||||||
let inputAudioStream: PassThrough;
|
let inputStream: PassThrough;
|
||||||
|
|
||||||
if (isURL(audio)) {
|
if (isURL(audio)) {
|
||||||
const timestamp = new Date().getTime();
|
const response = await axios.get(audio, { responseType: 'stream' });
|
||||||
const url = `${audio}?timestamp=${timestamp}`;
|
inputStream = response.data;
|
||||||
|
|
||||||
const config: any = {
|
|
||||||
responseType: 'stream',
|
|
||||||
};
|
|
||||||
|
|
||||||
const response = await axios.get(url, config);
|
|
||||||
inputAudioStream = response.data.pipe(new PassThrough());
|
|
||||||
} else {
|
} else {
|
||||||
const audioBuffer = Buffer.from(audio, 'base64');
|
const audioBuffer = Buffer.from(audio, 'base64');
|
||||||
inputAudioStream = new PassThrough();
|
inputStream = new PassThrough();
|
||||||
inputAudioStream.end(audioBuffer);
|
inputStream.end(audioBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<Buffer>((resolve, reject) => {
|
||||||
const outputAudioStream = new PassThrough();
|
const ffmpegProcess = spawn(ffmpegPath.path, [
|
||||||
const chunks: Buffer[] = [];
|
'-i',
|
||||||
|
'pipe:0',
|
||||||
|
'-vn',
|
||||||
|
'-ab',
|
||||||
|
'128k',
|
||||||
|
'-ar',
|
||||||
|
'44100',
|
||||||
|
'-f',
|
||||||
|
'mp4',
|
||||||
|
'-movflags',
|
||||||
|
'frag_keyframe+empty_moov',
|
||||||
|
'pipe:1',
|
||||||
|
]);
|
||||||
|
|
||||||
outputAudioStream.on('data', (chunk) => chunks.push(chunk));
|
const outputChunks: Buffer[] = [];
|
||||||
outputAudioStream.on('end', () => {
|
let stderrData = '';
|
||||||
const outputBuffer = Buffer.concat(chunks);
|
|
||||||
resolve(outputBuffer);
|
ffmpegProcess.stdout.on('data', (chunk) => {
|
||||||
|
outputChunks.push(chunk);
|
||||||
});
|
});
|
||||||
|
|
||||||
outputAudioStream.on('error', (error) => {
|
ffmpegProcess.stderr.on('data', (data) => {
|
||||||
console.log('error', error);
|
stderrData += data.toString();
|
||||||
|
this.logger.verbose(`ffmpeg stderr: ${data}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
ffmpegProcess.on('error', (error) => {
|
||||||
|
console.error('Error in ffmpeg process', error);
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
ffmpeg.setFfmpegPath(ffmpegPath.path);
|
ffmpegProcess.on('close', (code) => {
|
||||||
|
if (code === 0) {
|
||||||
|
this.logger.verbose('Audio converted to mp4');
|
||||||
|
const outputBuffer = Buffer.concat(outputChunks);
|
||||||
|
resolve(outputBuffer);
|
||||||
|
} else {
|
||||||
|
this.logger.error(`ffmpeg exited with code ${code}`);
|
||||||
|
this.logger.error(`ffmpeg stderr: ${stderrData}`);
|
||||||
|
reject(new Error(`ffmpeg exited with code ${code}: ${stderrData}`));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
ffmpeg(inputAudioStream)
|
inputStream.pipe(ffmpegProcess.stdin);
|
||||||
.outputFormat('mp4')
|
|
||||||
.noVideo()
|
inputStream.on('error', (err) => {
|
||||||
.audioCodec('aac')
|
console.error('Error in inputStream', err);
|
||||||
.audioBitrate('128k')
|
ffmpegProcess.stdin.end();
|
||||||
.audioFrequency(44100)
|
reject(err);
|
||||||
.addOutputOptions('-f ipod')
|
});
|
||||||
.pipe(outputAudioStream, { end: true })
|
|
||||||
.on('error', function (error) {
|
|
||||||
console.log('error', error);
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2995,28 +3012,33 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
const typeMessage = getContentType(msg.message);
|
const typeMessage = getContentType(msg.message);
|
||||||
|
|
||||||
const ext = mime.getExtension(mediaMessage?.['mimetype']);
|
const ext = mime.getExtension(mediaMessage?.['mimetype']);
|
||||||
|
|
||||||
const fileName = mediaMessage?.['fileName'] || `${msg.key.id}.${ext}` || `${v4()}.${ext}`;
|
const fileName = mediaMessage?.['fileName'] || `${msg.key.id}.${ext}` || `${v4()}.${ext}`;
|
||||||
|
|
||||||
if (convertToMp4 && typeMessage === 'audioMessage') {
|
if (convertToMp4 && typeMessage === 'audioMessage') {
|
||||||
const convert = await this.processAudioMp4(buffer.toString('base64'));
|
try {
|
||||||
|
const convert = await this.processAudioMp4(buffer.toString('base64'));
|
||||||
|
|
||||||
if (Buffer.isBuffer(convert)) {
|
if (Buffer.isBuffer(convert)) {
|
||||||
const result = {
|
const result = {
|
||||||
mediaType,
|
mediaType,
|
||||||
fileName,
|
fileName,
|
||||||
caption: mediaMessage['caption'],
|
caption: mediaMessage['caption'],
|
||||||
size: {
|
size: {
|
||||||
fileLength: mediaMessage['fileLength'],
|
fileLength: mediaMessage['fileLength'],
|
||||||
height: mediaMessage['height'],
|
height: mediaMessage['height'],
|
||||||
width: mediaMessage['width'],
|
width: mediaMessage['width'],
|
||||||
},
|
},
|
||||||
mimetype: 'audio/mp4',
|
mimetype: 'audio/mp4',
|
||||||
base64: convert,
|
base64: convert.toString('base64'),
|
||||||
buffer: getBuffer ? convert : null,
|
buffer: getBuffer ? convert : null,
|
||||||
};
|
};
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error('Error converting audio to mp4:');
|
||||||
|
this.logger.error(error);
|
||||||
|
throw new BadRequestException('Failed to convert audio to MP4');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3034,6 +3056,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
buffer: getBuffer ? buffer : null,
|
buffer: getBuffer ? buffer : null,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
this.logger.error('Error processing media message:');
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
throw new BadRequestException(error.toString());
|
throw new BadRequestException(error.toString());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user