Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,35 +2,6 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import os
|
| 4 |
import soundfile as sf
|
| 5 |
-
from pydub import AudioSegment
|
| 6 |
-
import math
|
| 7 |
-
import shutil
|
| 8 |
-
|
| 9 |
-
class SplitWavAudio:
|
| 10 |
-
def __init__(self, audio_file):
|
| 11 |
-
self.audio_file = audio_file
|
| 12 |
-
self.audio_data, self.sample_rate = sf.read(audio_file)
|
| 13 |
-
if len(self.audio_data.shape) > 1:
|
| 14 |
-
self.audio_data = np.mean(self.audio_data, axis=1)
|
| 15 |
-
self.audio_info = sf.info(audio_file)
|
| 16 |
-
self.bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(self.audio_info.subtype, 0)
|
| 17 |
-
|
| 18 |
-
def get_duration(self):
|
| 19 |
-
return self.audio_info.duration
|
| 20 |
-
|
| 21 |
-
def single_split(self, from_sec, to_sec, split_filename):
|
| 22 |
-
t1 = int(from_sec * self.sample_rate)
|
| 23 |
-
t2 = int(to_sec * self.sample_rate)
|
| 24 |
-
split_audio = self.audio_data[t1:t2]
|
| 25 |
-
sf.write(split_filename, split_audio, self.sample_rate, subtype=self.audio_info.subtype)
|
| 26 |
-
|
| 27 |
-
def multiple_split(self, sec_per_split):
|
| 28 |
-
total_sec = self.get_duration()
|
| 29 |
-
for i in range(0, int(total_sec), sec_per_split):
|
| 30 |
-
split_filename = f"{i}_{os.path.basename(self.audio_file)}"
|
| 31 |
-
self.single_split(i, i + sec_per_split, split_filename)
|
| 32 |
-
print(f"{i} seconds Done")
|
| 33 |
-
print('All split successfully')
|
| 34 |
|
| 35 |
def main():
|
| 36 |
# Gradio Interface
|
|
@@ -45,50 +16,47 @@ def main():
|
|
| 45 |
with gr.Column():
|
| 46 |
audio_input = gr.Audio(type='filepath')
|
| 47 |
create_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
|
| 48 |
-
interval_input = gr.Number(visible=False)
|
| 49 |
-
cut_audio_butt = gr.Button(value='Cut Audio', variant='success', visible=False)
|
| 50 |
with gr.Column():
|
| 51 |
output_markdown = gr.Markdown(value="", visible=True)
|
| 52 |
|
| 53 |
-
create_info_butt.click(fn=get_audio_file_info, inputs=[audio_input,
|
| 54 |
-
|
| 55 |
-
cut_audio_butt.click(fn=cut_audio, inputs=[audio_input, interval_input])
|
| 56 |
-
|
| 57 |
app.queue(max_size=1022).launch(share=True)
|
| 58 |
|
| 59 |
-
def get_audio_file_info(audio_file
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
info_table = f"""
|
|
|
|
| 66 |
| Information | Value |
|
| 67 |
| :---: | :---: |
|
| 68 |
| File Name | {os.path.basename(audio_file)} |
|
| 69 |
| Duration | {int(minutes)} minutes - {int(seconds)} seconds |
|
| 70 |
| Bitrate | {speed_in_kbps} kbp/s |
|
| 71 |
-
| Audio Channels | {audio_info.
|
| 72 |
-
| Samples per second | {audio_info.
|
|
|
|
| 73 |
"""
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
cut_audio_butt.visible = True
|
| 77 |
-
|
| 78 |
return info_table
|
| 79 |
|
| 80 |
-
def cut_audio(audio_file, interval_input):
|
| 81 |
-
audio_info = SplitWavAudio(audio_file)
|
| 82 |
-
sec_per_split = interval_input
|
| 83 |
-
temp_folder = 'temp_audio_splits'
|
| 84 |
-
os.makedirs(temp_folder, exist_ok=True)
|
| 85 |
-
|
| 86 |
-
try:
|
| 87 |
-
audio_info.multiple_split(sec_per_split)
|
| 88 |
-
shutil.make_archive('audio_splits', 'zip', temp_folder)
|
| 89 |
-
print('Audio splits compiled successfully!')
|
| 90 |
-
finally:
|
| 91 |
-
shutil.rmtree(temp_folder)
|
| 92 |
-
|
| 93 |
# Create the Gradio interface
|
| 94 |
-
main()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import os
|
| 4 |
import soundfile as sf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def main():
|
| 7 |
# Gradio Interface
|
|
|
|
| 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):
|
| 27 |
+
# Read the audio data from the file
|
| 28 |
+
audio_data, sample_rate = sf.read(audio_file)
|
| 29 |
+
|
| 30 |
+
# Convert to mono if it's not mono
|
| 31 |
+
if len(audio_data.shape) > 1:
|
| 32 |
+
audio_data = np.mean(audio_data, axis=1)
|
| 33 |
|
| 34 |
+
# Get the audio file info
|
| 35 |
+
audio_info = sf.info(audio_file)
|
| 36 |
+
|
| 37 |
+
bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
|
| 38 |
+
|
| 39 |
+
# Convert duration to minutes and seconds
|
| 40 |
+
minutes, seconds = divmod(audio_info.duration, 60)
|
| 41 |
+
|
| 42 |
+
# Convert bitrate to kbps
|
| 43 |
+
speed_in_kbps = audio_info.samplerate * bit_depth / 1000
|
| 44 |
+
|
| 45 |
+
# Create a table with the audio file info
|
| 46 |
info_table = f"""
|
| 47 |
+
|
| 48 |
| Information | Value |
|
| 49 |
| :---: | :---: |
|
| 50 |
| File Name | {os.path.basename(audio_file)} |
|
| 51 |
| Duration | {int(minutes)} minutes - {int(seconds)} seconds |
|
| 52 |
| Bitrate | {speed_in_kbps} kbp/s |
|
| 53 |
+
| Audio Channels | {audio_info.channels} |
|
| 54 |
+
| Samples per second | {audio_info.samplerate} Hz |
|
| 55 |
+
|
| 56 |
"""
|
| 57 |
+
|
| 58 |
+
# Return the info table
|
|
|
|
|
|
|
| 59 |
return info_table
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# Create the Gradio interface
|
| 62 |
+
main()
|