File size: 2,416 Bytes
a17935c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)