Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,114 +1,4 @@
|
|
1 |
|
2 |
-
import streamlit as st
|
3 |
-
import yt_dlp
|
4 |
-
import os
|
5 |
-
from pathlib import Path
|
6 |
-
|
7 |
-
# Set page config
|
8 |
-
st.set_page_config(page_title="YouTube Video Downloader", page_icon="📺")
|
9 |
-
|
10 |
-
# Set the title of the app
|
11 |
-
st.title("YouTube Video Downloader 📺")
|
12 |
-
|
13 |
-
# Create output directory if it doesn't exist
|
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 |
-
|
24 |
-
ydl_opts = {
|
25 |
-
'format': 'bestvideo+bestaudio/best',
|
26 |
-
'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
27 |
-
'merge_output_format': 'webm',
|
28 |
-
'quiet': False, # Change to False to see more detailed error output
|
29 |
-
'no_warnings': False,
|
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', # Ensure this file path is correct
|
33 |
-
'socket_timeout': 30,
|
34 |
-
'http_chunk_size': 10485760, # 10MB
|
35 |
-
'retries': 10, # Increase retry attempts
|
36 |
-
'headers': {
|
37 |
-
'Accept-Language': 'en-US,en;q=0.9',
|
38 |
-
'Accept-Encoding': 'gzip, deflate, br',
|
39 |
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
|
40 |
-
'Upgrade-Insecure-Requests': '1',
|
41 |
-
}
|
42 |
-
}
|
43 |
-
|
44 |
-
# Progress placeholder
|
45 |
-
progress_bar = st.progress(0)
|
46 |
-
status_text = st.empty()
|
47 |
-
|
48 |
-
def progress_hook(d):
|
49 |
-
if d['status'] == 'downloading':
|
50 |
-
try:
|
51 |
-
progress = d['downloaded_bytes'] / d['total_bytes']
|
52 |
-
progress_bar.progress(progress)
|
53 |
-
status_text.text(f"Downloading: {progress:.1%}")
|
54 |
-
except:
|
55 |
-
status_text.text("Downloading... (size unknown)")
|
56 |
-
elif d['status'] == 'finished':
|
57 |
-
progress_bar.progress(1.0)
|
58 |
-
status_text.text("Processing...")
|
59 |
-
|
60 |
-
ydl_opts['progress_hooks'] = [progress_hook]
|
61 |
-
|
62 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
63 |
-
info = ydl.extract_info(url, download=True)
|
64 |
-
filename = ydl.prepare_filename(info)
|
65 |
-
return filename
|
66 |
-
|
67 |
-
except Exception as e:
|
68 |
-
st.error(f"An error occurred: {str(e)}")
|
69 |
-
return None
|
70 |
-
|
71 |
-
# Download button
|
72 |
-
if st.button("Download"):
|
73 |
-
if video_url:
|
74 |
-
try:
|
75 |
-
with st.spinner("Preparing download..."):
|
76 |
-
downloaded_file_path = download_video(video_url)
|
77 |
-
|
78 |
-
if downloaded_file_path and os.path.exists(downloaded_file_path):
|
79 |
-
with open(downloaded_file_path, 'rb') as file:
|
80 |
-
st.download_button(
|
81 |
-
label="Click here to download",
|
82 |
-
data=file,
|
83 |
-
file_name=os.path.basename(downloaded_file_path),
|
84 |
-
mime="application/octet-stream"
|
85 |
-
)
|
86 |
-
st.success("✅ Download ready!")
|
87 |
-
else:
|
88 |
-
st.error("❌ Download failed. Please try again.")
|
89 |
-
except Exception as e:
|
90 |
-
st.error(f"❌ An error occurred: {str(e)}")
|
91 |
-
else:
|
92 |
-
st.warning("⚠️ Please enter a valid YouTube URL.")
|
93 |
-
|
94 |
-
# Help section
|
95 |
-
with st.expander("ℹ️ Help & Troubleshooting"):
|
96 |
-
st.markdown("""
|
97 |
-
**If you're experiencing download issues:**
|
98 |
-
|
99 |
-
1. Cookie Authentication Method:
|
100 |
-
- Install a browser extension like "Get cookies.txt"
|
101 |
-
- Visit YouTube and ensure you're logged in
|
102 |
-
- Export cookies to 'youtube.com_cookies.txt'
|
103 |
-
- Place the file in the same directory as this app
|
104 |
-
|
105 |
-
2. Alternative Solutions:
|
106 |
-
- Try different videos
|
107 |
-
- Check if the video is public/not age-restricted
|
108 |
-
- Try again later if YouTube is blocking requests
|
109 |
-
""")
|
110 |
-
|
111 |
-
|
112 |
# import streamlit as st
|
113 |
# import yt_dlp
|
114 |
# import os
|
@@ -124,35 +14,33 @@ with st.expander("ℹ️ Help & Troubleshooting"):
|
|
124 |
# output_dir = Path("downloads")
|
125 |
# output_dir.mkdir(exist_ok=True)
|
126 |
|
127 |
-
# # Check if cookies file exists
|
128 |
-
# COOKIES_FILE = 'youtube.com_cookies.txt'
|
129 |
-
# has_cookies = os.path.exists(COOKIES_FILE)
|
130 |
-
|
131 |
-
# if has_cookies:
|
132 |
-
# st.success("✅ Cookie file detected")
|
133 |
-
# else:
|
134 |
-
# st.warning("⚠️ No cookie file found - Some videos might be restricted")
|
135 |
-
|
136 |
# # Input field for YouTube video URL
|
137 |
# video_url = st.text_input("Enter YouTube Video URL:", placeholder="https://www.youtube.com/watch?v=...")
|
138 |
|
|
|
139 |
# def download_video(url):
|
140 |
# try:
|
|
|
141 |
# ydl_opts = {
|
142 |
# 'format': 'bestvideo+bestaudio/best',
|
143 |
# 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
144 |
# 'merge_output_format': 'webm',
|
145 |
-
# 'quiet':
|
146 |
-
# 'no_warnings':
|
147 |
# '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',
|
148 |
# 'referer': 'https://www.youtube.com/',
|
149 |
-
# '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
# }
|
151 |
|
152 |
-
# # Add cookies file if available
|
153 |
-
# if has_cookies:
|
154 |
-
# ydl_opts['cookiefile'] = COOKIES_FILE
|
155 |
-
|
156 |
# # Progress placeholder
|
157 |
# progress_bar = st.progress(0)
|
158 |
# status_text = st.empty()
|
@@ -204,21 +92,133 @@ with st.expander("ℹ️ Help & Troubleshooting"):
|
|
204 |
# st.warning("⚠️ Please enter a valid YouTube URL.")
|
205 |
|
206 |
# # Help section
|
207 |
-
# with st.expander("ℹ️ Help &
|
208 |
# st.markdown("""
|
209 |
-
# **
|
210 |
-
|
211 |
-
#
|
212 |
-
#
|
|
|
|
|
|
|
213 |
|
214 |
-
#
|
215 |
-
#
|
216 |
-
#
|
217 |
-
#
|
218 |
-
# 4. Try again later if YouTube is blocking requests
|
219 |
# """)
|
220 |
|
221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
# import streamlit as st
|
223 |
# import yt_dlp
|
224 |
# import os
|
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
# import streamlit as st
|
3 |
# import yt_dlp
|
4 |
# import os
|
|
|
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 |
+
|
24 |
# ydl_opts = {
|
25 |
# 'format': 'bestvideo+bestaudio/best',
|
26 |
# 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
27 |
# 'merge_output_format': 'webm',
|
28 |
+
# 'quiet': False, # Change to False to see more detailed error output
|
29 |
+
# 'no_warnings': False,
|
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', # Ensure this file path is correct
|
33 |
+
# 'socket_timeout': 30,
|
34 |
+
# 'http_chunk_size': 10485760, # 10MB
|
35 |
+
# 'retries': 10, # Increase retry attempts
|
36 |
+
# 'headers': {
|
37 |
+
# 'Accept-Language': 'en-US,en;q=0.9',
|
38 |
+
# 'Accept-Encoding': 'gzip, deflate, br',
|
39 |
+
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
|
40 |
+
# 'Upgrade-Insecure-Requests': '1',
|
41 |
+
# }
|
42 |
# }
|
43 |
|
|
|
|
|
|
|
|
|
44 |
# # Progress placeholder
|
45 |
# progress_bar = st.progress(0)
|
46 |
# status_text = st.empty()
|
|
|
92 |
# st.warning("⚠️ Please enter a valid YouTube URL.")
|
93 |
|
94 |
# # Help section
|
95 |
+
# with st.expander("ℹ️ Help & Troubleshooting"):
|
96 |
# st.markdown("""
|
97 |
+
# **If you're experiencing download issues:**
|
98 |
+
|
99 |
+
# 1. Cookie Authentication Method:
|
100 |
+
# - Install a browser extension like "Get cookies.txt"
|
101 |
+
# - Visit YouTube and ensure you're logged in
|
102 |
+
# - Export cookies to 'youtube.com_cookies.txt'
|
103 |
+
# - Place the file in the same directory as this app
|
104 |
|
105 |
+
# 2. Alternative Solutions:
|
106 |
+
# - Try different videos
|
107 |
+
# - Check if the video is public/not age-restricted
|
108 |
+
# - Try again later if YouTube is blocking requests
|
|
|
109 |
# """)
|
110 |
|
111 |
|
112 |
+
import streamlit as st
|
113 |
+
import yt_dlp
|
114 |
+
import os
|
115 |
+
from pathlib import Path
|
116 |
+
|
117 |
+
# Set page config
|
118 |
+
st.set_page_config(page_title="YouTube Video Downloader", page_icon="📺")
|
119 |
+
|
120 |
+
# Set the title of the app
|
121 |
+
st.title("YouTube Video Downloader 📺")
|
122 |
+
|
123 |
+
# Create output directory if it doesn't exist
|
124 |
+
output_dir = Path("downloads")
|
125 |
+
output_dir.mkdir(exist_ok=True)
|
126 |
+
|
127 |
+
# Check if cookies file exists
|
128 |
+
COOKIES_FILE = 'youtube.com_cookies.txt'
|
129 |
+
has_cookies = os.path.exists(COOKIES_FILE)
|
130 |
+
|
131 |
+
if has_cookies:
|
132 |
+
st.success("✅ Cookie file detected")
|
133 |
+
else:
|
134 |
+
st.warning("⚠️ No cookie file found - Some videos might be restricted")
|
135 |
+
|
136 |
+
# Input field for YouTube video URL
|
137 |
+
video_url = st.text_input("Enter YouTube Video URL:", placeholder="https://www.youtube.com/watch?v=...")
|
138 |
+
|
139 |
+
def download_video(url):
|
140 |
+
try:
|
141 |
+
ydl_opts = {
|
142 |
+
'format': 'bestvideo+bestaudio/best',
|
143 |
+
'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
144 |
+
'merge_output_format': 'webm',
|
145 |
+
'quiet': True,
|
146 |
+
'no_warnings': True,
|
147 |
+
'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',
|
148 |
+
'referer': 'https://www.youtube.com/',
|
149 |
+
'http_chunk_size': 10485760
|
150 |
+
}
|
151 |
+
|
152 |
+
# Add cookies file if available
|
153 |
+
if has_cookies:
|
154 |
+
ydl_opts['cookiefile'] = COOKIES_FILE
|
155 |
+
|
156 |
+
# Progress placeholder
|
157 |
+
progress_bar = st.progress(0)
|
158 |
+
status_text = st.empty()
|
159 |
+
|
160 |
+
def progress_hook(d):
|
161 |
+
if d['status'] == 'downloading':
|
162 |
+
try:
|
163 |
+
progress = d['downloaded_bytes'] / d['total_bytes']
|
164 |
+
progress_bar.progress(progress)
|
165 |
+
status_text.text(f"Downloading: {progress:.1%}")
|
166 |
+
except:
|
167 |
+
status_text.text("Downloading... (size unknown)")
|
168 |
+
elif d['status'] == 'finished':
|
169 |
+
progress_bar.progress(1.0)
|
170 |
+
status_text.text("Processing...")
|
171 |
+
|
172 |
+
ydl_opts['progress_hooks'] = [progress_hook]
|
173 |
+
|
174 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
175 |
+
info = ydl.extract_info(url, download=True)
|
176 |
+
filename = ydl.prepare_filename(info)
|
177 |
+
return filename
|
178 |
+
|
179 |
+
except Exception as e:
|
180 |
+
st.error(f"An error occurred: {str(e)}")
|
181 |
+
return None
|
182 |
+
|
183 |
+
# Download button
|
184 |
+
if st.button("Download"):
|
185 |
+
if video_url:
|
186 |
+
try:
|
187 |
+
with st.spinner("Preparing download..."):
|
188 |
+
downloaded_file_path = download_video(video_url)
|
189 |
+
|
190 |
+
if downloaded_file_path and os.path.exists(downloaded_file_path):
|
191 |
+
with open(downloaded_file_path, 'rb') as file:
|
192 |
+
st.download_button(
|
193 |
+
label="Click here to download",
|
194 |
+
data=file,
|
195 |
+
file_name=os.path.basename(downloaded_file_path),
|
196 |
+
mime="application/octet-stream"
|
197 |
+
)
|
198 |
+
st.success("✅ Download ready!")
|
199 |
+
else:
|
200 |
+
st.error("❌ Download failed. Please try again.")
|
201 |
+
except Exception as e:
|
202 |
+
st.error(f"❌ An error occurred: {str(e)}")
|
203 |
+
else:
|
204 |
+
st.warning("⚠️ Please enter a valid YouTube URL.")
|
205 |
+
|
206 |
+
# Help section
|
207 |
+
with st.expander("ℹ️ Help & Information"):
|
208 |
+
st.markdown("""
|
209 |
+
**About Cookie Authentication:**
|
210 |
+
- This app uses cookie authentication to bypass YouTube's bot detection
|
211 |
+
- Cookies help the app behave like a logged-in browser
|
212 |
+
- No personal data is stored or transmitted
|
213 |
+
|
214 |
+
**If downloads fail:**
|
215 |
+
1. Check if the video is public
|
216 |
+
2. Verify the URL is correct
|
217 |
+
3. Try a different video
|
218 |
+
4. Try again later if YouTube is blocking requests
|
219 |
+
""")
|
220 |
+
|
221 |
+
|
222 |
# import streamlit as st
|
223 |
# import yt_dlp
|
224 |
# import os
|