用pyDub剪切长音频文件
我想用pyDub这个工具来处理一个很长的WAV文件,这个文件里包含了单独的单词和它们之间的静音部分。我想把所有的静音去掉,然后把剩下的部分输出成一个个单独的WAV文件。文件名可以用顺序编号,比如001.wav、002.wav、003.wav等等。
在Github页面上有一个叫“Yet another Example?”的例子,它做的事情很相似,不过它不是把每个部分输出成单独的文件,而是把去掉静音的部分重新合并成一个文件:
from pydub import AudioSegment
from pydub.utils import db_to_float
# Let's load up the audio we need...
podcast = AudioSegment.from_mp3("podcast.mp3")
intro = AudioSegment.from_wav("intro.wav")
outro = AudioSegment.from_wav("outro.wav")
# Let's consider anything that is 30 decibels quieter than
# the average volume of the podcast to be silence
average_loudness = podcast.rms
silence_threshold = average_loudness * db_to_float(-30)
# filter out the silence
podcast_parts = (ms for ms in podcast if ms.rms > silence_threshold)
# combine all the chunks back together
podcast = reduce(lambda a, b: a + b, podcast_parts)
# add on the bumpers
podcast = intro + podcast + outro
# save the result
podcast.export("podcast_processed.mp3", format="mp3")
有没有办法把这些podcast_parts的片段输出成单独的WAV文件呢?如果可以的话,应该怎么做?
谢谢!
1 个回答
14
这个示例代码比较简单,你可能想看看 strip_silence
这个函数:
https://github.com/jiaaro/pydub/blob/2644289067aa05dbb832974ac75cdc91c3ea6911/pydub/effects.py#L98
然后你可以导出每个小片段,而不是把它们合在一起。
这个示例和 strip_silence
函数的主要区别在于,示例是看一毫秒的小片段,这样对低频声音的处理就不太好。比如,一个40赫兹的声音波形长度是25毫秒,所以一毫秒的切片可能会漏掉一些信息。
不过,回答你最初的问题是,原始音频段的所有这些小片段也是音频段,所以你可以直接对它们调用导出方法 :)
更新:你可能想看看我刚刚上传到主分支的 静音工具;特别是 split_on_silence()
这个函数,它可以做到这一点(假设使用正确的参数),像这样:
from pydub import AudioSegment
from pydub.silence import split_on_silence
sound = AudioSegment.from_mp3("my_file.mp3")
chunks = split_on_silence(sound,
# must be silent for at least half a second
min_silence_len=500,
# consider it silent if quieter than -16 dBFS
silence_thresh=-16
)
你可以像这样导出所有单独的小片段为wav文件:
for i, chunk in enumerate(chunks):
chunk.export("/path/to/ouput/dir/chunk{0}.wav".format(i), format="wav")
这样输出的每个文件会被命名为 "chunk0.wav"、"chunk1.wav"、"chunk2.wav" 等等。