This commit is contained in:
Lizandro Guarnizo
2026-01-22 11:35:05 -05:00
parent ee21b25557
commit 66dc55cc15
2 changed files with 41 additions and 17 deletions
+35 -17
View File
@@ -98,27 +98,43 @@ try {
} }
// Si es audio WebM, intentar convertir a OGG/Opus (formatos más compatibles con WhatsApp) // Si es audio WebM, intentar convertir a OGG/Opus (formatos más compatibles con WhatsApp)
$converted = false;
$conversion_error = null;
if (strpos($fileType, 'audio/webm') === 0) { if (strpos($fileType, 'audio/webm') === 0) {
$ffmpeg = trim(@shell_exec('which ffmpeg 2>/dev/null')); // Evitar fatal si shell_exec está deshabilitado en el hosting
if ($ffmpeg) { if (function_exists('shell_exec')) {
$convertedName = uniqid('media_', true) . '.ogg'; $ffmpeg = trim(@shell_exec('which ffmpeg 2>/dev/null'));
$convertedPath = $uploadDir . $convertedName; if ($ffmpeg) {
// Convertir a Opus (bitrate moderado) try {
$cmd = escapeshellcmd($ffmpeg) . ' -y -i ' . escapeshellarg($uploadPath) . ' -c:a libopus -b:a 64000 ' . escapeshellarg($convertedPath) . ' 2>&1'; $convertedName = uniqid('media_', true) . '.ogg';
$out = shell_exec($cmd); $convertedPath = $uploadDir . $convertedName;
if (file_exists($convertedPath) && filesize($convertedPath) > 0) { // Convertir a Opus (bitrate moderado)
// Reemplazar archivo original por el convertido $cmd = escapeshellcmd($ffmpeg) . ' -y -i ' . escapeshellarg($uploadPath) . ' -c:a libopus -b:a 64000 ' . escapeshellarg($convertedPath) . ' 2>&1';
@unlink($uploadPath); $out = @shell_exec($cmd);
$uploadPath = $convertedPath; if (file_exists($convertedPath) && filesize($convertedPath) > 0) {
$uniqueName = $convertedName; // Reemplazar archivo original por el convertido
$fileType = 'audio/ogg'; @unlink($uploadPath);
error_log('upload_media.php - Audio convertido a OGG/Opus: ' . $convertedName); $uploadPath = $convertedPath;
error_log('upload_media.php - ffmpeg output: ' . substr($out, 0, 2000)); $uniqueName = $convertedName;
$fileType = 'audio/ogg';
$converted = true;
error_log('upload_media.php - Audio convertido a OGG/Opus: ' . $convertedName);
error_log('upload_media.php - ffmpeg output: ' . substr($out ?: '', 0, 2000));
} else {
$conversion_error = 'Conversion failed or no output';
error_log('upload_media.php - Falló conversión con ffmpeg. Output: ' . substr($out ?: '', 0, 2000));
}
} catch (Exception $ex) {
$conversion_error = 'Conversion exception: ' . $ex->getMessage();
error_log('upload_media.php - Conversion exception: ' . $ex->getMessage());
}
} else { } else {
error_log('upload_media.php - Falló conversión con ffmpeg. Output: ' . substr($out, 0, 2000)); $conversion_error = 'ffmpeg not found';
error_log('upload_media.php - ffmpeg no encontrado, no se puede convertir audio/webm.');
} }
} else { } else {
error_log('upload_media.php - ffmpeg no encontrado, no se puede convertir audio/webm.'); $conversion_error = 'shell_exec disabled';
error_log('upload_media.php - shell_exec disabled, no se puede convertir audio/webm.');
} }
} }
@@ -160,6 +176,8 @@ try {
'media_type' => $mediaType, 'media_type' => $mediaType,
'mime_type' => $fileType, 'mime_type' => $fileType,
'size' => $fileSize, 'size' => $fileSize,
'converted' => isset($converted) ? (bool)$converted : false,
'conversion_error' => isset($conversion_error) ? $conversion_error : null,
// Mantener compatibilidad con código existente // Mantener compatibilidad con código existente
'data' => [ 'data' => [
'id' => $mediaId, 'id' => $mediaId,
+6
View File
@@ -1461,6 +1461,12 @@ if (empty($user_id)) {
if (!uploadData.success) { if (!uploadData.success) {
throw new Error(uploadData.error || 'Error subiendo archivo'); throw new Error(uploadData.error || 'Error subiendo archivo');
} }
// Avisar si la conversión falló en el servidor (ej. shell_exec deshabilitado o sin ffmpeg)
if (uploadData.conversion_error) {
console.warn('upload conversion issue:', uploadData.conversion_error);
showAlert('Advertencia: La conversión de audio no se realizó en el servidor. El audio puede no reproducirse en algunos clientes. (' + uploadData.conversion_error + ')', 'warning');
}
// Enviar mensaje con el archivo // Enviar mensaje con el archivo
const caption = document.getElementById('mediaCaption').value.trim(); const caption = document.getElementById('mediaCaption').value.trim();