@@ -125,6 +125,7 @@ import { isArray, isBase64, isURL } from 'class-validator';
125125import { randomBytes } from 'crypto' ;
126126import EventEmitter2 from 'eventemitter2' ;
127127import ffmpeg from 'fluent-ffmpeg' ;
128+ import FormData from 'form-data' ;
128129import { readFileSync } from 'fs' ;
129130import Long from 'long' ;
130131import mime from 'mime' ;
@@ -2631,53 +2632,75 @@ export class BaileysStartupService extends ChannelStartupService {
26312632 }
26322633
26332634 public async processAudio ( audio : string ) : Promise < Buffer > {
2634- let inputAudioStream : PassThrough ;
2635+ if ( process . env . API_AUDIO_CONVERTER ) {
2636+ const formData = new FormData ( ) ;
26352637
2636- if ( isURL ( audio ) ) {
2637- const timestamp = new Date ( ) . getTime ( ) ;
2638- const url = `${ audio } ?timestamp=${ timestamp } ` ;
2638+ if ( isURL ( audio ) ) {
2639+ formData . append ( 'url' , audio ) ;
2640+ } else {
2641+ formData . append ( 'base64' , audio ) ;
2642+ }
26392643
2640- const config : any = {
2641- responseType : 'stream' ,
2642- } ;
2644+ const { data } = await axios . post ( process . env . API_AUDIO_CONVERTER , formData , {
2645+ headers : {
2646+ ...formData . getHeaders ( ) ,
2647+ } ,
2648+ } ) ;
2649+
2650+ if ( ! data . audio ) {
2651+ throw new InternalServerErrorException ( 'Failed to convert audio' ) ;
2652+ }
26432653
2644- const response = await axios . get ( url , config ) ;
2645- inputAudioStream = response . data . pipe ( new PassThrough ( ) ) ;
2654+ return Buffer . from ( data . audio , 'base64' ) ;
26462655 } else {
2647- const audioBuffer = Buffer . from ( audio , 'base64' ) ;
2648- inputAudioStream = new PassThrough ( ) ;
2649- inputAudioStream . end ( audioBuffer ) ;
2650- }
2656+ let inputAudioStream : PassThrough ;
26512657
2652- return new Promise ( ( resolve , reject ) => {
2653- const outputAudioStream = new PassThrough ( ) ;
2654- const chunks : Buffer [ ] = [ ] ;
2658+ if ( isURL ( audio ) ) {
2659+ const timestamp = new Date ( ) . getTime ( ) ;
2660+ const url = ` ${ audio } ?timestamp= ${ timestamp } ` ;
26552661
2656- outputAudioStream . on ( 'data' , ( chunk ) => chunks . push ( chunk ) ) ;
2657- outputAudioStream . on ( 'end' , ( ) => {
2658- const outputBuffer = Buffer . concat ( chunks ) ;
2659- resolve ( outputBuffer ) ;
2660- } ) ;
2662+ const config : any = {
2663+ responseType : 'stream' ,
2664+ } ;
26612665
2662- outputAudioStream . on ( 'error' , ( error ) => {
2663- console . log ( 'error' , error ) ;
2664- reject ( error ) ;
2665- } ) ;
2666+ const response = await axios . get ( url , config ) ;
2667+ inputAudioStream = response . data . pipe ( new PassThrough ( ) ) ;
2668+ } else {
2669+ const audioBuffer = Buffer . from ( audio , 'base64' ) ;
2670+ inputAudioStream = new PassThrough ( ) ;
2671+ inputAudioStream . end ( audioBuffer ) ;
2672+ }
26662673
2667- ffmpeg . setFfmpegPath ( ffmpegPath . path ) ;
2674+ return new Promise ( ( resolve , reject ) => {
2675+ const outputAudioStream = new PassThrough ( ) ;
2676+ const chunks : Buffer [ ] = [ ] ;
26682677
2669- ffmpeg ( inputAudioStream )
2670- . outputFormat ( 'ogg' )
2671- . noVideo ( )
2672- . audioCodec ( 'libopus' )
2673- . addOutputOptions ( '-avoid_negative_ts make_zero' )
2674- . audioChannels ( 1 )
2675- . pipe ( outputAudioStream , { end : true } )
2676- . on ( 'error' , function ( error ) {
2678+ outputAudioStream . on ( 'data' , ( chunk ) => chunks . push ( chunk ) ) ;
2679+ outputAudioStream . on ( 'end' , ( ) => {
2680+ const outputBuffer = Buffer . concat ( chunks ) ;
2681+ resolve ( outputBuffer ) ;
2682+ } ) ;
2683+
2684+ outputAudioStream . on ( 'error' , ( error ) => {
26772685 console . log ( 'error' , error ) ;
26782686 reject ( error ) ;
26792687 } ) ;
2680- } ) ;
2688+
2689+ ffmpeg . setFfmpegPath ( ffmpegPath . path ) ;
2690+
2691+ ffmpeg ( inputAudioStream )
2692+ . outputFormat ( 'ogg' )
2693+ . noVideo ( )
2694+ . audioCodec ( 'libopus' )
2695+ . addOutputOptions ( '-avoid_negative_ts make_zero' )
2696+ . audioChannels ( 1 )
2697+ . pipe ( outputAudioStream , { end : true } )
2698+ . on ( 'error' , function ( error ) {
2699+ console . log ( 'error' , error ) ;
2700+ reject ( error ) ;
2701+ } ) ;
2702+ } ) ;
2703+ }
26812704 }
26822705
26832706 public async audioWhatsapp ( data : SendAudioDto , file ?: any , isIntegration = false ) {
0 commit comments