how to convert wav to mp3 in live using python? how to convert wav to mp3 in live using python? windows windows

how to convert wav to mp3 in live using python?


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


I was able to figure out a working approach using flask and ffmpeg...

import selectimport subprocessimport numpyfrom flask import Flaskfrom flask import Responseapp = Flask(__name__)def get_microphone_audio(num_samples):    # TODO: Add the above microphone code.     audio = numpy.random.rand(num_samples).astype(numpy.float32) * 2 - 1    assert audio.max() <= 1.0    assert audio.min() >= -1.0    assert audio.dtype == numpy.float32    return audiodef response():    pipe = subprocess.Popen(        'ffmpeg -f f32le -acodec pcm_f32le -ar 24000 -ac 1 -i pipe: -f mp3 pipe:'        .split(),        stdin=subprocess.PIPE,        stdout=subprocess.PIPE,        stderr=subprocess.PIPE)    poll = select.poll()    poll.register(pipe.stdout, select.POLLIN)    while True:        pipe.stdin.write(get_synthetic_audio(24000).tobytes())        while poll.poll(0):            yield pipe.stdout.readline()@app.route('/stream.mp3', methods=['GET'])def stream():    return Response(        response(),        headers={            # NOTE: Ensure stream is not cached.            'Cache-Control': 'no-cache, no-store, must-revalidate',            'Pragma': 'no-cache',            'Expires': '0',        },        mimetype='audio/mpeg')if __name__ == "__main__":    app.run(host='0.0.0.0', port=8000, debug=True)

This solution allows for live streaming and is supported in Chrome, Firefox, and Safari.

This solution also worked for this similar question: How to stream MP3 chunks given a NumPy array in Python?