【问题标题】:Updating/appending to a .wav file in Python在 Python 中更新/附加到 .wav 文件
【发布时间】:2019-03-01 00:57:01
【问题描述】:

我的 Python 脚本中有一个 PCM 音频帧流,我可以将这些帧的块保存为 .wav 文件,如下所示:

def update_wav():
    filename = "test.wav"
    wav_file = wave.open(filename, "wb")
    n_frames = len(audio)

    wav_file.setparams((n_channels, sample_width, sample_rate, n_frames, comptype, compname))
    for sample in audio:
        wav_file.writeframes(struct.pack('h', int(sample * 32767.0)))
    wav_file.close()

但是,我希望随着新帧的出现而不断更新。有没有办法以附加到现有 .wav 文件的方式写入帧?现在我只能完成覆盖。

【问题讨论】:

    标签: python audio udp wav pcm


    【解决方案1】:

    我找到了一种使用SciPy 的方法,它实际上似乎是他们写作方法的默认功能。

    import scipy.io.wavfile
    
    def update_wav():
        numpy_data = numpy.array(audio, dtype=float)
        scipy.io.wavfile.write("test.wav", 8000, numpy_data)
    

    【讨论】:

    • 使用最新的 SciPy 包 (1.6.0) 无法重现此行为。 scipy.io.wavfile.write 将覆盖现有数据。
    猜你喜欢
    • 2020-08-05
    • 2020-02-13
    • 2012-10-31
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多