fix: convertir audio webm → ogg con ffmpeg antes de subir a WhatsApp en turnero

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 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-03 17:56:15 -05:00
co-authored by Claude Sonnet 4.6
parent a526a29a68
commit 99bfaa4c2f
+20 -4
View File
@@ -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);
}