Nymbo commited on
Commit
7caa3b7
·
verified ·
1 Parent(s): 1aafe8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +168 -140
app.py CHANGED
@@ -1,154 +1,182 @@
1
  import gradio as gr
2
  import subprocess
3
  import os
4
- import tempfile
5
 
6
- # Function to run yt-dlp commands and return the file path
7
- def run_yt_dlp(command, url, bitrate=None, start_time=None, end_time=None, playlist_items=None, archive_file=None):
8
- # Temporary directory to store the downloaded files
9
- temp_dir = tempfile.mkdtemp()
10
-
11
- # Base command for yt-dlp
12
- base_command = ["yt-dlp", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{temp_dir}/%(title)s.%(ext)s"]
13
-
14
- if command == "list_formats":
15
- base_command = ["yt-dlp", "-F", url]
16
- elif command == "download_mp3_max_quality":
17
- base_command.extend(["--postprocessor-args", "-b:a 320k", url])
18
- elif command == "basic_high_quality_mp3":
19
- base_command.append(url)
20
- elif command == "custom_bitrate":
21
- base_command.extend(["--postprocessor-args", f"-b:a {bitrate}k", url])
22
- elif command == "add_metadata_tags":
23
- base_command.extend(["--embed-metadata", url])
24
- elif command == "download_playlist":
25
- base_command.append(url)
26
- elif command == "download_playlist_range":
27
- base_command.extend(["--playlist-items", playlist_items, url])
28
- elif command == "download_hq_soundcloud":
29
- base_command.extend(["-o", f"{temp_dir}/%(uploader)s - %(title)s.%(ext)s", url])
30
- elif command == "embed_album_art":
31
- base_command.extend(["--embed-thumbnail", url])
32
- elif command == "download_opus":
33
- base_command.extend(["--audio-format", "opus", url])
34
- elif command == "skip_downloaded_files":
35
- base_command.extend(["--download-archive", archive_file, url])
36
- elif command == "download_multiple_files":
37
- base_command.extend(url.split())
38
- elif command == "download_split_chapters":
39
- base_command.extend(["--split-chapters", url])
40
- elif command == "extract_portion":
41
- base_command.extend(["-f", "bestvideo+bestaudio", "--external-downloader", "ffmpeg", "--external-downloader-args", f"ffmpeg_i:-ss {start_time} -to {end_time}", url])
42
- elif command == "update_metadata":
43
- base_command.extend(["--download-archive", archive_file, "--skip-download", "--embed-metadata", "--embed-thumbnail", url])
44
- elif command == "auto_rename":
45
- base_command.extend(["--force-overwrites", url])
46
- elif command == "download_url_list":
47
- base_command.extend(["-a", url])
48
- elif command == "limit_speed":
49
- base_command.extend(["--rate-limit", "1M", url])
50
- elif command == "download_vimeo":
51
- base_command.extend(["-o", f"{temp_dir}/Vimeo/%(uploader)s - %(title)s.%(ext)s", url])
52
- elif command == "download_facebook":
53
- base_command.extend(["-o", f"{temp_dir}/Facebook/%(uploader)s - %(title)s.%(ext)s", url])
54
- elif command == "download_instagram":
55
- base_command.extend(["-o", f"{temp_dir}/Instagram/%(uploader)s - %(title)s.%(ext)s", url])
56
- elif command == "download_twitter":
57
- base_command.extend(["-o", f"{temp_dir}/Twitter/%(uploader)s - %(title)s.%(ext)s", url])
58
- elif command == "download_tiktok":
59
- base_command.extend(["-o", f"{temp_dir}/TikTok/%(uploader)s - %(title)s.%(ext)s", url])
60
- elif command == "download_reddit":
61
- base_command.extend(["-o", f"{temp_dir}/Reddit/%(uploader)s - %(title)s.%(ext)s", url])
62
- elif command == "download_dailymotion":
63
- base_command.extend(["-o", f"{temp_dir}/Dailymotion/%(uploader)s - %(title)s.%(ext)s", url])
64
- elif command == "download_transcript_text":
65
- base_command.extend(["--write-auto-subs", "--sub-lang", "en", "--skip-download", url])
66
- elif command == "download_transcript_markdown":
67
- base_command.extend(["--write-auto-subs", "--sub-lang", "en", "--skip-download", "--convert-subs", "srt", "-o", f"{temp_dir}/%(title)s.md", url])
68
- elif command == "download_video_info":
69
- base_command.extend(["--write-info-json", "--skip-download", "-o", f"{temp_dir}/%(title)s.json", url])
70
- elif command == "list_formats_no_download":
71
- base_command = ["yt-dlp", "-F", url]
72
- elif command == "download_age_restricted":
73
- base_command.extend(["--username", "YOUR_YOUTUBE_EMAIL", "--password", "YOUR_PASSWORD", "-f", "bestvideo+bestaudio", url])
74
-
75
- try:
76
- result = subprocess.run(base_command, capture_output=True, text=True)
77
- if result.returncode != 0:
78
- return None, result.stderr
79
- else:
80
- # Find the downloaded file
81
- downloaded_files = [f for f in os.listdir(temp_dir) if os.path.isfile(os.path.join(temp_dir, f))]
82
- if downloaded_files:
83
- file_path = os.path.join(temp_dir, downloaded_files[0])
84
- return file_path, result.stdout
85
- else:
86
- return None, "No file downloaded."
87
- except Exception as e:
88
- return None, str(e)
89
 
90
- # Gradio Interface
91
- with gr.Blocks() as demo:
92
- gr.Markdown("# yt-dlp Command Executor")
93
 
94
- with gr.Row():
95
- command = gr.Dropdown(choices=[
96
- "list_formats",
97
- "download_mp3_max_quality",
98
- "basic_high_quality_mp3",
99
- "custom_bitrate",
100
- "add_metadata_tags",
101
- "download_playlist",
102
- "download_playlist_range",
103
- "download_hq_soundcloud",
104
- "embed_album_art",
105
- "download_opus",
106
- "skip_downloaded_files",
107
- "download_multiple_files",
108
- "download_split_chapters",
109
- "extract_portion",
110
- "update_metadata",
111
- "auto_rename",
112
- "download_url_list",
113
- "limit_speed",
114
- "download_vimeo",
115
- "download_facebook",
116
- "download_instagram",
117
- "download_twitter",
118
- "download_tiktok",
119
- "download_reddit",
120
- "download_dailymotion",
121
- "download_transcript_text",
122
- "download_transcript_markdown",
123
- "download_video_info",
124
- "list_formats_no_download",
125
- "download_age_restricted"
126
- ], label="Command")
127
 
128
- url = gr.Textbox(label="URL", placeholder="Enter the URL here")
 
129
 
130
- with gr.Row():
131
- bitrate = gr.Textbox(label="Bitrate (for custom bitrate)", placeholder="Enter bitrate in kbps, e.g., 192")
132
- start_time = gr.Textbox(label="Start Time (HH:MM:SS)", placeholder="Enter start time, e.g., 00:01:00")
133
- end_time = gr.Textbox(label="End Time (HH:MM:SS)", placeholder="Enter end time, e.g., 00:05:00")
134
- playlist_items = gr.Textbox(label="Playlist Items (e.g., 1-5)", placeholder="Enter range of items, e.g., 1-5")
135
- archive_file = gr.Textbox(label="Archive File", placeholder="Enter path to archive file, e.g., P:/Local Music/downloaded.txt")
136
 
137
- execute_button = gr.Button("Execute Command")
138
- file_output = gr.File(label="Downloaded File")
139
- output = gr.Textbox(label="Output", lines=10)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
- def execute_and_download(command, url, bitrate, start_time, end_time, playlist_items, archive_file):
142
- file_path, result = run_yt_dlp(command, url, bitrate, start_time, end_time, playlist_items, archive_file)
143
- if file_path:
144
- return file_path, result
 
 
 
 
 
 
 
145
  else:
146
- return None, result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
- execute_button.click(
149
- fn=execute_and_download,
150
- inputs=[command, url, bitrate, start_time, end_time, playlist_items, archive_file],
151
- outputs=[file_output, output]
152
  )
153
 
154
- demo.launch()
 
 
1
  import gradio as gr
2
  import subprocess
3
  import os
 
4
 
5
+ def run_ytdlp_command(command_type, url_or_id, quality, bitrate, start_time, end_time, playlist_range, output_dir="downloads", archive_file="", sub_lang="en", additional_urls="", speed_limit=""):
6
+ """Runs the yt-dlp command based on the selected command type and parameters."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # sanitize output_dir to prevent arbitrary code execution
9
+ output_dir = os.path.abspath(output_dir)
 
10
 
11
+ # Create output directory if it doesn't exist
12
+ os.makedirs(output_dir, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ # Basic command structure
15
+ base_command = ["yt-dlp", "-o", f"{output_dir}/%(title)s.%(ext)s"]
16
 
17
+ # Add archive file if provided
18
+ if archive_file:
19
+ base_command.extend(["--download-archive", archive_file])
 
 
 
20
 
21
+ # Construct command based on type
22
+ if command_type == "List Formats":
23
+ command = ["yt-dlp", "-F", url_or_id]
24
+ elif command_type == "Download MP3 (Max Quality)":
25
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--postprocessor-args", "-b:a 320k", url_or_id]
26
+ elif command_type == "Basic MP3 Download":
27
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", quality, url_or_id]
28
+ elif command_type == "Download with Custom Bitrate":
29
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--postprocessor-args", f"-b:a {bitrate}k", url_or_id]
30
+ elif command_type == "Download with Metadata":
31
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--embed-metadata", url_or_id]
32
+ elif command_type == "Download Playlist":
33
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{output_dir}/%(playlist_title)s/%(title)s.%(ext)s", url_or_id]
34
+ elif command_type == "Download Playlist (Range)":
35
+ command = base_command + ["--playlist-items", playlist_range, "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{output_dir}/%(playlist_title)s/%(title)s.%(ext)s", url_or_id]
36
+ elif command_type == "Download with Album Art":
37
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--embed-thumbnail", "--audio-quality", "0", url_or_id]
38
+ elif command_type == "Download Opus":
39
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "opus", url_or_id]
40
+ elif command_type == "Skip Downloaded":
41
+ command = base_command + ["--download-archive", archive_file, "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", url_or_id]
42
+ elif command_type == "Download Multiple URLs":
43
+ if additional_urls:
44
+ urls = [url_or_id] + additional_urls.split(",")
45
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--add-metadata", "--embed-thumbnail", "--write-description", "--postprocessor-args", "-b:a 320k", *urls]
46
+ else:
47
+ return "Please provide additional URLs separated by commas."
48
+ elif command_type == "Download with Chapters":
49
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--split-chapters", url_or_id]
50
+ elif command_type == "Extract Portion":
51
+ command = base_command + ["-f", "bestvideo+bestaudio", "--external-downloader", "ffmpeg", "--external-downloader-args", f"ffmpeg_i:-ss {start_time} -to {end_time}", url_or_id]
52
+ elif command_type == "Update Metadata":
53
+ command = ["yt-dlp", "--download-archive", archive_file, "--skip-download", "--embed-metadata", "--embed-thumbnail", "--write-description", url_or_id]
54
+ elif command_type == "Rename Duplicate":
55
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--force-overwrites", url_or_id]
56
+ elif command_type == "Download from URL List":
57
+ command = ["yt-dlp", "-a", url_or_id, "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{output_dir}/%(title)s.%(ext)s"]
58
+ elif command_type == "Limit Download Speed":
59
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--rate-limit", speed_limit, url_or_id]
60
+ elif command_type == "Download Transcript (Text)":
61
+ command = ["yt-dlp", "--write-auto-subs", "--sub-lang", sub_lang, "--skip-download", "-o", f"{output_dir}/%(title)s.%(ext)s", url_or_id]
62
+ elif command_type == "Download Transcript (Markdown)":
63
+ command = ["yt-dlp", "--write-auto-subs", "--sub-lang", sub_lang, "--skip-download", "--convert-subs", "srt", "-o", f"{output_dir}/%(title)s.md", url_or_id]
64
+ elif command_type == "Download Video Info (JSON)":
65
+ command = ["yt-dlp", "--write-info-json", "--skip-download", "-o", f"{output_dir}/%(title)s.json", url_or_id]
66
+ # SoundCloud Commands
67
+ elif command_type == "SoundCloud - Download MP3 (HQ)":
68
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{output_dir}/SoundCloud/%(uploader)s - %(title)s.%(ext)s", url_or_id]
69
+ elif command_type == "SoundCloud - Download with Metadata":
70
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--embed-metadata", "-o", f"{output_dir}/SoundCloud/%(uploader)s - %(title)s.%(ext)s", url_or_id]
71
+ elif command_type == "SoundCloud - Download Playlist":
72
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{output_dir}/SoundCloud/%(playlist_title)s/%(title)s.%(ext)s", url_or_id]
73
+ elif command_type == "SoundCloud - Download Playlist (Range)":
74
+ command = base_command + ["--playlist-items", playlist_range, "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{output_dir}/SoundCloud/%(playlist_title)s/%(title)s.%(ext)s", url_or_id]
75
+ elif command_type == "SoundCloud - Download with Album Art":
76
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--embed-thumbnail", "-o", f"{output_dir}/SoundCloud/%(uploader)s - %(title)s.%(ext)s", url_or_id]
77
+ elif command_type == "SoundCloud - Skip Downloaded":
78
+ command = base_command + ["--download-archive", archive_file, "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{output_dir}/SoundCloud/%(uploader)s - %(title)s.%(ext)s", url_or_id]
79
+ elif command_type == "SoundCloud - Download Multiple Tracks":
80
+ if additional_urls:
81
+ urls = [url_or_id] + additional_urls.split(",")
82
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{output_dir}/SoundCloud/%(uploader)s - %(title)s.%(ext)s", *urls]
83
+ else:
84
+ return "Please provide additional URLs separated by commas."
85
+ elif command_type == "SoundCloud - Limit Download Speed":
86
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--rate-limit", speed_limit, "-o", f"{output_dir}/SoundCloud/%(uploader)s - %(title)s.%(ext)s", url_or_id]
87
+ elif command_type == "SoundCloud - Download with Chapters":
88
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--split-chapters", "-o", f"{output_dir}/SoundCloud/%(uploader)s - %(title)s.%(ext)s", url_or_id]
89
+ elif command_type == "SoundCloud - Extract Portion":
90
+ command = base_command + ["-f", "bestaudio", "--external-downloader", "ffmpeg", "--external-downloader-args", f"ffmpeg_i:-ss {start_time} -to {end_time}", "-o", f"{output_dir}/SoundCloud/%(uploader)s - %(title)s.%(ext)s", url_or_id]
91
+ elif command_type == "SoundCloud - Update Metadata":
92
+ command = ["yt-dlp", "--download-archive", archive_file, "--skip-download", "--embed-metadata", "--embed-thumbnail", url_or_id]
93
+ elif command_type == "SoundCloud - Rename Duplicate":
94
+ command = base_command + ["-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "--force-overwrites", "-o", f"{output_dir}/SoundCloud/%(uploader)s - %(title)s.%(ext)s", url_or_id]
95
+ elif command_type == "SoundCloud - Download from URL List":
96
+ command = ["yt-dlp", "-a", url_or_id, "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", f"{output_dir}/SoundCloud/%(uploader)s - %(title)s.%(ext)s"]
97
+ else:
98
+ return "Invalid command type selected."
99
 
100
+ try:
101
+ # Execute command and capture output
102
+ process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
103
+ stdout, stderr = process.communicate()
104
+
105
+ # Decode output to string
106
+ stdout = stdout.decode("utf-8")
107
+ stderr = stderr.decode("utf-8")
108
+
109
+ if process.returncode != 0:
110
+ return f"Error executing command:\n{stderr}"
111
  else:
112
+ return f"Command executed successfully:\n{stdout}"
113
+ except Exception as e:
114
+ return f"An error occurred: {e}"
115
+
116
+ # Define Gradio interface
117
+ with gr.Blocks() as demo:
118
+ gr.Markdown("# YT-DLP Gradio Interface")
119
+ with gr.Row():
120
+ with gr.Column():
121
+ command_type = gr.Dropdown(
122
+ choices=[
123
+ "List Formats",
124
+ "Download MP3 (Max Quality)",
125
+ "Basic MP3 Download",
126
+ "Download with Custom Bitrate",
127
+ "Download with Metadata",
128
+ "Download Playlist",
129
+ "Download Playlist (Range)",
130
+ "Download with Album Art",
131
+ "Download Opus",
132
+ "Skip Downloaded",
133
+ "Download Multiple URLs",
134
+ "Download with Chapters",
135
+ "Extract Portion",
136
+ "Update Metadata",
137
+ "Rename Duplicate",
138
+ "Download from URL List",
139
+ "Limit Download Speed",
140
+ "Download Transcript (Text)",
141
+ "Download Transcript (Markdown)",
142
+ "Download Video Info (JSON)",
143
+ "SoundCloud - Download MP3 (HQ)",
144
+ "SoundCloud - Download with Metadata",
145
+ "SoundCloud - Download Playlist",
146
+ "SoundCloud - Download Playlist (Range)",
147
+ "SoundCloud - Download with Album Art",
148
+ "SoundCloud - Skip Downloaded",
149
+ "SoundCloud - Download Multiple Tracks",
150
+ "SoundCloud - Limit Download Speed",
151
+ "SoundCloud - Download with Chapters",
152
+ "SoundCloud - Extract Portion",
153
+ "SoundCloud - Update Metadata",
154
+ "SoundCloud - Rename Duplicate",
155
+ "SoundCloud - Download from URL List"
156
+ ],
157
+ value="Download MP3 (Max Quality)",
158
+ label="Command Type"
159
+ )
160
+ url_or_id = gr.Textbox(label="URL or Video/Playlist ID")
161
+ quality = gr.Textbox(label="Quality (e.g., 0 for best)", value="0")
162
+ bitrate = gr.Textbox(label="Bitrate (e.g., 192 for 192k)", value="320")
163
+ start_time = gr.Textbox(label="Start Time (HH:MM:SS)", value="00:00:00")
164
+ end_time = gr.Textbox(label="End Time (HH:MM:SS)", value="00:10:00")
165
+ playlist_range = gr.Textbox(label="Playlist Range (e.g., 1-5)", value="")
166
+ output_dir = gr.Textbox(label="Output Directory", value="downloads")
167
+ archive_file = gr.Textbox(label="Archive File Path", value="")
168
+ sub_lang = gr.Textbox(label="Subtitle Language (e.g., en, pt)", value="en")
169
+ additional_urls = gr.Textbox(label="Additional URLs (comma-separated)", value="")
170
+ speed_limit = gr.Textbox(label="Download Speed Limit (e.g., 1M, 512K)", value="")
171
+ submit_btn = gr.Button("Run Command")
172
+ with gr.Column():
173
+ output_text = gr.Textbox(label="Output", lines=10)
174
 
175
+ submit_btn.click(
176
+ run_ytdlp_command,
177
+ inputs=[command_type, url_or_id, quality, bitrate, start_time, end_time, playlist_range, output_dir, archive_file, sub_lang, additional_urls, speed_limit],
178
+ outputs=output_text
179
  )
180
 
181
+ if __name__ == "__main__":
182
+ demo.launch()