Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,10 @@ import yt_dlp
|
|
3 |
import os
|
4 |
from pydub import AudioSegment
|
5 |
|
6 |
-
def process_youtube_url(url, uploaded_file
|
7 |
try:
|
8 |
filename = None
|
9 |
-
|
10 |
if url:
|
11 |
ydl_opts = {
|
12 |
'format': 'bestaudio/best',
|
@@ -16,33 +16,33 @@ def process_youtube_url(url, uploaded_file, state):
|
|
16 |
|
17 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
18 |
info = ydl.extract_info(url, download=True)
|
19 |
-
|
20 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
21 |
|
22 |
elif uploaded_file:
|
23 |
-
filename = uploaded_file
|
24 |
|
25 |
else:
|
26 |
-
|
27 |
|
28 |
if not os.path.exists(filename):
|
29 |
-
|
30 |
|
31 |
-
|
|
|
32 |
audio = AudioSegment.from_file(filename)
|
33 |
audio.export(mp3_filename, format="mp3")
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
ringtone_audio.
|
38 |
-
|
39 |
-
ringtone_filename_m4r = 'Apple.m4r'
|
40 |
-
os.rename(ringtone_filename_m4a, ringtone_filename_m4r)
|
41 |
|
42 |
-
return mp3_filename, ringtone_filename_m4r
|
43 |
|
44 |
-
except Exception:
|
45 |
-
|
|
|
46 |
|
47 |
with gr.Blocks() as interface:
|
48 |
gr.HTML("""
|
@@ -51,16 +51,15 @@ with gr.Blocks() as interface:
|
|
51 |
""")
|
52 |
|
53 |
with gr.Row():
|
54 |
-
youtube_url = gr.Textbox(label="Enter YouTube URL
|
55 |
-
mp3_upload = gr.File(label="Upload MP3 (Optional)"
|
56 |
-
|
57 |
-
|
58 |
with gr.Row():
|
59 |
mp3_download = gr.File(label="Android Ringtone")
|
60 |
iphone_ringtone = gr.File(label="iPhone Ringtone")
|
61 |
|
62 |
process_button = gr.Button("Create Ringtones")
|
63 |
-
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload
|
64 |
|
65 |
interface.launch()
|
66 |
|
@@ -75,3 +74,4 @@ interface.launch()
|
|
75 |
|
76 |
|
77 |
|
|
|
|
3 |
import os
|
4 |
from pydub import AudioSegment
|
5 |
|
6 |
+
def process_youtube_url(url, uploaded_file):
|
7 |
try:
|
8 |
filename = None
|
9 |
+
|
10 |
if url:
|
11 |
ydl_opts = {
|
12 |
'format': 'bestaudio/best',
|
|
|
16 |
|
17 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
18 |
info = ydl.extract_info(url, download=True)
|
19 |
+
|
20 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
21 |
|
22 |
elif uploaded_file:
|
23 |
+
filename = uploaded_file
|
24 |
|
25 |
else:
|
26 |
+
return None, None
|
27 |
|
28 |
if not os.path.exists(filename):
|
29 |
+
return None, None
|
30 |
|
31 |
+
# Convert to MP3
|
32 |
+
mp3_filename = "Ringtone.mp3"
|
33 |
audio = AudioSegment.from_file(filename)
|
34 |
audio.export(mp3_filename, format="mp3")
|
35 |
|
36 |
+
# Create ringtone (first 20 seconds)
|
37 |
+
ringtone_filename_m4r = "Apple.m4r"
|
38 |
+
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000]
|
39 |
+
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
|
|
|
|
40 |
|
41 |
+
return mp3_filename, ringtone_filename_m4r
|
42 |
|
43 |
+
except Exception as e:
|
44 |
+
print(f"Error: {e}")
|
45 |
+
return None, None
|
46 |
|
47 |
with gr.Blocks() as interface:
|
48 |
gr.HTML("""
|
|
|
51 |
""")
|
52 |
|
53 |
with gr.Row():
|
54 |
+
youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
|
55 |
+
mp3_upload = gr.File(label="Upload MP3 (Optional)")
|
56 |
+
|
|
|
57 |
with gr.Row():
|
58 |
mp3_download = gr.File(label="Android Ringtone")
|
59 |
iphone_ringtone = gr.File(label="iPhone Ringtone")
|
60 |
|
61 |
process_button = gr.Button("Create Ringtones")
|
62 |
+
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[mp3_download, iphone_ringtone])
|
63 |
|
64 |
interface.launch()
|
65 |
|
|
|
74 |
|
75 |
|
76 |
|
77 |
+
|