简体   繁体   English

gTTS错误:另存为WAV但另存为MPEG

[英]gTTS error: saving as wav but saved as MPEG

Trying to convert the text to speech and save as wav file using gTTS module. 尝试使用gTTS模块将文本转换为语音并另存为wav文件。

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 ? 为什么我得到不同的保存格式?

The error in your code is in the very first line . 代码中的错误在第一行

Instead of using import gTTS , you should have used: 代替使用import gTTS您应该使用:

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 . 通过此简单的修改,您的文本到语音代码运行良好,并且音频按预期以.wav格式保存 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')

You might not be able to save it. 您可能无法保存它。 gTTS provides options to save the audio clip as mp3. gTTS提供了将音频剪辑另存为mp3的选项。 even if you give the name as .wav, it doesn't recognise and use the default option for saving. 即使您将名称指定为.wav,它也无法识别并使用默认选项进行保存。 Incase, if you need you wav file alone change the file format using pydub module. 如果您需要单独使用wav文件,请使用pydub模块更改文件格式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM