📜  使用 Pydub 在Python中处理 wav 文件

📅  最后修改于: 2022-05-13 01:54:49.947000             🧑  作者: Mango

使用 Pydub 在Python中处理 wav 文件

音频文件是一种广泛的信息传输方式。那么让我们看看如何使用Python处理音频文件。 Python提供了一个名为pydub的模块来处理音频文件。 pydub是一个Python库,只能处理 .wav文件。通过使用这个库,我们可以播放、拆分、合并、编辑我们的. wav 音频文件。

安装

这个模块没有内置在Python中。要安装它,请在终端中键入以下命令。

pip install pydub

以下是 pydub 可以执行的一些功能:

  1. 播放音频文件。
  2. 我们可以得到文件的某些信息,比如长度通道。
  3. 增加/减少给定 .wav 文件的音量。
  4. 合并两个或多个音频文件。
  5. 导出音频文件。
  6. 分割音频文件。

要访问输入声音文件,请单击 这里。让我们看看 pydub 库的一些功能的代码:

1)播放音频文件:这是使用play()方法完成的。

Python3
# import required libraries
from pydub import AudioSegment 
from pydub.playback import play 
  
# Import an audio file 
# Format parameter only
# for readability 
wav_file = AudioSegment.from_file(file = "Sample.wav", 
                                  format = "wav") 
  
# Play the audio file
play(wav_file)


Python3
# import required library
from pydub import AudioSegment 
  
# import the audio file
wav_file = AudioSegment.from_file(file="Sample.wav", format="wav") 
  
# data type fo the file
print(type(wav_file)) 
# OUTPUT: 
  
#  To find frame rate of song/file
print(wav_file.frame_rate)   
# OUTPUT: 22050 
  
# To know about channels of file
print(wav_file.channels) 
# OUTPUT: 1
  
# Find the number of bytes per sample 
print(wav_file.sample_width ) 
# OUTPUT : 2
  
  
# Find Maximum amplitude 
print(wav_file.max)
# OUTPUT 17106
  
# To know length of audio file
print(len(wav_file))
# OUTPUT 60000 
  
'''
We can change the attrinbutes of file by 
changeed_audio_segment = audio_segment.set_ATTRIBUTENAME(x) 
'''
wav_file_new = wav_file.set_frame_rate(50) 
print(wav_file_new.frame_rate)


Python3
# import required library
import pydub 
from pydub.playback import play
  
wav_file =  pydub.AudioSegment.from_file(file = "Sample.wav", 
                                         format = "wav") 
# Increase the volume by 10 dB 
new_wav_file = wav_file + 10 
  
# Reducing volume by 5
silent_wav_file = wav_file - 5
  
#  Playing silent file
play(silent_wav_file)
  
#  Playing original file
play(wav_file)
  
#  Playing louder file
play(new_wav_file)
  
# Feel the difference!


Python3
# import required libraries
from pydub import AudioSegment
from pydub.playback import play
  
wav_file_1 = AudioSegment.from_file("noice.wav") 
wav_file_2 = AudioSegment.from_file("Sample.wav")
  
# Combine the two audio files 
wav_file_3 = wav_file_1 + wav_file_2
   
# play the sound 
play(wav_file_3)


Python3
# import library
from pydub import AudioSegment 
   
# Import audio file 
wav_file = AudioSegment.from_file("Sample.wav") 
'''
     You can do anything like remixing and export 
     I'm increasing volume just for sake of my simplicity
     Increase by 10 decibels 
  
'''
louder_wav_file = wav_file + 10 
  
# Export louder audio file 
louder_wav_file.export(out_f = "louder_wav_file.wav", 
                       format = "wav")


Python3
# import required libraries
from pydub import AudioSegment 
from pydub.playback import play
  
# importing audio file
a = AudioSegment.from_file("pzm12.wav") 
  
# Split stereo to mono 
b = a.split_to_mono() 
print(b) 
print(b[0].channels )
  
  
b[0].export(out_f="outNow.wav",format="wav")


输出:

2)了解 .wav 文件:为此,我们将使用音频文件对象的属性

Python3

# import required library
from pydub import AudioSegment 
  
# import the audio file
wav_file = AudioSegment.from_file(file="Sample.wav", format="wav") 
  
# data type fo the file
print(type(wav_file)) 
# OUTPUT: 
  
#  To find frame rate of song/file
print(wav_file.frame_rate)   
# OUTPUT: 22050 
  
# To know about channels of file
print(wav_file.channels) 
# OUTPUT: 1
  
# Find the number of bytes per sample 
print(wav_file.sample_width ) 
# OUTPUT : 2
  
  
# Find Maximum amplitude 
print(wav_file.max)
# OUTPUT 17106
  
# To know length of audio file
print(len(wav_file))
# OUTPUT 60000 
  
'''
We can change the attrinbutes of file by 
changeed_audio_segment = audio_segment.set_ATTRIBUTENAME(x) 
'''
wav_file_new = wav_file.set_frame_rate(50) 
print(wav_file_new.frame_rate)

输出:


22050
1
2
17106
60000
50

3)增加/减少文件的体积:通过使用' +'和' -'运算符。

Python3

# import required library
import pydub 
from pydub.playback import play
  
wav_file =  pydub.AudioSegment.from_file(file = "Sample.wav", 
                                         format = "wav") 
# Increase the volume by 10 dB 
new_wav_file = wav_file + 10 
  
# Reducing volume by 5
silent_wav_file = wav_file - 5
  
#  Playing silent file
play(silent_wav_file)
  
#  Playing original file
play(wav_file)
  
#  Playing louder file
play(new_wav_file)
  
# Feel the difference!

输出:

4)合并文件:这是使用'+' 运算符完成的。

Python3

# import required libraries
from pydub import AudioSegment
from pydub.playback import play
  
wav_file_1 = AudioSegment.from_file("noice.wav") 
wav_file_2 = AudioSegment.from_file("Sample.wav")
  
# Combine the two audio files 
wav_file_3 = wav_file_1 + wav_file_2
   
# play the sound 
play(wav_file_3)

输出:

5) 导出文件:这是使用export()方法完成的。

Python3

# import library
from pydub import AudioSegment 
   
# Import audio file 
wav_file = AudioSegment.from_file("Sample.wav") 
'''
     You can do anything like remixing and export 
     I'm increasing volume just for sake of my simplicity
     Increase by 10 decibels 
  
'''
louder_wav_file = wav_file + 10 
  
# Export louder audio file 
louder_wav_file.export(out_f = "louder_wav_file.wav", 
                       format = "wav")

输出:

6)分割音频:使用split_to_mono()方法分割音频。

Python3

# import required libraries
from pydub import AudioSegment 
from pydub.playback import play
  
# importing audio file
a = AudioSegment.from_file("pzm12.wav") 
  
# Split stereo to mono 
b = a.split_to_mono() 
print(b) 
print(b[0].channels )
  
  
b[0].export(out_f="outNow.wav",format="wav")

输出:

[, ]
1