【问题标题】:gTTS error: saving as wav but saved as MPEGgTTS 错误:另存为 wav 但另存为 MPEG
【发布时间】:2018-06-06 12:14:21
【问题描述】:

尝试使用 gTTS 模块将文本转换为语音并保存为 wav 文件。

我的代码:

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

文件已保存,但当我检查文件信息时:

$ 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%)

为什么我得到不同的保存格式?

【问题讨论】:

    标签: python python-3.x mpeg google-text-to-speech


    【解决方案1】:

    您可能无法保存它。 gTTS 提供了将音频剪辑保存为 mp3 的选项。即使您将名称命名为 .wav,它也无法识别并使用默认选项进行保存。如果您需要单独使用 pydub 模块更改文件格式,则需要单独使用 wav 文件。

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

    【讨论】:

      【解决方案2】:

      您的代码中的错误在第一行

      而不是使用import gTTS你应该使用:

      from gtts import gTTS
      

      通过这个简单的修改,您的文本转语音代码运行良好,音频按预期保存为 .wav 格式。解决方案代码如下:

      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')
      

      【讨论】:

      • 它将文件保存为 .wav 但它仍然是一个 mp3 文件。当您在音频程序中打开它时它会起作用,因为它会将其识别为 mp3 并以 mp3 播放。
      猜你喜欢
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多