Spaces:
Runtime error
Runtime error
File size: 15,760 Bytes
5603df0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | import os
import re
import json
import uuid
import shutil
import subprocess
import threading
from pathlib import Path
from typing import Optional, Dict, Any, List
from datetime import datetime
print("[YTDL] Starting...")
print("[YTDL] Gradio version:", __import__("gradio").__version__)
from gradio import Server
from gradio.data_classes import FileData
from fastapi.responses import HTMLResponse, FileResponse
import spaces
print("[YTDL] All imports OK, Server class available")
# ===== Config =====
DOWNLOAD_DIR = "./downloads"
PROGRESS_DIR = "./.progress"
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
os.makedirs(PROGRESS_DIR, exist_ok=True)
QUALITY_ORDER = ['2160p', '1440p', '1080p', '720p', '480p', '360p', '240p', '144p']
def _yt_dlp_path():
candidates = [
shutil.which("yt-dlp"),
os.path.expanduser("~/.local/bin/yt-dlp"),
"/usr/local/bin/yt-dlp",
"/usr/bin/yt-dlp",
]
for c in candidates:
if c and os.path.isfile(c):
return c
raise FileNotFoundError("yt-dlp not found")
YTDLP = _yt_dlp_path()
def _safe_filename(title: str, max_len: int = 80) -> str:
name = re.sub(r'[^\w\s\-\(\)\[\].]', '', title).strip()
name = re.sub(r'[-\s]+', '_', name)
return name[:max_len] if name else "video"
def _run_ytdlp(args: List[str], timeout: int = 600) -> Dict[str, Any]:
cmd = [YTDLP] + args
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
if result.returncode != 0:
stderr = result.stderr.strip()
if "ERROR:" in stderr:
error_msg = stderr.split("ERROR:")[-1].strip().split("\n")[0]
else:
error_msg = stderr or f"yt-dlp exited with code {result.returncode}"
return {"success": False, "error": error_msg}
return {"success": True, "data": result.stdout.strip()}
except subprocess.TimeoutExpired:
return {"success": False, "error": f"Download timed out after {timeout}s"}
except FileNotFoundError:
return {"success": False, "error": "yt-dlp binary not found"}
except Exception as e:
return {"success": False, "error": str(e)}
def _save_progress(task_id: str, data: Dict[str, Any]):
filepath = os.path.join(PROGRESS_DIR, f"{task_id}.json")
with open(filepath, "w") as f:
json.dump(data, f)
def _get_progress_file(task_id: str) -> Optional[Dict[str, Any]]:
filepath = os.path.join(PROGRESS_DIR, f"{task_id}.json")
if os.path.exists(filepath):
with open(filepath, "r") as f:
return json.load(f)
return None
def _cleanup_old_downloads(max_age_hours: int = 24):
now = datetime.now().timestamp()
for entry in os.scandir(DOWNLOAD_DIR):
if entry.is_dir():
try:
if now - entry.stat().st_mtime > max_age_hours * 3600:
shutil.rmtree(entry.path)
except OSError:
pass
# ===== Core Functions =====
def get_video_info(url: str) -> Dict[str, Any]:
result = _run_ytdlp([
"--dump-json", "--no-playlist", "--no-warnings", "--no-download", url,
])
if not result["success"]:
return result
try:
data = json.loads(result["data"])
thumbnail = ""
if data.get("thumbnail"):
thumbnail = data["thumbnail"]
elif data.get("thumbnails"):
thumbs = sorted(
[t for t in data["thumbnails"] if t.get("url")],
key=lambda t: t.get("width", 0) or 0, reverse=True,
)
thumbnail = thumbs[0]["url"] if thumbs else ""
duration = data.get("duration", 0) or 0
h, rem = divmod(duration, 3600)
m, s = divmod(rem, 60)
duration_str = f"{h}:{m:02d}:{s:02d}" if h else f"{m}:{s:02d}"
views = data.get("view_count", 0) or 0
upload_date = data.get("upload_date")
if upload_date and len(upload_date) == 8:
upload_date = f"{upload_date[:4]}-{upload_date[4:6]}-{upload_date[6:8]}"
captions = list(data["subtitles"].keys()) if data.get("subtitles") else []
return {
"success": True,
"info": {
"id": data.get("id", ""),
"title": data.get("title", "Unknown Title"),
"author": data.get("uploader") or data.get("channel", "Unknown"),
"duration": duration,
"duration_string": duration_str,
"views": views,
"description": (data.get("description") or "")[:800],
"upload_date": upload_date,
"thumbnail": thumbnail,
"keywords": data.get("tags", []) or [],
"captions": captions,
"is_live": data.get("is_live", False),
},
}
except (json.JSONDecodeError, KeyError) as e:
return {"success": False, "error": f"Failed to parse video info: {str(e)}"}
def get_available_streams(url: str) -> Dict[str, Any]:
result = _run_ytdlp([
"--dump-json", "--no-playlist", "--no-warnings", "--no-download", url,
])
if not result["success"]:
return result
try:
data = json.loads(result["data"])
formats = data.get("formats", [])
progressive, video_only, audio_only = [], [], []
seen = set()
for f in formats:
fid = str(f.get("format_id", ""))
if fid in seen:
continue
seen.add(fid)
is_audio = f.get("vcodec") in ("none", None)
is_video = f.get("acodec") in ("none", None)
is_prog = not is_audio and not is_video
height = f.get("height")
# Normalize resolution to "720p" format
raw_res = f.get("resolution") or ""
if is_audio:
resolution = "audio"
elif height:
resolution = f"{height}p"
elif raw_res and "x" in raw_res:
try:
resolution = f"{int(raw_res.split('x')[1])}p"
except (ValueError, IndexError):
resolution = raw_res
else:
resolution = raw_res or None
ext = f.get("ext", "mp4")
filesize = f.get("filesize") or f.get("filesize_approx")
filesize_mb = round(filesize / (1024 * 1024), 2) if filesize else None
tbr = f.get("tbr")
entry = {
"format_id": fid,
"resolution": resolution,
"ext": ext,
"filesize_mb": filesize_mb,
"tbr": round(tbr) if tbr else None,
"vcodec": f.get("vcodec"),
"acodec": f.get("acodec"),
"fps": f.get("fps"),
"label": "",
}
if is_audio:
abr = f.get("abr")
bitrate = f"{round(abr)}kbps" if abr else "Audio"
entry["abr"] = bitrate
entry["label"] = f"{bitrate} {ext.upper()}"
audio_only.append(entry)
elif is_video:
codec = (f.get("vcodec") or "").split(".")[0]
entry["label"] = f"{resolution or 'Video'} {ext.upper()}" + (f" ({codec})" if codec else "")
video_only.append(entry)
elif is_prog:
entry["label"] = f"{resolution or 'Video'} {ext.upper()}"
progressive.append(entry)
def res_sort_key(x):
r = x.get("resolution", "")
try:
return QUALITY_ORDER.index(r) if r in QUALITY_ORDER else 999
except ValueError:
return 999
progressive.sort(key=res_sort_key)
video_only.sort(key=res_sort_key)
audio_only.sort(key=lambda x: x.get("tbr") or 0, reverse=True)
all_res = list(set(
s["resolution"] for s in progressive + video_only
if s.get("resolution") and s["resolution"] != "audio"
))
def res_str_sort_key(r):
try:
return QUALITY_ORDER.index(r) if r in QUALITY_ORDER else 999
except ValueError:
return 999
all_res_sorted = sorted(all_res, key=res_str_sort_key)
return {
"success": True,
"progressive": progressive,
"video_only": video_only,
"audio_only": audio_only,
"available_resolutions": all_res_sorted,
"best_resolution": all_res_sorted[0] if all_res_sorted else None,
}
except (json.JSONDecodeError, KeyError) as e:
return {"success": False, "error": f"Failed to parse streams: {str(e)}"}
def download_file(url: str, format_id: str, is_audio: bool = False,
audio_quality: str = "128") -> Dict[str, Any]:
task_id = uuid.uuid4().hex[:12]
info_result = get_video_info(url)
if not info_result["success"]:
return info_result
video_info = info_result["info"]
video_id = video_info["id"]
safe_title = _safe_filename(video_info["title"])
out_dir = Path(DOWNLOAD_DIR) / video_id
out_dir.mkdir(parents=True, exist_ok=True)
_save_progress(task_id, {
"status": "starting", "percent": 0,
"speed": None, "eta": None, "filename": None,
})
if is_audio:
output_template = str(out_dir / f"{safe_title}_audio.%(ext)s")
args = ["-x", "--audio-format", "mp3", "--audio-quality", audio_quality,
"-o", output_template, "--no-playlist", "--newline", "--progress", url]
else:
output_template = str(out_dir / f"{safe_title}_{format_id}.%(ext)s")
args = ["-f", format_id, "--merge-output-format", "mp4",
"-o", output_template, "--no-playlist", "--newline", "--progress", url]
def _download():
try:
_save_progress(task_id, {"status": "downloading", "percent": 0, "speed": None, "eta": None})
process = subprocess.Popen([YTDLP] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
last_percent = 0
while True:
line = process.stdout.readline()
if not line and process.poll() is not None:
break
line = line.strip()
if not line:
continue
match = re.search(r'(\d+(?:\.\d+)?)%', line)
if match:
percent = float(match.group(1))
last_percent = percent
speed = None
eta = None
sm = re.search(r'at\s+([\d.]+\w+/s)', line)
em = re.search(r'ETA\s+([\d:]+)', line)
if sm:
speed = sm.group(1)
if em:
eta = em.group(1)
_save_progress(task_id, {"status": "downloading", "percent": percent, "speed": speed, "eta": eta})
process.wait()
if process.returncode != 0:
stderr = process.stderr.read()
error_msg = "Download failed"
if "ERROR:" in stderr:
error_msg = stderr.split("ERROR:")[-1].strip().split("\n")[0]
_save_progress(task_id, {"status": "error", "percent": last_percent, "error": error_msg})
return
files = list(out_dir.glob(f"{safe_title}*"))
files = [f for f in files if f.suffix not in (".part", ".ytdl", ".temp")]
if not files:
files = [f for f in out_dir.iterdir() if f.is_file() and f.stat().st_size > 1024]
if files:
downloaded = max(files, key=lambda f: f.stat().st_mtime)
_save_progress(task_id, {"status": "completed", "percent": 100, "filename": downloaded.name, "filepath": str(downloaded)})
else:
_save_progress(task_id, {"status": "error", "percent": last_percent, "error": "File not found after download"})
except Exception as e:
_save_progress(task_id, {"status": "error", "percent": 0, "error": str(e)})
thread = threading.Thread(target=_download, daemon=True)
thread.start()
return {"success": True, "task_id": task_id, "message": "Download started"}
def get_progress(task_id: str) -> Dict[str, Any]:
progress = _get_progress_file(task_id)
if progress is None:
return {"success": False, "error": "Task not found"}
result = {"success": True}
result.update(progress)
return result
def download_subtitles(url: str, lang: str = "en") -> Dict[str, Any]:
info_result = get_video_info(url)
if not info_result["success"]:
return info_result
video_id = info_result["info"]["id"]
safe_title = _safe_filename(info_result["info"]["title"])
out_dir = Path(DOWNLOAD_DIR) / video_id
out_dir.mkdir(parents=True, exist_ok=True)
output_template = str(out_dir / f"{safe_title}_subtitles")
result = _run_ytdlp([
"--write-sub", "--write-auto-sub", "--sub-langs", lang,
"--sub-format", "srt", "--skip-download", "-o", output_template, "--no-playlist", url,
], timeout=60)
if not result["success"]:
return result
sub_files = list(out_dir.glob("*.srt"))
if sub_files:
return {"success": True, "message": f"Subtitles downloaded in {len(sub_files)} file(s)", "files": [f.name for f in sub_files]}
return {"success": False, "error": "No subtitles found for this video"}
# ===== Gradio Server =====
print("[YTDL] Creating Server instance...")
demo = Server()
print("[YTDL] Server instance created")
@demo.api(name="get_video_info")
def video_info_api(url: str) -> Dict[str, Any]:
return get_video_info(url)
@demo.api(name="get_streams")
def streams_info_api(url: str) -> Dict[str, Any]:
return get_available_streams(url)
@demo.api(name="download")
def download_api(url: str, format_id: str, is_audio: bool = False,
audio_quality: str = "128") -> Dict[str, Any]:
return download_file(url, format_id, is_audio, audio_quality)
@demo.api(name="get_progress")
def progress_api(task_id: str) -> Dict[str, Any]:
return get_progress(task_id)
@demo.api(name="download_subtitles")
def subtitles_api(url: str, lang: str = "en") -> Dict[str, Any]:
return download_subtitles(url, lang)
@demo.get("/")
async def homepage():
html_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "index.html")
if os.path.exists(html_path):
with open(html_path, "r", encoding="utf-8") as f:
return HTMLResponse(f.read())
return HTMLResponse("<h1>YTDL</h1><p>index.html not found.</p>")
@demo.get("/api-guide")
async def api_guide_page():
guide_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "api_guide.html")
if os.path.exists(guide_path):
with open(guide_path, "r", encoding="utf-8") as f:
return HTMLResponse(f.read())
return HTMLResponse("<h1>API Guide</h1>")
@demo.get("/serve/{video_id}/{filename:path}")
async def serve_download(video_id: str, filename: str):
filepath = os.path.join(DOWNLOAD_DIR, video_id, filename)
real_path = os.path.realpath(filepath)
real_dir = os.path.realpath(DOWNLOAD_DIR)
if not real_path.startswith(real_dir):
return HTMLResponse("Forbidden", status_code=403)
if not os.path.exists(real_path):
return HTMLResponse("File not found", status_code=404)
return FileResponse(real_path, filename=os.path.basename(real_path), media_type="application/octet-stream")
print("[YTDL] Cleaning old downloads...")
_cleanup_old_downloads()
print("[YTDL] All ready. HF Space launcher will call demo.launch()") |