jpjp9292 commited on
Commit
d2685cf
โ€ข
1 Parent(s): 70f9375

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -7
app.py CHANGED
@@ -142,6 +142,154 @@
142
 
143
 
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  import streamlit as st
146
  import yt_dlp
147
  import os
@@ -184,7 +332,7 @@ def download_video(url, progress_bar, status_text):
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,7 +355,7 @@ def download_video(url, progress_bar, status_text):
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)
@@ -219,8 +367,8 @@ def download_video(url, progress_bar, status_text):
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:
@@ -228,8 +376,8 @@ def download_video(url, progress_bar, status_text):
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:
@@ -286,4 +434,5 @@ if download_button:
286
  # ์ดˆ๊ธฐํ™” ๋กœ์ง
287
  if reset_button:
288
  st.session_state.video_url = ""
289
- st.rerun()
 
 
142
 
143
 
144
 
145
+ # import streamlit as st
146
+ # import yt_dlp
147
+ # import os
148
+ # from pathlib import Path
149
+ # import random
150
+ # import time
151
+
152
+ # # ํŽ˜์ด์ง€ ์„ค์ •
153
+ # st.set_page_config(page_title="Simple YouTube Downloader", page_icon="๐Ÿ“บ")
154
+ # st.title("Simple YouTube Downloader ๐Ÿ“บ")
155
+
156
+ # # ์Šคํƒ€์ผ ์„ค์ •
157
+ # st.markdown("""
158
+ # <style>
159
+ # .stButton > button {
160
+ # width: 100%;
161
+ # height: 60px;
162
+ # }
163
+ # </style>
164
+ # """, unsafe_allow_html=True)
165
+
166
+ # # ๋‹ค์šด๋กœ๋“œ ํด๋” ์ƒ์„ฑ
167
+ # output_dir = Path("downloads")
168
+ # output_dir.mkdir(exist_ok=True)
169
+
170
+ # def download_video(url, progress_bar, status_text):
171
+ # try:
172
+ # user_agents = [
173
+ # 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
174
+ # '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',
175
+ # 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0'
176
+ # ]
177
+
178
+ # ydl_opts = {
179
+ # 'format': 'best',
180
+ # 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
181
+ # 'quiet': True,
182
+ # 'no_warnings': True,
183
+ # 'cookiefile': 'youtube.com_cookies.txt',
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': {
191
+ # 'Accept-Language': 'en-US,en;q=0.9',
192
+ # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
193
+ # },
194
+ # }
195
+
196
+ # def progress_hook(d):
197
+ # if d['status'] == 'downloading':
198
+ # try:
199
+ # progress = d['downloaded_bytes'] / d['total_bytes']
200
+ # progress_bar.progress(progress)
201
+ # status_text.text(f"๋‹ค์šด๋กœ๋“œ ์ค‘: {progress:.1%}")
202
+ # except:
203
+ # status_text.text("๋‹ค์šด๋กœ๋“œ ์ค‘...")
204
+ # elif d['status'] == 'finished':
205
+ # progress_bar.progress(1.0)
206
+ # status_text.text("์ฒ˜๋ฆฌ ์™„๋ฃŒ!")
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)
214
+ # filename = ydl.prepare_filename(info)
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)}")
237
+ # return None
238
+
239
+ # # ์ฟ ํ‚ค ํŒŒ์ผ ์ฒดํฌ
240
+ # if not os.path.exists('youtube.com_cookies.txt'):
241
+ # st.warning("โš ๏ธ 'youtube.com_cookies.txt' ํŒŒ์ผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. YouTube์— ๋กœ๊ทธ์ธ๋œ ์ฟ ํ‚ค ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”.")
242
+ # uploaded_file = st.file_uploader("์ฟ ํ‚ค ํŒŒ์ผ ์—…๋กœ๋“œ", type=['txt'])
243
+ # if uploaded_file is not None:
244
+ # with open('youtube.com_cookies.txt', 'wb') as f:
245
+ # f.write(uploaded_file.getvalue())
246
+ # st.success("โœ… ์ฟ ํ‚ค ํŒŒ์ผ์ด ์—…๋กœ๋“œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!")
247
+ # st.rerun()
248
+
249
+ # # ๋ฉ”์ธ UI
250
+ # if 'video_url' not in st.session_state:
251
+ # st.session_state.video_url = ""
252
+
253
+ # video_url = st.text_input("YouTube URL ์ž…๋ ฅ:",
254
+ # value=st.session_state.video_url,
255
+ # placeholder="https://www.youtube.com/watch?v=...")
256
+
257
+ # # ๋ฒ„ํŠผ์„ ๊ฐ™์€ ์ค„์— ๋ฐฐ์น˜
258
+ # col1, col2 = st.columns(2)
259
+
260
+ # with col1:
261
+ # download_button = st.button("๋‹ค์šด๋กœ๋“œ", type="primary", key="download_btn", use_container_width=True)
262
+
263
+ # with col2:
264
+ # reset_button = st.button("์ดˆ๊ธฐํ™”", key="reset_btn", use_container_width=True)
265
+
266
+ # # ๋‹ค์šด๋กœ๋“œ ๋กœ์ง
267
+ # if download_button:
268
+ # if video_url:
269
+ # progress_bar = st.progress(0)
270
+ # status_text = st.empty()
271
+
272
+ # downloaded_file = download_video(video_url, progress_bar, status_text)
273
+
274
+ # if downloaded_file and os.path.exists(downloaded_file):
275
+ # with open(downloaded_file, 'rb') as file:
276
+ # st.download_button(
277
+ # label="โฌ‡๏ธ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ",
278
+ # data=file,
279
+ # file_name=os.path.basename(downloaded_file),
280
+ # mime="video/mp4",
281
+ # use_container_width=True
282
+ # )
283
+ # else:
284
+ # st.warning("โš ๏ธ YouTube URL์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
285
+
286
+ # # ์ดˆ๊ธฐํ™” ๋กœ์ง
287
+ # if reset_button:
288
+ # st.session_state.video_url = ""
289
+ # st.rerun()
290
+
291
+
292
+
293
  import streamlit as st
294
  import yt_dlp
295
  import os
 
332
  'user_agent': random.choice(user_agents),
333
  'referer': 'https://www.youtube.com/',
334
  'http_chunk_size': random.randint(10000000, 15000000),
335
+ 'retries': 5,
336
  'sleep_interval': 3,
337
  'max_sleep_interval': 8,
338
  'headers': {
 
355
 
356
  ydl_opts['progress_hooks'] = [progress_hook]
357
 
358
+ for attempt in range(5):
359
  try:
360
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
361
  info = ydl.extract_info(url, download=True)
 
367
  time.sleep(random.uniform(3, 5))
368
  continue
369
  status_text.text(f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
370
+ st.error(f"๋‹ค์šด๋กœ๋“œ ์‹คํŒจ: {str(e)}")
371
+ st.info("๋ฌธ์ œ๊ฐ€ ์ง€์†๋  ๊ฒฝ์šฐ URL์ด ์˜ฌ๋ฐ”๋ฅธ์ง€ ํ™•์ธํ•˜๊ณ  ๋‹ค์‹œ ์‹œ๋„ํ•˜์„ธ์š”.")
372
  return None
373
  except Exception as e:
374
  if attempt < 4:
 
376
  time.sleep(2)
377
  continue
378
  status_text.text(f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
379
+ st.error(f"๋‹ค์šด๋กœ๋“œ ์‹คํŒจ: {str(e)}")
380
+ st.info("๋ฌธ์ œ๊ฐ€ ์ง€์†๋  ๊ฒฝ์šฐ URL์ด ์˜ฌ๋ฐ”๋ฅธ์ง€ ํ™•์ธํ•˜๊ณ  ๋‹ค์‹œ ์‹œ๋„ํ•˜์„ธ์š”.")
381
  return None
382
 
383
  except Exception as e:
 
434
  # ์ดˆ๊ธฐํ™” ๋กœ์ง
435
  if reset_button:
436
  st.session_state.video_url = ""
437
+ video_url = "" # ์ž…๋ ฅ๋ž€๋„ ๋น„์›Œ์คŒ
438
+ st.experimental_rerun() # ์ƒˆ๋กœ๊ณ ์นจ์„ ํ†ตํ•ด ์ƒํƒœ๋ฅผ ์ดˆ๊ธฐํ™”