Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import gradio as gr
|
|
| 4 |
import re
|
| 5 |
from pydub import AudioSegment
|
| 6 |
|
| 7 |
-
#downloads directory
|
| 8 |
os.makedirs("downloads", exist_ok=True)
|
| 9 |
|
| 10 |
def sanitize_filename(filename):
|
|
@@ -27,11 +27,13 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
| 27 |
filename = ydl.prepare_filename(info)
|
| 28 |
song_name = sanitize_filename(info['title'])
|
| 29 |
|
| 30 |
-
|
| 31 |
if not filename.endswith(".m4a"):
|
| 32 |
possible_file = f"downloads/{song_name}.m4a"
|
| 33 |
if os.path.exists(possible_file):
|
| 34 |
filename = possible_file
|
|
|
|
|
|
|
| 35 |
|
| 36 |
elif uploaded_audio is not None:
|
| 37 |
filename = uploaded_audio
|
|
@@ -39,7 +41,6 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
| 39 |
|
| 40 |
# Validate file existence
|
| 41 |
if not filename or not os.path.exists(filename):
|
| 42 |
-
print(f"File {filename} not found!")
|
| 43 |
return None, None
|
| 44 |
|
| 45 |
# Process audio
|
|
@@ -50,14 +51,13 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
| 50 |
|
| 51 |
trimmed_audio = audio[start_ms:end_ms]
|
| 52 |
|
| 53 |
-
#MP3
|
| 54 |
mp3_filename = f"downloads/{song_name}.mp3"
|
| 55 |
trimmed_audio.export(mp3_filename, format="mp3")
|
| 56 |
|
| 57 |
-
#M4R
|
| 58 |
-
m4a_filename = filename
|
| 59 |
m4r_filename = f"downloads/{song_name}.m4r"
|
| 60 |
-
|
| 61 |
|
| 62 |
return mp3_filename, m4r_filename
|
| 63 |
|
|
@@ -65,7 +65,7 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
| 65 |
print(f"Error: {e}")
|
| 66 |
return None, None
|
| 67 |
|
| 68 |
-
#Gradio
|
| 69 |
with gr.Blocks() as interface:
|
| 70 |
gr.HTML("""
|
| 71 |
<h1 style="font-size: 2rem; text-align: center;"><i class="fas fa-music"></i> PYTR - Python YouTube Ringtones</h1>
|
|
|
|
| 4 |
import re
|
| 5 |
from pydub import AudioSegment
|
| 6 |
|
| 7 |
+
# Ensure downloads directory exists
|
| 8 |
os.makedirs("downloads", exist_ok=True)
|
| 9 |
|
| 10 |
def sanitize_filename(filename):
|
|
|
|
| 27 |
filename = ydl.prepare_filename(info)
|
| 28 |
song_name = sanitize_filename(info['title'])
|
| 29 |
|
| 30 |
+
# Ensure filename is correct
|
| 31 |
if not filename.endswith(".m4a"):
|
| 32 |
possible_file = f"downloads/{song_name}.m4a"
|
| 33 |
if os.path.exists(possible_file):
|
| 34 |
filename = possible_file
|
| 35 |
+
else:
|
| 36 |
+
filename = None
|
| 37 |
|
| 38 |
elif uploaded_audio is not None:
|
| 39 |
filename = uploaded_audio
|
|
|
|
| 41 |
|
| 42 |
# Validate file existence
|
| 43 |
if not filename or not os.path.exists(filename):
|
|
|
|
| 44 |
return None, None
|
| 45 |
|
| 46 |
# Process audio
|
|
|
|
| 51 |
|
| 52 |
trimmed_audio = audio[start_ms:end_ms]
|
| 53 |
|
| 54 |
+
# Save as MP3 (Android)
|
| 55 |
mp3_filename = f"downloads/{song_name}.mp3"
|
| 56 |
trimmed_audio.export(mp3_filename, format="mp3")
|
| 57 |
|
| 58 |
+
# Convert to M4R (iPhone)
|
|
|
|
| 59 |
m4r_filename = f"downloads/{song_name}.m4r"
|
| 60 |
+
trimmed_audio.export(m4r_filename, format="m4r") # Fix: Convert instead of renaming
|
| 61 |
|
| 62 |
return mp3_filename, m4r_filename
|
| 63 |
|
|
|
|
| 65 |
print(f"Error: {e}")
|
| 66 |
return None, None
|
| 67 |
|
| 68 |
+
# Gradio UI
|
| 69 |
with gr.Blocks() as interface:
|
| 70 |
gr.HTML("""
|
| 71 |
<h1 style="font-size: 2rem; text-align: center;"><i class="fas fa-music"></i> PYTR - Python YouTube Ringtones</h1>
|