Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import subprocess
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from tkinter import Tk, filedialog
|
| 6 |
+
from groq import Groq
|
| 7 |
+
|
| 8 |
+
# Function to check if yt-dlp is installed
|
| 9 |
+
def check_yt_dlp_installed():
|
| 10 |
+
"""
|
| 11 |
+
Checks if yt-dlp is installed and available.
|
| 12 |
+
"""
|
| 13 |
+
if not shutil.which("yt-dlp"):
|
| 14 |
+
subprocess.run(["pip", "install", "yt-dlp"], check=True)
|
| 15 |
+
|
| 16 |
+
# Function to download YouTube video
|
| 17 |
+
def download_youtube_video_yt_dlp(url, save_path):
|
| 18 |
+
"""
|
| 19 |
+
Downloads a YouTube video using yt-dlp in MP4 format and saves it to the specified directory.
|
| 20 |
+
"""
|
| 21 |
+
if not os.path.exists(save_path):
|
| 22 |
+
os.makedirs(save_path) # Create the directory if it doesn't exist
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
command = [
|
| 26 |
+
"yt-dlp",
|
| 27 |
+
"-f", "mp4", # Download in mp4 format
|
| 28 |
+
"-o", f"{save_path}/%(title)s.%(ext)s", # Save with video title and extension
|
| 29 |
+
url
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 33 |
+
|
| 34 |
+
if result.returncode == 0:
|
| 35 |
+
return f"Video downloaded successfully in MP4 format and saved in {save_path}."
|
| 36 |
+
else:
|
| 37 |
+
return f"An error occurred:\n{result.stderr}"
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"An unexpected error occurred: {e}"
|
| 40 |
+
|
| 41 |
+
# Function to open a folder selection dialog
|
| 42 |
+
def get_download_directory():
|
| 43 |
+
"""
|
| 44 |
+
Opens a folder selection dialog and returns the chosen directory.
|
| 45 |
+
"""
|
| 46 |
+
root = Tk()
|
| 47 |
+
root.withdraw() # Hide the main window
|
| 48 |
+
|
| 49 |
+
folder_selected = filedialog.askdirectory(title="Select the folder to save the video")
|
| 50 |
+
|
| 51 |
+
if folder_selected:
|
| 52 |
+
return folder_selected
|
| 53 |
+
else:
|
| 54 |
+
return None
|
| 55 |
+
|
| 56 |
+
# Gradio Interface Function
|
| 57 |
+
def download_video(url):
|
| 58 |
+
# Check if yt-dlp is installed
|
| 59 |
+
check_yt_dlp_installed()
|
| 60 |
+
|
| 61 |
+
# Get the folder to save the video
|
| 62 |
+
download_directory = get_download_directory()
|
| 63 |
+
|
| 64 |
+
if download_directory is None:
|
| 65 |
+
return "No folder selected. Please select a folder."
|
| 66 |
+
|
| 67 |
+
# Call the function to download the video and return the result
|
| 68 |
+
result = download_youtube_video_yt_dlp(url, download_directory)
|
| 69 |
+
return result
|
| 70 |
+
|
| 71 |
+
# Gradio Interface with instructions
|
| 72 |
+
interface = gr.Interface(
|
| 73 |
+
fn=download_video,
|
| 74 |
+
inputs=gr.Textbox(label="Enter YouTube URL"),
|
| 75 |
+
outputs=gr.Textbox(label="Download Status"),
|
| 76 |
+
live=True,
|
| 77 |
+
title="YouTube Video Downloader",
|
| 78 |
+
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.",
|
| 79 |
+
theme="compact",
|
| 80 |
+
allow_flagging="never"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Launch the interface
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
interface.launch()
|