diablofx commited on
Commit
e79a8a8
1 Parent(s): b8bdee6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -3
app.py CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
2
  import numpy as np
3
  import os
4
  import soundfile as sf
 
 
5
 
6
  def main():
7
  # Gradio Interface
@@ -16,11 +18,13 @@ def main():
16
  with gr.Column():
17
  audio_input = gr.Audio(type='filepath')
18
  create_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
 
19
  with gr.Column():
20
  output_markdown = gr.Markdown(value="", visible=True)
21
-
22
  create_info_butt.click(fn=get_audio_file_info, inputs=[audio_input], outputs=[output_markdown])
23
-
 
24
  app.queue(max_size=1022).launch(share=True)
25
 
26
  def get_audio_file_info(audio_file):
@@ -58,5 +62,30 @@ def get_audio_file_info(audio_file):
58
  # Return the info table
59
  return info_table
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # Create the Gradio interface
62
- main()
 
2
  import numpy as np
3
  import os
4
  import soundfile as sf
5
+ from pydub import AudioSegment
6
+ from io import BytesIO
7
 
8
  def main():
9
  # Gradio Interface
 
18
  with gr.Column():
19
  audio_input = gr.Audio(type='filepath')
20
  create_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
21
+ split_audio_butt = gr.Button(value='Split Audio into 5s Clips', variant='success')
22
  with gr.Column():
23
  output_markdown = gr.Markdown(value="", visible=True)
24
+
25
  create_info_butt.click(fn=get_audio_file_info, inputs=[audio_input], outputs=[output_markdown])
26
+ split_audio_butt.click(fn=split_audio_into_clips, inputs=[audio_input])
27
+
28
  app.queue(max_size=1022).launch(share=True)
29
 
30
  def get_audio_file_info(audio_file):
 
62
  # Return the info table
63
  return info_table
64
 
65
+ def split_audio_into_clips(audio_file):
66
+ # Read the audio data from the file
67
+ audio_data, sample_rate = sf.read(audio_file)
68
+
69
+ # Convert to mono if it's not mono
70
+ if len(audio_data.shape) > 1:
71
+ audio_data = np.mean(audio_data, axis=1)
72
+
73
+ # Convert to Pydub AudioSegment
74
+ audio_segment = AudioSegment(audio_data.tobytes(), frame_rate=sample_rate, sample_width=audio_data.dtype.itemsize, channels=1)
75
+
76
+ # Split the audio into 5s clips
77
+ clips = [audio_segment[i:i + 5000] for i in range(0, len(audio_segment), 5000)]
78
+
79
+ # Save each clip and create download buttons
80
+ download_buttons = []
81
+ for i, clip in enumerate(clips):
82
+ clip_data = clip.raw_data
83
+ clip_io = BytesIO(clip_data)
84
+ clip_name = f"clip_{i+1}.wav"
85
+ sf.write(clip_name, clip_io.read(), sample_rate)
86
+ download_buttons.append(gr.Download(label=f"Download Clip {i+1}", data=clip_name))
87
+
88
+ return download_buttons
89
+
90
  # Create the Gradio interface
91
+ main()