如何使用python连接多个.wav文件 [英] How to join multiple .wav files with python

查看:342
本文介绍了如何使用python连接多个.wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一个将两个.wav文件连接在一起的帖子,但是我想知道如何使用python来连接多个.wav文件?我正在使用python 3.6.0.如果有人有办法做到这一点,请教我.我看到另一篇文章要求加入2个.wav文件,并且我在下面的注释中使用了此代码:

I saw a post that joined two .wav files together, but I was wondering how can I join multiple .wav files using python? I am using python 3.6.0. If anyone has a way to do this please teach me. I saw that the other post ask for joining 2 .wav files and with that I used this code form the comments there:

import wave
infiles = ["sound_1.wav", "sound_2.wav"]
outfile = "sounds.wav"

data= []
for infile in infiles:
    w = wave.open(infile, 'rb')
    data.append( [w.getparams(), w.readframes(w.getnframes())] )
    w.close()

output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
output.writeframes(data[0][1])
output.writeframes(data[1][1])
output.close()

我想从一个路径中读取.wav文件,然后将其作为一个文件加入.我在想,在添加前两个.wav文件后,我将其删除,然后继续加入和删除,直到仅剩下一个.wav文件,如下所示:

I want to read my .wav files from a path then join it as one. I was thinking that after I add the first 2 .wav files I would delete them and just keep joining and deleting until only one .wav file remains like this:

file:
sound1.wav
sound2.wav
sound3.wav

code:
sound1.wav + sound2.wav = sound4.wav

file:
sound3.wav
sound4.wav

code:
sound4.wav + sound3.wav = sound5.wav

file:
sound5.wav

我只是不知道该怎么编码.我是python编程的新手,但总体而言,我对编程语言的编程并不满意. 在此先感谢.

I just don't know how to code this. I am new to coding with python and I am not that good with coding programming languages in general. Thanks in advances.

推荐答案

使用像pydub这样的模块

Use module like pydub

#!/usr/bin/env python
from pydub import AudioSegment
sound1 = AudioSegment.from_wav("filename01.wav")
sound2 = AudioSegment.from_wav("filename02.wav")
sound3 = AudioSegment.from_wav("filename03.wav")
combined_sounds = sound1 + sound2 + sound3 
combined_sounds.export("joinedFile.wav", format="wav")

这篇关于如何使用python连接多个.wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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