Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import numpy as np | |
| import os | |
| import soundfile as sf | |
| 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') | |
| get_info_butt = gr.Button(value='Get Audio File Info', variant='primary') | |
| interval_input = gr.Number(value=5, label="Enter Interval (seconds):", min=1, step=1) | |
| with gr.Column(): | |
| output_markdown = gr.Markdown(value="", visible=True) | |
| cut_audio_butt = gr.Button(value='Cut and Compile Audio', variant='success', disabled=True) | |
| get_info_butt.click(fn=get_audio_file_info, inputs=[audio_input, interval_input], outputs=[output_markdown, cut_audio_butt]) | |
| cut_audio_butt.click(fn=cut_and_compile_audio, inputs=[audio_input, interval_input], outputs=[output_markdown]) | |
| app.queue(max_size=1022).launch(share=True) | |
| def get_audio_file_info(audio_file, interval): | |
| # 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 | | |
| """ | |
| # Enable the button for cutting audio | |
| return info_table | |
| def cut_and_compile_audio(audio_file, interval): | |
| # 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) | |
| # Calculate the number of frames for the specified interval | |
| frames_per_interval = int(sample_rate * interval) | |
| # Cut the audio into intervals | |
| audio_clips = [audio_data[i:i + frames_per_interval] for i in range(0, len(audio_data), frames_per_interval)] | |
| # Compile audio clips into a single array | |
| compiled_audio = np.concatenate(audio_clips) | |
| # Save the compiled audio to a new file | |
| compiled_file_path = 'compiled_audio.wav' | |
| sf.write(compiled_file_path, compiled_audio, sample_rate) | |
| # Provide a download link for the compiled audio | |
| download_link = f"[Download Compiled Audio](sandbox:/mnt/data/{compiled_file_path})" | |
| return f"Audio cut and compiled successfully. {download_link}" | |
| # Create the Gradio interface | |
| main() | |