如何在python中将音频文件(wav格式)拼接为1秒的拼接?

我正在寻找一个python函数,将音频文件(wav格式)拼接为1秒持续时间的拼接,并将每个新拼接(持续时间为1秒)存储到一个新的.wav文件中。

回答:

它真正的简单易用的pydub模块,其详细信息在此处和此处

pydub有一个make_chunks您可以chunk length在中指定的方法milliseconds。

make_chunks(your_audio_file_object, chunk_length_ms)

这是将wav文件拆分为一秒的工作代码。我有一个8.5秒的文件,所以程序创建了9个1秒的块playable。最后一块将较小,具体取决于音频持续时间。

from pydub import AudioSegment

from pydub.utils import make_chunks

myaudio = AudioSegment.from_file("myAudio.wav" , "wav")

chunk_length_ms = 1000 # pydub calculates in millisec

chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec

#Export all of the individual chunks as wav files

for i, chunk in enumerate(chunks):

chunk_name = "chunk{0}.wav".format(i)

print "exporting", chunk_name

chunk.export(chunk_name, format="wav")

输出量

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32

Type "copyright", "credits" or "license()" for more information.

>>> ================================ RESTART ================================

>>>

exporting chunk0.wav

exporting chunk1.wav

exporting chunk2.wav

exporting chunk3.wav

exporting chunk4.wav

exporting chunk5.wav

exporting chunk6.wav

exporting chunk7.wav

exporting chunk8.wav

>>>

以上是 如何在python中将音频文件(wav格式)拼接为1秒的拼接? 的全部内容, 来源链接: utcz.com/qa/420268.html

回到顶部