Update chat_window.php
This commit is contained in:
@@ -474,6 +474,8 @@ if (empty($user_id)) {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
<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>
|
<script>
|
||||||
// Variables globales
|
// Variables globales
|
||||||
const userId = <?php echo json_encode($user_id); ?>;
|
const userId = <?php echo json_encode($user_id); ?>;
|
||||||
@@ -541,6 +543,43 @@ if (empty($user_id)) {
|
|||||||
|
|
||||||
// Inicializar manager
|
// Inicializar manager
|
||||||
whatsappManager = new WhatsAppManager();
|
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
|
// Función para escapar HTML
|
||||||
function escapeHtml(text) {
|
function escapeHtml(text) {
|
||||||
@@ -1446,6 +1485,17 @@ if (empty($user_id)) {
|
|||||||
try {
|
try {
|
||||||
showTyping();
|
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
|
// Subir archivo
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', selectedFile);
|
formData.append('file', selectedFile);
|
||||||
|
|||||||
Reference in New Issue
Block a user