Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,16 +28,16 @@ def process_youtube_url(url, uploaded_file):
|
|
| 28 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 29 |
info = ydl.extract_info(url, download=True)
|
| 30 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
| 31 |
-
song_name = info['title']
|
| 32 |
|
| 33 |
# Use uploaded file if URL is not provided
|
| 34 |
elif uploaded_file:
|
| 35 |
filename = uploaded_file.name
|
| 36 |
-
song_name = os.path.splitext(uploaded_file.name)[0]
|
| 37 |
|
| 38 |
# Ensure file exists
|
| 39 |
if not filename or not os.path.exists(filename):
|
| 40 |
-
return None, None
|
| 41 |
|
| 42 |
# Convert to MP3 if not already converted
|
| 43 |
mp3_filename = f"downloads/{song_name}.mp3"
|
|
@@ -51,12 +51,12 @@ def process_youtube_url(url, uploaded_file):
|
|
| 51 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
|
| 52 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
| 53 |
|
| 54 |
-
# Return file
|
| 55 |
-
return
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
print(f"Error: {e}")
|
| 59 |
-
return None, None
|
| 60 |
|
| 61 |
# Gradio UI
|
| 62 |
with gr.Blocks() as interface:
|
|
@@ -70,12 +70,11 @@ with gr.Blocks() as interface:
|
|
| 70 |
mp3_upload = gr.File(label="Upload MP3 (Optional)")
|
| 71 |
|
| 72 |
with gr.Row():
|
| 73 |
-
song_name_output = gr.Textbox(label="Song Name", interactive=False)
|
| 74 |
mp3_download = gr.File(label="Download Android Ringtone")
|
| 75 |
iphone_ringtone = gr.File(label="Download iPhone Ringtone")
|
| 76 |
|
| 77 |
process_button = gr.Button("Create Ringtones")
|
| 78 |
-
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[
|
| 79 |
|
| 80 |
interface.launch(share=True)
|
| 81 |
|
|
@@ -94,3 +93,4 @@ interface.launch(share=True)
|
|
| 94 |
|
| 95 |
|
| 96 |
|
|
|
|
|
|
| 28 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 29 |
info = ydl.extract_info(url, download=True)
|
| 30 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
| 31 |
+
song_name = sanitize_filename(info['title'])
|
| 32 |
|
| 33 |
# Use uploaded file if URL is not provided
|
| 34 |
elif uploaded_file:
|
| 35 |
filename = uploaded_file.name
|
| 36 |
+
song_name = sanitize_filename(os.path.splitext(uploaded_file.name)[0])
|
| 37 |
|
| 38 |
# Ensure file exists
|
| 39 |
if not filename or not os.path.exists(filename):
|
| 40 |
+
return None, None, None
|
| 41 |
|
| 42 |
# Convert to MP3 if not already converted
|
| 43 |
mp3_filename = f"downloads/{song_name}.mp3"
|
|
|
|
| 51 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
|
| 52 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
| 53 |
|
| 54 |
+
# Return file names for download
|
| 55 |
+
return mp3_filename, ringtone_filename_m4r
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
print(f"Error: {e}")
|
| 59 |
+
return None, None
|
| 60 |
|
| 61 |
# Gradio UI
|
| 62 |
with gr.Blocks() as interface:
|
|
|
|
| 70 |
mp3_upload = gr.File(label="Upload MP3 (Optional)")
|
| 71 |
|
| 72 |
with gr.Row():
|
|
|
|
| 73 |
mp3_download = gr.File(label="Download Android Ringtone")
|
| 74 |
iphone_ringtone = gr.File(label="Download iPhone Ringtone")
|
| 75 |
|
| 76 |
process_button = gr.Button("Create Ringtones")
|
| 77 |
+
process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[mp3_download, iphone_ringtone])
|
| 78 |
|
| 79 |
interface.launch(share=True)
|
| 80 |
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
+
|