lcjln commited on
Commit
e19df97
ยท
verified ยท
1 Parent(s): e541197

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -49
app.py CHANGED
@@ -1,49 +1,66 @@
1
- import streamlit as st
2
- from pytube import YouTube
3
- import os
4
-
5
- # Streamlit ์ œ๋ชฉ
6
- st.title('YouTube Video Downloader')
7
-
8
- # ์œ ์ €๋กœ๋ถ€ํ„ฐ ์œ ํŠœ๋ธŒ ๋งํฌ ์ž…๋ ฅ๋ฐ›๊ธฐ
9
- youtube_url = st.text_input('Enter YouTube video URL')
10
-
11
- if youtube_url:
12
- try:
13
- # YouTube ๊ฐ์ฒด ์ƒ์„ฑ
14
- yt = YouTube(youtube_url)
15
-
16
- # ์ธ๋„ค์ผ ์ด๋ฏธ์ง€ ํ‘œ์‹œ
17
- thumbnail_url = yt.thumbnail_url
18
- st.image(thumbnail_url, caption='Video Thumbnail', use_column_width=True)
19
-
20
- # ํ•ด์ƒ๋„ ์„ ํƒ
21
- resolution = st.selectbox('Select resolution', ['1080p', '720p'])
22
-
23
- # ์„ ํƒ๋œ ํ•ด์ƒ๋„์˜ ๋น„๋””์˜ค ์ŠคํŠธ๋ฆผ ์„ ํƒ
24
- stream = yt.streams.filter(res=resolution, progressive=True, file_extension='mp4').first()
25
-
26
- # 1080p๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด 720p ์ŠคํŠธ๋ฆผ ์„ ํƒ
27
- if resolution == '1080p' and stream is None:
28
- st.warning("1080p๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 720p๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.")
29
- stream = yt.streams.filter(res='720p', progressive=True, file_extension='mp4').first()
30
-
31
- if stream is None:
32
- st.error(f"{resolution} ํ•ด์ƒ๋„์˜ ์ŠคํŠธ๋ฆผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
33
- else:
34
- # ๋‹ค์šด๋กœ๋“œ ๋ฒ„ํŠผ
35
- if st.button('Download Video'):
36
- download_path = stream.download()
37
- st.success('Video downloaded successfully!')
38
- st.video(download_path)
39
-
40
- # ๋‹ค์šด๋กœ๋“œ๋œ ํŒŒ์ผ ์ œ๊ณต (optional)
41
- with open(download_path, 'rb') as file:
42
- btn = st.download_button(
43
- label="Download Video File",
44
- data=file,
45
- file_name=os.path.basename(download_path),
46
- mime='video/mp4'
47
- )
48
- except Exception as e:
49
- st.error(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pytube import YouTube
3
+ import os
4
+ import time
5
+
6
+ # Streamlit ์ œ๋ชฉ
7
+ st.title('YouTube Video Downloader')
8
+
9
+ # ์œ ์ €๋กœ๋ถ€ํ„ฐ ์œ ํŠœ๋ธŒ ๋งํฌ ์ž…๋ ฅ๋ฐ›๊ธฐ
10
+ youtube_url = st.text_input('Enter YouTube video URL')
11
+
12
+ # "ํ™•์ธ" ๋ฒ„ํŠผ ์ถ”๊ฐ€
13
+ if st.button('ํ™•์ธ'):
14
+ if youtube_url:
15
+ try:
16
+ yt = YouTube(youtube_url)
17
+ # ์ธ๋„ค์ผ ์ด๋ฏธ์ง€ ํ‘œ์‹œ
18
+ thumbnail_url = yt.thumbnail_url
19
+ st.image(thumbnail_url, caption='Video Thumbnail', use_column_width=True)
20
+
21
+ # ํ•ด์ƒ๋„ ์„ ํƒ
22
+ resolution = st.selectbox('Select resolution', ['1080p', '1080p60', '1080pHD', '720p'])
23
+
24
+ def fetch_video_stream(youtube_url, resolution):
25
+ yt = YouTube(youtube_url)
26
+
27
+ # ๋”œ๋ ˆ์ด ์ถ”๊ฐ€
28
+ time.sleep(2) # 2์ดˆ ๋”œ๋ ˆ์ด
29
+
30
+ # ์„ ํƒ๋œ ํ•ด์ƒ๋„์˜ ๋น„๋””์˜ค ์ŠคํŠธ๋ฆผ ์„ ํƒ
31
+ stream = yt.streams.filter(res=resolution.replace('pHD', 'p'), progressive=True, file_extension='mp4').first()
32
+
33
+ # 1080p ๋ณ€ํ˜•๋“ค์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด 720p ์ŠคํŠธ๋ฆผ ์„ ํƒ
34
+ if '1080p' in resolution and stream is None:
35
+ st.warning(f"{resolution}๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 720p๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.")
36
+ stream = yt.streams.filter(res='720p', progressive=True, file_extension='mp4').first()
37
+
38
+ return yt, stream
39
+
40
+ # "์ถ”์ถœ" ๋ฒ„ํŠผ ์ถ”๊ฐ€
41
+ if st.button('์ถ”์ถœ'):
42
+ yt, stream = fetch_video_stream(youtube_url, resolution)
43
+
44
+ if stream is None:
45
+ st.error(f"{resolution} ํ•ด์ƒ๋„์˜ ์ŠคํŠธ๋ฆผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
46
+ else:
47
+ # ๋‹ค์šด๋กœ๋“œ
48
+ download_path = stream.download()
49
+ st.success('Video downloaded successfully!')
50
+ st.video(download_path)
51
+
52
+ # ๋‹ค์šด๋กœ๋“œ๋œ ํŒŒ์ผ ์ œ๊ณต (optional)
53
+ with open(download_path, 'rb') as file:
54
+ btn = st.download_button(
55
+ label="Download Video File",
56
+ data=file,
57
+ file_name=os.path.basename(download_path),
58
+ mime='video/mp4'
59
+ )
60
+ except Exception as e:
61
+ if '429' in str(e):
62
+ st.error("Too many requests. Please wait a moment and try again.")
63
+ else:
64
+ st.error(f"An error occurred: {e}")
65
+ else:
66
+ st.warning('Please enter a valid YouTube video URL')