使用python将静音帧添加到wav文件 [英] Adding silent frame to wav file using python

查看:683
本文介绍了使用python将静音帧添加到wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次在这里发布,让我们看看如何进行.

First time posting here, lets see how this goes.

我试图用python编写一个脚本,该脚本会在wav文件的开头添加一秒钟的静默,但是到目前为止,这样做没有成功.

I trying to write a script in python which would add a second of silence in the beginning of the wav file, but so far been unsuccessfully in doing so.

我想做的是在wav标头中读取,然后使用wave模块在开头添加\ 0,但效果不佳.这是基于此处的代码 http://andrewslotnick.com/posts/audio-delay-with-python.html

What I was trying to do is read in the wav header and then adding a \0 to the beginning using the wave module but that did not work quite well. Here is the code which is based from here http://andrewslotnick.com/posts/audio-delay-with-python.html

import wave
from audioop import add

def input_wave(filename,frames=10000000): #10000000 is an arbitrary large number of frames
    wave_file = wave.open(filename,'rb')
    params=wave_file.getparams()
    audio=wave_file.readframes(frames)
    wave_file.close()

    return params, audio

#output to file so we can use ipython notebook's Audio widget
def output_wave(audio, params, stem, suffix):
    #dynamically format the filename by passing in data
    filename=stem.replace('.wav','_{}.wav'.format(suffix))
    wave_file = wave.open(filename,'wb')
    wave_file.setparams(params)
    wave_file.writeframes(audio)

# delay the audio
def delay(audio_bytes,params,offset_ms):
    """version 1: delay after 'offset_ms' milliseconds"""
    #calculate the number of bytes which corresponds to the offset in milliseconds
    offset= params[0]*offset_ms*int(params[2]/1000)
    #create some silence
    beginning= b'\0'
    #remove space from the end
    end= audio_bytes        
    return add(audio_bytes, beginning+end, params[0])

audio_params, aduio_bytes = input_wave(<audio_file>)
output_wave(delay(aduio_bytes,audio_params,10000), audio_params, <audio_file>, <audio_file_suffics> )

使用上面的代码,当我尝试添加静音时会出现错误,因为音频长度与输入的长度不同.

Using the above code I get an error when I try to add the silence as the audio length is not the same as the input.

我对音频处理也很陌生,所以现在我只是尝试任何事情,看看有什么问题.

I am also quite new to audio processing so now I am just trying anything and see what sticks.

任何有关方法的建议或想法都很好:).

Any advice or ideas how to approach would be great :).

我也在使用python 2.7.5

Also I am using python 2.7.5

非常感谢.

推荐答案

有些库可以用最少的代码轻松地进行这类音频操作.这样的就是 pydub .

There are libraries that can do these kind of audio manipulation easily with least amount of code. One such is pydub.

您可以按如下所示安装pydub,有关依赖项的详细信息位于此处
pip install pydub

You can install pydub as below and detail about dependencies are here
pip install pydub

使用pydub,您可以读取不同的音频格式(在本例中为wav),将它们转换为音频片段,然后执行操作或简单地播放.

Using pydub, you can read different audio formats (wav in this case), convert them to audio-segment and then perform manipulations or simply play it.

您还可以创建一个设置周期的无声音频段,并使用'+'运算符添加两个段.

You can also create an silent audio-segment of set period and add two segment with '+' operator.

源代码

from pydub import AudioSegment
from pydub.playback import play

audio_in_file = "in_sine.wav"
audio_out_file = "out_sine.wav"

# create 1 sec of silence audio segment
one_sec_segment = AudioSegment.silent(duration=1000)  #duration in milliseconds

#read wav file to an audio segment
song = AudioSegment.from_wav(audio_in_file)

#Add above two audio segments    
final_song = one_sec_segment + song

#Either save modified audio
final_song.export(audio_out_file, format="wav")

#Or Play modified audio
play(final_song)

这篇关于使用python将静音帧添加到wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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