Ytstock / app.py
Artificial-superintelligence's picture
Update app.py
c764b90 verified
raw
history blame
1.69 kB
import gradio as gr
import yt_dlp
import os
import time
# Function to download video
def download_video(url):
download_path = './downloads' # Folder to store downloads
if not os.path.exists(download_path):
os.makedirs(download_path)
ydl_opts = {
'outtmpl': download_path + '/%(title)s.%(ext)s',
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=False)
title = info_dict.get('title', None)
video_file = f"{download_path}/{title}.mp4"
ydl.download([url])
# Wait a moment to ensure the file is fully written
time.sleep(1) # Add a delay for the file to be available
# Return the download link
return f"Downloaded successfully: {title}", video_file
except Exception as e:
return f"Error: {e}", None
# Create Gradio interface
def gradio_interface():
with gr.Blocks() as demo:
gr.Markdown("# YouTube Video Downloader")
gr.Markdown("Enter the YouTube URL to download the video.")
# Input for YouTube URL
url_input = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video URL here...")
# Output display
output = gr.Textbox(label="Status")
video_link = gr.File(label="Download Link")
# Download button
download_btn = gr.Button("Download Video")
# Define the action on button click
download_btn.click(download_video, inputs=url_input, outputs=[output, video_link])
return demo
# Launch the interface
if __name__ == "__main__":
gradio_interface().launch()