from flask import Flask, render_template_string, request, send_file, after_this_request import yt_dlp import os import uuid app = Flask(__name__) HTML_CODE = """ UniLoader — Made by Deepu
⚡ Universal Video Downloader
UniLoader
Download anything. Instantly.
✦ Made by Deepu ✦
YouTube TikTok Instagram Facebook 1000+ Sites
720pMax Quality
MP4Format
FastDirect
{% if error %}
{{ error }}
{% endif %}
""" # ────────────────────────────────────────── # 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", "").strip() filename = f"video_{uuid.uuid4().hex[:8]}.mp4" is_youtube = any(x in url for x in ["youtube.com", "youtu.be"]) is_instagram = "instagram.com" in url is_tiktok = "tiktok.com" in url ydl_opts = { "format": ( "bv*[ext=mp4][height<=720]/" "bv*[height<=720]/" "best[height<=720]/best" ), "outtmpl" : filename, "merge_output_format": "mp4", "quiet" : True, "no_warnings" : True, "nocheckcertificate" : True, "retries" : 5, "fragment_retries" : 5, "http_headers": { "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0.0.0 Safari/537.36", "Accept-Language": "en-US,en;q=0.9", }, } # YouTube — Android client bypass (avoids bot detection) if is_youtube: ydl_opts["extractor_args"] = { "youtube": { "player_client": ["android", "ios", "web"], "player_skip" : ["webpage"], } } ydl_opts["http_headers"]["User-Agent"] = ( "com.google.android.youtube/17.36.4 (Linux; U; Android 12; GB) gzip" ) # Instagram — real app headers if is_instagram: ydl_opts["http_headers"] = { "User-Agent" : "Instagram 219.0.0.12.117 Android (31/12; 420dpi; 1080x2400; samsung; SM-G991B; o1s; exynos2100; en_US; 346877738)", "Accept" : "*/*", "Accept-Language" : "en-US", "X-IG-App-ID" : "936619743392459", } # TikTok if is_tiktok: ydl_opts["http_headers"]["User-Agent"] = ( "TikTok 26.2.0 rv:262018 (iPhone; iOS 14.4.2; en_US) Cronet" ) try: with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) if not os.path.exists(filename): raise FileNotFoundError("Download completed but output file not found.") @after_this_request def cleanup(resp): try: if os.path.exists(filename): os.remove(filename) except Exception: pass return resp return send_file(filename, as_attachment=True) except Exception as e: if os.path.exists(filename): os.remove(filename) err = str(e).lower() if "sign in" in err or "login" in err or "private" in err: msg = "This video is private or requires login." elif "429" in err or "too many" in err: msg = "Rate limited. Please wait a few minutes and try again." elif "instagram" in err: msg = "Instagram blocked this download. Try uploading/downloading the video manually." elif "copyright" in err or "blocked" in err: msg = "This video is blocked or restricted." else: msg = str(e) return render_template_string(HTML_CODE, error=msg) # ────────────────────────────────────────── # RUN # ────────────────────────────────────────── if __name__ == "__main__": app.run(host="0.0.0.0", port=7860)