sudo-soldier commited on
Commit
419ec35
·
verified ·
1 Parent(s): 30a9d88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -13,12 +13,12 @@ def sanitize_filename(filename):
13
  """Sanitize filenames to avoid special characters."""
14
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
15
 
16
- def process_youtube_or_audio(url, recorded_audio):
17
  try:
18
  filename = None
19
  song_name = None
20
 
21
-
22
  if url:
23
  ydl_opts = {
24
  'format': 'bestaudio/best',
@@ -38,24 +38,27 @@ def process_youtube_or_audio(url, recorded_audio):
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
- if len(audio) > 20000:
46
- audio = audio[:20000]
47
 
48
-
 
 
 
49
  mp3_filename = f"downloads/{song_name}.mp3"
50
- audio.export(mp3_filename, format="mp3")
51
 
52
-
53
  m4a_filename = f"downloads/{song_name}.m4a"
54
  subprocess.run([
55
  'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
56
  ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
57
 
58
-
59
  m4r_filename = f"downloads/{song_name}.m4r"
60
  os.rename(m4a_filename, m4r_filename)
61
 
@@ -99,6 +102,13 @@ with gr.Blocks(css="""
99
  gr.HTML('<label><i class="fas fa-microphone"></i>&nbsp;Record Audio</label>')
100
  audio_record = gr.Audio(sources=["microphone"], type="filepath", show_label=False)
101
 
 
 
 
 
 
 
 
102
  with gr.Row():
103
  process_button = gr.Button("Create Ringtones", elem_classes="light-btn")
104
 
@@ -113,7 +123,7 @@ with gr.Blocks(css="""
113
  iphone_ringtone = gr.File(label="Apple")
114
  iphone_instructions = gr.Textbox(label="Install", placeholder="Swipe left and open GarageBand. Long-press the ringtone file, select share, then choose 'Ringtone' to export.", lines=2)
115
 
116
- process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone])
117
 
118
  interface.launch(share=True)
119
 
 
13
  """Sanitize filenames to avoid special characters."""
14
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
15
 
16
+ def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
17
  try:
18
  filename = None
19
  song_name = None
20
 
21
+ # Download YouTube audio
22
  if url:
23
  ydl_opts = {
24
  'format': 'bestaudio/best',
 
38
  if not filename or not os.path.exists(filename):
39
  return None, None
40
 
41
+ # Load audio file
42
  audio = AudioSegment.from_file(filename)
43
 
44
+ # Ensure valid trimming times
45
+ start_time = max(0, min(start_time, len(audio)))
46
+ end_time = max(start_time, min(end_time, len(audio)))
47
 
48
+ # Trim the audio
49
+ trimmed_audio = audio[start_time:end_time]
50
+
51
+ # Convert to MP3
52
  mp3_filename = f"downloads/{song_name}.mp3"
53
+ trimmed_audio.export(mp3_filename, format="mp3")
54
 
55
+ # Convert to M4R (iPhone format)
56
  m4a_filename = f"downloads/{song_name}.m4a"
57
  subprocess.run([
58
  'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
59
  ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
60
 
61
+ # Rename to M4R
62
  m4r_filename = f"downloads/{song_name}.m4r"
63
  os.rename(m4a_filename, m4r_filename)
64
 
 
102
  gr.HTML('<label><i class="fas fa-microphone"></i>&nbsp;Record Audio</label>')
103
  audio_record = gr.Audio(sources=["microphone"], type="filepath", show_label=False)
104
 
105
+ with gr.Row():
106
+ gr.HTML("<h3>Trim Audio (Optional)</h3>")
107
+
108
+ with gr.Row():
109
+ start_time = gr.Slider(0, 20000, value=0, label="Start Time (ms)")
110
+ end_time = gr.Slider(1000, 20000, value=20000, label="End Time (ms)")
111
+
112
  with gr.Row():
113
  process_button = gr.Button("Create Ringtones", elem_classes="light-btn")
114
 
 
123
  iphone_ringtone = gr.File(label="Apple")
124
  iphone_instructions = gr.Textbox(label="Install", placeholder="Swipe left and open GarageBand. Long-press the ringtone file, select share, then choose 'Ringtone' to export.", lines=2)
125
 
126
+ process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record, start_time, end_time], outputs=[mp3_download, iphone_ringtone])
127
 
128
  interface.launch(share=True)
129