Spaces:
Sleeping
Sleeping
File size: 1,854 Bytes
5bf6331 01f7b4a ab91ff7 |
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 |
%pip install yt-dlp gradio
import yt_dlp
import gradio as gr
import os
import shutil
# 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() |