Ramzan0553's picture
Create app.py
304eb79 verified
import yt_dlp
import gradio as gr
import os
# βœ… Function to download YouTube Shorts or Video
def download_youtube_video(url):
output_filename = "downloads/%(title)s.%(ext)s" # Save file inside "downloads" folder
# Ensure the "downloads" folder exists
if not os.path.exists("downloads"):
os.makedirs("downloads")
# yt-dlp options
ydl_opts = {
"format": "bestvideo+bestaudio/best", # Download best quality
"outtmpl": output_filename, # Save inside "downloads" folder
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info_dict) # Get the file name
# Check if file exists
if os.path.exists(filename):
return filename # Return file for Gradio download
else:
return "Download failed."
except Exception as e:
return f"Error: {e}"
# βœ… Gradio Interface
iface = gr.Interface(
fn=download_youtube_video,
inputs=gr.Textbox(label="Enter YouTube Link"),
outputs=gr.File(label="Download Your Video"),
title="YouTube Shorts Downloader",
description="Paste a YouTube link and download the video.",
)
# βœ… Launch the Gradio app
iface.launch()