diablofx's picture
Update app.py
ffd1790
raw
history blame
2.84 kB
import gradio as gr
from pydub import AudioSegment
import os
from io import BytesIO
import math
def get_audio_file_info(audio_file):
# Read the audio data from the file
audio_data, sample_rate = sf.read(audio_file)
# Convert to mono if it's not mono
if len(audio_data.shape) > 1:
audio_data = np.mean(audio_data, axis=1)
# Get the audio file info
audio_info = sf.info(audio_file)
bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
# Convert duration to minutes and seconds
minutes, seconds = divmod(audio_info.duration, 60)
# Convert bitrate to kbps
speed_in_kbps = audio_info.samplerate * bit_depth / 1000
# Create a table with the audio file info
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.channels} |
| Samples per second | {audio_info.samplerate} Hz |
"""
# Return the info table
return info_table
def split_audio(input_file, duration):
# Read the audio data from the file
audio = AudioSegment.from_file(input_file)
# Calculate the total length and the number of parts
total_length = len(audio)
num_parts = math.ceil(total_length / (duration * 1000))
# Create a folder to store the split audio files
output_folder = "output"
os.makedirs(output_folder, exist_ok=True)
# Split the audio and save each part
output_files = []
for i in range(num_parts):
start = i * duration * 1000
end = (i + 1) * duration * 1000
split_audio = audio[start:end]
output_path = os.path.join(output_folder, f"part_{i + 1}.mp3")
split_audio.export(output_path, format="mp3")
output_files.append(output_path)
return output_files
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', label="Upload Audio File", file_extensions=["mp3", "wav"])
split_audio_butt = gr.Button(value='Split Audio into 5s Clips', variant='success')
with gr.Column():
output_text = gr.Textbox(value="", label="Output Files", disabled=True)
split_audio_butt.click(fn=split_audio, inputs=[audio_input], outputs=[output_text])
app.queue(max_size=1022).launch()
# Create the Gradio interface
main()