Spaces:
Sleeping
Sleeping
import streamlit as st | |
import yt_dlp | |
# Function to get video info (formats, quality, etc.) | |
def get_video_info(url): | |
ydl_opts = { | |
'quiet': True, | |
'noplaylist': True, | |
} | |
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
info = ydl.extract_info(url, download=False) | |
return info | |
# Function to download video | |
def download_video(url, format): | |
ydl_opts = { | |
'format': format, | |
'outtmpl': '%(title)s.%(ext)s', # Name the file based on the video title | |
'noplaylist': True, | |
} | |
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
ydl.download([url]) | |
# Streamlit layout | |
st.title("Video Downloader App") | |
# User input for video URL | |
url = st.text_input('Enter Video URL') | |
if url: | |
try: | |
# Get video information (formats) | |
video_info = get_video_info(url) | |
formats = video_info.get('formats', []) | |
# Prepare list of available formats (e.g., 'best', 'mp4', 'webm') | |
format_options = [] | |
for f in formats: | |
# Ensure format has 'format_note' and 'ext' to avoid key errors | |
if 'format_note' in f and 'ext' in f and 'filesize' in f: | |
format_options.append(f"{f['format_note']} - {f['ext']} - {f['filesize']} bytes") | |
if format_options: | |
# User selects a format | |
selected_format = st.selectbox('Select Video Quality', format_options) | |
# Convert selected format to yt-dlp's format identifier | |
selected_format_id = formats[format_options.index(selected_format)]['format_id'] | |
if st.button('Download'): | |
with st.spinner('Downloading...'): | |
download_video(url, selected_format_id) | |
st.success('Download complete!') | |
else: | |
st.error("No formats available.") | |
except Exception as e: | |
st.error(f"Error fetching video info: {e}") | |