I am trying to send pyttsx3.save_to_file to an api endpoint using python. e.g. i create a Flask api endpoint which receive some text from user -> convert text to speech -> and return the mp3 as byte array.
How this can be acheived?
I could help better if you shared your code, but in general I do it like this:
import base64
import pyttsx3
from flask import Flask, request, Response
app = Flask(__name__)
@app.route('/', methods=['POST'])
def index():
text = request.data.decode('utf-8')
file_name = "file.mp3"
engine = pyttsx3.init()
engine.save_to_file(text, file_name)
engine.runAndWait()
with open(file_name, 'rb') as f:
mp3_bytes = f.read()
mp3_file = base64.b64encode(mp3_bytes)
return Response(mp3_file, mimetype='application/json')
Also you need to install libespeak1 and ffmpeg libraries:
sudo apt-get install libespeak1
sudo apt-get install ffmpeg
You can test it as follows:
curl -X POST -H "Content-Type: text/plain" --data "Hello world" http://localhost:5000/ > file.mp3