File size: 1,594 Bytes
9430c4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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()