Spaces:
Runtime error
Runtime error
File size: 2,875 Bytes
198fe3f 9f4bd77 198fe3f ffd1790 198fe3f e79a8a8 198fe3f b8ea52d ad26144 eda1a48 d0dbc19 198fe3f e79a8a8 198fe3f e79a8a8 198fe3f e79a8a8 198fe3f e79a8a8 198fe3f 60ec3ce 198fe3f 164929d 60ec3ce 164929d 198fe3f 1b01768 164929d 198fe3f 164929d 198fe3f e79a8a8 198fe3f e79a8a8 164929d e79a8a8 b1f4e47 198fe3f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import gradio as gr
import numpy as np
import os
import soundfile as sf
from pydub import AudioSegment
from io import BytesIO
import torchaudio
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_into_clips(audio_file):
# Read the audio data from the file
audio_data, sample_rate = torchaudio.load(audio_file, normalize=True)
# Calculate the duration of each 5-second segment
segment_duration = 5 * sample_rate
# Split the audio into segments
segments = [audio_data[:, i:i+segment_duration] for i in range(0, audio_data.size(1), segment_duration)]
# Save each segment to a separate file
output_files = []
for i, segment in enumerate(segments):
output_file = f"output_segment_{i+1}.wav"
torchaudio.save(output_file, segment, sample_rate)
output_files.append(output_file)
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')
create_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
split_audio_butt = gr.Button(value='Split Audio into 5s Clips', variant='success')
with gr.Column():
output_markdown = gr.Markdown(value="", visible=True)
create_info_butt.click(fn=get_audio_file_info, inputs=[audio_input], outputs=[output_markdown])
split_audio_butt.click(fn=split_audio_into_clips, inputs=[audio_input])
app.queue(max_size=1022).launch()
# Create the Gradio interface
main()
|