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

查看:123
本文介绍了使用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 模块在开头添加一个 ,但效果不佳.这是基于这里的代码 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 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''
    #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天全站免登陆