Nymbo commited on
Commit
8fbf63a
·
verified ·
1 Parent(s): c91f6d5

allow multiple URLS

Browse files
Files changed (1) hide show
  1. app.py +35 -13
app.py CHANGED
@@ -1,34 +1,56 @@
1
  import gradio as gr
2
  import os
3
  import subprocess
 
 
4
 
5
- def fetch(url, name, ext):
6
  opts = {
7
  "mp3": "-f \"ba\" -x --audio-format mp3", # MP3 format
8
  "mp4": "-f \"bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best\"", # MP4 format for video + audio
9
  "flac": "-f \"ba\" -x --audio-format flac", # FLAC format for lossless audio
10
  "aac": "-f \"ba\" -x --audio-format aac" # AAC format for lossy high-quality audio
11
  }[ext]
 
 
 
 
 
 
 
 
12
 
13
- filename = f"{name}.{ext}"
 
 
 
 
 
 
 
 
 
14
 
15
- try:
16
- # Run yt-dlp command using subprocess to capture errors
17
- result = subprocess.run(f"yt-dlp {opts} {url} -o {filename}", shell=True, check=True)
18
- return filename
19
- except subprocess.CalledProcessError as e:
20
- return f"Error: {str(e)}"
 
 
 
21
 
22
  # Create the Gradio interface
23
  interface = gr.Interface(
24
- fn=fetch,
25
  inputs=[
26
- gr.Textbox(label="Link Address", placeholder="Enter the URL here ..."),
27
- gr.Textbox(label="File Name", placeholder="Example: file_name"),
28
  gr.Dropdown(["mp3", "mp4", "flac", "aac"], value="mp3", label="File Type")
29
  ],
30
- outputs=gr.File(label="Download!"),
31
- description="Download from X, Youtube, Reddit, etc..",
32
  theme="Nymbo/Nymbo_Theme"
33
  )
34
 
 
1
  import gradio as gr
2
  import os
3
  import subprocess
4
+ import shutil
5
+ import zipfile
6
 
7
+ def fetch_album(urls, name, ext):
8
  opts = {
9
  "mp3": "-f \"ba\" -x --audio-format mp3", # MP3 format
10
  "mp4": "-f \"bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best\"", # MP4 format for video + audio
11
  "flac": "-f \"ba\" -x --audio-format flac", # FLAC format for lossless audio
12
  "aac": "-f \"ba\" -x --audio-format aac" # AAC format for lossy high-quality audio
13
  }[ext]
14
+
15
+ # Create a directory to store the downloaded files
16
+ download_dir = f"{name}_downloads"
17
+ if not os.path.exists(download_dir):
18
+ os.makedirs(download_dir)
19
+
20
+ urls_list = urls.split("\n") # Assume each URL is on a new line
21
+ downloaded_files = []
22
 
23
+ for idx, url in enumerate(urls_list):
24
+ filename = f"{name}_{idx + 1}.{ext}"
25
+ filepath = os.path.join(download_dir, filename)
26
+
27
+ try:
28
+ # Run yt-dlp command using subprocess to download the file
29
+ result = subprocess.run(f"yt-dlp {opts} {url} -o {filepath}", shell=True, check=True)
30
+ downloaded_files.append(filepath)
31
+ except subprocess.CalledProcessError as e:
32
+ return f"Error downloading file {filename}: {str(e)}"
33
 
34
+ # Zip the downloaded files
35
+ zip_filename = f"{name}.zip"
36
+ zip_filepath = os.path.join(download_dir, zip_filename)
37
+
38
+ with zipfile.ZipFile(zip_filepath, 'w') as zipf:
39
+ for file in downloaded_files:
40
+ zipf.write(file, os.path.basename(file))
41
+
42
+ return zip_filepath
43
 
44
  # Create the Gradio interface
45
  interface = gr.Interface(
46
+ fn=fetch_album,
47
  inputs=[
48
+ gr.Textbox(label="Album URLs", placeholder="Enter each URL on a new line..."),
49
+ gr.Textbox(label="Album Name", placeholder="Example: album_name"),
50
  gr.Dropdown(["mp3", "mp4", "flac", "aac"], value="mp3", label="File Type")
51
  ],
52
+ outputs=gr.File(label="Download Zipped Album"),
53
+ description="Download a full album from X, Youtube, Reddit, etc., and receive a zipped folder.",
54
  theme="Nymbo/Nymbo_Theme"
55
  )
56