如何拼接的音频文件(WAV格式)转换成在python 1秒接头? [英] How to splice an audio file (wav format) into 1 sec splices in python?
问题描述
我要寻找一个Python函数拼接的音频文件(WAV格式)到1秒的持续时间接头和每一个新的接头(1秒的持续时间)的存储到一个新的.wav文件。
I am looking for a python function to splice an audio file (wav format) into 1 sec duration splices and store each of the new splices (of 1 sec duration ) into a new .wav file.
推荐答案
其真正的简单,使用 pydub
模块的细节容易,其中超过的这里和这里
Its real simple and easy using pydub
module, details of which are over here and here
pydub
有一个名为方法 make_chunks
来,你可以指定块长度
在毫秒
。
pydub
has a method called make_chunks
to which you can specify chunk length
in milliseconds
.
make_chunks(your_audio_file_object,chunk_length_ms)
下面是一个工作code,它分割WAV文件在1秒块。我有一个8.5秒文件,所以程序创建91秒块是可玩
。最后一个块将根据音频的持续时间比较小。
Here is a working code that splits wav file in one sec chunks. I had a 8.5 seconds file, so the program created 9 one seconds chunks that are playable
. Last chunk will be smaller depending on audio duration.
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
>>>
这篇关于如何拼接的音频文件(WAV格式)转换成在python 1秒接头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!