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