Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Install yt-dlp (this runs once when the space starts)
|
| 6 |
+
subprocess.run(["pip", "install", "yt-dlp"], check=True)
|
| 7 |
+
|
| 8 |
+
import yt_dlp
|
| 9 |
+
|
| 10 |
+
def download_video(url, output_format):
|
| 11 |
+
try:
|
| 12 |
+
ydl_opts = {
|
| 13 |
+
'format': output_format or 'best',
|
| 14 |
+
'outtmpl': '/tmp/%(title)s.%(ext)s',
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 18 |
+
info = ydl.extract_info(url, download=True)
|
| 19 |
+
filename = ydl.prepare_filename(info)
|
| 20 |
+
return f"✓ Downloaded: {info['title']}", filename
|
| 21 |
+
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"✗ Error: {str(e)}", None
|
| 24 |
+
|
| 25 |
+
# Create Gradio interface
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("# YouTube Downloader with yt-dlp")
|
| 28 |
+
|
| 29 |
+
with gr.Row():
|
| 30 |
+
url_input = gr.Textbox(label="Video URL", placeholder="https://www.youtube.com/watch?v=...")
|
| 31 |
+
format_input = gr.Textbox(label="Format (optional)", placeholder="best, worst, 18, etc.")
|
| 32 |
+
|
| 33 |
+
download_btn = gr.Button("Download", variant="primary")
|
| 34 |
+
output_text = gr.Textbox(label="Status", interactive=False)
|
| 35 |
+
output_file = gr.File(label="Downloaded File")
|
| 36 |
+
|
| 37 |
+
download_btn.click(
|
| 38 |
+
fn=download_video,
|
| 39 |
+
inputs=[url_input, format_input],
|
| 40 |
+
outputs=[output_text, output_file]
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|