使用python在音频文件中的某个点插入WAV

时间:2010-03-12 21:17:50

标签: python audio

我的问题如下: 我有一个2分钟长的WAV文件,我的目的是插入另一个WAV文件(7秒长),在第一个WAV文件中的某个点(比如0:48),基本上组合两个WAV,使用python 。不幸的是,我无法弄清楚如何做到这一点,并想知道是否有一些明显的解决方案,我错过了,或者是否甚至可以使用python。是否有可用的库可以提供解决方案? 感谢所有提前。

根据OP的评论进行更新:

我应该澄清一点,我希望插入的wav能够“重叠”原来的wav,以便两者都能玩,我的道歉。有没有办法达到这样的效果?

5 个答案:

答案 0 :(得分:1)

如果它们是PCM编码的,那么您可以使用wave,否则请使用pygst之类的内容。

答案 1 :(得分:1)

松散地基于Justin的代码,这里有一些其他代码可能做你想要的:

import wave, audioop

def merge_wav_at_offset(wav_in1, wav_in2, offset, wav_out):
    """Merge two wave files, with the second wave starting at offset seconds
    The two input wave files should have the same frame rate, channels, depth
    Also, offset should be non-negative and can be floating point."""
    wf1= wave.open(wav_in1, 'rb')
    wf2= wave.open(wav_in2, 'rb')
    wfo= wave.open(wav_out, 'wb')

    wfout.setparams(wf1.getparams())

    frame_rate = wf1.getframerate()
    sample_width= wf1.getsampwidth()
    if offset < 0:
        offset= 0
    prologue_frames= int(frame_rate*offset)
    merge_frames= wf2.getnframes()

    # prologue
    frames_to_read= prologue_frames
    while frames_to_read > 0:
        chunk_size= min(frame_rate, frames_to_read)
        wfo.writeframes(wf1.readframes(chunk_size))
        frames_to_read-= chunk_size

    # merging
    frames_to_read= merge_frames
    while frames_to_read > 0:
        chunk_size= min(frame_rate, frames_to_read)
        frames2= wf2.readframes(chunk_size)

        if frames2:
            frames1= wf1.readframes(chunk_size)
            if len(frames1) != len(frames2): # sanity check
                # obviously you should cater for this case too
                raise NotImplementedError, "offset+duration(wf2) > duration(wf1)"
            merged_frames= audioop.add(frames1, frames2, sample_width)
            wfo.writeframes(merged_frames)
        else: # early end of wf2 data; improbable but possible
            break

        frames_to_read-= chunk_size

    # epilogue
    while True:
        frames= wf1.readframes(frame_rate)
        if not frames: break
        wfo.writeframes(frames)

    for wave_file in wf1, wf2, wfo:
        wave_file.close()

我只是在没有测试的情况下编写代码,因此我可能有一个错误(甚至是语法错误);但是,我对Python的经验是,代码经常按原样运行;-) 如果您还需要更多信息,请与我联系。

答案 2 :(得分:1)

使用pydub package(完全披露 - 我写了它)

from pydub import AudioSegment

sound1 = AudioSegment.from_wav('your_wave.mp3')
the_wave = AudioSegment.from_wav('the_7sec_wave.wav')

sound_with_wave = sound1.overlay(the_wave, position=48*1000)

sound_with_wave.export('overlaid.wav', format='wav')

答案 3 :(得分:0)

这里有一些代码可以帮助您找到正确的方向:

wf = wave.open('in1.wav','rb')
wf2 = wave.open('in2.wav','rb')
wfout = wave.open('out.wav','wb')

wfout.setparams(wf.getparams())

sr = wf.getframerate()
for x in xrange(48):
    wfout.writeframes(wf.readframes(sr)
wfout.writeframes(wf2.readframes(sr))
for x in xrange(72):
    wfout.writeframes(wf.readframes(sr))

这应该按照您在问题中描述的内容(在2分钟的歌曲中添加1秒钟片段48秒),只要波形采用相同的格式(相同的采样率,相同的通道数等)。 )。您可以读取/写入比一秒更大的块,但我将它们作为1秒块进行安全。

答案 4 :(得分:0)

以下是合并2个音频样本缓冲区的方法

假设两个缓冲区/文件的格式相同/相同数量的通道/相同的采样频率,则所有3 data都是相同长度的bin字符串样本缓冲区(!important),sampleFormat是每个样本的字节数,例如:对于16位采样,它将是= 2:

import audioop

merged_data = audioop.add(firstWave_data, secondWave_data, sampleFormat)

否则准备首先执行以下缓冲剂准备:

  1. 进行单声道/立体声变换
  2. 进行格式转换
  3. 重新采样转换
相关问题