Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,10 @@ from pydub import AudioSegment
|
|
6 |
|
7 |
def process_youtube_url(url, uploaded_file, state):
|
8 |
log = []
|
9 |
-
|
10 |
try:
|
|
|
|
|
11 |
if url:
|
12 |
log.append("Starting download...")
|
13 |
|
@@ -20,7 +22,7 @@ def process_youtube_url(url, uploaded_file, state):
|
|
20 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
21 |
info = ydl.extract_info(url, download=True)
|
22 |
log.append(f"Downloaded: {info['title']}")
|
23 |
-
|
24 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
25 |
|
26 |
elif uploaded_file:
|
@@ -30,18 +32,26 @@ def process_youtube_url(url, uploaded_file, state):
|
|
30 |
else:
|
31 |
raise ValueError("No YouTube URL or MP3 file provided.")
|
32 |
|
33 |
-
|
|
|
34 |
|
|
|
|
|
|
|
35 |
mp3_filename = 'Ringtone.mp3'
|
36 |
-
|
37 |
-
|
38 |
log.append(f"MP3 saved: {mp3_filename}")
|
39 |
|
|
|
|
|
|
|
40 |
ringtone_filename_m4a = 'temp_ringtone.m4a'
|
41 |
-
|
42 |
-
|
43 |
log.append(f"Ringtone saved as M4A: {ringtone_filename_m4a}")
|
44 |
|
|
|
45 |
ringtone_filename_m4r = 'Apple.m4r'
|
46 |
os.rename(ringtone_filename_m4a, ringtone_filename_m4r)
|
47 |
log.append(f"Ringtone renamed to .m4r: {ringtone_filename_m4r}")
|
@@ -54,8 +64,8 @@ def process_youtube_url(url, uploaded_file, state):
|
|
54 |
|
55 |
with gr.Blocks() as interface:
|
56 |
gr.HTML("""
|
57 |
-
<h1>
|
58 |
-
<p>Python YouTube Ringtones<br/>
|
59 |
""")
|
60 |
|
61 |
with gr.Row():
|
@@ -64,11 +74,11 @@ with gr.Blocks() as interface:
|
|
64 |
state = gr.State()
|
65 |
|
66 |
with gr.Row():
|
67 |
-
status_log = gr.Textbox(label="Status
|
68 |
-
mp3_download = gr.File(label="
|
69 |
-
iphone_ringtone = gr.File(label="
|
70 |
|
71 |
-
process_button = gr.Button("Create
|
72 |
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload, state], outputs=[status_log, mp3_download, iphone_ringtone])
|
73 |
|
74 |
interface.launch()
|
|
|
6 |
|
7 |
def process_youtube_url(url, uploaded_file, state):
|
8 |
log = []
|
9 |
+
|
10 |
try:
|
11 |
+
filename = None
|
12 |
+
|
13 |
if url:
|
14 |
log.append("Starting download...")
|
15 |
|
|
|
22 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
23 |
info = ydl.extract_info(url, download=True)
|
24 |
log.append(f"Downloaded: {info['title']}")
|
25 |
+
|
26 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
27 |
|
28 |
elif uploaded_file:
|
|
|
32 |
else:
|
33 |
raise ValueError("No YouTube URL or MP3 file provided.")
|
34 |
|
35 |
+
if not os.path.exists(filename):
|
36 |
+
raise FileNotFoundError(f"File not found: {filename}")
|
37 |
|
38 |
+
log.append("Converting audio to MP3 manually...")
|
39 |
+
|
40 |
+
|
41 |
mp3_filename = 'Ringtone.mp3'
|
42 |
+
audio = AudioSegment.from_file(filename)
|
43 |
+
audio.export(mp3_filename, format="mp3")
|
44 |
log.append(f"MP3 saved: {mp3_filename}")
|
45 |
|
46 |
+
log.append("Creating ringtone...")
|
47 |
+
|
48 |
+
|
49 |
ringtone_filename_m4a = 'temp_ringtone.m4a'
|
50 |
+
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 sec clip
|
51 |
+
ringtone_audio.export(ringtone_filename_m4a, format="mp4")
|
52 |
log.append(f"Ringtone saved as M4A: {ringtone_filename_m4a}")
|
53 |
|
54 |
+
|
55 |
ringtone_filename_m4r = 'Apple.m4r'
|
56 |
os.rename(ringtone_filename_m4a, ringtone_filename_m4r)
|
57 |
log.append(f"Ringtone renamed to .m4r: {ringtone_filename_m4r}")
|
|
|
64 |
|
65 |
with gr.Blocks() as interface:
|
66 |
gr.HTML("""
|
67 |
+
<h1>P.Y.T.R.</h1>
|
68 |
+
<p>Python YouTube Ringtones<br/>insert a URL to make ringtones. Upload an MP3 to convert it too.</p>
|
69 |
""")
|
70 |
|
71 |
with gr.Row():
|
|
|
74 |
state = gr.State()
|
75 |
|
76 |
with gr.Row():
|
77 |
+
status_log = gr.Textbox(label="Status")
|
78 |
+
mp3_download = gr.File(label="Android Ringtone")
|
79 |
+
iphone_ringtone = gr.File(label="iPhone Ringtone")
|
80 |
|
81 |
+
process_button = gr.Button("Create Ringtones")
|
82 |
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload, state], outputs=[status_log, mp3_download, iphone_ringtone])
|
83 |
|
84 |
interface.launch()
|