Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import yt_dlp
|
3 |
+
import os
|
4 |
+
from pydub import AudioSegment
|
5 |
+
import re
|
6 |
+
|
7 |
+
os.makedirs("downloads", exist_ok=True)
|
8 |
+
|
9 |
+
def sanitize_filename(filename):
|
10 |
+
"""Sanitize the filename by removing or replacing special characters."""
|
11 |
+
return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
|
12 |
+
|
13 |
+
def process_youtube_or_audio(url, recorded_audio):
|
14 |
+
"""Process YouTube URL or recorded audio to create ringtones."""
|
15 |
+
try:
|
16 |
+
filename = None
|
17 |
+
song_name = None
|
18 |
+
|
19 |
+
|
20 |
+
if url:
|
21 |
+
ydl_opts = {
|
22 |
+
'format': 'bestaudio/best',
|
23 |
+
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
24 |
+
'cookiefile': 'cookies.txt' #cookies.txt
|
25 |
+
}
|
26 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
27 |
+
info = ydl.extract_info(url, download=True)
|
28 |
+
filename = os.path.join('downloads', f"{info['id']}.webm")
|
29 |
+
song_name = sanitize_filename(info['title'])
|
30 |
+
|
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 |
+
|
40 |
+
mp3_filename = f"downloads/{song_name}.mp3"
|
41 |
+
if not os.path.exists(mp3_filename):
|
42 |
+
audio = AudioSegment.from_file(filename)
|
43 |
+
audio.export(mp3_filename, format="mp3")
|
44 |
+
|
45 |
+
|
46 |
+
ringtone_filename_m4r = f"downloads/{song_name}.m4r"
|
47 |
+
if not os.path.exists(ringtone_filename_m4r):
|
48 |
+
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
|
49 |
+
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
50 |
+
|
51 |
+
return mp3_filename, ringtone_filename_m4r
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
print(f"Error: {e}")
|
55 |
+
return None, None
|
56 |
+
|
57 |
+
# Gradio UI
|
58 |
+
with gr.Blocks() as interface:
|
59 |
+
gr.HTML("""
|
60 |
+
<h1>Python YouTube Ringtones</h1>
|
61 |
+
<p>Insert a URL to create ringtones or record your own audio.</p>
|
62 |
+
""")
|
63 |
+
|
64 |
+
with gr.Row():
|
65 |
+
youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
|
66 |
+
audio_record = gr.Audio(sources=["microphone"], type="filepath", label="Record Audio")
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
mp3_download = gr.File(label="Download Android Ringtone")
|
70 |
+
iphone_ringtone = gr.File(label="Download iPhone Ringtone")
|
71 |
+
|
72 |
+
process_button = gr.Button("Create Ringtones")
|
73 |
+
process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone])
|
74 |
+
|
75 |
+
interface.launch(share=True)
|