pydub是Python的一个音频处理库,可以处理各种音频格式,如mp3、wav、flv等等。它是一个轻量级、快速且易于使用的库。silence库是pydub的一个扩展库,可以在音频文件中根据静默部分进行分割,非常方便。
1 pydub安装
pip install pydub
2 pydub模块使用
2.1 打开音频文件
from pydub import AudioSegment
# 打开wav格式音频文件
sound = AudioSegment.from_wav('./soundfile.wav')
# 打开mp3音频文件,AudioSegment原生只支持wav,raw文件,安装ffmpeg可支持其所有格式
sound = AudioSegment.from_mp3('./soundfile.mp3')
# 打开音频文件,生成AudioSegment对象
sound = AudioSegment.from_file('./soundfile.mp3') # format默认值为"mp3"
sound = AudioSegment.from_file('./soundfile.wav', format='wav')
# 打开raw文件需要额外参数,sample_width(采样位数),frame_rate(采样频率),channels(声通道)
# sample_width: 1、8bit,2、16bit,3、32bit
# frame_rate: 44100(44.1kHz CD audio), 48000(48kHz DVD audio)
# channels: 1、单声道,2、立体声
sound = AudioSegment.from_file('./soundfile.raw', format='raw', frame_rate=44100, channels=2, sample_width=2)
2.2 导出音频文件
sound = AudioSegment.from_file('./soundfile.wav', format='wav')
# 导出音频文件
sound.export('./outputsoundfile.mp3', format='mp3')
2.3 创建音频文件
# 生成一个时间长度为0秒的AudioSegment对象,一般用于多个音频合并
sound = AudioSegment.empty()
# 生成一个时间长度为5秒的无声AduioSegment对象
# 参数duration:时间长度(毫秒),frame_rate:频率,默认为11025Hz
sound = AudioSegment.silent(duration=50000)
2.4 合并音频文件
# 两个单声道音频合并为一个多声道音频
left_channel = AudioSegment.from_wav('sound_left.wav')
right_channel = AudioSegment.from_wav('sound_right.wav')
stereo_sound = AudioSegment.from_mono_auidosegments(left_channel,right_channel)
# 将多声道音频分解成两个单声道
sound = AudioSegment.from_wav('sound.wav')
sound.split_to_mono()
# 两个音频文件合并为一个音频文件
sound1 = AudioSegment.from_file('sound1.wav', format='wav')
sound2 = AudioSegment.from_file('sound2.wav', format='wav')
# 无交叉淡入淡出
sound = sound1 + sound2
# OR
sound = sound1.append(sound2, crossfade=0)
# 淡入淡出,参数crossfade:效果持续时间,默认100毫秒
sound = sound1.append(sound2, crossfade=100)
# 两个音频文件叠加为一个音频文件
# 例,长音频sound1,30s
sound1 = AudioSegment.from_file('sound1.wav', format='wav')
# 例,短音频sound2,10s
sound2 = AudioSegment.from_file('sound2.wav', format='wav')
# 将sound2叠加至sound1上,参数postions:从sound1音频10秒处开始叠加,默认为0,得到的音频文件,会从10秒处开始出现sound2音频,20秒处sound2音频结束,只余sound1音频
sound = sound1.overlay(sound2, postions=10000)
# 将sound2叠加至sound1上,参数loop:开启循环,默认为Fasle,得到的音频文件sound2将会循环播放至sound1结束
sound = sound1.overlay(sound2, loop=true)
# 将sound2叠加至sound1上,参数times:重复次数,默认为1,得到的音频文件sound2将会重复两次,在两个sound2的时间结束sound2音频
sound = sound1.overlay(sound2, times=2)
# 将sound1叠加至sound2上,10秒后sound2音频结束,sound1音频截断至10s结束,最终该音频长度只有10s,即
sound = sound2.overlay(sound1)
len(sound) == sound2
2.5 音频文件信息
sound = AudioSegment.from_file('soundfile.wav', format='wav')
# 声道数
schannel = sound.channels
# 采样位数,也称位深度,是指每个采样样本所包含的位数(字节数),通常有8 bit、16 bit
sbytes = sound.sample_width
# 采样频率,也称采样率,是指在单位时间内对声音信号的采样数或样本数,采样频率越能高表现的频率范围越大(电话:8kHZ,无线电广播:22.05kHz,CD:44.1kHz,DVD:48kHZ,蓝光:96kHz,192kHz)
srate = sound.frame_rate
# 帧位数, 帧包含每个声道的采样位数frame_width = channels * sample_width
sframebytes = sound.frame_width
# 音频文件帧数, 可选参数ms:时间长度,默认None
sframe = sound.frame_count()
# 音频文件时长,单位秒
stime = sound.duration_seconds
# OR
stime = (len(sound) / 1000)
2.6 处理音频文件
sound = AudioSegment.from_file('soundfile.wav', format='wav')
# 音频前5秒,pydub中时间以毫秒(ms)为单位
audio_begin = sound[:5000]
# 音频后5秒,pydub中时间以毫秒(ms)为单位
audio_end = sound[-5000:]
# 改变音频幅度,以分贝(dB)为单位
# 调高幅度
raise_via = sound.apply_gain(+5)
raise_via = sound + 5
# 调低音量
lower_via = sound.apply_gain(-5)
lower_via = sound - 5
# 淡入淡出效果
# 淡入,参数duration:持续时间
sound.fade_in(duration=5000)
# 淡出,参数duration:持续时间
sound.fade_out(duration=5000)
# 倒放音频文件
newsound = sound.reverse()
2.7 播放音频文件
from pydub import playback
from pydub import AduioSegment
from pydub.playback import play
sound = AduioSegment.from_wav('sound.wav')
# 播放音频
play(sound)
2.8 分割音频
from pydub import utils
from pydub import AudioSegment
from pydub.utils import make_chanks
sound = AduioSegment.from_wav('sound.wav')
# 分割音频, 参数chunk_length:一段音频多长时间
make_chanks(sound, chunk_length=5000)
3 使用pydub根据静音分割音频
代码首先使用AudioSegment类从audio.mp3文件中读取音频数据,然后设置了分割参数min_silence_len、silence_thresh和keep_silence。min_silence_len是最小静音长度,silence_thresh是静音阈值,keep_silence是保留静音长度。这些参数的具体含义可以根据实际情况进行调整。最后,根据分割参数使用split_on_silence函数对音频文件进行分割。
from pydub import AudioSegment
from pydub.silence import split_on_silence
# 读取音频文件
audio = AudioSegment.from_file("D:/dataset/lyb01.wav", format="wav")
# 设置分割参数
min_silence_len = 300 # 最小静音长度
silence_thresh = -50 # 静音阈值,越小越严格
keep_silence = 200 # 保留静音长度
# 分割音频文件
segments = split_on_silence(audio, min_silence_len=min_silence_len, silence_thresh=silence_thresh,
keep_silence=keep_silence)
for idx in range(len(segments)):
segments[idx].export(f'wav_{idx:04}.wav')
运行结果显示:
[<pydub.audio_segment.AudioSegment object at 0x00000263D65E1D60>, <pydub.audio_segment.AudioSegment object at 0x00000263D660E3D0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660E370>, <pydub.audio_segment.AudioSegment object at 0x00000263D660E340>, <pydub.audio_segment.AudioSegment object at 0x00000263D660E0A0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660E310>, <pydub.audio_segment.AudioSegment object at 0x00000263D660E8E0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660E910>, <pydub.audio_segment.AudioSegment object at 0x00000263D660E940>, <pydub.audio_segment.AudioSegment object at 0x00000263D660E9A0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660E970>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EC10>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EC40>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EC70>, <pydub.audio_segment.AudioSegment object at 0x00000263D660ECA0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660ECD0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660ED00>, <pydub.audio_segment.AudioSegment object at 0x00000263D660ED30>, <pydub.audio_segment.AudioSegment object at 0x00000263D660ED60>, <pydub.audio_segment.AudioSegment object at 0x00000263D660ED90>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EDC0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EDF0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EE20>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EE50>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EE80>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EEB0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EEE0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EF10>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EF40>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EF70>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EFA0>, <pydub.audio_segment.AudioSegment object at 0x00000263D660EFD0>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622040>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622070>, <pydub.audio_segment.AudioSegment object at 0x00000263D66220A0>, <pydub.audio_segment.AudioSegment object at 0x00000263D66220D0>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622100>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622130>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622160>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622190>, <pydub.audio_segment.AudioSegment object at 0x00000263D66221C0>, <pydub.audio_segment.AudioSegment object at 0x00000263D66221F0>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622220>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622250>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622280>, <pydub.audio_segment.AudioSegment object at 0x00000263D66222B0>, <pydub.audio_segment.AudioSegment object at 0x00000263D66222E0>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622310>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622340>, <pydub.audio_segment.AudioSegment object at 0x00000263D6622370>]