From 170d5d8ad2336d4cb91999acb25d5d2c02cada09 Mon Sep 17 00:00:00 2001 From: edisoncm-ti Date: Fri, 16 May 2025 19:09:14 -0300 Subject: [PATCH] fix(audio): ensure full WhatsApp compatibility for audio conversion (libopus, 48kHz, mono) --- main.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 7faaded..b187421 100644 --- a/main.go +++ b/main.go @@ -159,19 +159,54 @@ func validateAPIKey(c *gin.Context) bool { func convertAudio(inputData []byte, format string) ([]byte, int, error) { var cmd *exec.Cmd + switch format { - case "mp3": - cmd = exec.Command("ffmpeg", "-i", "pipe:0", - "-ar", "44100", // sample rate - "-ac", "2", // canais (stereo) - "-b:a", "128k", // bitrate - "-c:a", "libmp3lame", // codec especĂ­fico - "-f", "mp3", // formato - "pipe:1") case "mp4": - cmd = exec.Command("ffmpeg", "-i", "pipe:0", "-vn", "-c:a", "aac", "-b:a", "128k", "-f", "adts", "pipe:1") + // Special handling for MP4 + cmd = exec.Command("ffmpeg", "-i", "pipe:0", + "-vn", + "-c:a", + "aac", + "-b:a", "128k", + "-f", "adts", + "pipe:1", + ) default: - cmd = exec.Command("ffmpeg", "-i", "pipe:0", "-ac", "1", "-ar", "16000", "-c:a", "libopus", "-f", "ogg", "pipe:1") + // Any other audio format (e.g., .oga, .ogg, .mp3, .mp4, .m4a, .wav, etc.) + cmd = exec.Command("ffmpeg", "-i", "pipe:0", + "-f", + "ogg", + "-vn", + "-c:a", + "libopus", + "-avoid_negative_ts", + "make_zero", + "-b:a", + "128k", + "-ar", + "48000", + "-ac", + "1", + "-write_xing", + "0", + "-compression_level", + "10", + "-application", + "voip", + "-fflags", + "+bitexact", + "-flags", + "+bitexact", + "-id3v2_version", + "0", + "-map_metadata", + "-1", + "-map_chapters", + "-1", + "-write_bext", + "0", + "pipe:1", + ) } outBuffer := bufferPool.Get().(*bytes.Buffer) errBuffer := bufferPool.Get().(*bytes.Buffer) @@ -611,4 +646,4 @@ func main() { router.POST("/transcribe", transcribeOnly) router.Run(":" + port) -} +} \ No newline at end of file