簡體   English   中英

如何將 append 音頻幀轉換為 wav 文件 python

[英]how to append audio frames to wav file python

我的 python 代碼中有一個 stream PCM 音頻幀。有沒有辦法以附加到現有.wav 文件的方式寫入幀。 我嘗試過的是我正在使用 2 個 wav 文件。 從 1 個 wav 文件中,我正在讀取數據並寫入現有的 wav 文件

import numpy
import wave
import scipy.io.wavfile
with open('testing_data.wav', 'rb') as fd:
   contents = fd.read()
contents1=bytearray(contents)
numpy_data = numpy.array(contents1, dtype=float)
scipy.io.wavfile.write("whatstheweatherlike.wav", 8000, numpy_data)

數據被附加到現有的 wav 文件中,但是當我嘗試在媒體播放器中播放時 wav 文件被損壞

使用wave庫,您可以通過以下方式做到這一點:

import wave

audiofile1="youraudiofile1.wav"
audiofile2="youraudiofile2.wav"

concantenated_file="youraudiofile3.wav"
frames=[]

wave0=wave.open(audiofile2,'rb')
frames.append([wave0.getparams(),wave0.readframes(wave0.getnframes())])
wave.close()

wave1=wave.open(audiofile2,'rb')
frames.append([wave1.getparams(),wave1.readframes(wave1.getnframes())])
wave1.close()

result=wave.open(concantenated_file,'wb')
result.setparams(frames[0][0])
result.writeframes(frames[0][1])
result.writeframes(frames[1][1])

result.close()

而連接的順序正是這里的書寫順序:

result.writeframes(frames[0][1]) #audiofile1
result.writeframes(frames[1][1]) #audiofile2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM