-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsicutter.py
41 lines (29 loc) · 1.44 KB
/
sicutter.py
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
from pydub import AudioSegment
from pydub.silence import split_on_silence
import os
# Define a function to normalize a chunk to a target amplitude.
def match_target_amplitude(aChunk, target_dBFS):
''' Normalize given audio chunk '''
change_in_dBFS = target_dBFS - aChunk.dBFS
return aChunk.apply_gain(change_in_dBFS)
def cut():
# Load your audio.
song = AudioSegment.from_file("raw//testaudio.wav")
# Use the loaded audio.
# Specify that a silent chunk must be at least 1 seconds or 1000 ms long.
# Consider a chunk silent if it's quieter than -40 dBFS.
# This will leave 100 ms of silence on either end
chunks = split_on_silence (song, min_silence_len = 1800, silence_thresh = -40, keep_silence=300)
#makes the directory to save the outputs
os.makedirs("chunks", exist_ok=True)
# Process each chunk with your parameters
for i, chunk in enumerate(chunks):
# Create a silence chunk that's 0.5 seconds (or 500 ms) long for padding.
silence_chunk = AudioSegment.silent(duration=500)
# Add the padding chunk to beginning and end of the entire chunk.
audio_chunk = silence_chunk + chunk + silence_chunk
# Normalize the entire chunk.
normalized_chunk = match_target_amplitude(audio_chunk, -20.0)
# Export the audio chunk
print("Exporting chunk{0}.wav.".format(i))
normalized_chunk.export(".//chunks//wav//chunk{0}.wav".format(i),format = "wav")