diablofx's picture
Update app.py
08fa3a5
raw
history blame
3.55 kB
import gradio as gr
import numpy as np
import os
import soundfile as sf
from pydub import AudioSegment
import math
import shutil
class SplitWavAudio:
def __init__(self, audio_file):
self.audio_file = audio_file
self.audio_data, self.sample_rate = sf.read(audio_file)
if len(self.audio_data.shape) > 1:
self.audio_data = np.mean(self.audio_data, axis=1)
self.audio_info = sf.info(audio_file)
self.bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(self.audio_info.subtype, 0)
def get_duration(self):
return self.audio_info.duration
def single_split(self, from_sec, to_sec, split_filename):
t1 = int(from_sec * self.sample_rate)
t2 = int(to_sec * self.sample_rate)
split_audio = self.audio_data[t1:t2]
sf.write(split_filename, split_audio, self.sample_rate, subtype=self.audio_info.subtype)
def multiple_split(self, sec_per_split):
total_sec = self.get_duration()
for i in range(0, int(total_sec), sec_per_split):
split_filename = f"{i}_{os.path.basename(self.audio_file)}"
self.single_split(i, i + sec_per_split, split_filename)
print(f"{i} seconds Done")
print('All split successfully')
def main():
# Gradio Interface
with gr.Blocks() as app:
gr.Markdown(
"""
# <div align="center"> diablofx Audio Interval Cutter (BETA) </div>
Want to [support](https://ko-fi.com/diablofx) me? Or [join AI HUB](https://discord.gg/aihub) for more Help!\n
"""
)
with gr.Row():
with gr.Column():
audio_input = gr.Audio(type='filepath')
create_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
interval_input = gr.Number(visible=False)
cut_audio_butt = gr.Button(value='Cut Audio', variant='success', visible=False)
with gr.Column():
output_markdown = gr.Markdown(value="", visible=True)
create_info_butt.click(fn=get_audio_file_info, inputs=[audio_input, interval_input, cut_audio_butt],
outputs=[output_markdown])
cut_audio_butt.click(fn=cut_audio, inputs=[audio_input, interval_input])
app.queue(max_size=1022).launch(share=True)
def get_audio_file_info(audio_file, interval_input, cut_audio_butt):
audio_info = SplitWavAudio(audio_file)
bit_depth = audio_info.bit_depth
minutes, seconds = divmod(audio_info.get_duration(), 60)
speed_in_kbps = audio_info.sample_rate * bit_depth / 1000
info_table = f"""
| Information | Value |
| :---: | :---: |
| File Name | {os.path.basename(audio_file)} |
| Duration | {int(minutes)} minutes - {int(seconds)} seconds |
| Bitrate | {speed_in_kbps} kbp/s |
| Audio Channels | {audio_info.audio_info.channels} |
| Samples per second | {audio_info.sample_rate} Hz |
"""
interval_input.visible = True
cut_audio_butt.visible = True
return info_table
def cut_audio(audio_file, interval_input):
audio_info = SplitWavAudio(audio_file)
sec_per_split = interval_input
temp_folder = 'temp_audio_splits'
os.makedirs(temp_folder, exist_ok=True)
try:
audio_info.multiple_split(sec_per_split)
shutil.make_archive('audio_splits', 'zip', temp_folder)
print('Audio splits compiled successfully!')
finally:
shutil.rmtree(temp_folder)
# Create the Gradio interface
main()