File size: 2,691 Bytes
2881c06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import shutil
import subprocess
import gradio as gr
from tkinter import Tk, filedialog
from groq import Groq

# Function to check if yt-dlp is installed
def check_yt_dlp_installed():
    """
    Checks if yt-dlp is installed and available.
    """
    if not shutil.which("yt-dlp"):
        subprocess.run(["pip", "install", "yt-dlp"], check=True)

# Function to download YouTube video
def download_youtube_video_yt_dlp(url, save_path):
    """
    Downloads a YouTube video using yt-dlp in MP4 format and saves it to the specified directory.
    """
    if not os.path.exists(save_path):
        os.makedirs(save_path)  # Create the directory if it doesn't exist

    try:
        command = [
            "yt-dlp",
            "-f", "mp4",  # Download in mp4 format
            "-o", f"{save_path}/%(title)s.%(ext)s",  # Save with video title and extension
            url
        ]
        
        result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
        
        if result.returncode == 0:
            return f"Video downloaded successfully in MP4 format and saved in {save_path}."
        else:
            return f"An error occurred:\n{result.stderr}"
    except Exception as e:
        return f"An unexpected error occurred: {e}"

# Function to open a folder selection dialog
def get_download_directory():
    """
    Opens a folder selection dialog and returns the chosen directory.
    """
    root = Tk()
    root.withdraw()  # Hide the main window

    folder_selected = filedialog.askdirectory(title="Select the folder to save the video")

    if folder_selected:
        return folder_selected
    else:
        return None

# Gradio Interface Function
def download_video(url):
    # Check if yt-dlp is installed
    check_yt_dlp_installed()

    # Get the folder to save the video
    download_directory = get_download_directory()

    if download_directory is None:
        return "No folder selected. Please select a folder."

    # Call the function to download the video and return the result
    result = download_youtube_video_yt_dlp(url, download_directory)
    return result

# Gradio Interface with instructions
interface = gr.Interface(
    fn=download_video,
    inputs=gr.Textbox(label="Enter YouTube URL"),
    outputs=gr.Textbox(label="Download Status"),
    live=True,
    title="YouTube Video Downloader",
    description="Enter a YouTube video URL, select the folder where you want to save the video, and it will download in MP4 format. Ensure 'yt-dlp' is installed on the system.",
    theme="compact",
    allow_flagging="never"
)

# Launch the interface
if __name__ == "__main__":
    interface.launch()