Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -18,7 +18,7 @@ def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
|
|
18 |
filename = None
|
19 |
song_name = None
|
20 |
|
21 |
-
|
22 |
if url:
|
23 |
ydl_opts = {
|
24 |
'format': 'bestaudio/best',
|
@@ -30,33 +30,29 @@ def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
|
|
30 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
31 |
song_name = sanitize_filename(info['title'])
|
32 |
|
33 |
-
|
34 |
elif recorded_audio:
|
35 |
-
filename = recorded_audio
|
36 |
song_name = "recorded_audio"
|
37 |
|
38 |
if not filename or not os.path.exists(filename):
|
39 |
return None, None
|
40 |
|
41 |
-
|
42 |
audio = AudioSegment.from_file(filename)
|
43 |
|
44 |
-
|
45 |
start_time_ms = start_time * 1000
|
46 |
end_time_ms = end_time * 1000
|
47 |
|
48 |
-
|
49 |
start_time_ms = max(0, min(start_time_ms, len(audio)))
|
50 |
end_time_ms = max(start_time_ms, min(end_time_ms, len(audio)))
|
51 |
|
52 |
-
|
53 |
trimmed_audio = audio[start_time_ms:end_time_ms]
|
54 |
|
55 |
-
|
56 |
mp3_filename = f"downloads/{song_name}.mp3"
|
57 |
trimmed_audio.export(mp3_filename, format="mp3")
|
58 |
|
59 |
-
|
60 |
m4a_filename = f"downloads/{song_name}.m4a"
|
61 |
subprocess.run([
|
62 |
'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
|
@@ -72,6 +68,7 @@ def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
|
|
72 |
return None, None
|
73 |
|
74 |
|
|
|
75 |
with gr.Blocks(css="""
|
76 |
body { font-family: Arial, sans-serif; text-align: center; }
|
77 |
.light-btn {
|
@@ -102,8 +99,8 @@ with gr.Blocks(css="""
|
|
102 |
youtube_url = gr.Textbox(placeholder="Enter the URL here...", show_label=False)
|
103 |
|
104 |
with gr.Column(scale=1, min_width=250):
|
105 |
-
gr.HTML('<label><i class="fas fa-
|
106 |
-
|
107 |
|
108 |
with gr.Row():
|
109 |
gr.HTML("<h3>Trim Audio (Optional)</h3>")
|
@@ -126,10 +123,11 @@ with gr.Blocks(css="""
|
|
126 |
iphone_ringtone = gr.File(label="Apple")
|
127 |
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)
|
128 |
|
129 |
-
process_button.click(process_youtube_or_audio, inputs=[youtube_url,
|
130 |
|
131 |
interface.launch(share=True)
|
132 |
|
133 |
|
134 |
|
135 |
|
|
|
|
18 |
filename = None
|
19 |
song_name = None
|
20 |
|
21 |
+
# If URL is provided, download the 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 |
+
# If an audio file is uploaded, use that instead
|
34 |
elif recorded_audio:
|
35 |
+
filename = recorded_audio.name # Use the uploaded file's name
|
36 |
song_name = "recorded_audio"
|
37 |
|
38 |
if not filename or not os.path.exists(filename):
|
39 |
return None, None
|
40 |
|
41 |
+
# Process the audio file (trim according to start and end time)
|
42 |
audio = AudioSegment.from_file(filename)
|
43 |
|
|
|
44 |
start_time_ms = start_time * 1000
|
45 |
end_time_ms = end_time * 1000
|
46 |
|
|
|
47 |
start_time_ms = max(0, min(start_time_ms, len(audio)))
|
48 |
end_time_ms = max(start_time_ms, min(end_time_ms, len(audio)))
|
49 |
|
|
|
50 |
trimmed_audio = audio[start_time_ms:end_time_ms]
|
51 |
|
52 |
+
# Export the audio to MP3 and M4R formats
|
53 |
mp3_filename = f"downloads/{song_name}.mp3"
|
54 |
trimmed_audio.export(mp3_filename, format="mp3")
|
55 |
|
|
|
56 |
m4a_filename = f"downloads/{song_name}.m4a"
|
57 |
subprocess.run([
|
58 |
'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
|
|
|
68 |
return None, None
|
69 |
|
70 |
|
71 |
+
# Gradio Interface
|
72 |
with gr.Blocks(css="""
|
73 |
body { font-family: Arial, sans-serif; text-align: center; }
|
74 |
.light-btn {
|
|
|
99 |
youtube_url = gr.Textbox(placeholder="Enter the URL here...", show_label=False)
|
100 |
|
101 |
with gr.Column(scale=1, min_width=250):
|
102 |
+
gr.HTML('<label><i class="fas fa-file-audio"></i> Upload Audio</label>')
|
103 |
+
audio_upload = gr.File(label="Upload Audio", type="file", show_label=False)
|
104 |
|
105 |
with gr.Row():
|
106 |
gr.HTML("<h3>Trim Audio (Optional)</h3>")
|
|
|
123 |
iphone_ringtone = gr.File(label="Apple")
|
124 |
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)
|
125 |
|
126 |
+
process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_upload, start_time, end_time], outputs=[mp3_download, iphone_ringtone])
|
127 |
|
128 |
interface.launch(share=True)
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
+
|