jpjp9292 commited on
Commit
69196c7
ยท
verified ยท
1 Parent(s): fd6962d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -28
app.py CHANGED
@@ -9,17 +9,22 @@ import time
9
  st.set_page_config(page_title="Simple YouTube Downloader", page_icon="๐Ÿ“บ")
10
  st.title("Simple YouTube Downloader ๐Ÿ“บ")
11
 
 
 
 
 
 
 
 
 
 
 
12
  # ๋‹ค์šด๋กœ๋“œ ํด๋” ์ƒ์„ฑ
13
  output_dir = Path("downloads")
14
  output_dir.mkdir(exist_ok=True)
15
 
16
- def reset_input():
17
- # session_state ๋Œ€์‹  ๋นˆ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
18
- return ""
19
-
20
  def download_video(url, progress_bar, status_text):
21
  try:
22
- # User Agent ๋ชฉ๋ก
23
  user_agents = [
24
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
25
  '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,7 +36,8 @@ def download_video(url, progress_bar, status_text):
31
  'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
32
  'quiet': True,
33
  'no_warnings': True,
34
- # ๋ด‡ ๊ฐ์ง€ ์šฐํšŒ ์„ค์ •
 
35
  'user_agent': random.choice(user_agents),
36
  'referer': 'https://www.youtube.com/',
37
  'http_chunk_size': random.randint(10000000, 15000000),
@@ -58,7 +64,6 @@ def download_video(url, progress_bar, status_text):
58
 
59
  ydl_opts['progress_hooks'] = [progress_hook]
60
 
61
- # ์žฌ์‹œ๋„ ๋กœ์ง ์ถ”๊ฐ€
62
  for attempt in range(3):
63
  try:
64
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
@@ -67,7 +72,7 @@ def download_video(url, progress_bar, status_text):
67
  return filename
68
  except yt_dlp.utils.ExtractorError as e:
69
  if "Sign in to confirm you're not a bot" in str(e):
70
- status_text.text(f"๋ด‡ ๊ฐ์ง€ ์šฐํšŒ ์ค‘... ({attempt + 1}/3)")
71
  time.sleep(random.uniform(3, 5))
72
  continue
73
  raise e
@@ -82,6 +87,16 @@ def download_video(url, progress_bar, status_text):
82
  st.error(f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
83
  return None
84
 
 
 
 
 
 
 
 
 
 
 
85
  # ๋ฉ”์ธ UI
86
  if 'video_url' not in st.session_state:
87
  st.session_state.video_url = ""
@@ -90,28 +105,36 @@ video_url = st.text_input("YouTube URL ์ž…๋ ฅ:",
90
  value=st.session_state.video_url,
91
  placeholder="https://www.youtube.com/watch?v=...")
92
 
 
93
  col1, col2 = st.columns(2)
94
 
95
  with col1:
96
- if st.button("๋‹ค์šด๋กœ๋“œ", type="primary"):
97
- if video_url:
98
- progress_bar = st.progress(0)
99
- status_text = st.empty()
100
-
101
- downloaded_file = download_video(video_url, progress_bar, status_text)
102
-
103
- if downloaded_file and os.path.exists(downloaded_file):
104
- with open(downloaded_file, 'rb') as file:
105
- st.download_button(
106
- label="โฌ‡๏ธ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ",
107
- data=file,
108
- file_name=os.path.basename(downloaded_file),
109
- mime="video/mp4"
110
- )
111
- else:
112
- st.warning("โš ๏ธ YouTube URL์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
113
 
114
  with col2:
115
- if st.button("์ดˆ๊ธฐํ™”"):
116
- st.session_state.video_url = ""
117
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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',
 
36
  'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
37
  'quiet': True,
38
  'no_warnings': True,
39
+ # ์ฟ ํ‚ค ํŒŒ์ผ ์„ค์ •
40
+ 'cookiefile': 'youtube.com_cookies.txt',
41
  'user_agent': random.choice(user_agents),
42
  'referer': 'https://www.youtube.com/',
43
  'http_chunk_size': random.randint(10000000, 15000000),
 
64
 
65
  ydl_opts['progress_hooks'] = [progress_hook]
66
 
 
67
  for attempt in range(3):
68
  try:
69
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
 
72
  return filename
73
  except yt_dlp.utils.ExtractorError as e:
74
  if "Sign in to confirm you're not a bot" in str(e):
75
+ status_text.text(f"์žฌ์‹œ๋„ ์ค‘... ({attempt + 1}/3)")
76
  time.sleep(random.uniform(3, 5))
77
  continue
78
  raise e
 
87
  st.error(f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
88
  return None
89
 
90
+ # ์ฟ ํ‚ค ํŒŒ์ผ ์ฒดํฌ
91
+ if not os.path.exists('youtube.com_cookies.txt'):
92
+ st.warning("โš ๏ธ 'youtube.com_cookies.txt' ํŒŒ์ผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. YouTube์— ๋กœ๊ทธ์ธ๋œ ์ฟ ํ‚ค ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”.")
93
+ uploaded_file = st.file_uploader("์ฟ ํ‚ค ํŒŒ์ผ ์—…๋กœ๋“œ", type=['txt'])
94
+ if uploaded_file is not None:
95
+ with open('youtube.com_cookies.txt', 'wb') as f:
96
+ f.write(uploaded_file.getvalue())
97
+ st.success("โœ… ์ฟ ํ‚ค ํŒŒ์ผ์ด ์—…๋กœ๋“œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!")
98
+ st.rerun()
99
+
100
  # ๋ฉ”์ธ UI
101
  if 'video_url' not in st.session_state:
102
  st.session_state.video_url = ""
 
105
  value=st.session_state.video_url,
106
  placeholder="https://www.youtube.com/watch?v=...")
107
 
108
+ # ๋ฒ„ํŠผ์„ ๊ฐ™์€ ์ค„์— ๋ฐฐ์น˜
109
  col1, col2 = st.columns(2)
110
 
111
  with col1:
112
+ download_button = st.button("๋‹ค์šด๋กœ๋“œ", type="primary", key="download_btn", use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
  with col2:
115
+ reset_button = st.button("์ดˆ๊ธฐํ™”", key="reset_btn", use_container_width=True)
116
+
117
+ # ๋‹ค์šด๋กœ๋“œ ๋กœ์ง
118
+ if download_button:
119
+ if video_url:
120
+ progress_bar = st.progress(0)
121
+ status_text = st.empty()
122
+
123
+ downloaded_file = download_video(video_url, progress_bar, status_text)
124
+
125
+ if downloaded_file and os.path.exists(downloaded_file):
126
+ with open(downloaded_file, 'rb') as file:
127
+ st.download_button(
128
+ label="โฌ‡๏ธ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ",
129
+ data=file,
130
+ file_name=os.path.basename(downloaded_file),
131
+ mime="video/mp4",
132
+ use_container_width=True
133
+ )
134
+ else:
135
+ st.warning("โš ๏ธ YouTube URL์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
136
+
137
+ # ์ดˆ๊ธฐํ™” ๋กœ์ง
138
+ if reset_button:
139
+ st.session_state.video_url = ""
140
+ st.rerun()