如何使用 pydub 在静音结束时拆分音频文件而不删除静音?

How do I split an audio file at the end of a silence, without deleting silence, using pydub?

Python 新手寻求帮助。

我正在使用 pydub 的 silence 模块来拆分和导出音频文件的块。我正在使用在这个问题的最高回复中找到的方法:

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")

虽然现在,沉默不包括在块中。我希望他们留在最后。我假设我必须对 pydub.silence 函数进行修改,但我不知道需要进行哪些更改。现在是 split_on_silence:

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])

感谢您的帮助。

~Eric.

您可能会发现 pydub.silence.detect_silence() function 有用。它 returns 检测到静音的范围列表(它在 split on silence 函数中使用)

还有 pydub.silence.detect_nonsilent() 以防更有帮助