Update chat_window.php

This commit is contained in:
Lizandro Guarnizo
2026-01-22 11:46:33 -05:00
parent 9ee82c6c3f
commit b3de3a86a9
+50
View File
@@ -474,6 +474,8 @@ if (empty($user_id)) {
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- FFmpeg WASM for client-side audio conversion (webm -> ogg) -->
<script src="https://unpkg.com/@ffmpeg/ffmpeg@0.11.8/dist/ffmpeg.min.js"></script>
<script>
// Variables globales
const userId = <?php echo json_encode($user_id); ?>;
@@ -541,6 +543,43 @@ if (empty($user_id)) {
// Inicializar manager
whatsappManager = new WhatsAppManager();
// ===== FFmpeg in-browser conversion helpers =====
let ffmpegInstance = null;
let ffmpegLoaded = false;
async function ensureFFmpeg() {
if (ffmpegLoaded) return;
if (!window.FFmpeg || !window.FFmpeg.createFFmpeg) {
throw new Error('FFmpeg WASM no está disponible en este navegador');
}
const { createFFmpeg, fetchFile } = FFmpeg;
ffmpegInstance = createFFmpeg({ log: false });
// Inform user loading may take some seconds
showAlert('Cargando conversor de audio en navegador...', 'info');
await ffmpegInstance.load();
ffmpegInstance._fetchFile = fetchFile; // store helper
ffmpegLoaded = true;
}
async function convertWebmToOgg(file) {
await ensureFFmpeg();
const inName = 'input.webm';
const outName = 'output.ogg';
try {
showAlert('Convirtiendo audio a OGG... puede tardar unos segundos', 'info');
const data = await ffmpegInstance._fetchFile(file);
ffmpegInstance.FS('writeFile', inName, data);
await ffmpegInstance.run('-i', inName, '-c:a', 'libopus', '-b:a', '64k', outName);
const outData = ffmpegInstance.FS('readFile', outName);
const blob = new Blob([outData.buffer], { type: 'audio/ogg' });
const newName = (file.name || 'audio').replace(/\.[^/.]+$/, '') + '.ogg';
const outFile = new File([blob], newName, { type: 'audio/ogg' });
return outFile;
} catch (err) {
console.error('Error converting to OGG:', err);
throw err;
}
}
// Función para escapar HTML
function escapeHtml(text) {
@@ -1446,6 +1485,17 @@ if (empty($user_id)) {
try {
showTyping();
// Si es WebM y no se convirtió en servidor, intentar convertir en el navegador a OGG
if (selectedFile && selectedFile.type === 'audio/webm') {
try {
selectedFile = await convertWebmToOgg(selectedFile);
showAlert('Conversión a OGG completada localmente', 'success');
} catch (convErr) {
console.warn('Conversión local falló:', convErr);
showAlert('No se pudo convertir el audio localmente. Se intentará subir el archivo original.', 'warning');
}
}
// Subir archivo
const formData = new FormData();
formData.append('file', selectedFile);