Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -197,6 +197,109 @@
|
|
197 |
|
198 |
|
199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
import streamlit as st
|
201 |
import yt_dlp
|
202 |
import os
|
@@ -212,26 +315,35 @@ st.title("YouTube Video Downloader 📺")
|
|
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': '
|
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 |
-
'
|
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()
|
@@ -283,18 +395,16 @@ if st.button("Download"):
|
|
283 |
st.warning("⚠️ Please enter a valid YouTube URL.")
|
284 |
|
285 |
# Help section
|
286 |
-
with st.expander("ℹ️ Help &
|
287 |
st.markdown("""
|
288 |
-
**
|
289 |
-
|
290 |
-
|
291 |
-
|
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 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
|
|
300 |
""")
|
|
|
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
|
305 |
import os
|
|
|
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 - Enhanced download capabilities enabled")
|
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': 'mp4',
|
|
|
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 |
+
'http_chunk_size': 10485760
|
|
|
|
|
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 |
st.warning("⚠️ Please enter a valid YouTube URL.")
|
396 |
|
397 |
# Help section
|
398 |
+
with st.expander("ℹ️ Help & Information"):
|
399 |
st.markdown("""
|
400 |
+
**About Cookie Authentication:**
|
401 |
+
- This app uses cookie authentication to bypass YouTube's bot detection
|
402 |
+
- Cookies help the app behave like a logged-in browser
|
403 |
+
- No personal data is stored or transmitted
|
|
|
|
|
|
|
404 |
|
405 |
+
**If downloads fail:**
|
406 |
+
1. Check if the video is public
|
407 |
+
2. Verify the URL is correct
|
408 |
+
3. Try a different video
|
409 |
+
4. Try again later if YouTube is blocking requests
|
410 |
""")
|