Stay Hungry, Stay Foolish

python切割和合并wav音频文件

Python 32560℃ 0评论

切割wav音频文件

如果没有pydub需要使用pip install pydub来安装模块

from pydub import AudioSegment
from pydub.utils import make_chunks

myaudio = AudioSegment.from_file("myAudio.wav" , "wav") 
chunk_length_ms = 1000 # 分块的毫秒数
chunks = make_chunks(myaudio, chunk_length_ms) #将文件切割成1秒每块

#保存切割的音频到文件

for i, chunk in enumerate(chunks):
    chunk_name = "chunk{0}.wav".format(i)
    print "exporting", chunk_name
    chunk.export(chunk_name, format="wav")

切割mp3音频文件

mp3 = AudioSegment.from_mp3(filename) # 打开mp3文件
        mp3[17*1000+500:].export(filename, format="mp3") # 切割前17.5秒并覆盖保存

合并wav音频文件

linux 需要先安装sox:

apt-get install sox

其他平台也需要先安装SoX
版本要求 14.4.2或更高级版本
然后安装Python模块

pip install sox

使用方法如下(详细用法):

import sox
# create combiner
cbn = sox.Combiner()
# pitch shift combined audio up 3 semitones
#cbn.pitch(3.0)
# convert output to 8000 Hz stereo
#cbn.convert(samplerate=8000, channels=2)
# create the output file
cbn.build(
    ['input1.wav', 'input2.wav', 'input3.wav'], 'output.wav', 'concatenate'
)
喜欢 (13)
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦


Warning: Use of undefined constant PRC - assumed 'PRC' (this will throw an Error in a future version of PHP) in C:\inetpub\wordpress\wp-content\themes\XHBlog\comments.php on line 17
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址