Raady
Raady

Reputation: 1726

gTTS error: saving as wav but saved as MPEG

Trying to convert the text to speech and save as wav file using gTTS module.

my code:

import gTTS
text = "This is my text in the saving folder"
tts = gTTS(text)
tts.save('sample.wav')

The file is saved but when I check my file info:

$ mediainfo sample.wav
General
Complete name                            : sample.wav
Format                                   : MPEG Audio
File size                                : 15.8 KiB
Duration                                 : 4 s 32 ms
Overall bit rate mode                    : Constant
Overall bit rate                         : 32.0 kb/s
FileExtension_Invalid                    : m1a mpa1 mp1 m2a mpa2 mp2 mp3

Audio
Format                                   : MPEG Audio
Format version                           : Version 2
Format profile                           : Layer 3
Duration                                 : 4 s 32 ms
Bit rate mode                            : Constant
Bit rate                                 : 32.0 kb/s
Channel(s)                               : 1 channel
Sampling rate                            : 24.0 kHz
Compression mode                         : Lossy
Stream size                              : 15.8 KiB (100%)

why I am getting different saved format ?

Upvotes: 1

Views: 8325

Answers (2)

Steffi Keran Rani J
Steffi Keran Rani J

Reputation: 4093

The error in your code is in the very first line.

Instead of using import gTTS, you should have used:

from gtts import gTTS

With this simple modification, your text-to-speech code was running fine and the audio was saved in .wav format as expected. The solution code is as follows:

from gtts import gTTS 

# The text that you want to convert to audio 
mytext = "This is my text in the saving folder"

# Language in which you want to convert 
language = 'en'

# Passing the text and language to the engine
tts = gTTS(text=mytext, lang=language, slow=False) 

# Saving the converted audio in a wav file named sample
tts.save('sample.wav')

Upvotes: 0

Raady
Raady

Reputation: 1726

You might not be able to save it. gTTS provides options to save the audio clip as mp3. even if you give the name as .wav, it doesn't recognise and use the default option for saving. Incase, if you need you wav file alone change the file format using pydub module.

from pydub import AudioSegment
sound = AudioSegment.from_mp3("myfile.mp3")
sound.export("myfile.wav", format="wav")

Upvotes: 5

Related Questions