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

最佳答案

它使用pydub模块真正简单易用,其详细信息超过了herehere
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 - 如何在python中将音频文件(wav格式)拼接为1秒的拼接?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36799902/

10-13 06:38