Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +44 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pytube
|
| 3 |
+
|
| 4 |
+
def get_video_details(url):
|
| 5 |
+
"""Fetches video details using pytube, handling potential errors."""
|
| 6 |
+
try:
|
| 7 |
+
yt = pytube.YouTube(url)
|
| 8 |
+
title = yt.title
|
| 9 |
+
streams = yt.streams.filter(progressive=True) # Focus on progressive downloads
|
| 10 |
+
return title, streams
|
| 11 |
+
except pytube.exceptions.RegexMatchError:
|
| 12 |
+
st.error("Invalid YouTube URL. Please try again.")
|
| 13 |
+
return None, None
|
| 14 |
+
except pytube.exceptions.PytubeError as e:
|
| 15 |
+
st.error(f"An error occurred: {e}")
|
| 16 |
+
return None, None
|
| 17 |
+
|
| 18 |
+
def download_video(streams, selected_quality):
|
| 19 |
+
"""Downloads the video based on user selection, handling potential errors."""
|
| 20 |
+
try:
|
| 21 |
+
stream = streams.get_by_resolution(selected_quality)
|
| 22 |
+
stream.download()
|
| 23 |
+
st.success(f"Video downloaded successfully!")
|
| 24 |
+
except pytube.exceptions.PytubeError as e:
|
| 25 |
+
st.error(f"Download failed: {e}")
|
| 26 |
+
|
| 27 |
+
def main():
|
| 28 |
+
"""Builds the Streamlit app with user interface and download functionality."""
|
| 29 |
+
st.title("YouTube Video Downloader")
|
| 30 |
+
|
| 31 |
+
url = st.text_input("Enter YouTube Video URL:")
|
| 32 |
+
if url:
|
| 33 |
+
title, streams = get_video_details(url)
|
| 34 |
+
if title and streams:
|
| 35 |
+
st.subheader(f"Video Title: {title}")
|
| 36 |
+
|
| 37 |
+
quality_options = [stream.resolution for stream in streams if stream.progressive]
|
| 38 |
+
selected_quality = st.selectbox("Select Video Quality:", quality_options)
|
| 39 |
+
|
| 40 |
+
if st.button("Download Video"):
|
| 41 |
+
download_video(streams, selected_quality)
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pytube
|
| 2 |
+
streamlit
|