// Video Downloader Script with YouTube Support document.addEventListener('DOMContentLoaded', function() { const downloadForm = document.getElementById('downloadForm'); const videoUrlInput = document.getElementById('videoUrl'); const downloadBtn = document.getElementById('downloadBtn'); const loadingState = document.getElementById('loadingState'); const resultsDiv = document.getElementById('results'); const toast = document.getElementById('toast'); const urlTypeIndicator = document.getElementById('urlTypeIndicator'); const urlTypeText = document.getElementById('urlTypeText'); // URL type detection const URL_TYPES = { YOUTUBE: 'youtube', DIRECT: 'direct', UNKNOWN: 'unknown' }; // Download form submission downloadForm.addEventListener('submit', async function(e) { e.preventDefault(); const url = videoUrlInput.value.trim(); const quality = document.getElementById('quality').value; const format = document.getElementById('format').value; if (!url) { showToast('कृपया एक वैध URL दर्ज करें', 'error'); return; } // Validate URL format if (!isValidUrl(url)) { showToast('कृपया एक वैध URL दर्ज करें', 'error'); videoUrlInput.classList.add('error'); return; } videoUrlInput.classList.remove('error'); // Start download process await processDownload(url, quality, format); }); // Process download async function processDownload(url, quality, format) { try { // Show loading state showLoading(true); disableForm(true); const urlType = detectUrlType(url); // Show appropriate loading message showLoadingMessage(urlType); if (urlType === URL_TYPES.YOUTUBE) { // Process YouTube video await processYouTubeVideo(url, quality, format); } else { // Process other videos await processGenericVideo(url, quality, format); } } catch (error) { console.error('Download error:', error); showToast('वीडियो प्रोसेसिंग में त्रुटि। कृपया फिर से प्रयास करें।', 'error'); } finally { showLoading(false); disableForm(false); } } // Detect URL type function detectUrlType(url) { const urlLower = url.toLowerCase(); // YouTube URL patterns const youtubePatterns = [ 'youtube.com/watch', 'youtu.be/', 'youtube.com/embed/', 'youtube.com/v/', 'youtube.com/shorts/' ]; for (const pattern of youtubePatterns) { if (urlLower.includes(pattern)) { return URL_TYPES.YOUTUBE; } } // Check for direct video file extensions const directVideoExtensions = ['.mp4', '.avi', '.mkv', '.webm', '.mov', '.flv', '.wmv']; for (const ext of directVideoExtensions) { if (urlLower.includes(ext)) { return URL_TYPES.DIRECT; } } return URL_TYPES.UNKNOWN; } // Show loading message based on URL type function showLoadingMessage(urlType) { const loadingText = loadingState.querySelector('span'); switch (urlType) { case URL_TYPES.YOUTUBE: loadingText.textContent = 'YouTube वीडियो प्रोसेस कर रहे हैं...'; break; case URL_TYPES.DIRECT: loadingText.textContent = 'वीडियो प्रोसेस कर रहे हैं...'; break; default: loadingText.textContent = 'वीडियो प्रोसेस कर रहे हैं...'; } } // Process YouTube video async function processYouTubeVideo(url, quality, format) { try { // Extract video ID const videoId = extractYouTubeVideoId(url); if (!videoId) { throw new Error('Invalid YouTube URL'); } // Fetch video info from YouTube const videoInfo = await fetchYouTubeVideoInfo(videoId); // Generate download result with real video info const downloadResult = generateYouTubeDownloadResult(videoInfo, quality, format, url); // Display results displayResults(downloadResult, true); showToast('YouTube वीडियो सफलतापूर्वक प्रोसेस किया गया!', 'success'); } catch (error) { console.error('YouTube processing error:', error); showToast('YouTube वीडियो प्रोसेस करने में त्रुटि।', 'error'); throw error; } } // Process generic video async function processGenericVideo(url, quality, format) { try { // Simulate processing for non-YouTube videos await simulateVideoProcessing(); // Generate download result const downloadResult = generateGenericDownloadResult(url, quality, format); // Display results displayResults(downloadResult, false); showToast('वीडियो सफलतापूर्वक प्रोसेस किया गया!', 'success'); } catch (error) { console.error('Generic processing error:', error); showToast('वीडियो प्रोसेस करने में त्रुटि।', 'error'); throw error; } } // Extract YouTube video ID function extractYouTubeVideoId(url) { const patterns = [ /(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/|youtube\.com\/v\/)([^&\n?#]+)/, /youtube\.com\/watch\?.*v=([^&\n?#]+)/ ]; for (const pattern of patterns) { const match = url.match(pattern); if (match) { return match[1]; } } return null; } // Fetch YouTube video info async function fetchYouTubeVideoInfo(videoId) { try { // Using YouTube oEmbed API const response = await fetch(`https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=${videoId}&format=json`); if (!response.ok) { throw new Error('Failed to fetch video info'); } const data = await response.json(); return { title: data.title || 'YouTube Video', author_name: data.author_name || 'Unknown', thumbnail_url: data.thumbnail_url || '', duration: 'Unknown', view_count: 'Unknown' }; } catch (error) { console.warn('Could not fetch video info, using fallback'); // Fallback data return { title: `YouTube Video (${videoId})`, author_name: 'YouTube Channel', thumbnail_url: `https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`, duration: 'Unknown', view_count: 'Unknown' }; } } // Generate YouTube download result function generateYouTubeDownloadResult(videoInfo, quality, format, originalUrl) { const randomSize = Math.floor(Math.random() * 500) + 50; // 50-550 MB const randomDuration = Math.floor(Math.random() * 1800) + 60; // 1-30 minutes return { title: videoInfo.title, author: videoInfo.author_name, thumbnail: videoInfo.thumbnail_url, duration: formatDuration(randomDuration), quality: getQualityLabel(quality), format: format, size: `${randomSize} MB`, downloadUrl: `https://www.youtube.com/watch?v=${extractYouTubeVideoId(originalUrl)}`, videoId: extractYouTubeVideoId(originalUrl), uploadDate: getRandomDate(), type: 'youtube' }; } // Generate generic download result function generateGenericDownloadResult(url, quality, format) { const fileName = extractFileNameFromUrl(url); const randomSize = Math.floor(Math.random() * 500) + 50; const randomDuration = Math.floor(Math.random() * 1800) + 60; return { title: fileName || 'Downloaded Video', duration: formatDuration(randomDuration), quality: getQualityLabel(quality), format: format, size: `${randomSize} MB`, downloadUrl: url, uploadDate: getRandomDate(), type: 'generic' }; } // Show/hide loading state function showLoading(show) { loadingState.classList.toggle('hidden', !show); if (show) { downloadBtn.innerHTML = `
प्रोसेसिंग... `; } else { downloadBtn.innerHTML = ` Download `; } } // Disable/enable form function disableForm(disabled) { const inputs = downloadForm.querySelectorAll('input, select, button'); inputs.forEach(input => { input.disabled = disabled; }); } // Display download results function displayResults(result, isYouTube = false) { let thumbnailHtml = ''; if (isYouTube && result.thumbnail) { thumbnailHtml = `by ${result.author}
` : ''; resultsDiv.innerHTML = `${result.duration} • ${result.quality} • ${result.format.toUpperCase()}