diablofx commited on
Commit
60ec3ce
1 Parent(s): b2c8f7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -41,24 +41,29 @@ def get_audio_file_info(audio_file):
41
  # Return the info table
42
  return info_table
43
 
44
- def split_audio_into_clips(audio_file):
45
  # Read the audio data from the file
46
- audio_data, sample_rate = torchaudio.load(audio_file, normalize=True)
47
 
48
- # Calculate the duration of each 5-second segment
49
- segment_duration = 5 * sample_rate
 
50
 
51
- # Split the audio into segments
52
- segments = [audio_data[:, i:i+segment_duration] for i in range(0, audio_data.size(1), segment_duration)]
 
53
 
54
- # Save each segment to a separate file
55
- download_links = ""
56
- for i, segment in enumerate(segments):
57
- output_file = f"output_segment_{i+1}.wav"
58
- torchaudio.save(output_file, segment, sample_rate)
59
- download_links += f"[Download Clip {i+1}](sandbox:/mnt/data/{output_file})\n"
 
 
 
60
 
61
- return download_links
62
 
63
  def main():
64
  # Gradio Interface
@@ -69,16 +74,14 @@ def main():
69
  Want to [support](https://ko-fi.com/diablofx) me? Or [join AI HUB](https://discord.gg/aihub) for more Help!\n
70
  """
71
  )
72
- with gr.Row():
73
  with gr.Column():
74
- audio_input = gr.Audio(type='filepath')
75
- create_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
76
  split_audio_butt = gr.Button(value='Split Audio into 5s Clips', variant='success')
77
  with gr.Column():
78
- output_markdown = gr.Markdown(value="", visible=True)
79
 
80
- create_info_butt.click(fn=get_audio_file_info, inputs=[audio_input], outputs=[output_markdown])
81
- split_audio_butt.click(fn=split_audio_into_clips, inputs=[audio_input], outputs=[output_markdown])
82
 
83
  app.queue(max_size=1022).launch()
84
 
 
41
  # Return the info table
42
  return info_table
43
 
44
+ def split_audio(input_file, duration):
45
  # Read the audio data from the file
46
+ audio = AudioSegment.from_file(input_file)
47
 
48
+ # Calculate the total length and the number of parts
49
+ total_length = len(audio)
50
+ num_parts = math.ceil(total_length / (duration * 1000))
51
 
52
+ # Create a folder to store the split audio files
53
+ output_folder = "output"
54
+ os.makedirs(output_folder, exist_ok=True)
55
 
56
+ # Split the audio and save each part
57
+ output_files = []
58
+ for i in range(num_parts):
59
+ start = i * duration * 1000
60
+ end = (i + 1) * duration * 1000
61
+ split_audio = audio[start:end]
62
+ output_path = os.path.join(output_folder, f"part_{i + 1}.mp3")
63
+ split_audio.export(output_path, format="mp3")
64
+ output_files.append(output_path)
65
 
66
+ return output_files
67
 
68
  def main():
69
  # Gradio Interface
 
74
  Want to [support](https://ko-fi.com/diablofx) me? Or [join AI HUB](https://discord.gg/aihub) for more Help!\n
75
  """
76
  )
77
+ with gr.Row():
78
  with gr.Column():
79
+ audio_input = gr.Audio(type='file', label="Upload Audio File")
 
80
  split_audio_butt = gr.Button(value='Split Audio into 5s Clips', variant='success')
81
  with gr.Column():
82
+ output_text = gr.Textbox(value="", label="Output Files", disabled=True)
83
 
84
+ split_audio_butt.click(fn=split_audio, inputs=[audio_input], outputs=[output_text])
 
85
 
86
  app.queue(max_size=1022).launch()
87