const downloadBtn = document.getElementById('downloadBtn'); downloadBtn.addEventListener('click', async () => { const videoUrl = document.getElementById('videoUrl').value.trim(); const format = document.getElementById('formatSelect').value; const quality = document.getElementById('qualitySelect').value; const audioFormat = document.getElementById('audioFormatSelect').value; const apiKey = '5069c6be-5955-4646-8e3e-4fdbda1d73af'; // Replace with your actual API key if (!videoUrl) { alert('Please paste a YouTube link.'); return; } // Construct the API request URL let apiUrl = `https://api.piloterr.com/api/v1/video/download?url=${encodeURIComponent(videoUrl)}`; if (format === 'audio') { apiUrl += `&format=${audioFormat}`; } else { apiUrl += `&format=mp4&quality=${quality}`; } try { const response = await fetch(apiUrl, { headers: { 'x-api-key': apiKey } }); if (!response.ok) { throw new Error('Network response was not ok.'); } const blob = await response.blob(); const downloadUrl = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = downloadUrl; a.download = 'downloaded-video'; document.body.appendChild(a); a.click(); a.remove(); } catch (error) { console.error('There was a problem with the fetch operation:', error); alert('Failed to download the video. Please try again.'); } });

Comments

Popular posts from this blog

Discover the Untold Stories of India: Journeying to its Secret Places