Spaces:
Paused
Paused
Modernize: Gradio 6.29, safe yt-dlp invocation, ffmpeg
Browse files- README.md +2 -1
- app.py +32 -20
- packages.txt +1 -0
- requirements.txt +1 -1
README.md
CHANGED
|
@@ -4,8 +4,9 @@ emoji: ⬇️
|
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
|
|
|
| 9 |
pinned: false
|
| 10 |
license: creativeml-openrail-m
|
| 11 |
header: mini
|
|
|
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 6.29.0
|
| 8 |
app_file: app.py
|
| 9 |
+
python_version: "3.12"
|
| 10 |
pinned: false
|
| 11 |
license: creativeml-openrail-m
|
| 12 |
header: mini
|
app.py
CHANGED
|
@@ -1,36 +1,48 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def fetch(url, name, ext):
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
# Create the Gradio interface
|
| 23 |
interface = gr.Interface(
|
| 24 |
-
fn=fetch,
|
| 25 |
inputs=[
|
| 26 |
gr.Textbox(label="Link Address", placeholder="Enter the URL here ..."),
|
| 27 |
gr.Textbox(label="File Name", placeholder="Example: file_name"),
|
| 28 |
-
gr.Dropdown(["mp3", "mp4", "flac", "aac"], value="mp3", label="File Type")
|
| 29 |
],
|
| 30 |
outputs=gr.File(label="Download!"),
|
| 31 |
description="Download from X, Youtube, Reddit, etc..",
|
| 32 |
-
|
| 33 |
)
|
| 34 |
|
| 35 |
# Launch the interface
|
| 36 |
-
interface.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import subprocess
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
FORMAT_OPTS = {
|
| 9 |
+
"mp3": ["-f", "ba/b", "-x", "--audio-format", "mp3"], # MP3 format
|
| 10 |
+
"mp4": ["-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best", "--merge-output-format", "mp4"], # MP4 video + audio
|
| 11 |
+
"flac": ["-f", "ba/b", "-x", "--audio-format", "flac"], # FLAC lossless audio
|
| 12 |
+
"aac": ["-f", "ba/b", "-x", "--audio-format", "aac"], # AAC lossy high-quality audio
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
|
| 16 |
def fetch(url, name, ext):
|
| 17 |
+
url = (url or "").strip()
|
| 18 |
+
if not url.startswith(("http://", "https://")):
|
| 19 |
+
raise gr.Error("Please enter a valid http(s) link.")
|
| 20 |
+
name = re.sub(r"[^\w\-. ]", "_", (name or "").strip()) or "download"
|
| 21 |
+
|
| 22 |
+
out_dir = tempfile.mkdtemp()
|
| 23 |
+
filename = os.path.join(out_dir, f"{name}.{ext}")
|
| 24 |
+
|
| 25 |
+
# Argument list (no shell) so the URL can't inject commands
|
| 26 |
+
cmd = ["yt-dlp", "--no-playlist", *FORMAT_OPTS[ext], "-o", filename, url]
|
| 27 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 28 |
+
if result.returncode != 0 or not os.path.exists(filename):
|
| 29 |
+
err = (result.stderr or result.stdout or "").strip().splitlines()
|
| 30 |
+
raise gr.Error("Download failed: " + (err[-1] if err else "unknown error"))
|
| 31 |
+
return filename
|
| 32 |
+
|
| 33 |
|
| 34 |
# Create the Gradio interface
|
| 35 |
interface = gr.Interface(
|
| 36 |
+
fn=fetch,
|
| 37 |
inputs=[
|
| 38 |
gr.Textbox(label="Link Address", placeholder="Enter the URL here ..."),
|
| 39 |
gr.Textbox(label="File Name", placeholder="Example: file_name"),
|
| 40 |
+
gr.Dropdown(["mp3", "mp4", "flac", "aac"], value="mp3", label="File Type"),
|
| 41 |
],
|
| 42 |
outputs=gr.File(label="Download!"),
|
| 43 |
description="Download from X, Youtube, Reddit, etc..",
|
| 44 |
+
flagging_mode="never",
|
| 45 |
)
|
| 46 |
|
| 47 |
# Launch the interface
|
| 48 |
+
interface.launch(theme="Nymbo/Nymbo_Theme")
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ffmpeg
|
requirements.txt
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
yt-dlp
|
|
|
|
| 1 |
+
yt-dlp[default]
|