File size: 3,116 Bytes
dcc09a9
c766d00
dcc09a9
 
c766d00
 
 
 
 
 
dcc09a9
c766d00
 
 
 
 
 
dcc09a9
c766d00
 
dcc09a9
c766d00
 
 
 
3fb14a2
c766d00
 
 
 
 
dcc09a9
3fb14a2
dcc09a9
 
 
c766d00
dcc09a9
c766d00
dcc09a9
 
 
c766d00
3fb14a2
c766d00
 
 
dcc09a9
c766d00
 
 
 
 
 
dcc09a9
c766d00
 
 
dcc09a9
c766d00
dcc09a9
c766d00
 
 
3fb14a2
 
dcc09a9
c766d00
 
 
 
 
 
 
 
 
dcc09a9
3fb14a2
c766d00
 
 
dcc09a9
 
c766d00
3fb14a2
c766d00
 
 
 
3fb14a2
c766d00
3fb14a2
dcc09a9
 
 
c766d00
3fb14a2
c766d00
 
3fb14a2
 
c766d00
 
 
3fb14a2
 
dcc09a9
 
 
c766d00
 
 
dcc09a9
 
 
c766d00
 
dcc09a9
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import gradio as gr
import os, uuid, re, tempfile
from pydub import AudioSegment, silence

# ----------------------------------
# Utils
# ----------------------------------
def clean_name(path, ext):
    name = re.sub(r'[^a-zA-Z0-9]+', '_', os.path.basename(path))
    return f"{name}_{uuid.uuid4().hex[:6]}.{ext}"

# ----------------------------------
# Convert to WAV (FAST PROCESSING)
# ----------------------------------
def ensure_wav(path):
    if path.lower().endswith(".wav"):
        return path

    audio = AudioSegment.from_file(path)
    audio = audio.set_channels(1)

    tmp = tempfile.gettempdir()
    wav_path = os.path.join(tmp, clean_name(path, "wav"))
    audio.export(wav_path, format="wav")
    return wav_path

# ----------------------------------
# SAFE Silence Removal
# ----------------------------------
def remove_silence_safe(path, keep_ms=250):
    audio = AudioSegment.from_wav(path)

    silence_thresh = audio.dBFS - 22

    chunks = silence.split_on_silence(
        audio,
        min_silence_len=350,
        silence_thresh=silence_thresh,
        keep_silence=keep_ms
    )

    if not chunks:
        return path

    out = AudioSegment.empty()
    for c in chunks:
        out += c.fade_in(20).fade_out(20)

    out_path = os.path.join(
        tempfile.gettempdir(),
        clean_name(path, "wav")
    )
    out.export(out_path, format="wav")
    return out_path

# ----------------------------------
# Duration
# ----------------------------------
def duration(path):
    return len(AudioSegment.from_wav(path)) / 1000

# ----------------------------------
# Main
# ----------------------------------
def process_audio(audio_file, keep_seconds):
    keep_ms = int(max(keep_seconds, 0.25) * 1000)

    wav = ensure_wav(audio_file)
    before = duration(wav)

    cleaned = remove_silence_safe(wav, keep_ms)
    after = duration(cleaned)

    # Export final MP3 (user friendly)
    final_mp3 = cleaned.replace(".wav", ".mp3")
    AudioSegment.from_wav(cleaned).export(final_mp3, format="mp3")

    info = (
        f"Old Duration : {before:.2f}s\n"
        f"New Duration : {after:.2f}s\n"
        f"Silence Removed : {before - after:.2f}s"
    )

    return final_mp3, final_mp3, info

# ----------------------------------
# UI
# ----------------------------------
with gr.Blocks(title="Fast Silence Remover") as demo:
    gr.Markdown(
        "## ⚡ Fast MP3/WAV Silence Remover (No Word Loss)"
    )

    with gr.Row():
        with gr.Column():
            audio = gr.Audio(
                type="filepath",
                sources=["upload"],
                label="Upload MP3 or WAV"
            )

            keep = gr.Number(
                value=0.25,
                label="Keep Silence (seconds)"
            )

            btn = gr.Button("Remove Silence")

        with gr.Column():
            out_audio = gr.Audio(label="Processed Audio")
            out_file = gr.File(label="Download")
            info = gr.Textbox(label="Info")

    btn.click(
        process_audio,
        [audio, keep],
        [out_audio, out_file, info]
    )

demo.queue().launch()