import gradio as gr import yt_dlp as youtube_dl import os def download_youtube_video(youtube_url): """Downloads a YouTube video using yt_dlp and returns the downloaded file path.""" try: ydl_opts = { 'format': 'best', 'outtmpl': '%(title)s.%(ext)s', } with youtube_dl.YoutubeDL(ydl_opts) as ydl: result = ydl.extract_info(youtube_url, download=True) video_title = result.get('title', None) video_ext = result.get('ext', None) downloaded_file = f"{video_title}.{video_ext}" return downloaded_file except Exception as e: print("An error occurred:", e) return None def app(video_link): video_path = download_youtube_video(video_link) if video_path: return video_path else: return "An error occurred during the download." with gr.Blocks() as interface: gr.Markdown("## YouTube Video Downloader") video_link = gr.Textbox(label="Enter YouTube link 🔗 To Download Video⬇️") download_button = gr.Button("Download") output = gr.File(label="Downloaded Video Path") download_button.click(fn=app, inputs=video_link, outputs=output) if __name__ == "__main__": interface.launch(debug=True)