Spaces:
Build error
Build error
| 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() | |