Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import yt_dlp | |
from pydub import AudioSegment | |
import re | |
import subprocess | |
if not os.path.exists("downloads"): | |
os.makedirs("downloads") | |
def sanitize_filename(filename): | |
return re.sub(r'[^a-zA-Z0-9_-]', '_', filename) | |
def process_youtube_or_audio(url, recorded_audio): | |
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 | |
audio = AudioSegment.from_file(filename) | |
if len(audio) > 20000: | |
audio = audio[:20000] | |
mp3_filename = f"downloads/{song_name}.mp3" | |
if not os.path.exists(mp3_filename): | |
audio.export(mp3_filename, format="mp3") | |
m4a_filename = f"downloads/{song_name}.m4a" | |
if not os.path.exists(m4a_filename): | |
subprocess.run(['ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename]) | |
m4r_filename = f"downloads/{song_name}.m4r" | |
if not os.path.exists(m4r_filename): | |
os.rename(m4a_filename, m4r_filename) | |
return mp3_filename, m4r_filename | |
except Exception as e: | |
print(f"Error: {e}") | |
return None, None | |
with gr.Blocks(css=""" | |
body { font-family: Arial, sans-serif; text-align: center; } | |
.light-btn { | |
background-color: #ADD8E6; | |
color: #333; | |
border: 2px solid #ccc; | |
padding: 10px 20px; | |
font-size: 16px; | |
cursor: pointer; | |
} | |
.light-btn:hover { | |
background-color: #87CEFA; | |
} | |
""") as interface: | |
gr.HTML(""" | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"> | |
<h1><i class="fas fa-music"></i> PYTR</h1> | |
<p>Python YouTube Ringtones. Enter a YouTube URL or record audio to create ringtones</p> | |
""") | |
with gr.Row(): | |
with gr.Column(scale=1, min_width=250): | |
gr.HTML('<label><i class="fas fa-link"></i> YouTube URL</label>') | |
youtube_url = gr.Textbox(placeholder="Enter the URL here...", show_label=False) | |
with gr.Column(scale=1, min_width=250): | |
gr.HTML('<label><i class="fas fa-microphone"></i> Record Audio</label>') | |
audio_record = gr.Audio(sources=["microphone"], type="filepath", show_label=False) | |
with gr.Row(): | |
process_button = gr.Button("π΅ Create Ringtones", elem_classes="light-btn") | |
with gr.Row(): | |
with gr.Column(scale=1, min_width=250): | |
gr.HTML('<label> Android Ringtone</label>') | |
mp3_download = gr.File(label="Android") | |
with gr.Column(scale=1, min_width=250): | |
gr.HTML('<label> iPhone Ringtone</label>') | |
iphone_ringtone = gr.File(label="Apple") | |
process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone]) | |
interface.launch(share=True) | |