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 |
-
# Ensure downloads directory exists
|
8 |
os.makedirs("downloads", exist_ok=True)
|
9 |
|
10 |
def sanitize_filename(filename):
|
@@ -20,7 +20,7 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
20 |
ydl_opts = {
|
21 |
'format': 'm4a/bestaudio',
|
22 |
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
23 |
-
'cookiefile': 'cookies.txt'
|
24 |
}
|
25 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
26 |
info = ydl.extract_info(url, download=True)
|
@@ -41,6 +41,7 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
41 |
|
42 |
# Validate file existence
|
43 |
if not filename or not os.path.exists(filename):
|
|
|
44 |
return None, None
|
45 |
|
46 |
# Process audio
|
@@ -55,9 +56,9 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
55 |
mp3_filename = f"downloads/{song_name}.mp3"
|
56 |
trimmed_audio.export(mp3_filename, format="mp3")
|
57 |
|
58 |
-
#
|
59 |
m4r_filename = f"downloads/{song_name}.m4r"
|
60 |
-
trimmed_audio.export(m4r_filename, format="m4r")
|
61 |
|
62 |
return mp3_filename, m4r_filename
|
63 |
|
@@ -67,34 +68,27 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
67 |
|
68 |
# Gradio UI
|
69 |
with gr.Blocks() as interface:
|
70 |
-
gr.
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
74 |
""")
|
75 |
|
76 |
with gr.Row():
|
77 |
-
youtube_url = gr.Textbox(placeholder="Enter
|
78 |
uploaded_audio = gr.File(label="Upload Audio File", type="filepath")
|
79 |
|
80 |
-
gr.
|
81 |
-
|
82 |
with gr.Row():
|
83 |
-
start_time = gr.Slider(0,
|
84 |
-
end_time = gr.Slider(1,
|
85 |
|
86 |
process_button = gr.Button("Create Ringtones")
|
87 |
|
88 |
with gr.Row():
|
89 |
-
mp3_download = gr.File(label="Android Ringtone")
|
90 |
-
iphone_ringtone = gr.File(label="iPhone Ringtone")
|
91 |
-
|
92 |
-
with gr.Row():
|
93 |
-
instructions = gr.Textbox(
|
94 |
-
value="Android: move the download to the 'Ringtones' folder.\nApple: import download with 'Garage Band' and export to ringtones.",
|
95 |
-
label="Install Ringtones",
|
96 |
-
interactive=False
|
97 |
-
)
|
98 |
|
99 |
process_button.click(process_youtube_or_audio, inputs=[youtube_url, uploaded_audio, start_time, end_time], outputs=[mp3_download, iphone_ringtone])
|
100 |
|
@@ -102,3 +96,4 @@ interface.launch(share=True)
|
|
102 |
|
103 |
|
104 |
|
|
|
|
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):
|
|
|
20 |
ydl_opts = {
|
21 |
'format': 'm4a/bestaudio',
|
22 |
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
23 |
+
'cookiefile': 'cookies.txt' # Ensure this file exists if needed
|
24 |
}
|
25 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
26 |
info = ydl.extract_info(url, download=True)
|
|
|
41 |
|
42 |
# Validate file existence
|
43 |
if not filename or not os.path.exists(filename):
|
44 |
+
print(f"File not found: {filename}")
|
45 |
return None, None
|
46 |
|
47 |
# Process audio
|
|
|
56 |
mp3_filename = f"downloads/{song_name}.mp3"
|
57 |
trimmed_audio.export(mp3_filename, format="mp3")
|
58 |
|
59 |
+
# Save as M4R (iPhone)
|
60 |
m4r_filename = f"downloads/{song_name}.m4r"
|
61 |
+
trimmed_audio.export(m4r_filename, format="m4r")
|
62 |
|
63 |
return mp3_filename, m4r_filename
|
64 |
|
|
|
68 |
|
69 |
# Gradio UI
|
70 |
with gr.Blocks() as interface:
|
71 |
+
gr.Markdown("""
|
72 |
+
# 🎵 PYTR - Python YouTube Ringtones
|
73 |
+
Create custom ringtones from YouTube or uploaded audio.
|
74 |
+
- **Enter a YouTube URL** to extract audio.
|
75 |
+
- **Upload a file** to trim an existing audio.
|
76 |
""")
|
77 |
|
78 |
with gr.Row():
|
79 |
+
youtube_url = gr.Textbox(placeholder="Enter YouTube URL...", label="YouTube URL")
|
80 |
uploaded_audio = gr.File(label="Upload Audio File", type="filepath")
|
81 |
|
82 |
+
gr.Markdown("## Trim Audio")
|
|
|
83 |
with gr.Row():
|
84 |
+
start_time = gr.Slider(0, 30, value=0, label="Start Time (seconds)")
|
85 |
+
end_time = gr.Slider(1, 30, value=10, label="End Time (seconds)")
|
86 |
|
87 |
process_button = gr.Button("Create Ringtones")
|
88 |
|
89 |
with gr.Row():
|
90 |
+
mp3_download = gr.File(label="Android Ringtone (MP3)")
|
91 |
+
iphone_ringtone = gr.File(label="iPhone Ringtone (M4R)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
process_button.click(process_youtube_or_audio, inputs=[youtube_url, uploaded_audio, start_time, end_time], outputs=[mp3_download, iphone_ringtone])
|
94 |
|
|
|
96 |
|
97 |
|
98 |
|
99 |
+
|