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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -168
app.py CHANGED
@@ -1,182 +1,158 @@
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()
 
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, cookies_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 cookies_file:
15
+ base_command.extend(["--cookies", cookies_file])
16
+
17
+ if command == "list_formats":
18
+ base_command = ["yt-dlp", "-F", url]
19
+ elif command == "download_mp3_max_quality":
20
+ base_command.extend(["--postprocessor-args", "-b:a 320k", url])
21
+ elif command == "basic_high_quality_mp3":
22
+ base_command.append(url)
23
+ elif command == "custom_bitrate":
24
+ base_command.extend(["--postprocessor-args", f"-b:a {bitrate}k", url])
25
+ elif command == "add_metadata_tags":
26
+ base_command.extend(["--embed-metadata", url])
27
+ elif command == "download_playlist":
28
+ base_command.append(url)
29
+ elif command == "download_playlist_range":
30
+ base_command.extend(["--playlist-items", playlist_items, url])
31
+ elif command == "download_hq_soundcloud":
32
+ base_command.extend(["-o", f"{temp_dir}/%(uploader)s - %(title)s.%(ext)s", url])
33
+ elif command == "embed_album_art":
34
+ base_command.extend(["--embed-thumbnail", url])
35
+ elif command == "download_opus":
36
+ base_command.extend(["--audio-format", "opus", url])
37
+ elif command == "skip_downloaded_files":
38
+ base_command.extend(["--download-archive", archive_file, url])
39
+ elif command == "download_multiple_files":
40
+ base_command.extend(url.split())
41
+ elif command == "download_split_chapters":
42
+ base_command.extend(["--split-chapters", url])
43
+ elif command == "extract_portion":
44
+ base_command.extend(["-f", "bestvideo+bestaudio", "--external-downloader", "ffmpeg", "--external-downloader-args", f"ffmpeg_i:-ss {start_time} -to {end_time}", url])
45
+ elif command == "update_metadata":
46
+ base_command.extend(["--download-archive", archive_file, "--skip-download", "--embed-metadata", "--embed-thumbnail", url])
47
+ elif command == "auto_rename":
48
+ base_command.extend(["--force-overwrites", url])
49
+ elif command == "download_url_list":
50
+ base_command.extend(["-a", url])
51
+ elif command == "limit_speed":
52
+ base_command.extend(["--rate-limit", "1M", url])
53
+ elif command == "download_vimeo":
54
+ base_command.extend(["-o", f"{temp_dir}/Vimeo/%(uploader)s - %(title)s.%(ext)s", url])
55
+ elif command == "download_facebook":
56
+ base_command.extend(["-o", f"{temp_dir}/Facebook/%(uploader)s - %(title)s.%(ext)s", url])
57
+ elif command == "download_instagram":
58
+ base_command.extend(["-o", f"{temp_dir}/Instagram/%(uploader)s - %(title)s.%(ext)s", url])
59
+ elif command == "download_twitter":
60
+ base_command.extend(["-o", f"{temp_dir}/Twitter/%(uploader)s - %(title)s.%(ext)s", url])
61
+ elif command == "download_tiktok":
62
+ base_command.extend(["-o", f"{temp_dir}/TikTok/%(uploader)s - %(title)s.%(ext)s", url])
63
+ elif command == "download_reddit":
64
+ base_command.extend(["-o", f"{temp_dir}/Reddit/%(uploader)s - %(title)s.%(ext)s", url])
65
+ elif command == "download_dailymotion":
66
+ base_command.extend(["-o", f"{temp_dir}/Dailymotion/%(uploader)s - %(title)s.%(ext)s", url])
67
+ elif command == "download_transcript_text":
68
+ base_command.extend(["--write-auto-subs", "--sub-lang", "en", "--skip-download", url])
69
+ elif command == "download_transcript_markdown":
70
+ base_command.extend(["--write-auto-subs", "--sub-lang", "en", "--skip-download", "--convert-subs", "srt", "-o", f"{temp_dir}/%(title)s.md", url])
71
+ elif command == "download_video_info":
72
+ base_command.extend(["--write-info-json", "--skip-download", "-o", f"{temp_dir}/%(title)s.json", url])
73
+ elif command == "list_formats_no_download":
74
+ base_command = ["yt-dlp", "-F", url]
75
+ elif command == "download_age_restricted":
76
+ base_command.extend(["--username", "YOUR_YOUTUBE_EMAIL", "--password", "YOUR_PASSWORD", "-f", "bestvideo+bestaudio", url])
77
+
78
+ try:
79
+ result = subprocess.run(base_command, capture_output=True, text=True)
80
+ if result.returncode != 0:
81
+ return None, result.stderr
82
+ else:
83
+ # Find the downloaded file
84
+ downloaded_files = [f for f in os.listdir(temp_dir) if os.path.isfile(os.path.join(temp_dir, f))]
85
+ if downloaded_files:
86
+ file_path = os.path.join(temp_dir, downloaded_files[0])
87
+ return file_path, result.stdout
88
+ else:
89
+ return None, "No file downloaded."
90
+ except Exception as e:
91
+ return None, str(e)
92
 
93
+ # Gradio Interface
94
+ with gr.Blocks() as demo:
95
+ gr.Markdown("# yt-dlp Command Executor")
96
 
97
+ with gr.Row():
98
+ command = gr.Dropdown(choices=[
99
+ "list_formats",
100
+ "download_mp3_max_quality",
101
+ "basic_high_quality_mp3",
102
+ "custom_bitrate",
103
+ "add_metadata_tags",
104
+ "download_playlist",
105
+ "download_playlist_range",
106
+ "download_hq_soundcloud",
107
+ "embed_album_art",
108
+ "download_opus",
109
+ "skip_downloaded_files",
110
+ "download_multiple_files",
111
+ "download_split_chapters",
112
+ "extract_portion",
113
+ "update_metadata",
114
+ "auto_rename",
115
+ "download_url_list",
116
+ "limit_speed",
117
+ "download_vimeo",
118
+ "download_facebook",
119
+ "download_instagram",
120
+ "download_twitter",
121
+ "download_tiktok",
122
+ "download_reddit",
123
+ "download_dailymotion",
124
+ "download_transcript_text",
125
+ "download_transcript_markdown",
126
+ "download_video_info",
127
+ "list_formats_no_download",
128
+ "download_age_restricted"
129
+ ], label="Command")
130
 
131
+ url = gr.Textbox(label="URL", placeholder="Enter the URL here")
 
 
132
 
133
+ with gr.Row():
134
+ bitrate = gr.Textbox(label="Bitrate (for custom bitrate)", placeholder="Enter bitrate in kbps, e.g., 192")
135
+ start_time = gr.Textbox(label="Start Time (HH:MM:SS)", placeholder="Enter start time, e.g., 00:01:00")
136
+ end_time = gr.Textbox(label="End Time (HH:MM:SS)", placeholder="Enter end time, e.g., 00:05:00")
137
+ playlist_items = gr.Textbox(label="Playlist Items (e.g., 1-5)", placeholder="Enter range of items, e.g., 1-5")
138
+ archive_file = gr.Textbox(label="Archive File", placeholder="Enter path to archive file, e.g., P:/Local Music/downloaded.txt")
139
+ cookies_file = gr.Textbox(label="Cookies File", placeholder="Enter path to cookies file, e.g., P:/cookies.txt")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
+ execute_button = gr.Button("Execute Command")
142
+ file_output = gr.File(label="Downloaded File")
143
+ output = gr.Textbox(label="Output", lines=10)
144
 
145
+ def execute_and_download(command, url, bitrate, start_time, end_time, playlist_items, archive_file, cookies_file):
146
+ file_path, result = run_yt_dlp(command, url, bitrate, start_time, end_time, playlist_items, archive_file, cookies_file)
147
+ if file_path:
148
+ return file_path, result
149
  else:
150
+ return None, result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
+ execute_button.click(
153
+ fn=execute_and_download,
154
+ inputs=[command, url, bitrate, start_time, end_time, playlist_items, archive_file, cookies_file],
155
+ outputs=[file_output, output]
156
  )
157
 
158
+ demo.launch()