KristofGaming39's picture
Update app.py
1d55815 verified
import yt_dlp
import gradio as gr
import os
# Function to download YouTube video
def download_video(url):
try:
# Temporary file path to save the downloaded video
download_folder = 'downloads'
if not os.path.exists(download_folder):
os.makedirs(download_folder)
# Setting the options for yt-dlp
ydl_opts = {
'format': 'best', # best available quality
'noplaylist': True, # avoid downloading playlists
'outtmpl': os.path.join(download_folder, '%(title)s.%(ext)s'), # save in 'downloads' folder
}
# Initialize yt-dlp
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
# Download the video
info_dict = ydl.extract_info(url, download=True)
video_title = info_dict.get('title', 'downloaded_video')
video_file_path = os.path.join(download_folder, f"{video_title}.mp4")
# Return the path to the downloaded video so Gradio can make it available for download
return video_file_path
except Exception as e:
# In case of error
return f"An error occurred: {e}"
# Create Gradio interface
def create_interface():
# Interface Layout
interface = gr.Interface(
fn=download_video, # Function to call on submit
inputs=gr.Textbox(label="YouTube Video URL", placeholder="Enter the video URL here", lines=1),
outputs=gr.File(label="Download Your Video"), # Provide a download link
title="YouTube Video Downloader", # Title of the Interface
description="Enter a YouTube video URL to download it as an MP4 with audio included.",
theme="compact", # Make it compact and clean
)
return interface
# Run Gradio Interface
interface = create_interface()
interface.launch()