Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
from typing import List, Optional
|
6 |
+
|
7 |
+
def run_ytdlp_command(command: List[str]) -> str:
|
8 |
+
"""Execute a yt-dlp command and return the output."""
|
9 |
+
try:
|
10 |
+
result = subprocess.run(command, capture_output=True, text=True)
|
11 |
+
return result.stdout if result.stdout else result.stderr
|
12 |
+
except Exception as e:
|
13 |
+
return f"Error: {str(e)}"
|
14 |
+
|
15 |
+
def get_available_formats(url: str) -> str:
|
16 |
+
"""List available formats for a given URL."""
|
17 |
+
command = ["yt-dlp", "-F", url]
|
18 |
+
return run_ytdlp_command(command)
|
19 |
+
|
20 |
+
def download_audio(
|
21 |
+
url: str,
|
22 |
+
output_dir: str,
|
23 |
+
audio_format: str = "mp3",
|
24 |
+
audio_quality: str = "320k",
|
25 |
+
extract_metadata: bool = True,
|
26 |
+
embed_thumbnail: bool = True,
|
27 |
+
playlist_items: Optional[str] = None,
|
28 |
+
split_chapters: bool = False,
|
29 |
+
time_range: Optional[str] = None,
|
30 |
+
download_archive: bool = False,
|
31 |
+
rate_limit: Optional[str] = None
|
32 |
+
) -> str:
|
33 |
+
"""Download audio with specified parameters."""
|
34 |
+
command = ["yt-dlp", "-f", "bestaudio", "--extract-audio"]
|
35 |
+
|
36 |
+
# Basic audio settings
|
37 |
+
command.extend([
|
38 |
+
"--audio-format", audio_format,
|
39 |
+
"--postprocessor-args", f"-b:a {audio_quality}",
|
40 |
+
"-o", os.path.join(output_dir, "%(title)s.%(ext)s")
|
41 |
+
])
|
42 |
+
|
43 |
+
# Optional features
|
44 |
+
if extract_metadata:
|
45 |
+
command.extend(["--embed-metadata", "--add-metadata"])
|
46 |
+
if embed_thumbnail:
|
47 |
+
command.append("--embed-thumbnail")
|
48 |
+
if playlist_items:
|
49 |
+
command.extend(["--playlist-items", playlist_items])
|
50 |
+
if split_chapters:
|
51 |
+
command.append("--split-chapters")
|
52 |
+
if time_range:
|
53 |
+
start, end = time_range.split("-")
|
54 |
+
command.extend([
|
55 |
+
"--external-downloader", "ffmpeg",
|
56 |
+
"--external-downloader-args", f"ffmpeg_i:-ss {start} -to {end}"
|
57 |
+
])
|
58 |
+
if download_archive:
|
59 |
+
command.extend(["--download-archive", os.path.join(output_dir, "downloaded.txt")])
|
60 |
+
if rate_limit:
|
61 |
+
command.extend(["--rate-limit", rate_limit])
|
62 |
+
|
63 |
+
command.append(url)
|
64 |
+
return run_ytdlp_command(command)
|
65 |
+
|
66 |
+
def download_transcript(url: str, output_dir: str, format: str = "srt") -> str:
|
67 |
+
"""Download video transcript."""
|
68 |
+
command = [
|
69 |
+
"yt-dlp",
|
70 |
+
"--write-auto-subs",
|
71 |
+
"--sub-lang", "en",
|
72 |
+
"--skip-download",
|
73 |
+
"--convert-subs", format,
|
74 |
+
"-o", os.path.join(output_dir, "%(title)s.%(ext)s"),
|
75 |
+
url
|
76 |
+
]
|
77 |
+
return run_ytdlp_command(command)
|
78 |
+
|
79 |
+
def download_info_json(url: str, output_dir: str) -> str:
|
80 |
+
"""Download video information as JSON."""
|
81 |
+
command = [
|
82 |
+
"yt-dlp",
|
83 |
+
"--write-info-json",
|
84 |
+
"--skip-download",
|
85 |
+
"-o", os.path.join(output_dir, "%(title)s.json"),
|
86 |
+
url
|
87 |
+
]
|
88 |
+
return run_ytdlp_command(command)
|
89 |
+
|
90 |
+
def create_interface():
|
91 |
+
with gr.Blocks(title="YT-DLP GUI") as app:
|
92 |
+
gr.Markdown("# YT-DLP Graphical Interface")
|
93 |
+
|
94 |
+
with gr.Tab("Basic Download"):
|
95 |
+
url_input = gr.Textbox(label="URL")
|
96 |
+
output_dir = gr.Textbox(label="Output Directory", value="downloads")
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
audio_format = gr.Dropdown(
|
100 |
+
choices=["mp3", "opus", "m4a", "wav"],
|
101 |
+
value="mp3",
|
102 |
+
label="Audio Format"
|
103 |
+
)
|
104 |
+
audio_quality = gr.Dropdown(
|
105 |
+
choices=["320k", "256k", "192k", "128k"],
|
106 |
+
value="320k",
|
107 |
+
label="Audio Quality"
|
108 |
+
)
|
109 |
+
|
110 |
+
with gr.Row():
|
111 |
+
extract_metadata = gr.Checkbox(label="Extract Metadata", value=True)
|
112 |
+
embed_thumbnail = gr.Checkbox(label="Embed Thumbnail", value=True)
|
113 |
+
|
114 |
+
download_btn = gr.Button("Download")
|
115 |
+
output = gr.Textbox(label="Output", lines=5)
|
116 |
+
|
117 |
+
download_btn.click(
|
118 |
+
download_audio,
|
119 |
+
inputs=[
|
120 |
+
url_input,
|
121 |
+
output_dir,
|
122 |
+
audio_format,
|
123 |
+
audio_quality,
|
124 |
+
extract_metadata,
|
125 |
+
embed_thumbnail
|
126 |
+
],
|
127 |
+
outputs=output
|
128 |
+
)
|
129 |
+
|
130 |
+
with gr.Tab("Advanced Options"):
|
131 |
+
adv_url = gr.Textbox(label="URL")
|
132 |
+
adv_output_dir = gr.Textbox(label="Output Directory", value="downloads")
|
133 |
+
|
134 |
+
with gr.Row():
|
135 |
+
playlist_items = gr.Textbox(label="Playlist Items (e.g., 1-5)")
|
136 |
+
rate_limit = gr.Textbox(label="Rate Limit (e.g., 1M)")
|
137 |
+
|
138 |
+
with gr.Row():
|
139 |
+
split_chapters = gr.Checkbox(label="Split by Chapters")
|
140 |
+
use_archive = gr.Checkbox(label="Use Download Archive")
|
141 |
+
|
142 |
+
time_range = gr.Textbox(label="Time Range (e.g., 00:01:00-00:05:00)")
|
143 |
+
|
144 |
+
adv_download_btn = gr.Button("Download with Advanced Options")
|
145 |
+
adv_output = gr.Textbox(label="Output", lines=5)
|
146 |
+
|
147 |
+
adv_download_btn.click(
|
148 |
+
download_audio,
|
149 |
+
inputs=[
|
150 |
+
adv_url,
|
151 |
+
adv_output_dir,
|
152 |
+
"mp3", # fixed format for simplicity
|
153 |
+
"320k", # fixed quality for simplicity
|
154 |
+
True, # always extract metadata
|
155 |
+
True, # always embed thumbnail
|
156 |
+
playlist_items,
|
157 |
+
split_chapters,
|
158 |
+
time_range,
|
159 |
+
use_archive,
|
160 |
+
rate_limit
|
161 |
+
],
|
162 |
+
outputs=adv_output
|
163 |
+
)
|
164 |
+
|
165 |
+
with gr.Tab("Format Info"):
|
166 |
+
format_url = gr.Textbox(label="URL")
|
167 |
+
format_btn = gr.Button("Get Available Formats")
|
168 |
+
format_output = gr.Textbox(label="Available Formats", lines=10)
|
169 |
+
|
170 |
+
format_btn.click(
|
171 |
+
get_available_formats,
|
172 |
+
inputs=format_url,
|
173 |
+
outputs=format_output
|
174 |
+
)
|
175 |
+
|
176 |
+
with gr.Tab("Extras"):
|
177 |
+
extra_url = gr.Textbox(label="URL")
|
178 |
+
extra_output_dir = gr.Textbox(label="Output Directory", value="downloads")
|
179 |
+
|
180 |
+
with gr.Row():
|
181 |
+
transcript_btn = gr.Button("Download Transcript")
|
182 |
+
info_json_btn = gr.Button("Download Info JSON")
|
183 |
+
|
184 |
+
extra_output = gr.Textbox(label="Output", lines=5)
|
185 |
+
|
186 |
+
transcript_btn.click(
|
187 |
+
download_transcript,
|
188 |
+
inputs=[extra_url, extra_output_dir],
|
189 |
+
outputs=extra_output
|
190 |
+
)
|
191 |
+
|
192 |
+
info_json_btn.click(
|
193 |
+
download_info_json,
|
194 |
+
inputs=[extra_url, extra_output_dir],
|
195 |
+
outputs=extra_output
|
196 |
+
)
|
197 |
+
|
198 |
+
return app
|
199 |
+
|
200 |
+
if __name__ == "__main__":
|
201 |
+
app = create_interface()
|
202 |
+
app.launch()
|