Split audio files using silence detection

后端 未结 3 773
无人共我
无人共我 2020-12-08 03:21

I\'ve more than 200 MP3 files and I need to split each one of them by using silence detection. I tried Audacity and WavePad but they do not have batch processes and it\'s ve

3条回答
  •  时光取名叫无心
    2020-12-08 03:44

    You can try using this for splitting audio on silence without the trouble of exploring possibilities for the silence threshold

    def split(file, filepath):
        sound = AudioSegment.from_wav(filepath)
        dBFS = sound.dBFS
        chunks = split_on_silence(sound, 
            min_silence_len = 500,
            silence_thresh = dBFS-16,
            keep_silence = 250 //optional
        )
    

    Note that the silence_thresh value need not be adjusted after using this.

    Additionally, if you want to split the audio by setting the min length of the audio chunk, you can add this after the above mentioned code.

    target_length = 25 * 1000 //setting minimum length of each chunk to 25 seconds
    output_chunks = [chunks[0]]
    for chunk in chunks[1:]:
        if len(output_chunks[-1]) < target_length:
            output_chunks[-1] += chunk
        else:
            # if the last output chunk is longer than the target length,
            # we can start a new one
            output_chunks.append(chunk)
    

    now we use output_chunks for further processing

提交回复
热议问题