import os import gradio as gr import yt_dlp from pydub import AudioSegment import re if not os.path.exists("downloads"): os.makedirs("downloads") def sanitize_filename(filename): """Sanitize the filename by removing or replacing special characters.""" return re.sub(r'[^a-zA-Z0-9_-]', '_', filename) def process_youtube_or_audio(url, recorded_audio): """Process YouTube URL or recorded audio to create ringtones.""" try: filename = None song_name = None if url: ydl_opts = { 'format': 'bestaudio/best', 'outtmpl': 'downloads/%(id)s.%(ext)s', 'cookiefile': 'cookies.txt' } with yt_dlp.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(url, download=True) filename = os.path.join('downloads', f"{info['id']}.webm") song_name = sanitize_filename(info['title']) elif recorded_audio: filename = recorded_audio song_name = "recorded_audio" if not filename or not os.path.exists(filename): return None, None mp3_filename = f"downloads/{song_name}.mp3" if not os.path.exists(mp3_filename): audio = AudioSegment.from_file(filename) audio.export(mp3_filename, format="mp3") ringtone_filename_m4r = f"downloads/{song_name}.m4r" if not os.path.exists(ringtone_filename_m4r): ringtone_audio = AudioSegment.from_file(mp3_filename) \ ringtone_audio = ringtone_audio[:20000] # 20 seconds ringtone_audio.export(ringtone_filename_m4r, format="aac") return mp3_filename, ringtone_filename_m4r except Exception as e: print(f"Error: {e}") return None, None # Gradio UI with gr.Blocks(css=""" body { font-family: Arial, sans-serif; text-align: center; } .light-btn { background-color: #f0f0f0; color: #333; border: 2px solid #ccc; } .light-btn:hover { background-color: #e0e0e0; } """) as interface: gr.HTML("""

 PYTRR

Enter a YouTube URL or record your own audio to create a ringtone.

""") with gr.Row(): with gr.Column(scale=1, min_width=250): gr.HTML('') youtube_url = gr.Textbox(placeholder="Paste YouTube URL here...") with gr.Column(scale=1, min_width=250): gr.HTML('') audio_record = gr.Audio(sources=["microphone"], type="filepath") with gr.Row(): process_button = gr.Button("🎵 Create Ringtones", elem_id="process-btn", elem_classes="light-btn") with gr.Row(): with gr.Column(scale=1, min_width=250): gr.HTML('') mp3_download = gr.File(label="MP3") with gr.Column(scale=1, min_width=250): gr.HTML('') iphone_ringtone = gr.File(label="M4R") process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone]) interface.launch(share=True)