Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import yt_dlp
|
3 |
import os
|
@@ -40,7 +184,7 @@ def download_video(url, progress_bar, status_text):
|
|
40 |
'user_agent': random.choice(user_agents),
|
41 |
'referer': 'https://www.youtube.com/',
|
42 |
'http_chunk_size': random.randint(10000000, 15000000),
|
43 |
-
'retries':
|
44 |
'sleep_interval': 3,
|
45 |
'max_sleep_interval': 8,
|
46 |
'headers': {
|
@@ -63,7 +207,7 @@ def download_video(url, progress_bar, status_text):
|
|
63 |
|
64 |
ydl_opts['progress_hooks'] = [progress_hook]
|
65 |
|
66 |
-
for attempt in range(
|
67 |
try:
|
68 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
69 |
info = ydl.extract_info(url, download=True)
|
@@ -71,16 +215,22 @@ def download_video(url, progress_bar, status_text):
|
|
71 |
return filename
|
72 |
except yt_dlp.utils.ExtractorError as e:
|
73 |
if "Sign in to confirm you're not a bot" in str(e):
|
74 |
-
status_text.text(f"μ¬μλ μ€... ({attempt + 1}/
|
75 |
time.sleep(random.uniform(3, 5))
|
76 |
continue
|
77 |
-
|
|
|
|
|
|
|
78 |
except Exception as e:
|
79 |
-
if attempt <
|
80 |
-
status_text.text(f"μ¬μλ μ€... ({attempt + 1}/
|
81 |
time.sleep(2)
|
82 |
continue
|
83 |
-
|
|
|
|
|
|
|
84 |
|
85 |
except Exception as e:
|
86 |
st.error(f"μ€λ₯ λ°μ: {str(e)}")
|
@@ -137,4 +287,3 @@ if download_button:
|
|
137 |
if reset_button:
|
138 |
st.session_state.video_url = ""
|
139 |
st.rerun()
|
140 |
-
|
|
|
1 |
+
# import streamlit as st
|
2 |
+
# import yt_dlp
|
3 |
+
# import os
|
4 |
+
# from pathlib import Path
|
5 |
+
# import random
|
6 |
+
# import time
|
7 |
+
|
8 |
+
# # νμ΄μ§ μ€μ
|
9 |
+
# st.set_page_config(page_title="Simple YouTube Downloader", page_icon="πΊ")
|
10 |
+
# st.title("Simple YouTube Downloader πΊ")
|
11 |
+
|
12 |
+
# # μ€νμΌ μ€μ
|
13 |
+
# st.markdown("""
|
14 |
+
# <style>
|
15 |
+
# .stButton > button {
|
16 |
+
# width: 100%;
|
17 |
+
# height: 60px;
|
18 |
+
# }
|
19 |
+
# </style>
|
20 |
+
# """, unsafe_allow_html=True)
|
21 |
+
|
22 |
+
# # λ€μ΄λ‘λ ν΄λ μμ±
|
23 |
+
# output_dir = Path("downloads")
|
24 |
+
# output_dir.mkdir(exist_ok=True)
|
25 |
+
|
26 |
+
# def download_video(url, progress_bar, status_text):
|
27 |
+
# try:
|
28 |
+
# user_agents = [
|
29 |
+
# 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
30 |
+
# 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15',
|
31 |
+
# 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0'
|
32 |
+
# ]
|
33 |
+
|
34 |
+
# ydl_opts = {
|
35 |
+
# 'format': 'best',
|
36 |
+
# 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
37 |
+
# 'quiet': True,
|
38 |
+
# 'no_warnings': True,
|
39 |
+
# 'cookiefile': 'youtube.com_cookies.txt',
|
40 |
+
# 'user_agent': random.choice(user_agents),
|
41 |
+
# 'referer': 'https://www.youtube.com/',
|
42 |
+
# 'http_chunk_size': random.randint(10000000, 15000000),
|
43 |
+
# 'retries': 3,
|
44 |
+
# 'sleep_interval': 3,
|
45 |
+
# 'max_sleep_interval': 8,
|
46 |
+
# 'headers': {
|
47 |
+
# 'Accept-Language': 'en-US,en;q=0.9',
|
48 |
+
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
49 |
+
# },
|
50 |
+
# }
|
51 |
+
|
52 |
+
# def progress_hook(d):
|
53 |
+
# if d['status'] == 'downloading':
|
54 |
+
# try:
|
55 |
+
# progress = d['downloaded_bytes'] / d['total_bytes']
|
56 |
+
# progress_bar.progress(progress)
|
57 |
+
# status_text.text(f"λ€μ΄λ‘λ μ€: {progress:.1%}")
|
58 |
+
# except:
|
59 |
+
# status_text.text("λ€μ΄λ‘λ μ€...")
|
60 |
+
# elif d['status'] == 'finished':
|
61 |
+
# progress_bar.progress(1.0)
|
62 |
+
# status_text.text("μ²λ¦¬ μλ£!")
|
63 |
+
|
64 |
+
# ydl_opts['progress_hooks'] = [progress_hook]
|
65 |
+
|
66 |
+
# for attempt in range(3):
|
67 |
+
# try:
|
68 |
+
# with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
69 |
+
# info = ydl.extract_info(url, download=True)
|
70 |
+
# filename = ydl.prepare_filename(info)
|
71 |
+
# return filename
|
72 |
+
# except yt_dlp.utils.ExtractorError as e:
|
73 |
+
# if "Sign in to confirm you're not a bot" in str(e):
|
74 |
+
# status_text.text(f"μ¬μλ μ€... ({attempt + 1}/3)")
|
75 |
+
# time.sleep(random.uniform(3, 5))
|
76 |
+
# continue
|
77 |
+
# raise e
|
78 |
+
# except Exception as e:
|
79 |
+
# if attempt < 2:
|
80 |
+
# status_text.text(f"μ¬μλ μ€... ({attempt + 1}/3)")
|
81 |
+
# time.sleep(2)
|
82 |
+
# continue
|
83 |
+
# raise e
|
84 |
+
|
85 |
+
# except Exception as e:
|
86 |
+
# st.error(f"μ€λ₯ λ°μ: {str(e)}")
|
87 |
+
# return None
|
88 |
+
|
89 |
+
# # μΏ ν€ νμΌ μ²΄ν¬
|
90 |
+
# if not os.path.exists('youtube.com_cookies.txt'):
|
91 |
+
# st.warning("β οΈ 'youtube.com_cookies.txt' νμΌμ΄ νμν©λλ€. YouTubeμ λ‘κ·ΈμΈλ μΏ ν€ νμΌμ μ
λ‘λν΄μ£ΌμΈμ.")
|
92 |
+
# uploaded_file = st.file_uploader("μΏ ν€ νμΌ μ
λ‘λ", type=['txt'])
|
93 |
+
# if uploaded_file is not None:
|
94 |
+
# with open('youtube.com_cookies.txt', 'wb') as f:
|
95 |
+
# f.write(uploaded_file.getvalue())
|
96 |
+
# st.success("β
μΏ ν€ νμΌμ΄ μ
λ‘λλμμ΅λλ€!")
|
97 |
+
# st.rerun()
|
98 |
+
|
99 |
+
# # λ©μΈ UI
|
100 |
+
# if 'video_url' not in st.session_state:
|
101 |
+
# st.session_state.video_url = ""
|
102 |
+
|
103 |
+
# video_url = st.text_input("YouTube URL μ
λ ₯:",
|
104 |
+
# value=st.session_state.video_url,
|
105 |
+
# placeholder="https://www.youtube.com/watch?v=...")
|
106 |
+
|
107 |
+
# # λ²νΌμ κ°μ μ€μ λ°°μΉ
|
108 |
+
# col1, col2 = st.columns(2)
|
109 |
+
|
110 |
+
# with col1:
|
111 |
+
# download_button = st.button("λ€μ΄λ‘λ", type="primary", key="download_btn", use_container_width=True)
|
112 |
+
|
113 |
+
# with col2:
|
114 |
+
# reset_button = st.button("μ΄κΈ°ν", key="reset_btn", use_container_width=True)
|
115 |
+
|
116 |
+
# # λ€μ΄λ‘λ λ‘μ§
|
117 |
+
# if download_button:
|
118 |
+
# if video_url:
|
119 |
+
# progress_bar = st.progress(0)
|
120 |
+
# status_text = st.empty()
|
121 |
+
|
122 |
+
# downloaded_file = download_video(video_url, progress_bar, status_text)
|
123 |
+
|
124 |
+
# if downloaded_file and os.path.exists(downloaded_file):
|
125 |
+
# with open(downloaded_file, 'rb') as file:
|
126 |
+
# st.download_button(
|
127 |
+
# label="β¬οΈ νμΌ λ€μ΄λ‘λ",
|
128 |
+
# data=file,
|
129 |
+
# file_name=os.path.basename(downloaded_file),
|
130 |
+
# mime="video/mp4",
|
131 |
+
# use_container_width=True
|
132 |
+
# )
|
133 |
+
# else:
|
134 |
+
# st.warning("β οΈ YouTube URLμ μ
λ ₯ν΄μ£ΌμΈμ.")
|
135 |
+
|
136 |
+
# # μ΄κΈ°ν λ‘μ§
|
137 |
+
# if reset_button:
|
138 |
+
# st.session_state.video_url = ""
|
139 |
+
# st.rerun()
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
|
145 |
import streamlit as st
|
146 |
import yt_dlp
|
147 |
import os
|
|
|
184 |
'user_agent': random.choice(user_agents),
|
185 |
'referer': 'https://www.youtube.com/',
|
186 |
'http_chunk_size': random.randint(10000000, 15000000),
|
187 |
+
'retries': 5, # μ¬μλ νμλ₯Ό 5λ‘ λλ¦Ό
|
188 |
'sleep_interval': 3,
|
189 |
'max_sleep_interval': 8,
|
190 |
'headers': {
|
|
|
207 |
|
208 |
ydl_opts['progress_hooks'] = [progress_hook]
|
209 |
|
210 |
+
for attempt in range(5): # μ¬μλ νμλ₯Ό 5λ‘ μ€μ
|
211 |
try:
|
212 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
213 |
info = ydl.extract_info(url, download=True)
|
|
|
215 |
return filename
|
216 |
except yt_dlp.utils.ExtractorError as e:
|
217 |
if "Sign in to confirm you're not a bot" in str(e):
|
218 |
+
status_text.text(f"μ¬μλ μ€... ({attempt + 1}/5)")
|
219 |
time.sleep(random.uniform(3, 5))
|
220 |
continue
|
221 |
+
status_text.text(f"μ€λ₯ λ°μ: {str(e)}")
|
222 |
+
st.error(f"λ€μ΄λ‘λ μ€ν¨: {str(e)}") # μ€ν¨ λ©μμ§ νμ
|
223 |
+
st.info("λ¬Έμ κ° μ§μλ κ²½μ° URLμ΄ μ¬λ°λ₯Έμ§ νμΈνκ³ λ€μ μλνμΈμ.") # μΆκ° λ©λͺ¨
|
224 |
+
return None
|
225 |
except Exception as e:
|
226 |
+
if attempt < 4:
|
227 |
+
status_text.text(f"μ¬μλ μ€... ({attempt + 1}/5)")
|
228 |
time.sleep(2)
|
229 |
continue
|
230 |
+
status_text.text(f"μ€λ₯ λ°μ: {str(e)}")
|
231 |
+
st.error(f"λ€μ΄λ‘λ μ€ν¨: {str(e)}") # μ€ν¨ λ©μμ§ νμ
|
232 |
+
st.info("λ¬Έμ κ° μ§μλ κ²½μ° URLμ΄ μ¬λ°λ₯Έμ§ νμΈνκ³ λ€μ μλνμΈμ.") # μΆκ° λ©λͺ¨
|
233 |
+
return None
|
234 |
|
235 |
except Exception as e:
|
236 |
st.error(f"μ€λ₯ λ°μ: {str(e)}")
|
|
|
287 |
if reset_button:
|
288 |
st.session_state.video_url = ""
|
289 |
st.rerun()
|
|