Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import yt_dlp
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
def get_video_info(url):
|
| 5 |
+
try:
|
| 6 |
+
ydl_opts = {"quiet": True, "skip_download": True}
|
| 7 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 8 |
+
info = ydl.extract_info(url, download=False)
|
| 9 |
+
|
| 10 |
+
formats = []
|
| 11 |
+
for f in info['formats']:
|
| 12 |
+
if f.get("vcodec") != "none" and f.get("acodec") != "none":
|
| 13 |
+
resolution = f.get("format_note") or f"{f.get('height')}p"
|
| 14 |
+
if resolution and f.get("url"):
|
| 15 |
+
formats.append({
|
| 16 |
+
"resolution": resolution,
|
| 17 |
+
"ext": f.get("ext"),
|
| 18 |
+
"url": f.get("url")
|
| 19 |
+
})
|
| 20 |
+
|
| 21 |
+
# remove duplicates and sort
|
| 22 |
+
unique = {f["resolution"]: f for f in formats}
|
| 23 |
+
sorted_formats = sorted(unique.values(), key=lambda x: int(x["resolution"].replace("p","").replace("k","000")))
|
| 24 |
+
|
| 25 |
+
return info["title"], sorted_formats
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return str(e), []
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def download_video(url, quality):
|
| 31 |
+
title, formats = get_video_info(url)
|
| 32 |
+
if not formats:
|
| 33 |
+
return None, "No formats available."
|
| 34 |
+
|
| 35 |
+
for f in formats:
|
| 36 |
+
if f["resolution"] == quality:
|
| 37 |
+
return f["url"], f"Click the link below to download {title} in {quality}"
|
| 38 |
+
|
| 39 |
+
return None, "Selected quality not available."
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def ui_app(url):
|
| 43 |
+
title, formats = get_video_info(url)
|
| 44 |
+
if not formats:
|
| 45 |
+
return "Error fetching video info", None, []
|
| 46 |
+
|
| 47 |
+
qualities = [f["resolution"] for f in formats]
|
| 48 |
+
return title, url, qualities
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("## 🎥 YouTube Video Downloader")
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
url_in = gr.Textbox(label="YouTube Video URL")
|
| 56 |
+
fetch_btn = gr.Button("Fetch Info")
|
| 57 |
+
|
| 58 |
+
title_out = gr.Textbox(label="Video Title", interactive=False)
|
| 59 |
+
quality_out = gr.Dropdown(label="Select Quality")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
download_btn = gr.Button("Get Download Link")
|
| 63 |
+
|
| 64 |
+
result_msg = gr.Textbox(label="Message", interactive=False)
|
| 65 |
+
download_link = gr.Textbox(label="Direct Download Link", interactive=False)
|
| 66 |
+
|
| 67 |
+
def fetch_info(url):
|
| 68 |
+
title, _, qualities = ui_app(url)
|
| 69 |
+
return title, gr.Dropdown.update(choices=qualities)
|
| 70 |
+
|
| 71 |
+
fetch_btn.click(fetch_info, inputs=[url_in], outputs=[title_out, quality_out])
|
| 72 |
+
download_btn.click(download_video, inputs=[url_in, quality_out], outputs=[download_link, result_msg])
|
| 73 |
+
|
| 74 |
+
demo.launch()
|