Pydub - combine split_on_silence with minimum length / file size

后端 未结 2 943
梦如初夏
梦如初夏 2020-12-31 20:30

I have two scripts, one of them splits audio of a certain length, the other one splits audio on every time there is a silent passage. Would it be possible to split the audio

2条回答
  •  自闭症患者
    2020-12-31 21:06

    My advice is to use pydub.silence.split_on_silence() and then recombine the segments as needed so that you have files that are roughly the size you're targeting.

    something like

    from pydub import AudioSegment
    from pydub.silence import split_on_silence
    
    sound = AudioSegment.from_file("/path/to/file.mp3", format="mp3")
    chunks = split_on_silence(
        sound,
    
        # split on silences longer than 1000ms (1 sec)
        min_silence_len=1000,
    
        # anything under -16 dBFS is considered silence
        silence_thresh=-16, 
    
        # keep 200 ms of leading/trailing silence
        keep_silence=200
    )
    
    # now recombine the chunks so that the parts are at least 90 sec long
    target_length = 90 * 1000
    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 your have chunks that are bigger than 90 seconds (except, possibly the last one)
    

    Alternatively, you can use pydub.silence.detect_nonsilent() to find the ranges and make your own decisions about where to slice the original audio

    note: I also posted this on a similar/duplicate github issue

提交回复
热议问题