Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,50 +2,158 @@ import streamlit as st
|
|
2 |
import yt_dlp
|
3 |
import os
|
4 |
import tempfile
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Set the title of the app
|
7 |
-
st.title("YouTube Video Downloader")
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Input field for YouTube video URL
|
10 |
-
video_url = st.text_input("Enter YouTube Video URL:")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Function to download video
|
13 |
-
def download_video(url):
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
ydl_opts = {
|
18 |
-
'
|
19 |
-
'
|
20 |
-
'
|
21 |
-
'
|
|
|
|
|
|
|
22 |
}
|
|
|
|
|
|
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
|
|
|
|
26 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
st.success("Download completed!")
|
33 |
-
|
34 |
-
ydl.params['progress_hooks'] = [progress_hook]
|
35 |
ydl.download([url])
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
39 |
|
40 |
# Download button
|
41 |
-
if st.button("Download
|
42 |
if video_url:
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
else:
|
51 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import yt_dlp
|
3 |
import os
|
4 |
import tempfile
|
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 |
+
# Format selection
|
21 |
+
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",
|
29 |
+
"360p"
|
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/best',
|
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)":
|
46 |
+
return {
|
47 |
+
'format': 'bestaudio/best',
|
48 |
+
'postprocessors': [{
|
49 |
+
'key': 'FFmpegExtractAudio',
|
50 |
+
'preferredcodec': 'mp3',
|
51 |
+
}]
|
52 |
+
}
|
53 |
+
elif selection == "720p":
|
54 |
+
return {
|
55 |
+
'format': 'bestvideo[height<=720]+bestaudio/best[height<=720]',
|
56 |
+
'merge_output_format': 'mp4'
|
57 |
+
}
|
58 |
+
elif selection == "480p":
|
59 |
+
return {
|
60 |
+
'format': 'bestvideo[height<=480]+bestaudio/best[height<=480]',
|
61 |
+
'merge_output_format': 'mp4'
|
62 |
+
}
|
63 |
+
elif selection == "360p":
|
64 |
+
return {
|
65 |
+
'format': 'bestvideo[height<=360]+bestaudio/best[height<=360]',
|
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 |
+
'extract_flat': False,
|
81 |
+
# Add cookies and user agent to bypass some restrictions
|
82 |
+
'cookiefile': 'cookies.txt', # If you have a cookies file
|
83 |
+
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
84 |
}
|
85 |
+
|
86 |
+
# Update options with format-specific settings
|
87 |
+
ydl_opts.update(format_opts)
|
88 |
|
89 |
+
# Progress placeholder
|
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:
|
97 |
+
progress = d['downloaded_bytes'] / d['total_bytes']
|
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)
|
105 |
+
status_text.text("Processing...")
|
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
|
119 |
+
|
120 |
+
except Exception as e:
|
121 |
+
st.error(f"An error occurred: {str(e)}")
|
122 |
+
return None
|
123 |
|
124 |
# Download button
|
125 |
+
if st.button("Download"):
|
126 |
if video_url:
|
127 |
+
try:
|
128 |
+
with st.spinner("Preparing 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",
|
136 |
+
data=file,
|
137 |
+
file_name=os.path.basename(downloaded_file_path),
|
138 |
+
mime="application/octet-stream"
|
139 |
+
)
|
140 |
+
st.success("✅ Download ready!")
|
141 |
+
else:
|
142 |
+
st.error("❌ Download failed. Please try again.")
|
143 |
+
except Exception as e:
|
144 |
+
st.error(f"❌ An error occurred: {str(e)}")
|
145 |
else:
|
146 |
+
st.warning("⚠️ Please enter a valid YouTube URL.")
|
147 |
+
|
148 |
+
# Add some helpful information
|
149 |
+
with st.expander("ℹ️ Help"):
|
150 |
+
st.markdown("""
|
151 |
+
**How to use:**
|
152 |
+
1. Paste a YouTube video URL in the input field
|
153 |
+
2. Select your preferred format
|
154 |
+
3. Click the Download button
|
155 |
+
4. Wait for the download to complete
|
156 |
+
5. Click the download button that appears
|
157 |
+
|
158 |
+
**Note:** Some videos might be restricted due to YouTube's policies.
|
159 |
+
""")
|