File size: 2,843 Bytes
9f4bd77
e79a8a8
ffd1790
e79a8a8
1b01768
b8ea52d
ad26144
eda1a48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0dbc19
60ec3ce
e79a8a8
60ec3ce
e79a8a8
60ec3ce
 
 
e79a8a8
60ec3ce
 
 
e79a8a8
60ec3ce
 
 
 
 
 
 
 
 
164929d
60ec3ce
164929d
ffd1790
164929d
 
 
 
 
 
 
 
ffd1790
1b01768
164929d
ffd1790
164929d
 
60ec3ce
e79a8a8
60ec3ce
e79a8a8
164929d
e79a8a8
b1f4e47
164929d
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
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()