aiqcamp commited on
Commit
9531489
Β·
verified Β·
1 Parent(s): decee26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -18
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  from pytube import YouTube
 
3
 
4
  class YouTubeDownloader:
5
  @staticmethod
@@ -8,27 +9,38 @@ class YouTubeDownloader:
8
  url = st.text_input("Enter YouTube URL to download:")
9
  if url:
10
  YouTubeDownloader.validate_url(url)
11
- with st.expander("preview video"):
12
- st.video(url)
13
- if st.button("Download"):
14
- YouTubeDownloader.cleanup()
15
- file_ = YouTubeDownloader.download_video(url)
16
- st.video(file_)
17
- YouTubeDownloader.helper_message()
 
 
 
 
18
  st.markdown("예) https://www.youtube.com/watch?v=hXpNj1ChxRI ")
19
 
20
-
21
  @staticmethod
22
  def download_video(url):
23
- with st.spinner("Downloading..."):
24
- local_file = (
25
- YouTube(url)
26
- .streams.filter(progressive=True, file_extension="mp4")
27
- .first()
28
- .download()
29
- )
30
- st.success("Downloaded")
31
- return local_file
 
 
 
 
 
 
 
 
32
 
33
  @staticmethod
34
  def validate_url(url):
@@ -53,6 +65,5 @@ class YouTubeDownloader:
53
  "μ˜μƒ ν™”λ©΄ 우츑 ν•˜λ‹¨ ... λ²„νŠΌμ„ ν΄λ¦­ν•˜λ©΄ λ‹€μš΄λ‘œλ“œ ν•  수 μžˆμŠ΅λ‹ˆλ‹€."
54
  )
55
 
56
-
57
  if __name__ == "__main__":
58
  YouTubeDownloader.run()
 
1
  import streamlit as st
2
  from pytube import YouTube
3
+ from pytube.exceptions import VideoUnavailable
4
 
5
  class YouTubeDownloader:
6
  @staticmethod
 
9
  url = st.text_input("Enter YouTube URL to download:")
10
  if url:
11
  YouTubeDownloader.validate_url(url)
12
+ try:
13
+ with st.expander("preview video"):
14
+ st.video(url)
15
+ if st.button("Download"):
16
+ YouTubeDownloader.cleanup()
17
+ file_ = YouTubeDownloader.download_video(url)
18
+ if file_: # file_ 이 None이 아닐 λ•Œλ§Œ λΉ„λ””μ˜€ ν‘œμ‹œ
19
+ st.video(file_)
20
+ YouTubeDownloader.helper_message()
21
+ except Exception as e:
22
+ st.error(f"Error: {str(e)}")
23
  st.markdown("예) https://www.youtube.com/watch?v=hXpNj1ChxRI ")
24
 
 
25
  @staticmethod
26
  def download_video(url):
27
+ try:
28
+ with st.spinner("Downloading..."):
29
+ yt = YouTube(url)
30
+ stream = yt.streams.filter(progressive=True, file_extension="mp4").first()
31
+ if stream:
32
+ local_file = stream.download()
33
+ st.success("Downloaded")
34
+ return local_file
35
+ else:
36
+ st.error("No suitable stream found for this video")
37
+ return None
38
+ except VideoUnavailable:
39
+ st.error("This video is unavailable")
40
+ return None
41
+ except Exception as e:
42
+ st.error(f"An error occurred: {str(e)}")
43
+ return None
44
 
45
  @staticmethod
46
  def validate_url(url):
 
65
  "μ˜μƒ ν™”λ©΄ 우츑 ν•˜λ‹¨ ... λ²„νŠΌμ„ ν΄λ¦­ν•˜λ©΄ λ‹€μš΄λ‘œλ“œ ν•  수 μžˆμŠ΅λ‹ˆλ‹€."
66
  )
67
 
 
68
  if __name__ == "__main__":
69
  YouTubeDownloader.run()