File size: 3,099 Bytes
9f4bd77
bbc6118
b1f4e47
bbc6118
e79a8a8
 
b1f4e47
 
 
 
 
 
 
58ef303
b1f4e47
 
 
 
 
bbc6118
e79a8a8
b1f4e47
 
e79a8a8
ad26144
e79a8a8
 
e03d387
 
b1f4e47
b8ea52d
ad26144
eda1a48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0dbc19
e79a8a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90be4f3
e79a8a8
 
 
b1f4e47
e79a8a8
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
89
90
91
92
93
94
import gradio as gr
import numpy as np
import os
import soundfile as sf
from pydub import AudioSegment
from io import BytesIO

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()



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 = 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)

    # Convert to Pydub AudioSegment
    audio_segment = AudioSegment(audio_data.tobytes(), frame_rate=sample_rate, sample_width=audio_data.dtype.itemsize, channels=1)

    # Split the audio into 5s clips
    clips = [audio_segment[i:i + 5000] for i in range(0, len(audio_segment), 5000)]

    # Save each clip and create download buttons
    download_buttons = []
    for i, clip in enumerate(clips):
        clip_data = clip.raw_data
        clip_io = BytesIO(clip_data)
        clip_name = f"clip_{i+1}.wav"
        sf.write(clip_name, clip_io.read(), sample_rate)
        download_buttons.append((f"Download Clip {i+1}", clip_name))

    return download_buttons

# Create the Gradio interface
main()