Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +93 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import yt_dlp, os, shutil, zipfile, time
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
def simulate_progress(progress_slider, status_box, msg):
|
| 6 |
+
for i in range(0, 101, 10):
|
| 7 |
+
progress_slider(i)
|
| 8 |
+
status_box(f"{msg} ({i}%)")
|
| 9 |
+
time.sleep(0.15)
|
| 10 |
+
|
| 11 |
+
def download_handler(link, playlist_mode, zip_output, custom_name, progress=gr.Progress(track_tqdm=False)):
|
| 12 |
+
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 13 |
+
safe = (custom_name or "").strip().replace(" ", "_")
|
| 14 |
+
|
| 15 |
+
def set_progress(val): yield gr.Slider.update(value=val)
|
| 16 |
+
def set_status(msg): yield gr.Textbox.update(value=msg)
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
if playlist_mode:
|
| 20 |
+
folder = safe or f"yt_playlist_{ts}"
|
| 21 |
+
os.makedirs(folder, exist_ok=True)
|
| 22 |
+
|
| 23 |
+
ydl_opts = {
|
| 24 |
+
"format": "bv*+ba/bestvideo+bestaudio/best",
|
| 25 |
+
"merge_output_format": "mp4",
|
| 26 |
+
"outtmpl": os.path.join(folder, "%(playlist_index)s_%(title)s.%(ext)s"),
|
| 27 |
+
"quiet": True,
|
| 28 |
+
"noplaylist": False,
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
simulate_progress(set_progress, set_status, "Downloading Playlist...")
|
| 32 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 33 |
+
ydl.download([link])
|
| 34 |
+
|
| 35 |
+
if zip_output:
|
| 36 |
+
zip_name = (safe or f"playlist_{ts}") + ".zip"
|
| 37 |
+
with zipfile.ZipFile(zip_name, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 38 |
+
for root, _, files in os.walk(folder):
|
| 39 |
+
for f in files:
|
| 40 |
+
zf.write(os.path.join(root, f), arcname=f)
|
| 41 |
+
shutil.rmtree(folder)
|
| 42 |
+
simulate_progress(set_progress, set_status, "Zipping Complete")
|
| 43 |
+
return "✅ Playlist downloaded & zipped!", zip_name, 100
|
| 44 |
+
else:
|
| 45 |
+
file_list = [os.path.join(folder, f) for f in os.listdir(folder)]
|
| 46 |
+
return "✅ Playlist downloaded!", file_list, 100
|
| 47 |
+
|
| 48 |
+
else:
|
| 49 |
+
filename = (safe or f"yt_video_{ts}") + ".mp4"
|
| 50 |
+
ydl_opts = {
|
| 51 |
+
"format": "bv*+ba/bestvideo+bestaudio/best",
|
| 52 |
+
"merge_output_format": "mp4",
|
| 53 |
+
"outtmpl": filename,
|
| 54 |
+
"quiet": True,
|
| 55 |
+
"noplaylist": True,
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
simulate_progress(set_progress, set_status, "Downloading Video...")
|
| 59 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 60 |
+
ydl.download([link])
|
| 61 |
+
|
| 62 |
+
return "✅ Video downloaded!", filename, 100
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return f"❌ Error: {e}", None, 0
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown("## 🎥 YouTube Downloader + Progress Bar")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
link_in = gr.Textbox(label="YouTube URL", scale=3)
|
| 73 |
+
custom_name = gr.Textbox(label="Custom name (optional)", scale=2)
|
| 74 |
+
|
| 75 |
+
playlist_chk = gr.Checkbox(label="Download entire playlist", value=False)
|
| 76 |
+
zip_chk = gr.Checkbox(label="Zip playlist into one file", value=False)
|
| 77 |
+
|
| 78 |
+
dl_btn = gr.Button("⬇️ Start Download")
|
| 79 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 80 |
+
progress = gr.Slider(label="Progress", minimum=0, maximum=100, value=0, interactive=False)
|
| 81 |
+
filesout = gr.File(label="Your file(s)", file_count="multiple")
|
| 82 |
+
|
| 83 |
+
def wrap_download(link, playlist, zipopt, cname):
|
| 84 |
+
msg, fpath, pval = download_handler(link, playlist, zipopt, cname)
|
| 85 |
+
return msg, fpath, pval
|
| 86 |
+
|
| 87 |
+
dl_btn.click(
|
| 88 |
+
fn=wrap_download,
|
| 89 |
+
inputs=[link_in, playlist_chk, zip_chk, custom_name],
|
| 90 |
+
outputs=[status, filesout, progress],
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
yt-dlp
|