192 lines
6.2 KiB
JavaScript
192 lines
6.2 KiB
JavaScript
let hls = null;
|
|
|
|
function playUrlInPreviewer(url) {
|
|
const previewVideo = document.getElementById('preview');
|
|
if (!url || !previewVideo) return;
|
|
|
|
if (hls) {
|
|
hls.destroy();
|
|
}
|
|
|
|
if (url.endsWith('.m3u8')) {
|
|
if (Hls.isSupported()) {
|
|
hls = new Hls();
|
|
hls.loadSource(url);
|
|
hls.attachMedia(previewVideo);
|
|
hls.on(Hls.Events.MANIFEST_PARSED, () => {
|
|
previewVideo.play().catch(e => console.log("Autoplay foi bloqueado."));
|
|
});
|
|
} else if (previewVideo.canPlayType('application/vnd.apple.mpegurl')) {
|
|
previewVideo.src = url;
|
|
previewVideo.play().catch(e => console.log("Autoplay foi bloqueado."));
|
|
}
|
|
} else {
|
|
previewVideo.src = url;
|
|
previewVideo.play().catch(e => console.log("Autoplay foi bloqueado."));
|
|
}
|
|
}
|
|
|
|
function playVideo(url) {
|
|
const streamUrlInput = document.getElementById('stream-url');
|
|
if(streamUrlInput) {
|
|
streamUrlInput.value = url;
|
|
}
|
|
playUrlInPreviewer(url);
|
|
|
|
const videoSection = document.querySelector('.video-section');
|
|
if(videoSection){
|
|
videoSection.scrollIntoView({ behavior: 'smooth' });
|
|
}
|
|
}
|
|
|
|
async function updateConversionProgress() {
|
|
const convertingRows = document.querySelectorAll('tr[data-status="converting"]');
|
|
if (convertingRows.length === 0) return;
|
|
|
|
for (const row of convertingRows) {
|
|
const streamId = row.dataset.streamId;
|
|
if (!streamId) continue;
|
|
|
|
try {
|
|
const response = await fetch(`api.php?action=get_conversion_progress&id=${streamId}`);
|
|
if (!response.ok) continue;
|
|
|
|
const result = await response.json();
|
|
if (result.success && result.progress !== undefined) {
|
|
const progressBar = row.querySelector('.progress-bar');
|
|
if (progressBar) {
|
|
progressBar.style.width = `${result.progress}%`;
|
|
}
|
|
|
|
if (result.progress >= 100) {
|
|
setTimeout(() => location.reload(), 1000);
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error('Erro ao atualizar progresso:', err);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function sendToDropbox(id, button) {
|
|
const tokenInput = document.getElementById('dropbox-token');
|
|
const token = tokenInput ? tokenInput.value : '';
|
|
if (!token) {
|
|
showToast('Por favor, insira seu token de acesso do Dropbox na página do conversor.', 'error');
|
|
return;
|
|
}
|
|
|
|
button.disabled = true;
|
|
const originalHtml = button.innerHTML;
|
|
button.innerHTML = '...';
|
|
showToast('Enviando para o Dropbox...', 'info');
|
|
|
|
try {
|
|
const response = await fetch('api.php?action=send_to_dropbox', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ id, token }),
|
|
});
|
|
const result = await response.json();
|
|
if (!response.ok) throw new Error(result.error || 'Erro ao enviar para o Dropbox');
|
|
|
|
showToast(result.message, 'success');
|
|
button.innerHTML = '✅';
|
|
|
|
} catch (err) {
|
|
showToast(err.message, 'error');
|
|
button.disabled = false;
|
|
button.innerHTML = originalHtml;
|
|
}
|
|
}
|
|
|
|
async function deleteVideo(id, button) {
|
|
if (!confirm('Deseja deletar este vídeo?')) return;
|
|
|
|
button.disabled = true;
|
|
showToast('Apagando...', 'info');
|
|
|
|
try {
|
|
const response = await fetch('api.php?action=delete', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ id }),
|
|
});
|
|
const result = await response.json();
|
|
if (!response.ok) throw new Error(result.error || 'Erro ao apagar');
|
|
|
|
showToast(result.message, 'success');
|
|
button.closest('tr').remove();
|
|
|
|
} catch (err) {
|
|
showToast(err.message, 'error');
|
|
button.disabled = false;
|
|
}
|
|
}
|
|
|
|
function showToast(message, type = 'info') {
|
|
const container = document.getElementById('toast-container');
|
|
const toast = document.createElement('div');
|
|
toast.className = `toast toast-${type}`;
|
|
toast.textContent = message;
|
|
container.appendChild(toast);
|
|
|
|
setTimeout(() => toast.classList.add('show'), 10);
|
|
|
|
setTimeout(() => {
|
|
toast.classList.remove('show');
|
|
setTimeout(() => toast.remove(), 400);
|
|
}, 4000);
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const streamUrlInput = document.getElementById('stream-url');
|
|
if (streamUrlInput && streamUrlInput.value) {
|
|
playUrlInPreviewer(streamUrlInput.value);
|
|
}
|
|
|
|
if(streamUrlInput) {
|
|
streamUrlInput.addEventListener('input', () => {
|
|
playUrlInPreviewer(streamUrlInput.value);
|
|
});
|
|
}
|
|
|
|
const saveBtn = document.getElementById('save-btn');
|
|
if(saveBtn) {
|
|
saveBtn.addEventListener('click', async () => {
|
|
const url = document.getElementById('stream-url').value;
|
|
const filename = document.getElementById('filename').value;
|
|
const token = document.getElementById('dropbox-token').value;
|
|
|
|
if (!url || !filename) {
|
|
showToast('Preencha a URL e o nome do arquivo.', 'error');
|
|
return;
|
|
}
|
|
|
|
saveBtn.disabled = true;
|
|
saveBtn.innerHTML = 'Salvando...';
|
|
|
|
try {
|
|
const saveResponse = await fetch('api.php', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ action: 'save', url, filename, token }),
|
|
});
|
|
const saveResult = await saveResponse.json();
|
|
if (!saveResponse.ok) throw new Error(saveResult.error || 'Erro ao salvar o stream');
|
|
|
|
showToast(saveResult.message, 'success');
|
|
setTimeout(() => location.href = '?page=converter', 1500);
|
|
|
|
} catch (err) {
|
|
showToast(err.message, 'error');
|
|
saveBtn.disabled = false;
|
|
saveBtn.innerHTML = '💾 Salvar e Converter';
|
|
}
|
|
});
|
|
}
|
|
|
|
updateConversionProgress();
|
|
setInterval(updateConversionProgress, 5000);
|
|
});
|