一, eyeD3
直接在 google 上搜索 python mp3 process , 推荐比较多的就是这个第三方库了. 先来看看官方介绍吧.
- About
- eyeD3 is a Python tool for working with audio files, specifically mp3 files containing ID3 metadata (i.e. song info).
- It provides a command-line tool (
- eyeD3
- ) and a Python library (
- import eyed3
- ) that can be used to write your own applications or plugins that are callable from the command-line tool.
- For example, to set some song information in an mp3 file called
- song.mp3
- :
- $ eyeD3 -a Nobunny -A "Love Visions" -t "I Am a Girlfriend" -n 4 song.mp3
简单来说, eyeD3 这个库只要是用来处理 MP3 文件的, 特别是带 ID3 metadata 的文件 (一般 MP3 文件都会带有一些额外信息, 比如说歌手, 专辑之类的, 后面会说怎么提取这些信息).eyeD3 提供了两种使用方法, 一种是使用 command line 直接在命令行中执行 eyeD3 --... 就可以对 MP3 进行处理, 还有一种是在 python 中使用 import eyed3 导入.
上面的例子就是官方提供的一个使用 eyeD3 命令行执行的语句,-a 是 --artist 的简写, 即添加歌手信息,-A 是 --album 的简写, 即添加专辑信息,-t 是 --title 的简写, 即添加歌曲名字,-n 是 --track-num 的简写, 即添加磁道数. 这些一般都是 MP3 文件 ID3 tag 的默认属性. 我们如果直接输入 eyeD3 song.mp3 就会直接显示歌曲的基本信息, 大概长下面这个样子:
- $ eyeD3 song.mp3
- song.mp3 [ 3.06 MB ]
- -------------------------------------------------------------------------
- ID3 v2.4:
- title: I Am a Girlfriend
- artist: Nobunny
- album: Love Visions
- album artist: Various Artists
- track: 4
------------------------------------------------------------------------- 如果你在 windows 下使用 eyeD3, 也许你会发现直接在 cmd 中输入 eyeD3 会提示找不到这个命令, 我就遇到了这个问题. 因为我是直接 pip 安装的, 去 site-packages 目录下, 没有找到名为 eyeD3.exe 的可执行文件, 全部是. py 文件, 官网对此没有说明, 去 google 上找了半天也没有找到相应的解决办法. 没办法, 只好转到 linux 下试一下了. linux 下 pip install eyeD3 之后, 然后在 shell 执行 eyeD3 song.mp3 是没有问题的. 下图 1 是我在 ubuntu14 下的执行结果 (部分): 图 1 可以看出确实常见的信息都显示出来了. 顺便提一下一个事情, 这首 MP3 是我从 win 下直接传到 unix 下的, 以前我在 windows 与 linux 下互传文件都是通过类似百度云的方式来传递的,
但是这种传递方式有时候太过于麻烦. 所以我在网上搜索了一下, 发现了 Putty 这个好东西. PuTTY is an SSH and telnet client,
- developed originally by Simon Tatham
- for the Windows platform.
PuTTY is open source software that is available with source code and is developed and supported by a group of volunteers. 即 Putty 是一个 SSH(安全外壳协议) 远程登录客户端, 专门在 windows 下使用的. 下载地址在这: http: //www.chiark.greenend.org.uk/~sgtatham/putty/download.html. 下载安装之后, 打开 putty, 然后会出现如下的界面 (图 2):
图 2 这个时候, 打开你的 linux 机器, 当然我的是装在 vmware 虚拟机里面的了, 在 shell 里面输入 ifconfig 命令, 找到你的 linux ip 地址, 如下图 3 所示:
图 3 比如我的机器的地址就是 192.168.152.130. 然后将这个 ip 地址填入图 2 的 Host Name 一栏, 注意默认端口为 22, 不要去改它, 然后点击 open, 就会弹出一个登陆界面, 接着输入你在 linux 下登陆的用户名和密码就可以 ssh 连上 linux 啦. http: //www.infoworld.com/article/2617683/linux/linux-moving-files-between-unix-and-windows-systems.html 这篇文章介绍了几种在 unix 与 windows 之间传递文件的方法. 我只讲第一种, 使用 putty 的 pscp 的 secure copy(安全拷贝). 你安装了 putty 之后, 会自带一个叫 pscp.exe 的可执行文件, 将其目录加入 windows 环境变量. 然后 cmd 执行命令 pscp myfile.txt shs@unixserver:/home/shs 即可以把你的文件 myfile.txt 复制到 linux 下的 /home/shs 目录下了. 解释一下, 你在执行的时候需要把 shs 换成你的用户名,@后面跟你的 linux ip 地址, /home/shs 是你要把文件移动的位置. 执行完之后, 文件就会成功拷贝过去了. 其他方法不讲了, 有兴趣可以自行搜索, 或者看我上面说的那篇文章. 回到正题, eyeD3 命令行讲完之后, 再来说下如何在 Python 中使用. 还是看官方给的例子吧:
- import eyed3
- audiofile = eyed3.load("song.mp3")
- audiofile.tag.artist = u"Nobunny"
- audiofile.tag.album = u"Love Visions"
- audiofile.tag.album_artist = u"Various Artists"
- audiofile.tag.title = u"I Am a Girlfriend"
- audiofile.tag.track_num = 4 9
- audiofile.tag.save()
上面的代码, 使用 import eyed3 导入 eyeD3 库, 然后使用 load 方法加载 mp3 文件, 后面的几行分别是设置 artist,album 等等 ID3 tag , 直接看代码就能看出来, 就不说了. 如果想显示 mp3 文件内部的 ID3 tag 信息, 直接 print 相应的 tag 就行了, 比如 print(audiofile.tag.artist) 等等, 当然, 前提是你的 MP3 metadata 得储存了这些信息. 其实还有一些更复杂和高级的用法, 我就不讲了, 大家有兴趣直接去官方文档看吧, 地址: http://eyed3.nicfit.net/index.html.eyeD3 主要就是处理 MP3 文件的 metadata 的, 至于解析音频之类的就得用其他的库了.
二, pydub
第一个介绍的 eyeD3 一般只能处理 MP3 文件, 功能上相对来说也是比较简单一点. 下面介绍的 pydub 库就要强大的多. 老规矩, 还是先看一下它的官方介绍: Manipulate audio with a simple and easy high level interface http: //pydub.com 就一句话, 简单, 易用的处理音频的高度抽象的接口, 嘿, 这不就是我们要找的么. github 项目地址为: https://github.com/jiaaro/pydub/ 有 1800 多的 star, 说明这个库还是很受欢迎的. 安装直接很简单, 直接 pip install pydub 就可以安装. 但是需要注意的是:
- Dependencies
- You can open and save WAV files with pure python. For opening and saving non-wav files - like mp3 - you'll need ffmpeg orlibav.
这里是说 python 自带的 wave 模块只能处理 wav 格式的音频文件, 如果要想处理类似 MP3 格式的文件, 就得要装 ffmpeg 或者 libav 了.
什么是 ffmpeg 呢?
A complete, cross-platform solution to record, convert and stream audio and video.
ffmpeg 是一个跨平台的 可以用来 记录, 转化音频与视频的工具, 如果你做过数字信号处理方面的工作, 对它应该不陌生. 还有一个 libav, 其实是从 ffmpeg 分出来的一个分支, 功能和 ffmpeg 差不多, 二者你任选一个下载就可以了. windows 下直接选择可执行文件安装即可.
还是看官网的例子来介绍吧.
I: 打开 mp3 或者 mp4 等文件
可以采用如下的命令:
- frompydubimport AudioSegment
- song = AudioSegment.from_wav("never_gonna_give_you_up.wav")
- song = AudioSegment.from_mp3("never_gonna_give_you_up.mp3")
- 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")
可以打开任何 ffmpeg 支持的文件类型, 从上面可以看出, 主要有 from_filetype() 方法, filetype 为具体的文件类型, 比如 wav,mp3 等
或者通用的 from_file() 方法, 但是这个方法必须在第二个参数指定打开文件的类型, 返回的结果都是 AudioSegment 对象.
II: 切割音频
- # pydub does things in milliseconds
- ten_seconds = 10 * 10003
- first_10_seconds = song[:ten_seconds]
- last_5_seconds = song[-5000:]
注意 pydub 中的标准时间为毫秒, 上面的代码就得到了音乐的前 10 秒和后 5 秒, 非常简单.
III: 调整音量
- # boost volume by 6dB
- beginning = first_10_seconds + 63
- # reduce volume by 3dB
- end = last_5_seconds - 3
+6 就表示将音乐的音量提高 6 分贝,-3 就表示将音乐的音量降低 3 分贝
IV: 拼接两段音乐
- without_the_middle = beginning + end
- without_the_middle.duration_seconds
拼接之后的音乐时长是两段音乐时长之和, 可以通过 .duration_seconds 方法来获取一段音乐的时长. 这与使用 len(audio)/1000.0 得到的结果是一样的.
V: 将音乐翻转 (reverse)
- # song is not modified
- # AudioSegments are immutable
- backwards = song.reverse()
注意 AudioSegment 对象是不可变的, 上面使用 reverse 方法不会改变 song 这个对象, 而是会返回一个新的 AudioSegment 对象, 其他的方法也是这样, 需要注意. reverse 简单来说就是 将音乐从尾部向头部开始逆序播放, 我试了一下, 发现转换之后还真的挺有意思的.
- VI:crossfade(交叉渐入渐出方法)
- # 1.5 second crossfade
- with_style = beginning.append(end, crossfade=1500)
crossfade 就是让一段音乐平缓地过渡到另一段音乐, 上面的 crossfade = 1500 表示过渡的时间是 1.5 秒.
- VII:repeat(重复音乐片段)
- # repeat the clip twicedo_it_over = with_style * 2
上面的代码让音乐重复播放两次
- VIII:fade in and fade out(逐渐增强与逐渐减弱)
- # 2 sec fade in, 3 sec fade outawesome = do_it_over.fade_in(2000).fade_out(3000)
逐渐增强 2 秒, 逐渐减弱 3 秒
- XI:save(保存)
- awesome.export("mashup.mp3", format = "mp3") awesome.export("mashup.mp3", format = "mp3", tags = {
- 'artist': 'Various artists',
- 'album': 'Best of 2011',
- 'comments': 'This album is awesome!'
- })
这里展示了两种保存的形式, 都是使用 export 方法, 要指定保存的格式, 使用 format 参数, 但第二种方法多了一个 tags 参数, 其实看一下应该就很容易明白, 是保存 歌曲 ID3 tag 信息的.
以上只是 pydub 使用方法的初步介绍, 还有其他非常多的功能, 请自行移步官方 API 文档: https://github.com/jiaaro/pydub/blob/master/API.markdown
介绍的非常详细.
三, PyAudio
又是一个功能强大的处理音频库. 官方介绍:
- PyAudio provides Python bindings for PortAudio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms. PyAudio is inspired by:
- pyPortAudio/fastaudio: Python bindings for PortAudio v18 API.
- tkSnack: cross-platform sound toolkit for Tcl/Tk and Python.
Pyaudio 提供了对于跨平台的 PortAudio(处理 audio 输入输出的库) 的绑定, PyAudio 可以让你轻松录制与播放音频.
废话不多说, 直接看官方文档 (https://people.csail.mit.edu/hubert/pyaudio/docs/) 提供的一个 quick start 的代码
- """PyAudio Example: Play a wave file."""
- import pyaudio
- import wave
- import sys
- CHUNK = 1024 8
- iflen(sys.argv) <2:
- print("Plays a wave file.\n\nUsage: %s filename.wav"% sys.argv[0])
- sys.exit(-1)
- wf = wave.open(sys.argv[1],'rb')
- # instantiate PyAudio (1)
- p = pyaudio.PyAudio()
- # open stream (2)
- stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
- channels=wf.getnchannels(),
- rate=wf.getframerate(),
- output=True)
- # read data
- data = wf.readframes(CHUNK)
- # play stream (3)
- whilelen(data)> 0:
- stream.write(data)
- data = wf.readframes(CHUNK)
- # stop stream (4)
- stream.stop_stream()
- stream.close()
- # close PyAudio (5)
- p.terminate()
当然, 这个提供的是使用命令行参数接收音频文件的形式, CHUNK 是一次读取的音频 byte 数量, p = pyaudio.PyAudio() 初始化一个
PyAudio 对象, 然后使用其 open 方法打开一个输入输出流, 这里指定了 output=True 说明这是一个输出流, 即我们是往 stream 中添加 data, 如果这里改为 input = True 就是变成输入流了, 一般是从设备的标准 audio device , 对于电脑来说可能就是麦克风了, 来读取音频 data. 使用 wave 打开一个 .wav 文件, 然后使用 readframes 方法每次读取 CHUNK 这么多的数据, 将数据写入 stream, 直到读完为止. 写入 stream 的 audio data 就会不断通过麦克风播放出来了, 于是我们就可以听到音乐了. 最后在结束的时候, 注意要关闭相应的对象以释放资源.
还有一种方法是使用 callback(回调函数) 函数, 代码如下:
- """PyAudio Example: Play a wave file (callback version)."""
- import pyaudio
- import wave
- import time
- import sys
- iflen(sys.argv) < 2:
- print("Plays a wave file.\n\nUsage: %s filename.wav"% sys.argv[0])
- sys.exit(-1)
- wf = wave.open(sys.argv[1],'rb')
- # instantiate PyAudio (1)
- p = pyaudio.PyAudio()
- # define callback (2)
- def callback(in_data, frame_count, time_info, status):
- data = wf.readframes(frame_count)
- return (data, pyaudio.paContinue)
- # open stream using callback (3)
- stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
- channels=wf.getnchannels(),
- rate=wf.getframerate(),
- output=True,
- stream_callback=callback)
- # start the stream (4)
- stream.start_stream()
- # wait for stream to finish (5)
- while stream.is_active():
- time.sleep(0.1)
- # stop stream (6)
- stream.stop_stream()
- stream.close()
- wf.close()
- # close PyAudio (7)
- p.terminate()
不细说了.
下面来看一个使用 pyaudio + numpy + pylab 可视化音频的代码, 下面的代码打开电脑的麦克风, 然后接受音频输入, 再以图像的形式展示出来.
- # -*- coding: utf-8 -*-
- """
- Created on Fri May 12 10:30:00 2017
- @author: Lyrichu
- @description: show the sound in graphs
- """
- import pyaudio
- import numpy as np
- import pylab
- import time
- RATE = 4410013CHUNK = int(RATE/20)# RATE/number of updates per second
- def sound_plot(stream):
- t1 = time.time()# time starting
- data = np.fromstring(stream.read(CHUNK),dtype = np.int16)
- pylab.plot(data)
- pylab.title(i)
- pylab.grid()
- pylab.axis([0,len(data),-2**8,2**8])
- pylab.savefig("sound.png",dpi=50)
- pylab.show(block = False)time.sleep(0.5)24pylab.close('all')
- print("took %.2f ms."% (time.time() - t1)*1000)
- if __name__=='__main__':
- p = pyaudio.PyAudio()
- stream = p.open(format = pyaudio.paInt16,channels = 1,rate = RATE,
- input = True,frames_per_buffer = CHUNK)
- foriinrange(int(20*RATE/CHUNK)):
- # for 10 seconds
- sound_plot(stream)
- stream.stop_stream()
- stream.close()
- p.terminate()
代码应该比较容易理解. 得到的大概是像下面这样的图形 (图 4):
图 4
需要注意的是, 如果不是在交互式命令下执行 pylab 或者 matplotlib 的 plot 命令, 其 plt.show() 函数是一个 block 函数, 这会导致最后的
plt.close('all') 关闭所有的窗口只会在手动关闭了图像之后才会执行, 所有我们无法看到连续变化的图像, 为了解决这个问题, 我们将 plt.show() 函数 block 参数设为 False, 这样 show 函数就不是 block 函数了, 可以直接执行 plt.close('all') 命令, 为了不因为图像刷新太快我们看不清变化, 所以使用 time.sleep(0.5) 暂停 0.5 秒.
其实还没介绍完, 还有 pygame 模块 (python 的一个做游戏的模块) 以及 librosa 库 (专业的数字信号处理库) 等没有讲, 等有机会再更吧. 敬请关注!
来源: http://www.cnblogs.com/lyrichu/p/6848732.html