使用pyDub砍起了长长的音频文件 [英] Using pyDub to chop up a long audio file

查看:2888
本文介绍了使用pyDub砍起了长长的音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用pyDub采取(之间和沉默)的单个词语长WAV文件作为输入,然后去掉所有的沉默,输出剩余块是单独的WAV文件。该文件名可以只是连续的数字,像001.wav,002.wav,003.wav等。

I'd like to use pyDub to take a long WAV file of individual words (and silence in between) as input, then strip out all the silence, and output the remaining chunks is individual WAV files. The filenames can just be sequential numbers, like 001.wav, 002.wav, 003.wav, etc.

另一个例子?比如GitHub的页面上做非常类似的东西,但不是输出单独的文件,它结合了沉默剥离部分回到一起到一个文件中:

The "Yet another Example?" example on the Github page does something very similar, but rather than outputting separate files, it combines the silence-stripped segments back together into one file:

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文件?如果是这样,怎么样?

Is it possible to output those podcast_parts fragments as individual WAV files? If so, how?

谢谢!

推荐答案

这个例子code是pretty简化,你可能会想看看 strip_silence 功能:

The example code is pretty simplified, you'll probably want to look at the strip_silence function:

https://github.com/jiaaro/pydub/ BLOB /主/ pydub / effects.py#L76

然后只导出每个块,而不是将它们组合起来的。

And then just export each chunk instead of combining them.

例和strip_silence功能之间的主要区别是该例子着眼于一毫秒的切片,它不计数低频声音非常好,因为一个40HZ声音的一个波形,例如,为25毫秒长。

The main difference between the example and the strip_silence function is the example looks at one millisecond slices, which doesn't count low frequency sound very well since one waveform of a 40hz sound, for example, is 25 milliseconds long.

回答你原来的问题虽然是原始音频段的所有这些切片也音频片段,所以你可以喊出口的方法对它们:)

The answer to your original question though, is that all those slices of the original audio segment are also audio segments, so you can just call the export method on them :)

更新:你可能想看看的的我刚刚被推成主分支nofollow的>沉默公用事业;尤其是 split_on_silence() 它可以这样做(假设权的具体参数),像这样:

update: you may want to take a look at the silence utilities I've just pushed up into the master branch; especially split_on_silence() which could do this (assuming the right specific arguments) like so:

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文件是这样的:

you could export all the individual chunks as wav files like this:

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,等

which would make output each one named "chunk0.wav", "chunk1.wav", "chunk2.wav", and so on

这篇关于使用pyDub砍起了长长的音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆