import streamlit as st import pytube def get_video_details(url): """Fetches video details using pytube, handling potential errors.""" try: yt = pytube.YouTube(url) title = yt.title streams = yt.streams.filter(progressive=True) # Focus on progressive downloads return title, streams except pytube.exceptions.RegexMatchError: st.error("Invalid YouTube URL. Please try again.") return None, None except pytube.exceptions.PytubeError as e: st.error(f"An error occurred: {e}") return None, None def download_video(streams, selected_quality): """Downloads the video based on user selection, handling potential errors.""" try: stream = streams.get_by_resolution(selected_quality) stream.download() st.success(f"Video downloaded successfully!") except pytube.exceptions.PytubeError as e: st.error(f"Download failed: {e}") def main(): """Builds the Streamlit app with user interface and download functionality.""" st.title("YouTube Video Downloader") url = st.text_input("Enter YouTube Video URL:") if url: title, streams = get_video_details(url) if title and streams: st.subheader(f"Video Title: {title}") quality_options = [stream.resolution for stream in streams if stream.progressive] selected_quality = st.selectbox("Select Video Quality:", quality_options) if st.button("Download Video"): download_video(streams, selected_quality) if __name__ == "__main__": main()