Spaces:
Paused
Paused
Update app2.py
Browse files
app2.py
CHANGED
|
@@ -2,8 +2,11 @@ import gradio as gr
|
|
| 2 |
from yt_dlp import YoutubeDL
|
| 3 |
import os
|
| 4 |
from pydub import AudioSegment
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
os.makedirs(DOWNLOADS_FOLDER, exist_ok=True)
|
| 8 |
|
| 9 |
def download_youtube_audio(url, file_format, duration_sec):
|
|
@@ -71,7 +74,11 @@ def download_youtube_audio(url, file_format, duration_sec):
|
|
| 71 |
if output_file != original_file and os.path.exists(original_file):
|
| 72 |
os.remove(original_file)
|
| 73 |
|
| 74 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
return None, f"❌ Error: {str(e)}"
|
|
@@ -138,13 +145,17 @@ def download_soundcloud_audio(url, file_format, duration_sec):
|
|
| 138 |
if output_file != original_file and os.path.exists(original_file):
|
| 139 |
os.remove(original_file)
|
| 140 |
|
| 141 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
return None, f"❌ Error: {str(e)}"
|
| 145 |
|
| 146 |
# Create Gradio interface
|
| 147 |
-
with gr.Blocks(title="Audio Downloader"
|
| 148 |
|
| 149 |
gr.Markdown("# 🎵 Audio Downloader")
|
| 150 |
gr.Markdown("Download audio from YouTube or SoundCloud")
|
|
@@ -189,7 +200,8 @@ with gr.Blocks(title="Audio Downloader", theme=gr.themes.Soft()) as iface:
|
|
| 189 |
|
| 190 |
youtube_output = gr.File(
|
| 191 |
label="Downloaded File",
|
| 192 |
-
interactive=False
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
# YouTube examples
|
|
@@ -244,7 +256,8 @@ with gr.Blocks(title="Audio Downloader", theme=gr.themes.Soft()) as iface:
|
|
| 244 |
|
| 245 |
soundcloud_output = gr.File(
|
| 246 |
label="Downloaded File",
|
| 247 |
-
interactive=False
|
|
|
|
| 248 |
)
|
| 249 |
|
| 250 |
# SoundCloud examples
|
|
@@ -277,7 +290,7 @@ with gr.Blocks(title="Audio Downloader", theme=gr.themes.Soft()) as iface:
|
|
| 277 |
- **WAV**: Lossless, large file size
|
| 278 |
|
| 279 |
### Notes:
|
| 280 |
-
- Files are saved in
|
| 281 |
- Some content may have download restrictions
|
| 282 |
- Long videos may take time to process
|
| 283 |
""")
|
|
@@ -301,5 +314,6 @@ if __name__ == "__main__":
|
|
| 301 |
server_name="127.0.0.1",
|
| 302 |
server_port=7860,
|
| 303 |
show_error=True,
|
| 304 |
-
share=False
|
|
|
|
| 305 |
)
|
|
|
|
| 2 |
from yt_dlp import YoutubeDL
|
| 3 |
import os
|
| 4 |
from pydub import AudioSegment
|
| 5 |
+
import tempfile
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
+
# Use temp directory for better security
|
| 9 |
+
DOWNLOADS_FOLDER = tempfile.mkdtemp(prefix="audio_downloader_")
|
| 10 |
os.makedirs(DOWNLOADS_FOLDER, exist_ok=True)
|
| 11 |
|
| 12 |
def download_youtube_audio(url, file_format, duration_sec):
|
|
|
|
| 74 |
if output_file != original_file and os.path.exists(original_file):
|
| 75 |
os.remove(original_file)
|
| 76 |
|
| 77 |
+
# For Gradio 6+, return a dictionary for gr.File component
|
| 78 |
+
return {
|
| 79 |
+
"path": output_file,
|
| 80 |
+
"name": os.path.basename(output_file)
|
| 81 |
+
}, f"✅ Downloaded: {os.path.basename(output_file)}"
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
return None, f"❌ Error: {str(e)}"
|
|
|
|
| 145 |
if output_file != original_file and os.path.exists(original_file):
|
| 146 |
os.remove(original_file)
|
| 147 |
|
| 148 |
+
# For Gradio 6+, return a dictionary for gr.File component
|
| 149 |
+
return {
|
| 150 |
+
"path": output_file,
|
| 151 |
+
"name": os.path.basename(output_file)
|
| 152 |
+
}, f"✅ Downloaded: {os.path.basename(output_file)}"
|
| 153 |
|
| 154 |
except Exception as e:
|
| 155 |
return None, f"❌ Error: {str(e)}"
|
| 156 |
|
| 157 |
# Create Gradio interface
|
| 158 |
+
with gr.Blocks(title="Audio Downloader") as iface: # Removed theme from here
|
| 159 |
|
| 160 |
gr.Markdown("# 🎵 Audio Downloader")
|
| 161 |
gr.Markdown("Download audio from YouTube or SoundCloud")
|
|
|
|
| 200 |
|
| 201 |
youtube_output = gr.File(
|
| 202 |
label="Downloaded File",
|
| 203 |
+
interactive=False,
|
| 204 |
+
file_count="single" # Explicitly specify file count
|
| 205 |
)
|
| 206 |
|
| 207 |
# YouTube examples
|
|
|
|
| 256 |
|
| 257 |
soundcloud_output = gr.File(
|
| 258 |
label="Downloaded File",
|
| 259 |
+
interactive=False,
|
| 260 |
+
file_count="single" # Explicitly specify file count
|
| 261 |
)
|
| 262 |
|
| 263 |
# SoundCloud examples
|
|
|
|
| 290 |
- **WAV**: Lossless, large file size
|
| 291 |
|
| 292 |
### Notes:
|
| 293 |
+
- Files are saved in a temporary folder
|
| 294 |
- Some content may have download restrictions
|
| 295 |
- Long videos may take time to process
|
| 296 |
""")
|
|
|
|
| 314 |
server_name="127.0.0.1",
|
| 315 |
server_port=7860,
|
| 316 |
show_error=True,
|
| 317 |
+
share=False,
|
| 318 |
+
theme=gr.themes.Soft() # Moved theme to launch() method
|
| 319 |
)
|