How to save pyttsx3 results to MP3 or WAV file?

The pyttsx3 is a Python library that provides a simple interface for text-to-speech (TTS) synthesis. Text to Speech converts written text to spoken words and allows customization of various speech parameters. This article demonstrates how to save pyttsx3 results to MP3 or WAV files using different methods.

Basic Process Overview

The general steps for saving pyttsx3 output to audio files are:

  • Initialize the pyttsx3 engine

  • Configure TTS properties (optional)

  • Use save_to_file() to create a temporary WAV file

  • Convert or copy the file to desired format

Creating the Base WAV File

First, let's create a basic WAV file using pyttsx3 −

import pyttsx3
import os

# Initialize the TTS engine
engine = pyttsx3.init()

# Set speech properties (optional)
engine.setProperty('rate', 150)    # Speed of speech
engine.setProperty('volume', 0.9)  # Volume level (0.0 to 1.0)

# Text to convert to speech
text = "Hello, this is a sample text for speech synthesis."

# Save speech to WAV file
engine.save_to_file(text, 'temp_output.wav')
engine.runAndWait()

print("WAV file created successfully!")
WAV file created successfully!

Method 1: Using pydub for MP3 Conversion

The pydub library provides an easy way to convert WAV files to MP3 format −

# Note: This example shows the conversion process
# Actual execution requires pydub installation: pip install pydub
# Also requires ffmpeg for MP3 support

from pydub import AudioSegment

# Load the WAV file
audio = AudioSegment.from_wav("temp_output.wav")

# Export to MP3 format
audio.export("output.mp3", format="mp3")

print("MP3 file saved successfully!")

Key Features:

  • Simple export() method for format conversion

  • Supports multiple audio formats

  • Requires ffmpeg installation for MP3 support

Method 2: Using wave Module for WAV Processing

The built-in wave module allows direct manipulation of WAV files −

import wave

# Read the original WAV file
with open("temp_output.wav", 'rb') as original_file:
    audio_data = original_file.read()

# WAV file parameters (channels, sample_width, framerate, frames, compression, compression_name)
params = (1, 2, 22050, 0, 'NONE', 'not compressed')

# Create a new WAV file with specified parameters
with wave.open("processed_output.wav", 'wb') as wav_file:
    wav_file.setparams(params)
    # Note: In practice, you'd need to extract and process the actual audio frames
    print("WAV file parameters set successfully!")

print("Processed WAV file created!")
WAV file parameters set successfully!
Processed WAV file created!

Method 3: Using soundfile for Advanced WAV Handling

The soundfile library offers comprehensive audio file operations −

# This example demonstrates the soundfile approach
# Requires: pip install soundfile

import soundfile as sf
import numpy as np

# Create sample audio data for demonstration
sample_rate = 22050
duration = 2.0
frequency = 440.0

# Generate a simple sine wave
t = np.linspace(0, duration, int(sample_rate * duration))
audio_data = 0.3 * np.sin(2 * np.pi * frequency * t)

# Save using soundfile
sf.write("soundfile_output.wav", audio_data, sample_rate)

print("SoundFile WAV created successfully!")
print(f"Sample rate: {sample_rate} Hz")
print(f"Duration: {duration} seconds")
SoundFile WAV created successfully!
Sample rate: 22050 Hz
Duration: 2.0 seconds

Comparison of Methods

Method Best For External Dependencies Format Support
pydub Format conversion ffmpeg (for MP3) Multiple formats
wave Basic WAV operations Built-in module WAV only
soundfile Professional audio processing soundfile library Multiple formats

Complete Example

Here's a complete example combining pyttsx3 with file saving −

import pyttsx3
import os

def text_to_speech_file(text, filename="speech_output.wav"):
    """Convert text to speech and save as WAV file"""
    
    # Initialize TTS engine
    engine = pyttsx3.init()
    
    # Configure speech properties
    engine.setProperty('rate', 180)
    engine.setProperty('volume', 0.8)
    
    # Save speech to file
    engine.save_to_file(text, filename)
    engine.runAndWait()
    
    # Check if file was created
    if os.path.exists(filename):
        file_size = os.path.getsize(filename)
        return f"File '{filename}' created successfully! Size: {file_size} bytes"
    else:
        return "Error: File was not created"

# Test the function
sample_text = "Welcome to TutorialsPoint. This is a text-to-speech demonstration."
result = text_to_speech_file(sample_text, "demo_speech.wav")
print(result)
File 'demo_speech.wav' created successfully! Size: 176444 bytes

Conclusion

You can save pyttsx3 results to audio files using save_to_file() for WAV format, then convert using pydub for MP3 or process with soundfile for advanced operations. Choose the method based on your format requirements and available dependencies.

Updated on: 2026-03-27T08:43:07+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements