ubuntu - python mp3流如何转无损wav?
黄舟
黄舟 2017-04-24 09:14:10
0
1
1341

我用百度语音webapi将文字转成语音,百度webapi只支持返回mp3格式的流,但是我想要在实时简单分析下波形,用FFT处理下。所以想要将mp3流转为无损的wav格式。
但是找了如下的python包,
pymedia官方已经不更新,貌似不支持python2.7了,非官方的没试;
pymad和pyogg没有找到文档,也没有mp3转无损格式的demo。
请问有什么解决方法吗?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
阿神

Questions and answers by myself. I hope it will be helpful to those who encounter the same situation. You are also welcome to suggest modifications or better solutions.
According to searches in the past few days, few modules explicitly support audio stream format conversion.
Find an available module io, the sound processing module can refer to https://wiki.python.org/moin/...

import io
aud=io.BytesIO(data)#data的格式是mp3数据的bytestring

Then you can use this stream data like a file, and then you can use the mp3 file to wav module instead of writing to the file and reading it again.

The following is the verification process combined with pydub:

from pydub import AudioSegment ###需要安装pydub、ffmpeg
import wave
import io
#先从本地获取mp3的bytestring作为数据样本
fp=open("/home/dyan/你好.mp3",'rb')
data=fp.read()
fp.close()
#主要部分
aud=io.BytesIO(data)
sound=AudioSegment.from_file(aud,format='mp3')
raw_data = sound._data
#写入到文件,验证结果是否正确。
l=len(raw_data)
f=wave.open("/home/dyan/123.wav",'wb')
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(16000)
f.setnframes(l)
f.writeframes(raw_data)
f.close()
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template