Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,66 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from pytube import YouTube
|
| 3 |
-
import os
|
| 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 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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')
|