Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,15 +3,11 @@ import yt_dlp
|
|
3 |
import os
|
4 |
from pydub import AudioSegment
|
5 |
|
6 |
-
def process_youtube_url(url, uploaded_file
|
7 |
-
log = []
|
8 |
-
|
9 |
try:
|
10 |
filename = None
|
11 |
|
12 |
if url:
|
13 |
-
log.append("Starting download...")
|
14 |
-
|
15 |
ydl_opts = {
|
16 |
'format': 'bestaudio/best',
|
17 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
@@ -20,42 +16,33 @@ def process_youtube_url(url, uploaded_file, state):
|
|
20 |
|
21 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
22 |
info = ydl.extract_info(url, download=True)
|
23 |
-
log.append(f"Downloaded: {info['title']}")
|
24 |
|
25 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
26 |
|
27 |
elif uploaded_file:
|
28 |
filename = uploaded_file.name
|
29 |
-
log.append(f"Uploaded MP3 file: {filename}")
|
30 |
|
31 |
else:
|
32 |
-
|
33 |
|
34 |
if not os.path.exists(filename):
|
35 |
-
|
36 |
-
|
37 |
-
log.append("Converting audio to MP3 manually...")
|
38 |
|
39 |
mp3_filename = 'Ringtone.mp3'
|
40 |
audio = AudioSegment.from_file(filename)
|
41 |
audio.export(mp3_filename, format="mp3")
|
42 |
-
log.append(f"MP3 saved: {mp3_filename}")
|
43 |
-
|
44 |
-
log.append("Creating ringtone...")
|
45 |
|
46 |
ringtone_filename_m4a = 'temp_ringtone.m4a'
|
47 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 sec clip
|
48 |
ringtone_audio.export(ringtone_filename_m4a, format="mp4")
|
49 |
-
log.append(f"Ringtone saved as M4A: {ringtone_filename_m4a}")
|
50 |
|
51 |
ringtone_filename_m4r = 'Apple.m4r'
|
52 |
os.rename(ringtone_filename_m4a, ringtone_filename_m4r)
|
53 |
-
log.append(f"Ringtone renamed to .m4r: {ringtone_filename_m4r}")
|
54 |
|
55 |
-
return mp3_filename, ringtone_filename_m4r
|
56 |
|
57 |
-
except Exception
|
58 |
-
return None, None
|
59 |
|
60 |
with gr.Blocks() as interface:
|
61 |
gr.HTML("""
|
@@ -66,14 +53,13 @@ with gr.Blocks() as interface:
|
|
66 |
with gr.Row():
|
67 |
youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
|
68 |
mp3_upload = gr.File(label="Upload MP3 (Optional)", type="filepath")
|
69 |
-
state = gr.State()
|
70 |
|
71 |
with gr.Row():
|
72 |
mp3_download = gr.File(label="Android Ringtone")
|
73 |
iphone_ringtone = gr.File(label="iPhone Ringtone")
|
74 |
|
75 |
process_button = gr.Button("Create Ringtones")
|
76 |
-
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload
|
77 |
|
78 |
interface.launch()
|
79 |
|
|
|
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',
|
13 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
|
|
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.name
|
|
|
24 |
|
25 |
else:
|
26 |
+
return None, None
|
27 |
|
28 |
if not os.path.exists(filename):
|
29 |
+
return None, None
|
|
|
|
|
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
|
43 |
|
44 |
+
except Exception:
|
45 |
+
return None, None
|
46 |
|
47 |
with gr.Blocks() as interface:
|
48 |
gr.HTML("""
|
|
|
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 |
|
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 |
|