Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import tempfile | |
| from pathlib import Path | |
| import yt_dlp | |
| # HF persistent storage | |
| DOWNLOAD_DIR = Path(tempfile.gettempdir()) / "downloads" | |
| DOWNLOAD_DIR.mkdir(exist_ok=True) | |
| def download_video(url, quality, audio_only): | |
| """Download from any supported site.""" | |
| if not url.strip(): | |
| return None, "β URL daalo!" | |
| output_template = str(DOWNLOAD_DIR / '%(title)s.%(ext)s') | |
| opts = { | |
| 'outtmpl': output_template, | |
| 'quiet': True, | |
| 'no_warnings': True, | |
| } | |
| if audio_only: | |
| opts['format'] = 'bestaudio/best' | |
| opts['postprocessors'] = [{ | |
| 'key': 'FFmpegExtractAudio', | |
| 'preferredcodec': 'mp3', | |
| 'preferredquality': '320', | |
| }] | |
| elif quality == "Best": | |
| opts['format'] = 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' | |
| elif quality == "Worst": | |
| opts['format'] = 'worst' | |
| else: | |
| h = quality.replace('p', '') | |
| opts['format'] = f'bestvideo[height<={h}][ext=mp4]+bestaudio[ext=m4a]/best[height<={h}]' | |
| try: | |
| with yt_dlp.YoutubeDL(opts) as ydl: | |
| info = ydl.extract_info(url, download=True) | |
| filename = ydl.prepare_filename(info) | |
| if audio_only: | |
| filename = filename.replace('.webm', '.mp3').replace('.m4a', '.mp3') | |
| fpath = Path(filename) | |
| size_mb = fpath.stat().st_size / 1024 / 1024 if fpath.exists() else 0 | |
| # HF mein file return karna padega | |
| return str(filename), f"β Done!\nTitle: {info.get('title', 'Unknown')}\nSize: {size_mb:.1f} MB\nBy: {info.get('uploader', 'Unknown')}" | |
| except Exception as e: | |
| return None, f"β Error: {str(e)[:300]}" | |
| def get_info(url): | |
| if not url.strip(): | |
| return "β URL daalo!" | |
| try: | |
| with yt_dlp.YoutubeDL({'quiet': True}) as ydl: | |
| info = ydl.extract_info(url, download=False) | |
| return f""" | |
| π¬ **{info.get('title', 'Unknown')}** | |
| π€ Uploader: {info.get('uploader', 'Unknown')} | |
| β±οΈ Duration: {info.get('duration', 0)//60}m {info.get('duration', 0)%60}s | |
| ποΈ Views: {info.get('view_count', 0):,} | |
| πΊ Platform: {info.get('extractor', 'Unknown')} | |
| """ | |
| except Exception as e: | |
| return f"β Error: {str(e)[:200]}" | |
| # βββ GRADIO UI βββ | |
| with gr.Blocks(title="β’οΈ Nuclear Downloader", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown(""" | |
| # β’οΈ Nuclear Video Downloader | |
| ### YouTube | Instagram | TikTok | Facebook | Twitter | Reddit | Vimeo | +1000 sites | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| url_input = gr.Textbox( | |
| label="π Video URL", | |
| placeholder="https://...", | |
| lines=2 | |
| ) | |
| quality = gr.Dropdown( | |
| label="ποΈ Quality", | |
| choices=["Best", "1080p", "720p", "480p", "360p", "Worst"], | |
| value="Best" | |
| ) | |
| audio_only = gr.Checkbox(label="π΅ Audio Only (MP3)", value=False) | |
| with gr.Row(): | |
| info_btn = gr.Button("βΉοΈ Get Info", variant="secondary") | |
| download_btn = gr.Button("β¬οΈ Download", variant="primary") | |
| with gr.Column(scale=1): | |
| output_file = gr.File(label="π₯ Downloaded File") | |
| status_text = gr.Textbox(label="π Status", lines=6, interactive=False) | |
| info_btn.click(fn=get_info, inputs=url_input, outputs=status_text) | |
| download_btn.click( | |
| fn=download_video, | |
| inputs=[url_input, quality, audio_only], | |
| outputs=[output_file, status_text] | |
| ) | |
| gr.Markdown(""" | |
| --- | |
| β Made for LO | |
| """) | |
| demo.launch() | |