Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os # Ensure it's at the top
|
2 |
+
import gradio as gr
|
3 |
+
import yt_dlp
|
4 |
+
from pydub import AudioSegment
|
5 |
+
import re
|
6 |
+
|
7 |
+
# Create downloads directory safely
|
8 |
+
if not os.path.exists("downloads"):
|
9 |
+
os.makedirs("downloads")
|
10 |
+
|
11 |
+
def sanitize_filename(filename):
|
12 |
+
"""Sanitize the filename by removing or replacing special characters."""
|
13 |
+
return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
|
14 |
+
|
15 |
+
def process_youtube_or_audio(url, recorded_audio):
|
16 |
+
"""Process YouTube URL or recorded audio to create ringtones."""
|
17 |
+
try:
|
18 |
+
filename = None
|
19 |
+
song_name = None
|
20 |
+
|
21 |
+
if url:
|
22 |
+
ydl_opts = {
|
23 |
+
'format': 'bestaudio/best',
|
24 |
+
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
25 |
+
'cookiefile': 'cookies.txt'
|
26 |
+
}
|
27 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
28 |
+
info = ydl.extract_info(url, download=True)
|
29 |
+
filename = os.path.join('downloads', f"{info['id']}.webm")
|
30 |
+
song_name = sanitize_filename(info['title'])
|
31 |
+
|
32 |
+
elif recorded_audio:
|
33 |
+
filename = recorded_audio
|
34 |
+
song_name = "recorded_audio"
|
35 |
+
|
36 |
+
if not filename or not os.path.exists(filename):
|
37 |
+
return None, None
|
38 |
+
|
39 |
+
mp3_filename = f"downloads/{song_name}.mp3"
|
40 |
+
if not os.path.exists(mp3_filename):
|
41 |
+
audio = AudioSegment.from_file(filename)
|
42 |
+
audio.export(mp3_filename, format="mp3")
|
43 |
+
|
44 |
+
ringtone_filename_m4r = f"downloads/{song_name}.m4r"
|
45 |
+
if not os.path.exists(ringtone_filename_m4r):
|
46 |
+
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
|
47 |
+
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
48 |
+
|
49 |
+
return mp3_filename, ringtone_filename_m4r
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
print(f"Error: {e}")
|
53 |
+
return None, None
|
54 |
+
|
55 |
+
# Gradio UI with Improved Layout
|
56 |
+
with gr.Blocks(css="body { font-family: Arial, sans-serif; text-align: center; }") as interface:
|
57 |
+
gr.HTML("""
|
58 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
59 |
+
<h1><i class="fa fa-music"></i> YouTube to Ringtone Converter</h1>
|
60 |
+
<p>Enter a YouTube URL or record your own audio to create a ringtone.</p>
|
61 |
+
""")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column(scale=1, min_width=250):
|
65 |
+
gr.HTML('<label><i class="fa fa-link"></i> YouTube URL</label>')
|
66 |
+
youtube_url = gr.Textbox(placeholder="Paste YouTube URL here...")
|
67 |
+
|
68 |
+
with gr.Column(scale=1, min_width=250):
|
69 |
+
gr.HTML('<label><i class="fa fa-microphone"></i> Record Audio</label>')
|
70 |
+
audio_record = gr.Audio(sources=["microphone"], type="filepath")
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
process_button = gr.Button("🎵 Create Ringtones", elem_id="process-btn")
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
with gr.Column(scale=1, min_width=250):
|
77 |
+
gr.HTML('<label><i class="fa fa-android"></i> Android Ringtone</label>')
|
78 |
+
mp3_download = gr.File(label="Download MP3")
|
79 |
+
|
80 |
+
with gr.Column(scale=1, min_width=250):
|
81 |
+
gr.HTML('<label><i class="fa fa-apple"></i> iPhone Ringtone</label>')
|
82 |
+
iphone_ringtone = gr.File(label="Download M4R")
|
83 |
+
|
84 |
+
process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone])
|
85 |
+
|
86 |
+
interface.launch(share=True)
|