jpjp9292 commited on
Commit
620a065
โ€ข
1 Parent(s): 5d05675

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -71
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
- # 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
 
@@ -65,46 +42,145 @@ def download_video(url):
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
 
 
 
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