Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import yt_dlp
|
|
| 4 |
from pydub import AudioSegment
|
| 5 |
import re
|
| 6 |
import subprocess
|
|
|
|
| 7 |
|
| 8 |
if not os.path.exists("downloads"):
|
| 9 |
os.makedirs("downloads")
|
|
@@ -59,79 +60,29 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
| 59 |
|
| 60 |
# Convert to m4r format for iPhone
|
| 61 |
m4a_filename = f"downloads/{song_name}.m4a"
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Error: {e}") # Debug: Print the error
|
| 76 |
return None, None
|
| 77 |
|
| 78 |
|
| 79 |
-
with gr.Blocks(css="""
|
| 80 |
-
body { font-family: Arial, sans-serif; text-align: center; }
|
| 81 |
-
.light-btn {
|
| 82 |
-
background-color: #ADD8E6;
|
| 83 |
-
color: #333;
|
| 84 |
-
border: 2px solid #ccc;
|
| 85 |
-
padding: 10px 20px;
|
| 86 |
-
font-size: 16px;
|
| 87 |
-
cursor: pointer;
|
| 88 |
-
}
|
| 89 |
-
.light-btn:hover { background-color: #87CEFA; }
|
| 90 |
-
""") as interface:
|
| 91 |
-
|
| 92 |
-
gr.HTML("""
|
| 93 |
-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 94 |
-
<h1><i class="fas fa-music"></i> PYTR</h1>
|
| 95 |
-
<p>Python YouTube Ringtones. Enter a YouTube URL or upload audio to create ringtones.</p>
|
| 96 |
-
<p>
|
| 97 |
-
<a href="https://ringtones.JesseJesse.xyz" target="_blank">Ringtones</a>
|
| 98 |
-
<a href="https://pub-c1de1cb456e74d6bbbee111ba9e6c757.r2.dev/iphone.png" target="_blank">iPhone xfers</a>
|
| 99 |
-
<a href="https://youtube.com" target="_blank">YouTube</a>
|
| 100 |
-
</p>
|
| 101 |
-
""")
|
| 102 |
-
|
| 103 |
-
with gr.Row():
|
| 104 |
-
with gr.Column(scale=1, min_width=250):
|
| 105 |
-
gr.HTML('<label><i class="fas fa-link"></i> YouTube URL</label>')
|
| 106 |
-
youtube_url = gr.Textbox(placeholder="Enter the URL here...", show_label=False)
|
| 107 |
-
|
| 108 |
-
with gr.Column(scale=1, min_width=250):
|
| 109 |
-
gr.HTML('<label><i class="fas fa-upload"></i> Upload Audio</label>')
|
| 110 |
-
# Change type to 'filepath' for Hugging Face compatibility
|
| 111 |
-
audio_upload = gr.File(label="Upload Audio", type="filepath", show_label=False)
|
| 112 |
-
|
| 113 |
-
with gr.Row():
|
| 114 |
-
gr.HTML("<h3>Trim Audio (Optional)</h3>")
|
| 115 |
-
|
| 116 |
-
with gr.Row():
|
| 117 |
-
start_time = gr.Slider(0, 20, value=0, label="Start Time (seconds)")
|
| 118 |
-
end_time = gr.Slider(1, 20, value=20, label="End Time (seconds)")
|
| 119 |
-
|
| 120 |
-
with gr.Row():
|
| 121 |
-
process_button = gr.Button("Create Ringtones", elem_classes="light-btn")
|
| 122 |
-
|
| 123 |
-
with gr.Row():
|
| 124 |
-
with gr.Column(scale=1, min_width=250):
|
| 125 |
-
gr.HTML('<label> Android Ringtone</label>')
|
| 126 |
-
mp3_download = gr.File(label="Android")
|
| 127 |
-
android_instructions = gr.Textbox(label="Install", placeholder="Move the .mp3 file to the ringtones folder", lines=2)
|
| 128 |
-
|
| 129 |
-
with gr.Column(scale=1, min_width=250):
|
| 130 |
-
gr.HTML('<label> iPhone Ringtone</label>')
|
| 131 |
-
iphone_ringtone = gr.File(label="Apple")
|
| 132 |
-
iphone_instructions = gr.Textbox(label="Install", placeholder="Open GarageBand on your iPhone. Create a new project with Audio Recorder. Tap on the Tracks view (≡ icon in top-left). Tap the Loop icon (top-right corner). Select 'Files', then tap 'Browse items from the Files app'. Locate your .m4r file and tap it to import into GarageBand.", lines=4)
|
| 133 |
-
|
| 134 |
-
process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_upload, start_time, end_time], outputs=[mp3_download, iphone_ringtone])
|
| 135 |
-
|
| 136 |
-
interface.launch(share=True)
|
| 137 |
-
|
|
|
|
| 4 |
from pydub import AudioSegment
|
| 5 |
import re
|
| 6 |
import subprocess
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
if not os.path.exists("downloads"):
|
| 10 |
os.makedirs("downloads")
|
|
|
|
| 60 |
|
| 61 |
# Convert to m4r format for iPhone
|
| 62 |
m4a_filename = f"downloads/{song_name}.m4a"
|
| 63 |
+
try:
|
| 64 |
+
subprocess.run([
|
| 65 |
+
'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
|
| 66 |
+
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30, check=True)
|
| 67 |
+
except subprocess.TimeoutExpired:
|
| 68 |
+
print("Error: ffmpeg took too long to process the file.")
|
| 69 |
+
return None, None
|
| 70 |
+
except subprocess.CalledProcessError as e:
|
| 71 |
+
print(f"ffmpeg error: {e}")
|
| 72 |
+
return None, None
|
| 73 |
|
| 74 |
+
# Ensure the file exists before renaming
|
| 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}") # Debug
|
| 79 |
+
return os.path.abspath(mp3_filename), os.path.abspath(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}") # Debug: Print the error
|
| 86 |
return None, None
|
| 87 |
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|