File size: 1,691 Bytes
af28c41 a74c154 5099e75 9a2b388 5099e75 e7c6748 a74c154 5099e75 a74c154 e7c6748 a74c154 c764b90 e7c6748 c764b90 a74c154 e7c6748 5099e75 a74c154 e7c6748 5099e75 a74c154 e7c6748 a74c154 e7c6748 5099e75 a74c154 e7c6748 5099e75 a74c154 5099e75 a74c154 |
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 |
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()
|