Skip to content

Instantly share code, notes, and snippets.

@tikhonova
Created April 16, 2023 21:54
Show Gist options
  • Save tikhonova/b8e6c32e711cb6cea222e40c41a5e2f1 to your computer and use it in GitHub Desktop.
Save tikhonova/b8e6c32e711cb6cea222e40c41a5e2f1 to your computer and use it in GitHub Desktop.
def remove_sil(file_path: str, file: str, dest_path: str, format="wav"):
sound = AudioSegment.from_file(os.path.join(file_path, file), format=format)
non_sil_times = detect_nonsilent(sound, min_silence_len=50, silence_thresh=sound.dBFS * 1.5)
if len(non_sil_times) == 0:
return None
elif len(non_sil_times) > 0:
non_sil_times_concat = [non_sil_times[0]]
if len(non_sil_times) > 1:
for t in non_sil_times[1:]:
if t[0] - non_sil_times_concat[-1][-1] < 200:
non_sil_times_concat[-1][-1] = t[1]
else:
non_sil_times_concat.append(t)
non_sil_times = [t for t in non_sil_times_concat if t[1] - t[0] > 350]
sound[non_sil_times[0][0]: non_sil_times[-1][1]].export(os.path.join(dest_path, file), format='wav')
# via https://github.com/tikhonova/what_would_alan_watts_say/blob/master/speech_synthesis/2_remove_silence.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment