Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,304 +1,3 @@
|
|
1 |
-
# import streamlit as st
|
2 |
-
# import yt_dlp
|
3 |
-
# import os
|
4 |
-
# from pathlib import Path
|
5 |
-
|
6 |
-
# # Set page config
|
7 |
-
# st.set_page_config(page_title="YouTube Video Downloader", page_icon="📺")
|
8 |
-
|
9 |
-
# # Set the title of the app
|
10 |
-
# st.title("YouTube Video Downloader 📺")
|
11 |
-
|
12 |
-
# # Create output directory if it doesn't exist
|
13 |
-
# output_dir = Path("downloads")
|
14 |
-
# output_dir.mkdir(exist_ok=True)
|
15 |
-
|
16 |
-
# # Input field for YouTube video URL
|
17 |
-
# video_url = st.text_input("Enter YouTube Video URL:", placeholder="https://www.youtube.com/watch?v=...")
|
18 |
-
|
19 |
-
# # Format selection
|
20 |
-
# format_option = st.selectbox(
|
21 |
-
# "Select Format:",
|
22 |
-
# options=[
|
23 |
-
# "Best Quality (Video + Audio)",
|
24 |
-
# "Audio Only (Best Quality)",
|
25 |
-
# "720p",
|
26 |
-
# "480p",
|
27 |
-
# "360p"
|
28 |
-
# ]
|
29 |
-
# )
|
30 |
-
|
31 |
-
# def get_format_options(selection):
|
32 |
-
# base_options = {
|
33 |
-
# 'format': 'best',
|
34 |
-
# 'merge_output_format': 'mp4',
|
35 |
-
# }
|
36 |
-
|
37 |
-
# if selection == "Best Quality (Video + Audio)":
|
38 |
-
# return base_options
|
39 |
-
# elif selection == "Audio Only (Best Quality)":
|
40 |
-
# return {
|
41 |
-
# 'format': 'bestaudio/best',
|
42 |
-
# 'postprocessors': [{
|
43 |
-
# 'key': 'FFmpegExtractAudio',
|
44 |
-
# 'preferredcodec': 'mp3',
|
45 |
-
# }]
|
46 |
-
# }
|
47 |
-
# elif selection == "720p":
|
48 |
-
# base_options['format'] = 'best[height<=720]'
|
49 |
-
# return base_options
|
50 |
-
# elif selection == "480p":
|
51 |
-
# base_options['format'] = 'best[height<=480]'
|
52 |
-
# return base_options
|
53 |
-
# elif selection == "360p":
|
54 |
-
# base_options['format'] = 'best[height<=360]'
|
55 |
-
# return base_options
|
56 |
-
|
57 |
-
# return base_options
|
58 |
-
|
59 |
-
# def validate_url(url):
|
60 |
-
# if not url:
|
61 |
-
# return False
|
62 |
-
# return 'youtube.com' in url or 'youtu.be' in url
|
63 |
-
|
64 |
-
# def download_video(url, format_selection):
|
65 |
-
# if not validate_url(url):
|
66 |
-
# st.error("Please enter a valid YouTube URL")
|
67 |
-
# return None
|
68 |
-
|
69 |
-
# try:
|
70 |
-
# format_opts = get_format_options(format_selection)
|
71 |
-
|
72 |
-
# ydl_opts = {
|
73 |
-
# 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
74 |
-
# 'quiet': True,
|
75 |
-
# 'no_warnings': True,
|
76 |
-
# 'nocheckcertificate': True,
|
77 |
-
# 'ignoreerrors': False, # Changed to False to catch errors
|
78 |
-
# 'no_color': True,
|
79 |
-
# 'noprogress': True,
|
80 |
-
# 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
81 |
-
# 'referer': 'https://www.youtube.com/',
|
82 |
-
# 'http-chunk-size': '10485760',
|
83 |
-
# 'extract_flat': True, # Only extract video metadata first
|
84 |
-
# }
|
85 |
-
|
86 |
-
# # Update options with format-specific settings
|
87 |
-
# ydl_opts.update(format_opts)
|
88 |
-
|
89 |
-
# # Progress elements
|
90 |
-
# progress_bar = st.progress(0)
|
91 |
-
# status_text = st.empty()
|
92 |
-
|
93 |
-
# def progress_hook(d):
|
94 |
-
# if d['status'] == 'downloading':
|
95 |
-
# try:
|
96 |
-
# progress = d['downloaded_bytes'] / d['total_bytes']
|
97 |
-
# progress_bar.progress(progress)
|
98 |
-
# status_text.text(f"Downloading: {progress:.1%}")
|
99 |
-
# except:
|
100 |
-
# status_text.text("Downloading... (size unknown)")
|
101 |
-
# elif d['status'] == 'finished':
|
102 |
-
# progress_bar.progress(1.0)
|
103 |
-
# status_text.text("Processing...")
|
104 |
-
|
105 |
-
# ydl_opts['progress_hooks'] = [progress_hook]
|
106 |
-
|
107 |
-
# # First, try to extract info without downloading
|
108 |
-
# with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
109 |
-
# try:
|
110 |
-
# info = ydl.extract_info(url, download=False)
|
111 |
-
# if info is None:
|
112 |
-
# st.error("Could not fetch video information. The video might be private or age-restricted.")
|
113 |
-
# return None
|
114 |
-
|
115 |
-
# # If info extraction successful, proceed with download
|
116 |
-
# ydl_opts['extract_flat'] = False # Now enable full extraction
|
117 |
-
# with yt_dlp.YoutubeDL(ydl_opts) as ydl_download:
|
118 |
-
# filename = ydl_download.prepare_filename(info)
|
119 |
-
# ydl_download.download([url])
|
120 |
-
|
121 |
-
# # Check if file exists and has size > 0
|
122 |
-
# if os.path.exists(filename) and os.path.getsize(filename) > 0:
|
123 |
-
# return filename
|
124 |
-
# else:
|
125 |
-
# st.error("Download completed but file appears to be empty or missing.")
|
126 |
-
# return None
|
127 |
-
|
128 |
-
# except yt_dlp.utils.DownloadError as e:
|
129 |
-
# st.error(f"Download Error: {str(e)}")
|
130 |
-
# return None
|
131 |
-
# except Exception as e:
|
132 |
-
# st.error(f"An unexpected error occurred: {str(e)}")
|
133 |
-
# return None
|
134 |
-
|
135 |
-
# except Exception as e:
|
136 |
-
# st.error(f"An error occurred during setup: {str(e)}")
|
137 |
-
# return None
|
138 |
-
|
139 |
-
# # Download button
|
140 |
-
# if st.button("Download"):
|
141 |
-
# if video_url:
|
142 |
-
# try:
|
143 |
-
# with st.spinner("Preparing download..."):
|
144 |
-
# downloaded_file_path = download_video(video_url, format_option)
|
145 |
-
|
146 |
-
# if downloaded_file_path and os.path.exists(downloaded_file_path):
|
147 |
-
# try:
|
148 |
-
# with open(downloaded_file_path, 'rb') as file:
|
149 |
-
# file_data = file.read()
|
150 |
-
# if len(file_data) > 0:
|
151 |
-
# st.download_button(
|
152 |
-
# label="Click here to download",
|
153 |
-
# data=file_data,
|
154 |
-
# file_name=os.path.basename(downloaded_file_path),
|
155 |
-
# mime="application/octet-stream"
|
156 |
-
# )
|
157 |
-
# st.success("✅ Download ready!")
|
158 |
-
# else:
|
159 |
-
# st.error("❌ Downloaded file is empty.")
|
160 |
-
# except IOError as e:
|
161 |
-
# st.error(f"❌ Error reading file: {str(e)}")
|
162 |
-
# else:
|
163 |
-
# st.error("❌ Download failed. Please try again with a different video or format.")
|
164 |
-
# except Exception as e:
|
165 |
-
# st.error(f"❌ An unexpected error occurred: {str(e)}")
|
166 |
-
# else:
|
167 |
-
# st.warning("⚠️ Please enter a valid YouTube URL.")
|
168 |
-
|
169 |
-
# # Add help section
|
170 |
-
# with st.expander("ℹ️ Help"):
|
171 |
-
# st.markdown("""
|
172 |
-
# **How to use:**
|
173 |
-
# 1. Paste a YouTube video URL in the input field
|
174 |
-
# 2. Select your preferred format
|
175 |
-
# 3. Click the Download button
|
176 |
-
# 4. Wait for the download to complete
|
177 |
-
# 5. Click the download button that appears
|
178 |
-
|
179 |
-
# **Troubleshooting:**
|
180 |
-
# - If download fails, try a different format
|
181 |
-
# - Some videos might be restricted or private
|
182 |
-
# - Make sure the URL is from YouTube
|
183 |
-
# """)
|
184 |
-
|
185 |
-
# # Clean up old files
|
186 |
-
# def cleanup_old_files():
|
187 |
-
# try:
|
188 |
-
# for file in output_dir.glob('*'):
|
189 |
-
# if file.is_file():
|
190 |
-
# file.unlink()
|
191 |
-
# except Exception as e:
|
192 |
-
# st.warning(f"Could not clean up old files: {str(e)}")
|
193 |
-
|
194 |
-
# # Run cleanup when the app starts
|
195 |
-
# cleanup_old_files()
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
# import streamlit as st
|
201 |
-
# import yt_dlp
|
202 |
-
# import os
|
203 |
-
# from pathlib import Path
|
204 |
-
|
205 |
-
# # Set page config
|
206 |
-
# st.set_page_config(page_title="YouTube Video Downloader", page_icon="📺")
|
207 |
-
|
208 |
-
# # Set the title of the app
|
209 |
-
# st.title("YouTube Video Downloader 📺")
|
210 |
-
|
211 |
-
# # Create output directory if it doesn't exist
|
212 |
-
# output_dir = Path("downloads")
|
213 |
-
# output_dir.mkdir(exist_ok=True)
|
214 |
-
|
215 |
-
# # Input field for YouTube video URL
|
216 |
-
# video_url = st.text_input("Enter YouTube Video URL:", placeholder="https://www.youtube.com/watch?v=...")
|
217 |
-
|
218 |
-
# # Function to download video
|
219 |
-
# def download_video(url):
|
220 |
-
# try:
|
221 |
-
# ydl_opts = {
|
222 |
-
# 'format': 'bestvideo+bestaudio/best',
|
223 |
-
# 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
224 |
-
# 'merge_output_format': 'webm',
|
225 |
-
# # Add these options to mimic browser behavior
|
226 |
-
# 'quiet': True,
|
227 |
-
# 'no_warnings': True,
|
228 |
-
# 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
229 |
-
# 'referer': 'https://www.youtube.com/',
|
230 |
-
# 'cookiefile': 'youtube.com_cookies.txt', # 쿠키 파일 사용
|
231 |
-
# 'socket_timeout': 30,
|
232 |
-
# 'http_chunk_size': 10485760, # 10MB
|
233 |
-
# }
|
234 |
-
|
235 |
-
# # Progress placeholder
|
236 |
-
# progress_bar = st.progress(0)
|
237 |
-
# status_text = st.empty()
|
238 |
-
|
239 |
-
# def progress_hook(d):
|
240 |
-
# if d['status'] == 'downloading':
|
241 |
-
# try:
|
242 |
-
# progress = d['downloaded_bytes'] / d['total_bytes']
|
243 |
-
# progress_bar.progress(progress)
|
244 |
-
# status_text.text(f"Downloading: {progress:.1%}")
|
245 |
-
# except:
|
246 |
-
# status_text.text("Downloading... (size unknown)")
|
247 |
-
# elif d['status'] == 'finished':
|
248 |
-
# progress_bar.progress(1.0)
|
249 |
-
# status_text.text("Processing...")
|
250 |
-
|
251 |
-
# ydl_opts['progress_hooks'] = [progress_hook]
|
252 |
-
|
253 |
-
# with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
254 |
-
# info = ydl.extract_info(url, download=True)
|
255 |
-
# filename = ydl.prepare_filename(info)
|
256 |
-
# return filename
|
257 |
-
|
258 |
-
# except Exception as e:
|
259 |
-
# st.error(f"An error occurred: {str(e)}")
|
260 |
-
# return None
|
261 |
-
|
262 |
-
# # Download button
|
263 |
-
# if st.button("Download"):
|
264 |
-
# if video_url:
|
265 |
-
# try:
|
266 |
-
# with st.spinner("Preparing download..."):
|
267 |
-
# downloaded_file_path = download_video(video_url)
|
268 |
-
|
269 |
-
# if downloaded_file_path and os.path.exists(downloaded_file_path):
|
270 |
-
# with open(downloaded_file_path, 'rb') as file:
|
271 |
-
# st.download_button(
|
272 |
-
# label="Click here to download",
|
273 |
-
# data=file,
|
274 |
-
# file_name=os.path.basename(downloaded_file_path),
|
275 |
-
# mime="application/octet-stream"
|
276 |
-
# )
|
277 |
-
# st.success("✅ Download ready!")
|
278 |
-
# else:
|
279 |
-
# st.error("❌ Download failed. Please try again.")
|
280 |
-
# except Exception as e:
|
281 |
-
# st.error(f"❌ An error occurred: {str(e)}")
|
282 |
-
# else:
|
283 |
-
# st.warning("⚠️ Please enter a valid YouTube URL.")
|
284 |
-
|
285 |
-
# # Help section
|
286 |
-
# with st.expander("ℹ️ Help & Troubleshooting"):
|
287 |
-
# st.markdown("""
|
288 |
-
# **If you're experiencing download issues:**
|
289 |
-
|
290 |
-
# 1. Cookie Authentication Method:
|
291 |
-
# - Install a browser extension like "Get cookies.txt"
|
292 |
-
# - Visit YouTube and ensure you're logged in
|
293 |
-
# - Export cookies to 'youtube.com_cookies.txt'
|
294 |
-
# - Place the file in the same directory as this app
|
295 |
-
|
296 |
-
# 2. Alternative Solutions:
|
297 |
-
# - Try different videos
|
298 |
-
# - Check if the video is public/not age-restricted
|
299 |
-
# - Try again later if YouTube is blocking requests
|
300 |
-
# """)
|
301 |
-
|
302 |
|
303 |
import streamlit as st
|
304 |
import yt_dlp
|
@@ -315,35 +14,26 @@ st.title("YouTube Video Downloader 📺")
|
|
315 |
output_dir = Path("downloads")
|
316 |
output_dir.mkdir(exist_ok=True)
|
317 |
|
318 |
-
# Check if cookies file exists
|
319 |
-
COOKIES_FILE = 'youtube.com_cookies.txt'
|
320 |
-
has_cookies = os.path.exists(COOKIES_FILE)
|
321 |
-
|
322 |
-
if has_cookies:
|
323 |
-
st.success("✅ Cookie file detected")
|
324 |
-
else:
|
325 |
-
st.warning("⚠️ No cookie file found - Some videos might be restricted")
|
326 |
-
|
327 |
# Input field for YouTube video URL
|
328 |
video_url = st.text_input("Enter YouTube Video URL:", placeholder="https://www.youtube.com/watch?v=...")
|
329 |
|
|
|
330 |
def download_video(url):
|
331 |
try:
|
332 |
ydl_opts = {
|
333 |
'format': 'bestvideo+bestaudio/best',
|
334 |
'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
335 |
'merge_output_format': 'webm',
|
|
|
336 |
'quiet': True,
|
337 |
'no_warnings': True,
|
338 |
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
339 |
'referer': 'https://www.youtube.com/',
|
340 |
-
'
|
|
|
|
|
341 |
}
|
342 |
|
343 |
-
# Add cookies file if available
|
344 |
-
if has_cookies:
|
345 |
-
ydl_opts['cookiefile'] = COOKIES_FILE
|
346 |
-
|
347 |
# Progress placeholder
|
348 |
progress_bar = st.progress(0)
|
349 |
status_text = st.empty()
|
@@ -395,18 +85,20 @@ if st.button("Download"):
|
|
395 |
st.warning("⚠️ Please enter a valid YouTube URL.")
|
396 |
|
397 |
# Help section
|
398 |
-
with st.expander("ℹ️ Help &
|
399 |
st.markdown("""
|
400 |
-
**
|
401 |
-
|
402 |
-
|
403 |
-
|
|
|
|
|
|
|
404 |
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
4. Try again later if YouTube is blocking requests
|
410 |
""")
|
411 |
|
412 |
|
@@ -425,69 +117,36 @@ with st.expander("ℹ️ Help & Information"):
|
|
425 |
# output_dir = Path("downloads")
|
426 |
# output_dir.mkdir(exist_ok=True)
|
427 |
|
428 |
-
# #
|
429 |
-
#
|
|
|
430 |
|
431 |
-
#
|
432 |
-
#
|
433 |
-
#
|
434 |
-
#
|
435 |
-
# )
|
436 |
|
437 |
-
#
|
438 |
-
#
|
439 |
-
# return {
|
440 |
-
# 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
441 |
-
# 'merge_output_format': 'mp4'
|
442 |
-
# }
|
443 |
-
# elif format_choice == "720p":
|
444 |
-
# return {
|
445 |
-
# 'format': 'bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720][ext=mp4]/best',
|
446 |
-
# 'merge_output_format': 'mp4'
|
447 |
-
# }
|
448 |
-
# elif format_choice == "480p":
|
449 |
-
# return {
|
450 |
-
# 'format': 'bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/best[height<=480][ext=mp4]/best',
|
451 |
-
# 'merge_output_format': 'mp4'
|
452 |
-
# }
|
453 |
-
# elif format_choice == "360p":
|
454 |
-
# return {
|
455 |
-
# 'format': 'bestvideo[height<=360][ext=mp4]+bestaudio[ext=m4a]/best[height<=360][ext=mp4]/best',
|
456 |
-
# 'merge_output_format': 'mp4'
|
457 |
-
# }
|
458 |
-
# else: # Audio Only
|
459 |
-
# return {
|
460 |
-
# 'format': 'bestaudio[ext=m4a]/best',
|
461 |
-
# 'merge_output_format': 'm4a'
|
462 |
-
# }
|
463 |
|
464 |
-
# def download_video(url
|
465 |
# try:
|
466 |
-
# # Base options
|
467 |
# ydl_opts = {
|
|
|
468 |
# 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
|
|
469 |
# 'quiet': True,
|
470 |
# 'no_warnings': True,
|
471 |
-
# '
|
472 |
-
# '
|
473 |
-
#
|
474 |
-
# 'socket_timeout': 30,
|
475 |
-
# 'retries': 3,
|
476 |
-
# 'ignoreerrors': True,
|
477 |
-
# 'nocheckcertificate': True,
|
478 |
-
# # Modern browser headers
|
479 |
-
# 'headers': {
|
480 |
-
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
481 |
-
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
482 |
-
# 'Accept-Language': 'en-us,en;q=0.5',
|
483 |
-
# 'Sec-Fetch-Mode': 'navigate',
|
484 |
-
# }
|
485 |
# }
|
486 |
-
|
487 |
-
# # Add
|
488 |
-
#
|
489 |
-
|
490 |
-
|
|
|
491 |
# progress_bar = st.progress(0)
|
492 |
# status_text = st.empty()
|
493 |
|
@@ -498,34 +157,20 @@ with st.expander("ℹ️ Help & Information"):
|
|
498 |
# progress_bar.progress(progress)
|
499 |
# status_text.text(f"Downloading: {progress:.1%}")
|
500 |
# except:
|
501 |
-
#
|
502 |
-
# if 'downloaded_bytes' in d:
|
503 |
-
# status_text.text(f"Downloading... ({d['downloaded_bytes'] / 1024 / 1024:.1f} MB)")
|
504 |
-
# else:
|
505 |
-
# status_text.text("Downloading... (size unknown)")
|
506 |
# elif d['status'] == 'finished':
|
507 |
# progress_bar.progress(1.0)
|
508 |
# status_text.text("Processing...")
|
509 |
-
|
510 |
# ydl_opts['progress_hooks'] = [progress_hook]
|
511 |
|
512 |
# with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
513 |
-
#
|
514 |
-
#
|
515 |
-
#
|
516 |
-
|
517 |
-
# info = ydl.extract_info(url, download=True)
|
518 |
-
# filename = ydl.prepare_filename(info)
|
519 |
-
# return filename
|
520 |
-
|
521 |
-
# except yt_dlp.utils.DownloadError as e:
|
522 |
-
# if "Sign in to confirm you're not a bot" in str(e):
|
523 |
-
# st.error("❌ YouTube is requesting verification. Please try again later or try a different video.")
|
524 |
-
# else:
|
525 |
-
# st.error(f"❌ Download error: {str(e)}")
|
526 |
-
# return None
|
527 |
# except Exception as e:
|
528 |
-
# st.error(f"
|
529 |
# return None
|
530 |
|
531 |
# # Download button
|
@@ -533,28 +178,19 @@ with st.expander("ℹ️ Help & Information"):
|
|
533 |
# if video_url:
|
534 |
# try:
|
535 |
# with st.spinner("Preparing download..."):
|
536 |
-
# downloaded_file_path = download_video(video_url
|
537 |
|
538 |
# if downloaded_file_path and os.path.exists(downloaded_file_path):
|
539 |
-
# # Get file size
|
540 |
-
# file_size = os.path.getsize(downloaded_file_path) / (1024 * 1024) # Convert to MB
|
541 |
-
|
542 |
# with open(downloaded_file_path, 'rb') as file:
|
543 |
# st.download_button(
|
544 |
-
# label=
|
545 |
# data=file,
|
546 |
# file_name=os.path.basename(downloaded_file_path),
|
547 |
# mime="application/octet-stream"
|
548 |
# )
|
549 |
# st.success("✅ Download ready!")
|
550 |
-
|
551 |
-
# # Clean up downloaded file after successful download
|
552 |
-
# try:
|
553 |
-
# os.remove(downloaded_file_path)
|
554 |
-
# except:
|
555 |
-
# pass
|
556 |
# else:
|
557 |
-
# st.error("❌ Download failed. Please try
|
558 |
# except Exception as e:
|
559 |
# st.error(f"❌ An error occurred: {str(e)}")
|
560 |
# else:
|
@@ -563,17 +199,14 @@ with st.expander("ℹ️ Help & Information"):
|
|
563 |
# # Help section
|
564 |
# with st.expander("ℹ️ Help & Information"):
|
565 |
# st.markdown("""
|
566 |
-
# **
|
567 |
-
# -
|
568 |
-
# -
|
569 |
-
# -
|
570 |
|
571 |
# **If downloads fail:**
|
572 |
-
# 1.
|
573 |
-
# 2. Verify the
|
574 |
-
# 3.
|
575 |
-
# 4.
|
576 |
-
|
577 |
-
# **Note:**
|
578 |
-
# This app respects YouTube's terms of service and is intended for downloading videos that are freely available and permitted to be downloaded.
|
579 |
-
# """)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
import streamlit as st
|
3 |
import yt_dlp
|
|
|
14 |
output_dir = Path("downloads")
|
15 |
output_dir.mkdir(exist_ok=True)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Input field for YouTube video URL
|
18 |
video_url = st.text_input("Enter YouTube Video URL:", placeholder="https://www.youtube.com/watch?v=...")
|
19 |
|
20 |
+
# Function to download video
|
21 |
def download_video(url):
|
22 |
try:
|
23 |
ydl_opts = {
|
24 |
'format': 'bestvideo+bestaudio/best',
|
25 |
'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
26 |
'merge_output_format': 'webm',
|
27 |
+
# Add these options to mimic browser behavior
|
28 |
'quiet': True,
|
29 |
'no_warnings': True,
|
30 |
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
31 |
'referer': 'https://www.youtube.com/',
|
32 |
+
'cookiefile': 'youtube.com_cookies.txt', # 쿠키 파일 사용
|
33 |
+
'socket_timeout': 30,
|
34 |
+
'http_chunk_size': 10485760, # 10MB
|
35 |
}
|
36 |
|
|
|
|
|
|
|
|
|
37 |
# Progress placeholder
|
38 |
progress_bar = st.progress(0)
|
39 |
status_text = st.empty()
|
|
|
85 |
st.warning("⚠️ Please enter a valid YouTube URL.")
|
86 |
|
87 |
# Help section
|
88 |
+
with st.expander("ℹ️ Help & Troubleshooting"):
|
89 |
st.markdown("""
|
90 |
+
**If you're experiencing download issues:**
|
91 |
+
|
92 |
+
1. Cookie Authentication Method:
|
93 |
+
- Install a browser extension like "Get cookies.txt"
|
94 |
+
- Visit YouTube and ensure you're logged in
|
95 |
+
- Export cookies to 'youtube.com_cookies.txt'
|
96 |
+
- Place the file in the same directory as this app
|
97 |
|
98 |
+
2. Alternative Solutions:
|
99 |
+
- Try different videos
|
100 |
+
- Check if the video is public/not age-restricted
|
101 |
+
- Try again later if YouTube is blocking requests
|
|
|
102 |
""")
|
103 |
|
104 |
|
|
|
117 |
# output_dir = Path("downloads")
|
118 |
# output_dir.mkdir(exist_ok=True)
|
119 |
|
120 |
+
# # Check if cookies file exists
|
121 |
+
# COOKIES_FILE = 'youtube.com_cookies.txt'
|
122 |
+
# has_cookies = os.path.exists(COOKIES_FILE)
|
123 |
|
124 |
+
# if has_cookies:
|
125 |
+
# st.success("✅ Cookie file detected")
|
126 |
+
# else:
|
127 |
+
# st.warning("⚠️ No cookie file found - Some videos might be restricted")
|
|
|
128 |
|
129 |
+
# # Input field for YouTube video URL
|
130 |
+
# video_url = st.text_input("Enter YouTube Video URL:", placeholder="https://www.youtube.com/watch?v=...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
+
# def download_video(url):
|
133 |
# try:
|
|
|
134 |
# ydl_opts = {
|
135 |
+
# 'format': 'bestvideo+bestaudio/best',
|
136 |
# 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
137 |
+
# 'merge_output_format': 'webm',
|
138 |
# 'quiet': True,
|
139 |
# 'no_warnings': True,
|
140 |
+
# 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
141 |
+
# 'referer': 'https://www.youtube.com/',
|
142 |
+
# 'http_chunk_size': 10485760
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
# }
|
144 |
+
|
145 |
+
# # Add cookies file if available
|
146 |
+
# if has_cookies:
|
147 |
+
# ydl_opts['cookiefile'] = COOKIES_FILE
|
148 |
+
|
149 |
+
# # Progress placeholder
|
150 |
# progress_bar = st.progress(0)
|
151 |
# status_text = st.empty()
|
152 |
|
|
|
157 |
# progress_bar.progress(progress)
|
158 |
# status_text.text(f"Downloading: {progress:.1%}")
|
159 |
# except:
|
160 |
+
# status_text.text("Downloading... (size unknown)")
|
|
|
|
|
|
|
|
|
161 |
# elif d['status'] == 'finished':
|
162 |
# progress_bar.progress(1.0)
|
163 |
# status_text.text("Processing...")
|
164 |
+
|
165 |
# ydl_opts['progress_hooks'] = [progress_hook]
|
166 |
|
167 |
# with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
168 |
+
# info = ydl.extract_info(url, download=True)
|
169 |
+
# filename = ydl.prepare_filename(info)
|
170 |
+
# return filename
|
171 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
# except Exception as e:
|
173 |
+
# st.error(f"An error occurred: {str(e)}")
|
174 |
# return None
|
175 |
|
176 |
# # Download button
|
|
|
178 |
# if video_url:
|
179 |
# try:
|
180 |
# with st.spinner("Preparing download..."):
|
181 |
+
# downloaded_file_path = download_video(video_url)
|
182 |
|
183 |
# if downloaded_file_path and os.path.exists(downloaded_file_path):
|
|
|
|
|
|
|
184 |
# with open(downloaded_file_path, 'rb') as file:
|
185 |
# st.download_button(
|
186 |
+
# label="Click here to download",
|
187 |
# data=file,
|
188 |
# file_name=os.path.basename(downloaded_file_path),
|
189 |
# mime="application/octet-stream"
|
190 |
# )
|
191 |
# st.success("✅ Download ready!")
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
# else:
|
193 |
+
# st.error("❌ Download failed. Please try again.")
|
194 |
# except Exception as e:
|
195 |
# st.error(f"❌ An error occurred: {str(e)}")
|
196 |
# else:
|
|
|
199 |
# # Help section
|
200 |
# with st.expander("ℹ️ Help & Information"):
|
201 |
# st.markdown("""
|
202 |
+
# **About Cookie Authentication:**
|
203 |
+
# - This app uses cookie authentication to bypass YouTube's bot detection
|
204 |
+
# - Cookies help the app behave like a logged-in browser
|
205 |
+
# - No personal data is stored or transmitted
|
206 |
|
207 |
# **If downloads fail:**
|
208 |
+
# 1. Check if the video is public
|
209 |
+
# 2. Verify the URL is correct
|
210 |
+
# 3. Try a different video
|
211 |
+
# 4. Try again later if YouTube is blocking requests
|
212 |
+
# """)
|
|
|
|
|
|