Skip to content

Instantly share code, notes, and snippets.

@growingspaghetti
Last active April 16, 2021 16:25
Show Gist options
  • Save growingspaghetti/adcd50453807fb51ac6378dabbc15701 to your computer and use it in GitHub Desktop.
Save growingspaghetti/adcd50453807fb51ac6378dabbc15701 to your computer and use it in GitHub Desktop.
pydub split_on_silence
from pydub import AudioSegment
from pydub.silence import split_on_silence
import glob
import os
mp3s = glob.glob("/media/dev/workspace/viewer/using-english/*_d_*.mp3")
mp3s.sort()
def split(mp3):
def export(chunk, filename):
chunk.export(
filename,
bitrate="128k",
format="mp3"
)
basename = os.path.basename(mp3)
song = AudioSegment.from_mp3(mp3)
dBFS = song.dBFS
chunks = split_on_silence(
song,
min_silence_len=550,
silence_thresh=dBFS-32,
keep_silence=True
)
# if len(chunks) != 2:
# raise Exception('too many segments')
export(chunks[0], basename.replace(".mp3", "_d.mp3"))
audio_chunk = chunks[1]
for i, chunk in enumerate(chunks):
if i > 1:
audio_chunk += chunk[i]
export(audio_chunk, basename.replace(".mp3", "_e.mp3"))
for mp3 in mp3s:
print(mp3)
split(mp3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment