Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,38 @@
|
|
1 |
-
|
2 |
import streamlit as st
|
3 |
import yt_dlp
|
4 |
import os
|
5 |
from pathlib import Path
|
6 |
|
7 |
-
#
|
8 |
-
st.set_page_config(page_title="YouTube
|
9 |
-
|
10 |
-
# Set the title of the app
|
11 |
-
st.title("YouTube Video Downloader ๐บ")
|
12 |
|
13 |
-
#
|
14 |
output_dir = Path("downloads")
|
15 |
output_dir.mkdir(exist_ok=True)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
def download_video(url):
|
22 |
try:
|
23 |
-
|
24 |
ydl_opts = {
|
25 |
-
'format': '
|
26 |
'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
27 |
-
'
|
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"
|
54 |
except:
|
55 |
-
status_text.text("
|
56 |
elif d['status'] == 'finished':
|
57 |
progress_bar.progress(1.0)
|
58 |
-
status_text.text("
|
59 |
|
60 |
ydl_opts['progress_hooks'] = [progress_hook]
|
61 |
|
@@ -65,46 +42,145 @@ def download_video(url):
|
|
65 |
return filename
|
66 |
|
67 |
except Exception as e:
|
68 |
-
st.error(f"
|
69 |
return None
|
70 |
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
st.download_button(
|
81 |
-
label="
|
82 |
data=file,
|
83 |
-
file_name=os.path.basename(
|
84 |
-
mime="
|
85 |
)
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
#
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import yt_dlp
|
3 |
import os
|
4 |
from pathlib import Path
|
5 |
|
6 |
+
# ํ์ด์ง ์ค์
|
7 |
+
st.set_page_config(page_title="Simple YouTube Downloader", page_icon="๐บ")
|
8 |
+
st.title("Simple YouTube Downloader ๐บ")
|
|
|
|
|
9 |
|
10 |
+
# ๋ค์ด๋ก๋ ํด๋ ์์ฑ
|
11 |
output_dir = Path("downloads")
|
12 |
output_dir.mkdir(exist_ok=True)
|
13 |
|
14 |
+
def reset_form():
|
15 |
+
st.session_state.url = ""
|
16 |
+
|
17 |
+
def download_video(url, progress_bar, status_text):
|
|
|
18 |
try:
|
|
|
19 |
ydl_opts = {
|
20 |
+
'format': 'best', # ์ต๊ณ ํ์ง์ ํตํฉ ํฌ๋งท
|
21 |
'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
22 |
+
'quiet': True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
}
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def progress_hook(d):
|
26 |
if d['status'] == 'downloading':
|
27 |
try:
|
28 |
progress = d['downloaded_bytes'] / d['total_bytes']
|
29 |
progress_bar.progress(progress)
|
30 |
+
status_text.text(f"๋ค์ด๋ก๋ ์ค: {progress:.1%}")
|
31 |
except:
|
32 |
+
status_text.text("๋ค์ด๋ก๋ ์ค...")
|
33 |
elif d['status'] == 'finished':
|
34 |
progress_bar.progress(1.0)
|
35 |
+
status_text.text("์ฒ๋ฆฌ ์๋ฃ!")
|
36 |
|
37 |
ydl_opts['progress_hooks'] = [progress_hook]
|
38 |
|
|
|
42 |
return filename
|
43 |
|
44 |
except Exception as e:
|
45 |
+
st.error(f"์ค๋ฅ ๋ฐ์: {str(e)}")
|
46 |
return None
|
47 |
|
48 |
+
# ๋ฉ์ธ UI
|
49 |
+
video_url = st.text_input("YouTube URL ์
๋ ฅ:", key="url",
|
50 |
+
placeholder="https://www.youtube.com/watch?v=...")
|
51 |
+
|
52 |
+
col1, col2 = st.columns(2)
|
53 |
+
|
54 |
+
with col1:
|
55 |
+
if st.button("๋ค์ด๋ก๋", type="primary"):
|
56 |
+
if video_url:
|
57 |
+
progress_bar = st.progress(0)
|
58 |
+
status_text = st.empty()
|
59 |
+
|
60 |
+
downloaded_file = download_video(video_url, progress_bar, status_text)
|
61 |
+
|
62 |
+
if downloaded_file and os.path.exists(downloaded_file):
|
63 |
+
with open(downloaded_file, 'rb') as file:
|
64 |
st.download_button(
|
65 |
+
label="โฌ๏ธ ํ์ผ ๋ค์ด๋ก๋",
|
66 |
data=file,
|
67 |
+
file_name=os.path.basename(downloaded_file),
|
68 |
+
mime="video/mp4"
|
69 |
)
|
70 |
+
else:
|
71 |
+
st.warning("โ ๏ธ YouTube URL์ ์
๋ ฅํด์ฃผ์ธ์.")
|
72 |
+
|
73 |
+
with col2:
|
74 |
+
if st.button("์ด๊ธฐํ"):
|
75 |
+
reset_form()
|
76 |
+
|
77 |
+
|
78 |
+
# import streamlit as st
|
79 |
+
# import yt_dlp
|
80 |
+
# import os
|
81 |
+
# from pathlib import Path
|
82 |
+
|
83 |
+
# # Set page config
|
84 |
+
# st.set_page_config(page_title="YouTube Video Downloader", page_icon="๐บ")
|
85 |
+
|
86 |
+
# # Set the title of the app
|
87 |
+
# st.title("YouTube Video Downloader ๐บ")
|
88 |
+
|
89 |
+
# # Create output directory if it doesn't exist
|
90 |
+
# output_dir = Path("downloads")
|
91 |
+
# output_dir.mkdir(exist_ok=True)
|
92 |
+
|
93 |
+
# # Input field for YouTube video URL
|
94 |
+
# video_url = st.text_input("Enter YouTube Video URL:", placeholder="https://www.youtube.com/watch?v=...")
|
95 |
+
|
96 |
+
# # Function to download video
|
97 |
+
# def download_video(url):
|
98 |
+
# try:
|
99 |
+
|
100 |
+
# ydl_opts = {
|
101 |
+
# 'format': 'bestvideo+bestaudio/best',
|
102 |
+
# 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
103 |
+
# 'merge_output_format': 'webm',
|
104 |
+
# 'quiet': False, # Change to False to see more detailed error output
|
105 |
+
# 'no_warnings': False,
|
106 |
+
# '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',
|
107 |
+
# 'referer': 'https://www.youtube.com/',
|
108 |
+
# 'cookiefile': 'youtube.com_cookies.txt', # Ensure this file path is correct
|
109 |
+
# 'socket_timeout': 30,
|
110 |
+
# 'http_chunk_size': 10485760, # 10MB
|
111 |
+
# 'retries': 10, # Increase retry attempts
|
112 |
+
# 'headers': {
|
113 |
+
# 'Accept-Language': 'en-US,en;q=0.9',
|
114 |
+
# 'Accept-Encoding': 'gzip, deflate, br',
|
115 |
+
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
|
116 |
+
# 'Upgrade-Insecure-Requests': '1',
|
117 |
+
# }
|
118 |
+
# }
|
119 |
+
|
120 |
+
# # Progress placeholder
|
121 |
+
# progress_bar = st.progress(0)
|
122 |
+
# status_text = st.empty()
|
123 |
+
|
124 |
+
# def progress_hook(d):
|
125 |
+
# if d['status'] == 'downloading':
|
126 |
+
# try:
|
127 |
+
# progress = d['downloaded_bytes'] / d['total_bytes']
|
128 |
+
# progress_bar.progress(progress)
|
129 |
+
# status_text.text(f"Downloading: {progress:.1%}")
|
130 |
+
# except:
|
131 |
+
# status_text.text("Downloading... (size unknown)")
|
132 |
+
# elif d['status'] == 'finished':
|
133 |
+
# progress_bar.progress(1.0)
|
134 |
+
# status_text.text("Processing...")
|
135 |
+
|
136 |
+
# ydl_opts['progress_hooks'] = [progress_hook]
|
137 |
+
|
138 |
+
# with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
139 |
+
# info = ydl.extract_info(url, download=True)
|
140 |
+
# filename = ydl.prepare_filename(info)
|
141 |
+
# return filename
|
142 |
+
|
143 |
+
# except Exception as e:
|
144 |
+
# st.error(f"An error occurred: {str(e)}")
|
145 |
+
# return None
|
146 |
+
|
147 |
+
# # Download button
|
148 |
+
# if st.button("Download"):
|
149 |
+
# if video_url:
|
150 |
+
# try:
|
151 |
+
# with st.spinner("Preparing download..."):
|
152 |
+
# downloaded_file_path = download_video(video_url)
|
153 |
+
|
154 |
+
# if downloaded_file_path and os.path.exists(downloaded_file_path):
|
155 |
+
# with open(downloaded_file_path, 'rb') as file:
|
156 |
+
# st.download_button(
|
157 |
+
# label="Click here to download",
|
158 |
+
# data=file,
|
159 |
+
# file_name=os.path.basename(downloaded_file_path),
|
160 |
+
# mime="application/octet-stream"
|
161 |
+
# )
|
162 |
+
# st.success("โ
Download ready!")
|
163 |
+
# else:
|
164 |
+
# st.error("โ Download failed. Please try again.")
|
165 |
+
# except Exception as e:
|
166 |
+
# st.error(f"โ An error occurred: {str(e)}")
|
167 |
+
# else:
|
168 |
+
# st.warning("โ ๏ธ Please enter a valid YouTube URL.")
|
169 |
+
|
170 |
+
# # Help section
|
171 |
+
# with st.expander("โน๏ธ Help & Troubleshooting"):
|
172 |
+
# st.markdown("""
|
173 |
+
# **If you're experiencing download issues:**
|
174 |
|
175 |
+
# 1. Cookie Authentication Method:
|
176 |
+
# - Install a browser extension like "Get cookies.txt"
|
177 |
+
# - Visit YouTube and ensure you're logged in
|
178 |
+
# - Export cookies to 'youtube.com_cookies.txt'
|
179 |
+
# - Place the file in the same directory as this app
|
180 |
|
181 |
+
# 2. Alternative Solutions:
|
182 |
+
# - Try different videos
|
183 |
+
# - Check if the video is public/not age-restricted
|
184 |
+
# - Try again later if YouTube is blocking requests
|
185 |
+
# """)
|
186 |
|