Nymbo commited on
Commit
219b6d6
·
verified ·
1 Parent(s): 19c2a10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -70
app.py CHANGED
@@ -1,85 +1,106 @@
1
  import gradio as gr
2
- import yt_dlp
3
- import os
4
- import tempfile
5
- from glob import glob
6
 
7
- def download_media(urls, output_format, audio_quality, custom_bitrate, embed_metadata, embed_thumbnail, write_description):
8
- # Create a temporary directory to store downloads
9
- temp_dir = tempfile.mkdtemp()
10
- output_template = os.path.join(temp_dir, '%(title)s.%(ext)s')
 
11
 
12
- # Prepare the options
13
- ydl_opts = {
14
- 'outtmpl': output_template,
15
- 'format': 'bestaudio/best',
16
- 'postprocessors': [],
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
- ydl_opts['addmetadata'] = True
38
- ydl_opts['postprocessors'].append({'key': 'FFmpegMetadata'})
39
  if embed_thumbnail:
40
- ydl_opts['writethumbnail'] = True
41
- ydl_opts['postprocessors'].append({'key': 'EmbedThumbnail'})
42
- if write_description:
43
- ydl_opts['writedescription'] = True
 
44
 
45
- # Process URLs
46
- url_list = urls.strip().split()
47
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
48
- ydl.download(url_list)
 
 
 
 
 
 
 
 
 
49
 
50
- # Collect output files
51
- downloaded_files = glob(os.path.join(temp_dir, '*'))
52
- # Return the list of file paths for download
53
- return downloaded_files
 
 
 
54
 
55
- def main():
56
- with gr.Blocks() as demo:
57
- gr.Markdown("# YT-DLP Gradio Interface")
 
58
  with gr.Row():
59
- url_input = gr.Textbox(label="Video URL(s)", placeholder="Enter one or more URLs separated by space or newline")
 
60
  with gr.Row():
61
- output_format = gr.Dropdown(['mp3', 'opus', 'bestvideo+bestaudio'], label="Output Format", value='mp3')
62
- audio_quality = gr.Dropdown(['Best', 'Custom'], label="Audio Quality", value='Best')
63
- custom_bitrate = gr.Textbox(label="Custom Bitrate (e.g., 192k)", visible=False)
 
 
 
 
 
 
 
 
 
 
 
64
  with gr.Row():
65
- embed_metadata = gr.Checkbox(label="Embed Metadata")
66
- embed_thumbnail = gr.Checkbox(label="Embed Thumbnail")
67
- write_description = gr.Checkbox(label="Write Description")
68
- start_button = gr.Button("Start Download")
69
- output_files = gr.Files(label="Downloaded Files")
70
 
71
- def show_custom_bitrate(audio_quality):
72
- if audio_quality == 'Custom':
73
- return gr.update(visible=True)
74
- else:
75
- return gr.update(visible=False)
76
 
77
- audio_quality.change(show_custom_bitrate, inputs=audio_quality, outputs=custom_bitrate)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- start_button.click(download_media,
80
- inputs=[url_input, output_format, audio_quality, custom_bitrate, embed_metadata, embed_thumbnail, write_description],
81
- outputs=output_files)
82
- demo.launch()
 
83
 
84
- if __name__ == "__main__":
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()