0

Python beginner looking for some assistance.

I am using pydub's silence module to split and export chunks of an audio file. I am using the method found in the top response to this question:

Using pyDub to chop up a long audio file

chunks = split_on_silence(sound, 
    # length of silence in ms
    min_silence_len=325,

    # consider it silent if quieter than -60 dBFS
    silence_thresh=-60
)

for i, chunk in enumerate(chunks):
    chunk.export(os.path.join(full_path, transcript_filename + "{0}.wav".format("%02d" % i)), format="wav")

Right now though, the silences are not included in the chunks. I would like them to be left on the end. I assume the modification I have to make is to the pydub.silence functions, but I don't know what change needs to be made. Here is split_on_silence now:

def split_on_silence(audio_segment, min_silence_len=1000, silence_thresh=-16, keep_silence=100):

    """
    audio_segment - original pydub.AudioSegment() object
    min_silence_len - (in ms) minimum length of a silence to be used for a split. default: 1000ms
    silence_thresh - (in dBFS) anything quieter than this will be considered silence. default: -16dBFS
    keep_silence - (in ms) amount of silence to leave at the beginning and end of the chunks. Keeps the sound from sounding like it is abruptly cut off. (default: 100ms)
    """

    not_silence_ranges = detect_nonsilent(audio_segment, min_silence_len, silence_thresh)

    chunks = []
    for start_i, end_i in not_silence_ranges:
        start_i = max(0, start_i - keep_silence)
        end_i += keep_silence

        chunks.append(audio_segment[start_i:end_i])

Thanks for any help.

~Eric.

Community
  • 1
  • 1

1 Answers1

3

You might find the pydub.silence.detect_silence() function useful. It returns a list of ranges where silence was detected (it’s used inside the split on silence function)

there is also pydub.silence.detect_nonsilent() in case that is more helpful

Jiaaro
  • 74,485
  • 42
  • 169
  • 190