Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,106 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import os
|
4 |
-
import tempfile
|
5 |
-
from glob import glob
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
'
|
15 |
-
'
|
16 |
-
'
|
17 |
-
|
18 |
-
|
19 |
-
# Set format options
|
20 |
-
if output_format in ['mp3', 'opus']:
|
21 |
-
extract_audio_postprocessor = {
|
22 |
-
'key': 'FFmpegExtractAudio',
|
23 |
-
'preferredcodec': output_format,
|
24 |
-
}
|
25 |
-
# Set postprocessor args
|
26 |
-
if audio_quality == 'Best':
|
27 |
-
extract_audio_postprocessor['preferredquality'] = '0' # Best quality
|
28 |
-
elif audio_quality == 'Custom' and custom_bitrate:
|
29 |
-
extract_audio_postprocessor['preferredquality'] = '5' # Default value
|
30 |
-
extract_audio_postprocessor['postprocessor_args'] = ['-b:a', custom_bitrate]
|
31 |
-
ydl_opts['postprocessors'].append(extract_audio_postprocessor)
|
32 |
-
else:
|
33 |
-
ydl_opts['format'] = 'bestvideo+bestaudio/best'
|
34 |
-
|
35 |
-
# Additional options
|
36 |
if embed_metadata:
|
37 |
-
|
38 |
-
ydl_opts['postprocessors'].append({'key': 'FFmpegMetadata'})
|
39 |
if embed_thumbnail:
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
58 |
with gr.Row():
|
59 |
-
|
|
|
60 |
with gr.Row():
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
with gr.Row():
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
start_button = gr.Button("Start Download")
|
69 |
-
output_files = gr.Files(label="Downloaded Files")
|
70 |
|
71 |
-
|
72 |
-
if audio_quality == 'Custom':
|
73 |
-
return gr.update(visible=True)
|
74 |
-
else:
|
75 |
-
return gr.update(visible=False)
|
76 |
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
83 |
|
84 |
-
|
85 |
-
main()
|
|
|
1 |
import gradio as gr
|
2 |
+
import subprocess
|
|
|
|
|
|
|
3 |
|
4 |
+
# Define functions for each command
|
5 |
+
def list_formats(url):
|
6 |
+
command = f'yt-dlp -F "{url}"'
|
7 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
8 |
+
return result.stdout
|
9 |
|
10 |
+
def download_mp3(url, output_folder="P:/Local Music", audio_quality="0", bitrate="320k", embed_metadata=False, embed_thumbnail=False):
|
11 |
+
options = [
|
12 |
+
f'yt-dlp -f bestaudio --extract-audio --audio-format mp3 --audio-quality {audio_quality}',
|
13 |
+
f'--postprocessor-args "-b:a {bitrate}"',
|
14 |
+
f'-o "{output_folder}/%(title)s.%(ext)s"'
|
15 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
if embed_metadata:
|
17 |
+
options.append('--embed-metadata')
|
|
|
18 |
if embed_thumbnail:
|
19 |
+
options.append('--embed-thumbnail')
|
20 |
+
options.append(f'"{url}"')
|
21 |
+
command = " ".join(options)
|
22 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
23 |
+
return result.stdout
|
24 |
|
25 |
+
def download_playlist(url, output_folder="P:/Local Music", range_items="", embed_metadata=False):
|
26 |
+
options = [
|
27 |
+
f'yt-dlp -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0',
|
28 |
+
f'-o "{output_folder}/%(playlist_title)s/%(title)s.%(ext)s"'
|
29 |
+
]
|
30 |
+
if range_items:
|
31 |
+
options.insert(1, f'--playlist-items {range_items}')
|
32 |
+
if embed_metadata:
|
33 |
+
options.append('--embed-metadata')
|
34 |
+
options.append(f'"{url}"')
|
35 |
+
command = " ".join(options)
|
36 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
37 |
+
return result.stdout
|
38 |
|
39 |
+
def download_custom(url_list, output_folder="P:/Local Music", command_template=""):
|
40 |
+
results = []
|
41 |
+
for url in url_list.splitlines():
|
42 |
+
command = command_template.replace("VIDEO_URL", url).replace("P:/Local Music", output_folder)
|
43 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
44 |
+
results.append(result.stdout)
|
45 |
+
return "\n".join(results)
|
46 |
|
47 |
+
# Gradio interface components
|
48 |
+
with gr.Blocks() as yt_dlp_app:
|
49 |
+
gr.Markdown("## YT-DLP Command Helper")
|
50 |
+
with gr.Tab("General Commands"):
|
51 |
with gr.Row():
|
52 |
+
url = gr.Textbox(label="Video URL")
|
53 |
+
output_folder = gr.Textbox(label="Output Folder", value="P:/Local Music")
|
54 |
with gr.Row():
|
55 |
+
quality = gr.Dropdown(["0", "1", "2", "3"], label="Audio Quality", value="0")
|
56 |
+
bitrate = gr.Textbox(label="Custom Bitrate", value="320k")
|
57 |
+
embed_metadata = gr.Checkbox(label="Embed Metadata")
|
58 |
+
embed_thumbnail = gr.Checkbox(label="Embed Thumbnail")
|
59 |
+
download_button = gr.Button("Download MP3")
|
60 |
+
download_output = gr.Textbox(label="Command Output", lines=10)
|
61 |
+
|
62 |
+
download_button.click(
|
63 |
+
download_mp3,
|
64 |
+
inputs=[url, output_folder, quality, bitrate, embed_metadata, embed_thumbnail],
|
65 |
+
outputs=download_output
|
66 |
+
)
|
67 |
+
|
68 |
+
with gr.Tab("List Formats"):
|
69 |
with gr.Row():
|
70 |
+
format_url = gr.Textbox(label="URL to List Formats")
|
71 |
+
format_button = gr.Button("List Available Formats")
|
72 |
+
format_output = gr.Textbox(label="Command Output", lines=10)
|
|
|
|
|
73 |
|
74 |
+
format_button.click(list_formats, inputs=[format_url], outputs=format_output)
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
with gr.Tab("Playlists"):
|
77 |
+
with gr.Row():
|
78 |
+
playlist_url = gr.Textbox(label="Playlist URL")
|
79 |
+
range_items = gr.Textbox(label="Playlist Range (e.g., 1-5)", placeholder="Optional")
|
80 |
+
embed_metadata_playlist = gr.Checkbox(label="Embed Metadata")
|
81 |
+
playlist_button = gr.Button("Download Playlist")
|
82 |
+
playlist_output = gr.Textbox(label="Command Output", lines=10)
|
83 |
+
|
84 |
+
playlist_button.click(
|
85 |
+
download_playlist,
|
86 |
+
inputs=[playlist_url, output_folder, range_items, embed_metadata_playlist],
|
87 |
+
outputs=playlist_output
|
88 |
+
)
|
89 |
+
|
90 |
+
with gr.Tab("Custom Commands"):
|
91 |
+
with gr.Row():
|
92 |
+
custom_urls = gr.Textbox(label="URLs (one per line)")
|
93 |
+
command_template = gr.Textbox(
|
94 |
+
label="Custom Command Template",
|
95 |
+
placeholder="Example: yt-dlp -f bestaudio --extract-audio --audio-format mp3 -o 'P:/Local Music/%(title)s.%(ext)s' VIDEO_URL"
|
96 |
+
)
|
97 |
+
custom_button = gr.Button("Run Custom Commands")
|
98 |
+
custom_output = gr.Textbox(label="Command Output", lines=10)
|
99 |
|
100 |
+
custom_button.click(
|
101 |
+
download_custom,
|
102 |
+
inputs=[custom_urls, output_folder, command_template],
|
103 |
+
outputs=custom_output
|
104 |
+
)
|
105 |
|
106 |
+
yt_dlp_app.launch()
|
|