How to join multiple .wav files with python [duplicate]

笑着哭i 提交于 2019-12-08 03:38:22

问题


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()

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

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.


回答1:


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")


来源:https://stackoverflow.com/questions/52698558/how-to-join-multiple-wav-files-with-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!