Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,10 @@ import yt_dlp
|
|
| 4 |
from pydub import AudioSegment
|
| 5 |
import re
|
| 6 |
import subprocess
|
| 7 |
-
import time
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
def sanitize_filename(filename):
|
| 13 |
"""Sanitize filenames to avoid special characters."""
|
|
@@ -18,48 +18,48 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
| 18 |
filename = None
|
| 19 |
song_name = None
|
| 20 |
|
| 21 |
-
print(f"URL: {url}")
|
| 22 |
-
print(f"Uploaded audio: {uploaded_audio}")
|
| 23 |
-
print(f"Start time: {start_time}, End time: {end_time}")
|
| 24 |
|
| 25 |
-
#
|
| 26 |
if url:
|
| 27 |
-
print("
|
| 28 |
ydl_opts = {
|
| 29 |
'format': 'bestaudio/best',
|
| 30 |
-
'outtmpl': '
|
| 31 |
-
'cookiefile': 'cookies.txt'
|
|
|
|
| 32 |
}
|
| 33 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 34 |
info = ydl.extract_info(url, download=True)
|
| 35 |
-
filename = os.path.join(
|
| 36 |
song_name = sanitize_filename(info['title'])
|
| 37 |
|
| 38 |
-
#
|
| 39 |
elif uploaded_audio:
|
| 40 |
print("Processing uploaded audio...")
|
| 41 |
-
filename = uploaded_audio
|
| 42 |
song_name = "uploaded_audio"
|
| 43 |
|
|
|
|
| 44 |
if not filename or not os.path.exists(filename):
|
| 45 |
-
print(f"Error: No valid file found at {filename}")
|
| 46 |
return None, None
|
| 47 |
|
| 48 |
-
# Load
|
| 49 |
audio = AudioSegment.from_file(filename)
|
| 50 |
-
start_time_ms = start_time * 1000
|
| 51 |
-
end_time_ms = end_time * 1000
|
| 52 |
-
start_time_ms = max(0, min(start_time_ms, len(audio)))
|
| 53 |
-
end_time_ms = max(start_time_ms, min(end_time_ms, len(audio)))
|
| 54 |
|
| 55 |
trimmed_audio = audio[start_time_ms:end_time_ms]
|
| 56 |
|
| 57 |
-
# Export trimmed audio
|
| 58 |
-
mp3_filename = f"
|
| 59 |
trimmed_audio.export(mp3_filename, format="mp3")
|
| 60 |
|
| 61 |
-
# Convert to
|
| 62 |
-
m4a_filename = f"
|
| 63 |
try:
|
| 64 |
subprocess.run([
|
| 65 |
'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
|
|
@@ -71,18 +71,73 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
| 71 |
print(f"ffmpeg error: {e}")
|
| 72 |
return None, None
|
| 73 |
|
| 74 |
-
#
|
|
|
|
| 75 |
if os.path.exists(m4a_filename):
|
| 76 |
-
m4r_filename = f"downloads/{song_name}.m4r"
|
| 77 |
os.rename(m4a_filename, m4r_filename)
|
| 78 |
-
print(f"Files saved: {mp3_filename}, {m4r_filename}")
|
| 79 |
-
return
|
| 80 |
else:
|
| 81 |
print("Error: M4A file not created.")
|
| 82 |
return None, None
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
-
print(f"Error: {e}")
|
| 86 |
return None, None
|
| 87 |
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from pydub import AudioSegment
|
| 5 |
import re
|
| 6 |
import subprocess
|
|
|
|
| 7 |
|
| 8 |
+
# Ensure static directory exists for storing audio files
|
| 9 |
+
DOWNLOAD_DIR = "static"
|
| 10 |
+
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
| 11 |
|
| 12 |
def sanitize_filename(filename):
|
| 13 |
"""Sanitize filenames to avoid special characters."""
|
|
|
|
| 18 |
filename = None
|
| 19 |
song_name = None
|
| 20 |
|
| 21 |
+
print(f"Processing URL: {url}")
|
| 22 |
+
print(f"Uploaded audio: {uploaded_audio}")
|
| 23 |
+
print(f"Start time: {start_time}, End time: {end_time}")
|
| 24 |
|
| 25 |
+
# Download from YouTube if URL is provided
|
| 26 |
if url:
|
| 27 |
+
print("Downloading from YouTube...")
|
| 28 |
ydl_opts = {
|
| 29 |
'format': 'bestaudio/best',
|
| 30 |
+
'outtmpl': f'{DOWNLOAD_DIR}/%(id)s.%(ext)s',
|
| 31 |
+
'cookiefile': 'cookies.txt',
|
| 32 |
+
'noplaylist': True,
|
| 33 |
}
|
| 34 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 35 |
info = ydl.extract_info(url, download=True)
|
| 36 |
+
filename = os.path.join(DOWNLOAD_DIR, f"{info['id']}.webm")
|
| 37 |
song_name = sanitize_filename(info['title'])
|
| 38 |
|
| 39 |
+
# Use uploaded audio if available
|
| 40 |
elif uploaded_audio:
|
| 41 |
print("Processing uploaded audio...")
|
| 42 |
+
filename = uploaded_audio
|
| 43 |
song_name = "uploaded_audio"
|
| 44 |
|
| 45 |
+
# If no valid file exists, return an error
|
| 46 |
if not filename or not os.path.exists(filename):
|
| 47 |
+
print(f"Error: No valid file found at {filename}")
|
| 48 |
return None, None
|
| 49 |
|
| 50 |
+
# Load and trim audio
|
| 51 |
audio = AudioSegment.from_file(filename)
|
| 52 |
+
start_time_ms = max(0, min(start_time * 1000, len(audio)))
|
| 53 |
+
end_time_ms = max(start_time_ms, min(end_time * 1000, len(audio)))
|
|
|
|
|
|
|
| 54 |
|
| 55 |
trimmed_audio = audio[start_time_ms:end_time_ms]
|
| 56 |
|
| 57 |
+
# Export trimmed audio as MP3
|
| 58 |
+
mp3_filename = f"{DOWNLOAD_DIR}/{song_name}.mp3"
|
| 59 |
trimmed_audio.export(mp3_filename, format="mp3")
|
| 60 |
|
| 61 |
+
# Convert MP3 to M4R for iPhone
|
| 62 |
+
m4a_filename = f"{DOWNLOAD_DIR}/{song_name}.m4a"
|
| 63 |
try:
|
| 64 |
subprocess.run([
|
| 65 |
'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
|
|
|
|
| 71 |
print(f"ffmpeg error: {e}")
|
| 72 |
return None, None
|
| 73 |
|
| 74 |
+
# Rename to M4R format for iPhone ringtones
|
| 75 |
+
m4r_filename = f"{DOWNLOAD_DIR}/{song_name}.m4r"
|
| 76 |
if os.path.exists(m4a_filename):
|
|
|
|
| 77 |
os.rename(m4a_filename, m4r_filename)
|
| 78 |
+
print(f"Files saved: {mp3_filename}, {m4r_filename}")
|
| 79 |
+
return mp3_filename, m4r_filename
|
| 80 |
else:
|
| 81 |
print("Error: M4A file not created.")
|
| 82 |
return None, None
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
+
print(f"Error: {e}")
|
| 86 |
return None, None
|
| 87 |
|
| 88 |
|
| 89 |
+
# === GRADIO UI ===
|
| 90 |
+
with gr.Blocks(css="""
|
| 91 |
+
body { font-family: Arial, sans-serif; text-align: center; }
|
| 92 |
+
.light-btn { background-color: #ADD8E6; color: #333; border: 2px solid #ccc; padding: 10px 20px; font-size: 16px; cursor: pointer; }
|
| 93 |
+
.light-btn:hover { background-color: #87CEFA; }
|
| 94 |
+
""") as interface:
|
| 95 |
+
|
| 96 |
+
gr.HTML("""
|
| 97 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 98 |
+
<h1><i class="fas fa-music"></i> PYTR</h1>
|
| 99 |
+
<p>Python YouTube Ringtones. Enter a YouTube URL or upload audio to create ringtones.</p>
|
| 100 |
+
<p>
|
| 101 |
+
<a href="https://ringtones.JesseJesse.xyz" target="_blank">Ringtones</a>
|
| 102 |
+
<a href="https://pub-c1de1cb456e74d6bbbee111ba9e6c757.r2.dev/iphone.png" target="_blank">iPhone xfers</a>
|
| 103 |
+
<a href="https://youtube.com" target="_blank">YouTube</a>
|
| 104 |
+
</p>
|
| 105 |
+
""")
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
with gr.Column(scale=1, min_width=250):
|
| 109 |
+
gr.HTML('<label><i class="fas fa-link"></i> YouTube URL</label>')
|
| 110 |
+
youtube_url = gr.Textbox(placeholder="Enter the URL here...", show_label=False)
|
| 111 |
+
|
| 112 |
+
with gr.Column(scale=1, min_width=250):
|
| 113 |
+
gr.HTML('<label><i class="fas fa-upload"></i> Upload Audio</label>')
|
| 114 |
+
audio_upload = gr.File(label="Upload Audio", type="filepath", show_label=False)
|
| 115 |
+
|
| 116 |
+
with gr.Row():
|
| 117 |
+
gr.HTML("<h3>Trim Audio (Optional)</h3>")
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
start_time = gr.Slider(0, 20, value=0, label="Start Time (seconds)")
|
| 121 |
+
end_time = gr.Slider(1, 20, value=20, label="End Time (seconds)")
|
| 122 |
+
|
| 123 |
+
with gr.Row():
|
| 124 |
+
process_button = gr.Button("Create Ringtones", elem_classes="light-btn")
|
| 125 |
+
|
| 126 |
+
with gr.Row():
|
| 127 |
+
with gr.Column(scale=1, min_width=250):
|
| 128 |
+
gr.HTML('<label> Android Ringtone</label>')
|
| 129 |
+
mp3_download = gr.File(label="Android")
|
| 130 |
+
android_instructions = gr.Textbox(label="Install", placeholder="Move the .mp3 file to the ringtones folder", lines=2)
|
| 131 |
+
|
| 132 |
+
with gr.Column(scale=1, min_width=250):
|
| 133 |
+
gr.HTML('<label> iPhone Ringtone</label>')
|
| 134 |
+
iphone_ringtone = gr.File(label="Apple")
|
| 135 |
+
iphone_instructions = gr.Textbox(label="Install", placeholder="Open GarageBand on your iPhone. Import the .m4r file and set it as a ringtone.", lines=4)
|
| 136 |
+
|
| 137 |
+
process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_upload, start_time, end_time], outputs=[mp3_download, iphone_ringtone])
|
| 138 |
+
|
| 139 |
+
# Launch with queue for stability
|
| 140 |
+
interface.queue().launch(share=True, server_port=7860)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|