Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,12 @@ from pydub import AudioSegment
|
|
| 5 |
import re
|
| 6 |
import subprocess
|
| 7 |
|
|
|
|
| 8 |
if not os.path.exists("downloads"):
|
| 9 |
os.makedirs("downloads")
|
| 10 |
|
| 11 |
def sanitize_filename(filename):
|
|
|
|
| 12 |
return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
|
| 13 |
|
| 14 |
def process_youtube_or_audio(url, recorded_audio):
|
|
@@ -16,6 +18,7 @@ def process_youtube_or_audio(url, recorded_audio):
|
|
| 16 |
filename = None
|
| 17 |
song_name = None
|
| 18 |
|
|
|
|
| 19 |
if url:
|
| 20 |
ydl_opts = {
|
| 21 |
'format': 'bestaudio/best',
|
|
@@ -27,6 +30,7 @@ def process_youtube_or_audio(url, recorded_audio):
|
|
| 27 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
| 28 |
song_name = sanitize_filename(info['title'])
|
| 29 |
|
|
|
|
| 30 |
elif recorded_audio:
|
| 31 |
filename = recorded_audio
|
| 32 |
song_name = "recorded_audio"
|
|
@@ -34,22 +38,26 @@ def process_youtube_or_audio(url, recorded_audio):
|
|
| 34 |
if not filename or not os.path.exists(filename):
|
| 35 |
return None, None
|
| 36 |
|
|
|
|
| 37 |
audio = AudioSegment.from_file(filename)
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
audio = audio[:20000]
|
| 41 |
|
|
|
|
| 42 |
mp3_filename = f"downloads/{song_name}.mp3"
|
| 43 |
-
|
| 44 |
-
audio.export(mp3_filename, format="mp3")
|
| 45 |
|
|
|
|
| 46 |
m4a_filename = f"downloads/{song_name}.m4a"
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
|
|
|
| 50 |
m4r_filename = f"downloads/{song_name}.m4r"
|
| 51 |
-
|
| 52 |
-
os.rename(m4a_filename, m4r_filename)
|
| 53 |
|
| 54 |
return os.path.abspath(mp3_filename), os.path.abspath(m4r_filename)
|
| 55 |
|
|
@@ -57,6 +65,7 @@ def process_youtube_or_audio(url, recorded_audio):
|
|
| 57 |
print(f"Error: {e}")
|
| 58 |
return None, None
|
| 59 |
|
|
|
|
| 60 |
with gr.Blocks(css="""
|
| 61 |
body { font-family: Arial, sans-serif; text-align: center; }
|
| 62 |
.light-btn {
|
|
@@ -67,20 +76,18 @@ with gr.Blocks(css="""
|
|
| 67 |
font-size: 16px;
|
| 68 |
cursor: pointer;
|
| 69 |
}
|
| 70 |
-
.light-btn:hover {
|
| 71 |
-
background-color: #87CEFA;
|
| 72 |
-
}
|
| 73 |
""") as interface:
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
with gr.Column(scale=1, min_width=250):
|
|
@@ -98,13 +105,14 @@ with gr.Blocks(css="""
|
|
| 98 |
with gr.Column(scale=1, min_width=250):
|
| 99 |
gr.HTML('<label> Android Ringtone</label>')
|
| 100 |
mp3_download = gr.File(label="Android")
|
| 101 |
-
android_instructions = gr.Textbox(label="
|
| 102 |
|
| 103 |
with gr.Column(scale=1, min_width=250):
|
| 104 |
gr.HTML('<label> iPhone Ringtone</label>')
|
| 105 |
iphone_ringtone = gr.File(label="Apple")
|
| 106 |
-
iphone_instructions = gr.Textbox(label="
|
| 107 |
|
| 108 |
process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone])
|
| 109 |
|
| 110 |
interface.launch(share=True)
|
|
|
|
|
|
| 5 |
import re
|
| 6 |
import subprocess
|
| 7 |
|
| 8 |
+
# Ensure the downloads directory exists
|
| 9 |
if not os.path.exists("downloads"):
|
| 10 |
os.makedirs("downloads")
|
| 11 |
|
| 12 |
def sanitize_filename(filename):
|
| 13 |
+
"""Sanitize filenames to avoid special characters."""
|
| 14 |
return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
|
| 15 |
|
| 16 |
def process_youtube_or_audio(url, recorded_audio):
|
|
|
|
| 18 |
filename = None
|
| 19 |
song_name = None
|
| 20 |
|
| 21 |
+
# Extract and download YouTube audio
|
| 22 |
if url:
|
| 23 |
ydl_opts = {
|
| 24 |
'format': 'bestaudio/best',
|
|
|
|
| 30 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
| 31 |
song_name = sanitize_filename(info['title'])
|
| 32 |
|
| 33 |
+
# Handle recorded audio
|
| 34 |
elif recorded_audio:
|
| 35 |
filename = recorded_audio
|
| 36 |
song_name = "recorded_audio"
|
|
|
|
| 38 |
if not filename or not os.path.exists(filename):
|
| 39 |
return None, None
|
| 40 |
|
| 41 |
+
# Load audio
|
| 42 |
audio = AudioSegment.from_file(filename)
|
| 43 |
+
|
| 44 |
+
# Trim to 20 seconds (iPhone ringtone limit)
|
| 45 |
+
if len(audio) > 20000:
|
| 46 |
audio = audio[:20000]
|
| 47 |
|
| 48 |
+
# Convert to MP3 (Android)
|
| 49 |
mp3_filename = f"downloads/{song_name}.mp3"
|
| 50 |
+
audio.export(mp3_filename, format="mp3")
|
|
|
|
| 51 |
|
| 52 |
+
# Convert to M4A first (needed for M4R)
|
| 53 |
m4a_filename = f"downloads/{song_name}.m4a"
|
| 54 |
+
subprocess.run([
|
| 55 |
+
'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
|
| 56 |
+
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 57 |
|
| 58 |
+
# Rename to M4R (iPhone)
|
| 59 |
m4r_filename = f"downloads/{song_name}.m4r"
|
| 60 |
+
os.rename(m4a_filename, m4r_filename)
|
|
|
|
| 61 |
|
| 62 |
return os.path.abspath(mp3_filename), os.path.abspath(m4r_filename)
|
| 63 |
|
|
|
|
| 65 |
print(f"Error: {e}")
|
| 66 |
return None, None
|
| 67 |
|
| 68 |
+
# Gradio UI
|
| 69 |
with gr.Blocks(css="""
|
| 70 |
body { font-family: Arial, sans-serif; text-align: center; }
|
| 71 |
.light-btn {
|
|
|
|
| 76 |
font-size: 16px;
|
| 77 |
cursor: pointer;
|
| 78 |
}
|
| 79 |
+
.light-btn:hover { background-color: #87CEFA; }
|
|
|
|
|
|
|
| 80 |
""") as interface:
|
| 81 |
|
| 82 |
+
gr.HTML("""
|
| 83 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 84 |
+
<h1><i class="fas fa-music"></i> PYTR</h1>
|
| 85 |
+
<p>Python YouTube Ringtones. Enter a YouTube URL or record audio to create ringtones.</p>
|
| 86 |
+
<p>
|
| 87 |
+
<a href="https://ringtone.JesseJesse.xyz" target="_blank">ringtone.JesseJesse.xyz</a> |
|
| 88 |
+
<a href="https://sudo-self.github.io/pytr/" target="_blank">GitHub</a>
|
| 89 |
+
</p>
|
| 90 |
+
""")
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
with gr.Column(scale=1, min_width=250):
|
|
|
|
| 105 |
with gr.Column(scale=1, min_width=250):
|
| 106 |
gr.HTML('<label> Android Ringtone</label>')
|
| 107 |
mp3_download = gr.File(label="Android")
|
| 108 |
+
android_instructions = gr.Textbox(label="Install", placeholder="Move the .mp3 file to the ringtones folder", lines=2)
|
| 109 |
|
| 110 |
with gr.Column(scale=1, min_width=250):
|
| 111 |
gr.HTML('<label> iPhone Ringtone</label>')
|
| 112 |
iphone_ringtone = gr.File(label="Apple")
|
| 113 |
+
iphone_instructions = gr.Textbox(label="Install", placeholder="Swipe left and open GarageBand. Long-press the ringtone file, select share, then choose 'Ringtone' to export.", lines=2)
|
| 114 |
|
| 115 |
process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone])
|
| 116 |
|
| 117 |
interface.launch(share=True)
|
| 118 |
+
|