image.png
pydub
Pydub可以让你用简单的方式处理音频。
Pydub提供了简洁的高层接口,极大的扩展了python处理音频文件的能力。
GitHub链接:pydub-github
GitHub:
You can open and save WAV files with pure python. For opening and saving non-wav files – like mp3 – you'll need ffmpeg or libav.
安装
代码语言:javascript复制pip install pydub
image.png
安装依赖软件
pydub的使用必须安装对应的依赖软件ffmpeg
或avconv
# ffmpeg
sudo apt-get install ffmpeg libavcodec-extra
验证是否安装成功:
image.png
API函数使用
打开接口
Open a WAV file
代码语言:javascript复制from pydub import AudioSegment
song = AudioSegment.from_wav("never_gonna_give_you_up.wav")
Open a mp3 file
代码语言:javascript复制song = AudioSegment.from_mp3("never_gonna_give_you_up.mp3")
Open a other file
代码语言:javascript复制ogg_version = AudioSegment.from_ogg("never_gonna_give_you_up.ogg")
flv_version = AudioSegment.from_flv("never_gonna_give_you_up.flv")
mp4_version = AudioSegment.from_file("never_gonna_give_you_up.mp4", "mp4")
wma_version = AudioSegment.from_file("never_gonna_give_you_up.wma", "wma")
aac_version = AudioSegment.from_file("never_gonna_give_you_up.aiff", "aac")
音频操作
切割音频
代码语言:javascript复制# pydub does things in milliseconds
ten_seconds = 10 * 1000
first_10_seconds = song[:ten_seconds]
last_5_seconds = song[-5000:]
分贝操作 分贝(decibel)是量度两个相同单位之数量比例的计量单位,主要用于度量声音强度,常用dB表示。
代码语言:javascript复制# boost volume by 6dB
beginning = first_10_seconds 6
# reduce volume by 3dB
end = last_5_seconds - 3
分贝 | 说明 |
---|---|
1分贝 | 刚能听到的声音 |
15 分贝以下 | 感觉安静 |
30 分贝 | 耳语的音量大小 |
40 分贝 | 冰箱的嗡嗡声 |
60分贝 | 正常交谈的声音 |
70分贝 | 相当于走在闹市区 |
85分贝 | 汽车穿梭的马路上 |
95分贝 | 摩托车启动声音 |
100分贝 | 装修电钻的声音 |
110分贝 | 卡拉OK、大声播放MP3 的声音 |
120分贝 | 飞机起飞时的声音 |
150分贝 | 燃放烟花爆竹的声音 |
音频链接 将一个文件添加到另一个文件的末尾
代码语言:javascript复制without_the_middle = beginning end
音频长度
代码语言:javascript复制without_the_middle.duration_seconds == 15.0
淡入淡出
代码语言:javascript复制# 1.5 second crossfade
with_style = beginning.append(end, crossfade=1500)
重复音频
代码语言:javascript复制# repeat the clip twice
do_it_over = with_style * 2
再次淡入淡出
代码语言:javascript复制# 2 sec fade in, 3 sec fade out
awesome = do_it_over.fade_in(2000).fade_out(3000)
保存音频
直接保存 所有ffmpeg支持的都支持
代码语言:javascript复制awesome.export("mashup.mp3", format="mp3")
用标签保存结果(元数据)
代码语言:javascript复制awesome.export("mashup.mp3", format="mp3", tags={'artist': 'Various artists', 'album': 'Best of 2011', 'comments': 'This album is awesome!'})
实例:
代码语言:javascript复制# -*- coding: utf-8 -*-
from glob import glob
from pydub import AudioSegment
playlist_songs = [AudioSegment.from_mp3(mp3_file) for mp3_file in glob("*.mp3")]
first_song = playlist_songs.pop(0)
# let's just include the first 30 seconds of the first song (slicing
# is done by milliseconds)
beginning_of_song = first_song[:30*1000]
playlist = beginning_of_song
for song in playlist_songs:
# We don't want an abrupt stop at the end, so let's do a 10 second crossfades
playlist = playlist.append(song, crossfade=(10 * 1000))
# let's fade out the end of the last song
playlist = playlist.fade_out(30)
# hmm I wonder how long it is... ( len(audio_segment) returns milliseconds )
playlist_length = len(playlist) / (1000*60)
# lets save it!
out_f = open("%s_minute_playlist.mp3" % playlist_length, 'wb')
playlist.export(out_f, format='mp3')
实战
将mp3文件转换成wav文件:
代码语言:javascript复制# -*- coding: utf-8 -*-
from pydub import AudioSegment
def trans_mp3_to_wav(filepath):
song = AudioSegment.from_mp3(filepath)
song.export("out.wav", format="wav")
if __name__ == "__main__":
trans_mp3_to_wav("一剪梅.mp3")
参考
Python音频处理库 pydub