|
%pip install yt-dlp gradio |
|
import yt_dlp |
|
import gradio as gr |
|
import os |
|
import shutil |
|
|
|
|
|
def download_video(url): |
|
try: |
|
|
|
download_folder = 'downloads' |
|
if not os.path.exists(download_folder): |
|
os.makedirs(download_folder) |
|
|
|
|
|
ydl_opts = { |
|
'format': 'best', |
|
'noplaylist': True, |
|
'outtmpl': os.path.join(download_folder, '%(title)s.%(ext)s'), |
|
} |
|
|
|
|
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl: |
|
|
|
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 video_file_path |
|
|
|
except Exception as e: |
|
|
|
return f"An error occurred: {e}" |
|
|
|
|
|
def create_interface(): |
|
|
|
interface = gr.Interface( |
|
fn=download_video, |
|
inputs=gr.Textbox(label="YouTube Video URL", placeholder="Enter the video URL here", lines=1), |
|
outputs=gr.File(label="Download Your Video"), |
|
title="YouTube Video Downloader", |
|
description="Enter a YouTube video URL to download it as an MP4 with audio included.", |
|
theme="compact", |
|
) |
|
|
|
return interface |
|
|
|
|
|
interface = create_interface() |
|
interface.launch() |