arnabbumba077 commited on
Commit
4185844
·
verified ·
1 Parent(s): 9430c4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -34
app.py CHANGED
@@ -1,44 +1,29 @@
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()
 
1
  import streamlit as st
2
+ from pytube import YouTube
3
 
4
+ def download_video(url, save_path):
 
5
  try:
6
+ yt = YouTube(url)
7
+ streams = yt.streams.filter(progressive=True, file_extension="mp4")
8
+ highest_res_stream = streams.get_highest_resolution()
9
+ highest_res_stream.download(output_path=save_path)
10
+ st.success("Video downloaded successfully!")
11
+ except Exception as e:
 
 
12
  st.error(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
 
13
 
14
  def main():
 
15
  st.title("YouTube Video Downloader")
16
+
17
+ # Input field for entering YouTube URL
18
+ video_url = st.text_input("Enter YouTube URL:")
19
+
20
+ # Button to trigger the download
21
+ if st.button("Download"):
22
+ if video_url.strip() == "":
23
+ st.warning("Please enter a valid YouTube URL.")
24
+ else:
25
+ # Download the video
26
+ download_video(video_url, "./downloads")
 
27
 
28
  if __name__ == "__main__":
29
  main()