how to convert wav to mp3 in live using python?

六月ゝ 毕业季﹏ 提交于 2019-12-06 19:14:00

问题


I have code like what is shown below to get audio from microphone:

import pyaudio
p = pyaudio.PyAudio()
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 1024*10
RECORD_SECONDS = 10
stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    send_via_socket(data) # function to send each frame to remote system

This code is working fine. However each data frame has a size of 4kb. That means 40kb of internet data is needed to send 1 sec of audio data. It's only 6kb of data When I saved the 10 frames (1 second audio) to disc and convert it to mp3 using the pdub module. How can I convert each wav frame to mp3 before sending via socket? (I just need to reduce the size of the frame to save network usage). For example:

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)  # data =4kb
    mp3_frame = wav_to_mp3(data) # mp3_frame should be 1kb or less
    send_via_socket(mp3_frame) # function to send each frame to remote system

回答1:


try python-audiotools. I think it will help you stream the audio file that you want.




回答2:


From reading the code for pydub, it appears that an AudioSegment only allows an output to a file using the out_f variable. So, you can read the WAV file and encode each chunk to a file and then read the file and send it out, decoding it on the other end. However, this is not very efficient. I'd suggest actually extending pydub to handle streams and contributing to the project. The export code is pretty straightforward and I bet it would not be too difficult to do. The author would likely be grateful.

The code for AudioSegment is here: https://github.com/jiaaro/pydub/blob/master/pydub/audio_segment.py



来源:https://stackoverflow.com/questions/25469161/how-to-convert-wav-to-mp3-in-live-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!