MySafeCode commited on
Commit
0309801
·
verified ·
1 Parent(s): d3ff763

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -43
app.py CHANGED
@@ -1,43 +1,43 @@
1
- import gradio as gr
2
- from yt_dlp import YoutubeDL
3
- import os
4
- from pydub import AudioSegment
5
-
6
- DOWNLOADS_FOLDER = "downloads"
7
- os.makedirs(DOWNLOADS_FOLDER, exist_ok=True)
8
-
9
- def download_soundcloud(url, file_format):
10
- # Download best audio first (usually m4a)
11
- ydl_opts = {
12
- 'format': 'bestaudio/best',
13
- 'outtmpl': os.path.join(DOWNLOADS_FOLDER, '%(title)s.%(ext)s')
14
- }
15
-
16
- with YoutubeDL(ydl_opts) as ydl:
17
- info = ydl.extract_info(url, download=True)
18
-
19
- original_file = os.path.join(DOWNLOADS_FOLDER, f"{info['title']}.{info['ext']}")
20
-
21
- # If user wants MP3 and it isn't already mp3, convert
22
- if file_format.lower() == "mp3" and not original_file.endswith(".mp3"):
23
- mp3_file = os.path.splitext(original_file)[0] + ".mp3"
24
- AudioSegment.from_file(original_file).export(mp3_file, format="mp3")
25
- return mp3_file
26
-
27
- # If user chose the original format, just return it
28
- return original_file
29
-
30
- # Gradio interface
31
- with gr.Blocks() as iface:
32
- url_input = gr.Textbox(label="SoundCloud URL")
33
- format_choice = gr.Dropdown(choices=["mp3", "m4a", "opus"], value="mp3", label="Select format")
34
- download_button = gr.Button("Download")
35
- download_file = gr.File(label="Download your track")
36
-
37
- download_button.click(
38
- fn=download_soundcloud,
39
- inputs=[url_input, format_choice],
40
- outputs=download_file
41
- )
42
-
43
- iface.launch()
 
1
+ import gradio as gr
2
+ from yt_dlp import YoutubeDL
3
+ import os
4
+ from pydub import AudioSegment
5
+
6
+ DOWNLOADS_FOLDER = "downloads"
7
+ os.makedirs(DOWNLOADS_FOLDER, exist_ok=True)
8
+
9
+ def download_soundcloud(url, file_format):
10
+ # Download best audio first (usually m4a)
11
+ ydl_opts = {
12
+ 'format': 'bestaudio/best',
13
+ 'outtmpl': os.path.join(DOWNLOADS_FOLDER, '%(title)s.%(ext)s')
14
+ }
15
+
16
+ with YoutubeDL(ydl_opts) as ydl:
17
+ info = ydl.extract_info(url, download=True)
18
+
19
+ original_file = os.path.join(DOWNLOADS_FOLDER, f"{info['title']}.{info['ext']}")
20
+
21
+ # If user wants MP3 and it isn't already mp3, convert
22
+ if file_format.lower() == "mp3" and not original_file.endswith(".mp3"):
23
+ mp3_file = os.path.splitext(original_file)[0] + ".mp3"
24
+ AudioSegment.from_file(original_file).export(mp3_file, format="mp3")
25
+ return mp3_file
26
+
27
+ # If user chose the original format, just return it
28
+ return original_file
29
+
30
+ # Gradio interface
31
+ with gr.Blocks() as iface:
32
+ url_input = gr.Textbox(label="SoundCloud URL")
33
+ format_choice = gr.Dropdown(choices=["mp3", "m4a", "opus"], value="mp3", label="Select format")
34
+ download_button = gr.Button("Download")
35
+ download_file = gr.File(label="Download your track")
36
+
37
+ download_button.click(
38
+ fn=download_soundcloud,
39
+ inputs=[url_input, format_choice],
40
+ outputs=download_file
41
+ )
42
+
43
+ iface.launch(show_error=True )