2

How to save pyttsx3 TTS directly to buffer without saving it to file?

I'm working on a project where I need to generate a video MP4 with a TTS voiceover. I'm using the pyttsx3 library to generate the audio, but the library saves the audio to a file on disk by default.

Is it possible to save the TTS audio directly to a buffer and return the bytes instead of saving to a file? My goal is to avoid writing unnecessary data to disk and to use the TTS audio bytes directly to generate the video.

Here's the code I'm currently using:

import pyttsx3
import io

def tts(text: str, speed: int = 100) -> io.BytesIO:
    engine = pyttsx3.init()
    engine.setProperty('rate', speed)
    engine.setProperty('voice', engine.getProperty('voices')[1])

    audio_bytes = io.BytesIO()
    engine.save_to_file(text, 'output.wav')
    engine.runAndWait()
    
    with open('output.wav', 'rb') as f:
        audio_bytes = io.BytesIO(f.read())

    return audio_bytes

tts_buffer = tts("Hello World!")

with open('output_test.wav', 'wb') as f:
    f.write(tts_buffer.getbuffer())
Johann
  • 75
  • 9

0 Answers0