Baileys: [BUG?] Some audios don't work on Android but work on Web

When I try to send an mp3 audio file, it gets muted on Android but works on the Web. The problem also occurs on other phones, but I’m not sure if it happens on iOS.

One example of what I’m attempting to do is download a WhatsApp video, convert it to mp3, and then respond with the audio:

export const videoToAudio = async (data: Buffer, start: number, end: number) => {
    const filePath = path.resolve(BASE_DIR, uuidv4())

    const input = `${filePath}.mp4`
    const output = `${filePath}.mp3`

    fs.writeFileSync(input, data)
    return await exec(input, output, start, end)
}

const exec = async (input: string, output: string, start: number, end: number): Promise<string> => {
    try {
        return new Promise((resolve, reject) => {
            ffmpeg.ffprobe(input, (_, metaData) => {
              const { duration } = metaData.format
            
              let clipDuration = duration
              if (end) clipDuration = end - start
            
              ffmpeg()
              .input(input)
              .inputOptions([`-ss ${start || 0}`])
              // .toFormat("mp3")
              .outputOptions([`-t ${clipDuration}`])
              .output(output)
              .on("end", () => {
                resolve(output)
                unlink([input])
              })
              .on("error", (err) => {
                unlink([input, output])
                reject(err)
              }).run()
            })
        })
    } catch (err) {
        console.log(err)
        throw new CommandError()
    }
}

On the other side:

const audio = await videoToAudio(data, start, end)

// the jid is being passed on the background, but the object is the same as the second argument from the native function
await group.sendMessage({ audio: { url: audio } }, true)

I’m not sure about my memory, because I just migrated the code from another lib. But I think that in my previous tests it was working fine.

Does anyone know what’s causing the issue or have some ideas for me to try?

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 20 (1 by maintainers)

Commits related to this issue

Most upvoted comments

@joseorlando-nsolucoes that worked perfectly, thank you! youre a life saver! well done!

I struggled a lot to solve this problem and it turned out to be something within the file metadata and its channels. I don’t understand much about audio and whatnot so anything I got wrong, please let me know.

During my tests I found out that some audios came up with metadata “time_start” negative, such as -0.67856 and that’s what caused the audio not to be playable in some devices.

I’m not really sure about the next one but I noticed that when an audio is recorded from an iOS it has only one channel. So to make sure it works accross every single device, I also set it to one channel only.

I now convert every audio that gets sent through my api to ogg using the following params:

 ffmpeg()
        .input(input)
        .audioChannels(1) //make it monochannel like WhatsApp does in iOS
        .audioCodec('opus')
        .toFormat('ogg')
        .addOutputOptions('-avoid_negative_ts make_zero') //this flag prevents the output file to be generated with negative 
        time_start metadata

I hope it helps! Everything is work fine round here after this workaround.

ffmpeg -i input.webm -vn -ab 128k -ar 44100 -f ipod -y output.mp3

send audio with mimetype: audio/mp4