Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
import yt_dlp
|
3 |
import os
|
4 |
-
import tempfile
|
5 |
from pathlib import Path
|
6 |
|
7 |
# Set page config
|
@@ -22,7 +21,6 @@ format_option = st.selectbox(
|
|
22 |
"Select Format:",
|
23 |
options=[
|
24 |
"Best Quality (Video + Audio)",
|
25 |
-
"Video Only (Best Quality)",
|
26 |
"Audio Only (Best Quality)",
|
27 |
"720p",
|
28 |
"480p",
|
@@ -30,16 +28,10 @@ format_option = st.selectbox(
|
|
30 |
]
|
31 |
)
|
32 |
|
33 |
-
# Function to get format based on selection
|
34 |
def get_format_options(selection):
|
35 |
if selection == "Best Quality (Video + Audio)":
|
36 |
return {
|
37 |
-
'format': 'bestvideo+bestaudio
|
38 |
-
'merge_output_format': 'mp4'
|
39 |
-
}
|
40 |
-
elif selection == "Video Only (Best Quality)":
|
41 |
-
return {
|
42 |
-
'format': 'bestvideo/best',
|
43 |
'merge_output_format': 'mp4'
|
44 |
}
|
45 |
elif selection == "Audio Only (Best Quality)":
|
@@ -52,35 +44,36 @@ def get_format_options(selection):
|
|
52 |
}
|
53 |
elif selection == "720p":
|
54 |
return {
|
55 |
-
'format': '
|
56 |
'merge_output_format': 'mp4'
|
57 |
}
|
58 |
elif selection == "480p":
|
59 |
return {
|
60 |
-
'format': '
|
61 |
'merge_output_format': 'mp4'
|
62 |
}
|
63 |
elif selection == "360p":
|
64 |
return {
|
65 |
-
'format': '
|
66 |
'merge_output_format': 'mp4'
|
67 |
}
|
68 |
|
69 |
-
# Function to download video
|
70 |
def download_video(url, format_selection):
|
71 |
try:
|
72 |
-
# Get format options
|
73 |
format_opts = get_format_options(format_selection)
|
74 |
|
75 |
-
# Base options
|
76 |
ydl_opts = {
|
77 |
'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
78 |
'quiet': True,
|
79 |
'no_warnings': True,
|
80 |
-
|
81 |
-
|
82 |
-
'
|
83 |
-
'
|
|
|
|
|
|
|
|
|
84 |
}
|
85 |
|
86 |
# Update options with format-specific settings
|
@@ -90,7 +83,6 @@ def download_video(url, format_selection):
|
|
90 |
progress_bar = st.progress(0)
|
91 |
status_text = st.empty()
|
92 |
|
93 |
-
# Custom progress hook
|
94 |
def progress_hook(d):
|
95 |
if d['status'] == 'downloading':
|
96 |
try:
|
@@ -98,7 +90,6 @@ def download_video(url, format_selection):
|
|
98 |
progress_bar.progress(progress)
|
99 |
status_text.text(f"Downloading: {progress:.1%}")
|
100 |
except:
|
101 |
-
# For cases where total_bytes is unknown
|
102 |
status_text.text("Downloading... (size unknown)")
|
103 |
elif d['status'] == 'finished':
|
104 |
progress_bar.progress(1.0)
|
@@ -106,13 +97,9 @@ def download_video(url, format_selection):
|
|
106 |
|
107 |
ydl_opts['progress_hooks'] = [progress_hook]
|
108 |
|
109 |
-
# Download the video
|
110 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
111 |
-
# First extract info to get the title
|
112 |
info = ydl.extract_info(url, download=False)
|
113 |
filename = ydl.prepare_filename(info)
|
114 |
-
|
115 |
-
# Then download
|
116 |
ydl.download([url])
|
117 |
|
118 |
return filename
|
@@ -129,7 +116,6 @@ if st.button("Download"):
|
|
129 |
downloaded_file_path = download_video(video_url, format_option)
|
130 |
|
131 |
if downloaded_file_path and os.path.exists(downloaded_file_path):
|
132 |
-
# Read the file and provide it for download
|
133 |
with open(downloaded_file_path, 'rb') as file:
|
134 |
st.download_button(
|
135 |
label="Click here to download",
|
@@ -145,7 +131,7 @@ if st.button("Download"):
|
|
145 |
else:
|
146 |
st.warning("⚠️ Please enter a valid YouTube URL.")
|
147 |
|
148 |
-
# Add
|
149 |
with st.expander("ℹ️ Help"):
|
150 |
st.markdown("""
|
151 |
**How to use:**
|
|
|
1 |
import streamlit as st
|
2 |
import yt_dlp
|
3 |
import os
|
|
|
4 |
from pathlib import Path
|
5 |
|
6 |
# Set page config
|
|
|
21 |
"Select Format:",
|
22 |
options=[
|
23 |
"Best Quality (Video + Audio)",
|
|
|
24 |
"Audio Only (Best Quality)",
|
25 |
"720p",
|
26 |
"480p",
|
|
|
28 |
]
|
29 |
)
|
30 |
|
|
|
31 |
def get_format_options(selection):
|
32 |
if selection == "Best Quality (Video + Audio)":
|
33 |
return {
|
34 |
+
'format': 'best', # Changed from bestvideo+bestaudio to best
|
|
|
|
|
|
|
|
|
|
|
35 |
'merge_output_format': 'mp4'
|
36 |
}
|
37 |
elif selection == "Audio Only (Best Quality)":
|
|
|
44 |
}
|
45 |
elif selection == "720p":
|
46 |
return {
|
47 |
+
'format': 'best[height<=720]', # Simplified format selection
|
48 |
'merge_output_format': 'mp4'
|
49 |
}
|
50 |
elif selection == "480p":
|
51 |
return {
|
52 |
+
'format': 'best[height<=480]',
|
53 |
'merge_output_format': 'mp4'
|
54 |
}
|
55 |
elif selection == "360p":
|
56 |
return {
|
57 |
+
'format': 'best[height<=360]',
|
58 |
'merge_output_format': 'mp4'
|
59 |
}
|
60 |
|
|
|
61 |
def download_video(url, format_selection):
|
62 |
try:
|
|
|
63 |
format_opts = get_format_options(format_selection)
|
64 |
|
|
|
65 |
ydl_opts = {
|
66 |
'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
|
67 |
'quiet': True,
|
68 |
'no_warnings': True,
|
69 |
+
# Enhanced options to bypass restrictions
|
70 |
+
'nocheckcertificate': True,
|
71 |
+
'ignoreerrors': True,
|
72 |
+
'no_color': True,
|
73 |
+
'noprogress': True,
|
74 |
+
'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',
|
75 |
+
'referer': 'https://www.youtube.com/',
|
76 |
+
'http-chunk-size': '10485760', # 10MB chunks for better reliability
|
77 |
}
|
78 |
|
79 |
# Update options with format-specific settings
|
|
|
83 |
progress_bar = st.progress(0)
|
84 |
status_text = st.empty()
|
85 |
|
|
|
86 |
def progress_hook(d):
|
87 |
if d['status'] == 'downloading':
|
88 |
try:
|
|
|
90 |
progress_bar.progress(progress)
|
91 |
status_text.text(f"Downloading: {progress:.1%}")
|
92 |
except:
|
|
|
93 |
status_text.text("Downloading... (size unknown)")
|
94 |
elif d['status'] == 'finished':
|
95 |
progress_bar.progress(1.0)
|
|
|
97 |
|
98 |
ydl_opts['progress_hooks'] = [progress_hook]
|
99 |
|
|
|
100 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
|
101 |
info = ydl.extract_info(url, download=False)
|
102 |
filename = ydl.prepare_filename(info)
|
|
|
|
|
103 |
ydl.download([url])
|
104 |
|
105 |
return filename
|
|
|
116 |
downloaded_file_path = download_video(video_url, format_option)
|
117 |
|
118 |
if downloaded_file_path and os.path.exists(downloaded_file_path):
|
|
|
119 |
with open(downloaded_file_path, 'rb') as file:
|
120 |
st.download_button(
|
121 |
label="Click here to download",
|
|
|
131 |
else:
|
132 |
st.warning("⚠️ Please enter a valid YouTube URL.")
|
133 |
|
134 |
+
# Add help section
|
135 |
with st.expander("ℹ️ Help"):
|
136 |
st.markdown("""
|
137 |
**How to use:**
|