Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ 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 |
|
@@ -23,44 +23,44 @@ def process_youtube_url(url, uploaded_file):
|
|
23 |
filename = uploaded_file.name
|
24 |
|
25 |
else:
|
26 |
-
|
27 |
|
28 |
if not os.path.exists(filename):
|
29 |
-
|
30 |
|
31 |
-
mp3_filename =
|
32 |
audio = AudioSegment.from_file(filename)
|
33 |
audio.export(mp3_filename, format="mp3")
|
34 |
|
35 |
-
ringtone_filename_m4a =
|
36 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 sec clip
|
37 |
ringtone_audio.export(ringtone_filename_m4a, format="mp4")
|
38 |
|
39 |
-
ringtone_filename_m4r =
|
40 |
os.rename(ringtone_filename_m4a, ringtone_filename_m4r)
|
41 |
|
42 |
-
return mp3_filename, ringtone_filename_m4r
|
43 |
|
44 |
-
except Exception
|
45 |
-
|
46 |
-
return None, None
|
47 |
|
48 |
with gr.Blocks() as interface:
|
49 |
gr.HTML("""
|
50 |
<h1>Python YouTube Ringtones</h1>
|
51 |
-
<p>Insert a URL to make ringtones or
|
52 |
""")
|
53 |
|
54 |
with gr.Row():
|
55 |
-
youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
|
56 |
mp3_upload = gr.File(label="Upload MP3 (Optional)", type="filepath")
|
|
|
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], outputs=[mp3_download, iphone_ringtone])
|
64 |
|
65 |
interface.launch()
|
66 |
|
@@ -74,3 +74,4 @@ interface.launch()
|
|
74 |
|
75 |
|
76 |
|
|
|
|
3 |
import os
|
4 |
from pydub import AudioSegment
|
5 |
|
6 |
+
def process_youtube_url(url, uploaded_file, state):
|
7 |
try:
|
8 |
filename = None
|
9 |
|
|
|
23 |
filename = uploaded_file.name
|
24 |
|
25 |
else:
|
26 |
+
raise ValueError("No YouTube URL or MP3 file provided.")
|
27 |
|
28 |
if not os.path.exists(filename):
|
29 |
+
raise FileNotFoundError(f"File not found: {filename}")
|
30 |
|
31 |
+
mp3_filename = 'Ringtone.mp3'
|
32 |
audio = AudioSegment.from_file(filename)
|
33 |
audio.export(mp3_filename, format="mp3")
|
34 |
|
35 |
+
ringtone_filename_m4a = 'temp_ringtone.m4a'
|
36 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 sec clip
|
37 |
ringtone_audio.export(ringtone_filename_m4a, format="mp4")
|
38 |
|
39 |
+
ringtone_filename_m4r = 'Apple.m4r'
|
40 |
os.rename(ringtone_filename_m4a, ringtone_filename_m4r)
|
41 |
|
42 |
+
return mp3_filename, ringtone_filename_m4r, state
|
43 |
|
44 |
+
except Exception:
|
45 |
+
return None, None, state
|
|
|
46 |
|
47 |
with gr.Blocks() as interface:
|
48 |
gr.HTML("""
|
49 |
<h1>Python YouTube Ringtones</h1>
|
50 |
+
<p>Insert a URL to make ringtones or Upload an MP3 to convert</p>
|
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)", type="filepath")
|
56 |
+
state = gr.State()
|
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, state], outputs=[mp3_download, iphone_ringtone])
|
64 |
|
65 |
interface.launch()
|
66 |
|
|
|
74 |
|
75 |
|
76 |
|
77 |
+
|