我想使用pyDub将单个单词的长WAV文件(以及其间的静音)作为输入,然后去掉所有的静音,并输出剩余的块是单独的WAV文件。文件名可以是序列号,如001.wav,002.wav,003.wav等。
" Yet another Example?" Github页面上的示例做了非常相似的事情,但它不是输出单独的文件,而是将沉默剥离的段组合在一起形成一个文件:
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文件输出?如果是这样,怎么样?
谢谢!
答案 0 :(得分:9)
示例代码非常简化,您可能希望查看strip_silence
函数:
https://github.com/jiaaro/pydub/blob/master/pydub/effects.py#L76
然后只导出每个块而不是组合它们。
示例和strip_silence函数之间的主要区别在于示例查看一毫秒切片,由于40hz声音的一个波形(例如,25毫秒长),因此不能很好地计算低频声音。
原始问题的答案是,原始音频片段的所有片段也都是音频片段,因此您只需调用它们的导出方法:)
更新:您可能需要查看我刚刚推进主分支的silence utilities;特别是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"等等