from flask import Flask, render_template_string, request, send_file, after_this_request
import yt_dlp
import os
import uuid
app = Flask(__name__)
# ---------------- HTML UI ----------------
HTML_CODE = """
Universal Video Downloader
UniLoader HF Edition
{% if error %}
{{ error }}
{% endif %}
Note: TikTok HD on Hugging Face is limited by platform.
"""
# ---------------- ROUTES ----------------
@app.route("/", methods=["GET"])
def index():
return render_template_string(HTML_CODE)
@app.route("/download", methods=["POST"])
def download():
url = request.form.get("url")
filename = f"video_{uuid.uuid4().hex[:6]}.mp4"
try:
ydl_opts = {
# 🔥 BEST POSSIBLE FORMAT (HF SAFE)
"format": (
"bv*[ext=mp4][height<=720]/"
"bv*[height<=720]/"
"best[height<=720]/best"
),
"outtmpl": filename,
"merge_output_format": "mp4",
# 🔥 Browser-like headers (slight quality improvement)
"http_headers": {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
"Accept-Language": "en-US,en;q=0.9",
},
"quiet": True,
"no_warnings": True,
"nocheckcertificate": True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
@after_this_request
def cleanup(resp):
try:
if os.path.exists(filename):
os.remove(filename)
except:
pass
return resp
return send_file(filename, as_attachment=True)
except Exception as e:
return render_template_string(HTML_CODE, error=str(e))
# ---------------- RUN ----------------
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)