Nymbo commited on
Commit
d48d430
·
verified ·
1 Parent(s): 457827d

multiple separate files submission

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -1,36 +1,54 @@
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
 
35
  # Launch the interface
36
- interface. Launch()
 
1
  import gradio as gr
2
  import os
3
  import subprocess
4
+ import tempfile
5
+ import shutil
6
+ from zipfile import ZipFile
7
 
8
+ def fetch(urls, ext):
9
+ # Create a temporary directory to store downloaded files
10
+ with tempfile.TemporaryDirectory() as temp_dir:
11
+ downloaded_files = []
12
+ url_list = urls.splitlines() # Split multiple URLs by line
13
+
14
+ for i, url in enumerate(url_list):
15
+ filename = f"file_{i+1}.{ext}" # Generate a generic filename for each URL
16
+ file_path = os.path.join(temp_dir, filename)
17
+
18
+ opts = {
19
+ "mp3": "-f \"ba\" -x --audio-format mp3",
20
+ "mp4": "-f \"bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best\"",
21
+ "flac": "-f \"ba\" -x --audio-format flac",
22
+ "aac": "-f \"ba\" -x --audio-format aac"
23
+ }[ext]
24
+
25
+ try:
26
+ # Run yt-dlp command using subprocess
27
+ subprocess.run(f"yt-dlp {opts} {url} -o \"{file_path}\"", shell=True, check=True)
28
+ downloaded_files.append(file_path)
29
+ except subprocess.CalledProcessError as e:
30
+ return f"Error downloading file {url}: {str(e)}"
31
+
32
+ # Create a zip file with all downloaded files
33
+ zip_filename = os.path.join(temp_dir, "downloaded_files.zip")
34
+ with ZipFile(zip_filename, 'w') as zipf:
35
+ for file in downloaded_files:
36
+ zipf.write(file, os.path.basename(file)) # Add file to zip, with filename
37
+
38
+ # Return the path to the zip file for downloading
39
+ return zip_filename
40
 
41
  # Create the Gradio interface
42
  interface = gr.Interface(
43
  fn=fetch,
44
  inputs=[
45
+ gr.Textbox(label="Link Addresses", placeholder="Enter one URL per line here ...", lines=5),
 
46
  gr.Dropdown(["mp3", "mp4", "flac", "aac"], value="mp3", label="File Type")
47
  ],
48
+ outputs=gr.File(label="Download ZIP!"),
49
+ description="Download multiple files from X, Youtube, Reddit, etc. and get them in a zip file.",
50
  theme="Nymbo/Nymbo_Theme"
51
  )
52
 
53
  # Launch the interface
54
+ interface.launch()