import gradio as gr import yt_dlp import os import browser_cookie3 from huggingface_hub import hf_hub_download, login if "cookies.txt" not in os.listdir("."): hf_hub_download("not-lain/secrets","cookies.txt",local_dir=".",repo_type="dataset") def get_cookies(): try: # Try to get cookies from multiple browsers cookies = [] for get_cookies in [ browser_cookie3.firefox, browser_cookie3.chrome, browser_cookie3.brave, ]: try: cookies.extend([c for c in get_cookies(domain_name=".youtube.com")]) except: continue return cookies except: return None def download_video(url): try: downloads_dir = "/downloads" if not os.path.exists(downloads_dir): os.makedirs(downloads_dir, exist_ok=True) # Use a pre-provided cookies file cookie_file = "cookies.txt" cookie_args = {} if os.path.exists(cookie_file): cookie_args["cookiefile"] = cookie_file ydl_opts = { "outtmpl": os.path.join(downloads_dir, "%(title)s.%(ext)s"), "format": "mp4/bestvideo+bestaudio/best", "merge_output_format": "mp4", **cookie_args, "paths": {"home": "/downloads", "temp": "/tmp"}, } with yt_dlp.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(url, download=True) video_path = ydl.prepare_filename(info) if os.path.exists(video_path): return f"Successfully downloaded: {info['title']}", video_path else: return f"Download completed but file not found: {info['title']}", None except Exception as e: return f"Error: {str(e)}", None iface = gr.Interface( fn=download_video, inputs=gr.Textbox(label="YouTube URL"), outputs=[gr.Textbox(label="Status"), gr.Video(label="Downloaded Video")], title="YouTube Video Downloader", description="Cookies created using the following extension : https://chromewebstore.google.com/detail/get-cookiestxt-clean/ahmnmhfbokciafffnknlekllgcnafnie?hl=en \n\nEnter a YouTube URL to download and view the video", examples=["https://www.youtube.com/watch?v=UiyDmqO59QE"], ) if __name__ == "__main__": iface.launch(server_name="0.0.0.0", allowed_paths=["/"], server_port=7860)