From 99bfaa4c2fa56cfe93a99dc3892187ea4495b716 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Fri, 3 Jul 2026 17:56:15 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20convertir=20audio=20webm=20=E2=86=92=20o?= =?UTF-8?q?gg=20con=20ffmpeg=20antes=20de=20subir=20a=20WhatsApp=20en=20tu?= =?UTF-8?q?rnero?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit El archivo grabado por el browser es WebM (no soportado por WhatsApp). conversations.php usa ffmpeg para convertirlo; chat_upload_media.php solo cambiaba el string del MIME pero enviaba el archivo WebM real, causando que el audio no llegara al destinatario. Ahora se convierte con los mismos parĂ¡metros que upload_media.php (libopus 32k mono 48kHz). Co-Authored-By: Claude Sonnet 4.6 --- modules/turnero/api/chat_upload_media.php | 24 +++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/modules/turnero/api/chat_upload_media.php b/modules/turnero/api/chat_upload_media.php index dd53e82..890695d 100644 --- a/modules/turnero/api/chat_upload_media.php +++ b/modules/turnero/api/chat_upload_media.php @@ -28,9 +28,25 @@ $caption = trim($_POST['caption'] ?? ''); $isVoice = !empty($_POST['is_voice']); // mime_content_type() returns video/webm for browser-recorded audio (same WebM container). -// WhatsApp rejects webm; map to audio/ogg (same Opus codec, accepted format). -$mime = (str_contains($detectedMime, 'webm') || str_contains($browserMime, 'webm')) - ? 'audio/ogg' : $detectedMime; +// WhatsApp only accepts OGG Opus, not WebM. Convert with ffmpeg when available. +$isWebm = str_contains($detectedMime, 'webm') || str_contains($browserMime, 'webm'); +$mime = $isWebm ? 'audio/ogg' : $detectedMime; + +$uploadFilePath = $file['tmp_name']; +if ($isWebm && function_exists('shell_exec')) { + $ffmpeg = trim((string)@shell_exec('which ffmpeg 2>/dev/null')); + if ($ffmpeg) { + $convertedPath = sys_get_temp_dir() . '/wa_audio_' . uniqid() . '.ogg'; + $cmd = escapeshellcmd($ffmpeg) + . ' -y -i ' . escapeshellarg($file['tmp_name']) + . ' -map 0:a:0 -c:a libopus -b:a 32k -vbr on -ar 48000 -ac 1 -f ogg ' + . escapeshellarg($convertedPath) . ' 2>/dev/null'; + @shell_exec($cmd); + if (file_exists($convertedPath) && filesize($convertedPath) > 0) { + $uploadFilePath = $convertedPath; + } + } +} // Determinar tipo de media if (str_starts_with($mime, 'image/')) $mediaType = 'image'; @@ -49,7 +65,7 @@ try { $wa = new WhatsAppService('turnero'); // Subir a WhatsApp Media API - $uploadResult = $wa->uploadMedia($file['tmp_name'], $mime); + $uploadResult = $wa->uploadMedia($uploadFilePath, $mime); if (!$uploadResult || !isset($uploadResult['id'])) { tmErr('No se pudo subir el archivo a WhatsApp', 502); }