iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 27
0
自我挑戰組

軟體開發隨筆雜記--試著解決問題系列 第 26

[Python]如何Text to Speech: pyttsx3, gTTS

https://pythonprogramminglanguage.com/text-to-speech/

pip3 install pyttsx3
Collecting pyttsx3
  Downloading https://files.pythonhosted.org/packages/33/9a/de4781245f5ad966646fd276259ef7cfd400ba3cf7d5db7c0d5aab310c20/pyttsx3-2.90-py3-none-any.whl
Collecting pypiwin32; platform_system == "Windows"
  Downloading https://files.pythonhosted.org/packages/d0/1b/2f292bbd742e369a100c91faa0483172cd91a1a422a6692055ac920946c5/pypiwin32-223-py3-none-any.whl
Collecting comtypes; platform_system == "Windows"
  Downloading https://files.pythonhosted.org/packages/fb/b8/f8aa21774acb4535e32f6a89055876ca497ff806f9b1b1912b469284a61e/comtypes-1.1.7.zip (180kB)
     |████████████████████████████████| 184kB 595kB/s
Requirement already satisfied: pywin32; platform_system == "Windows" in c:\python37\lib\site-packages (from pyttsx3) (227)
Building wheels for collected packages: comtypes
  Building wheel for comtypes (setup.py) ... done
  Created wheel for comtypes: filename=comtypes-1.1.7-cp37-none-any.whl size=164609 sha256=bde239267e712d96b531e8fc4d69b455e74f34510da515c39585b0bbab3d3bf3
  Stored in directory: C:\Users\USER\AppData\Local\pip\Cache\wheels\a7\a5\e1\49a7738e763143027c7ec97514213055e6c5a978a385541668
Successfully built comtypes
Installing collected packages: pypiwin32, comtypes, pyttsx3
Successfully installed comtypes-1.1.7 pypiwin32-223 pyttsx3-2.90
import pyttsx3

# 初始化
engine = pyttsx3.init()

voices = engine.getProperty('voices')
# 語速控制
rate = engine.getProperty('rate')
print(rate)
engine.setProperty('rate', rate-20)

# 音量控制
volume = engine.getProperty('volume')
print(volume)
engine.setProperty('volume', volume-0.25)

engine.say('hello world')
engine.say('你好,世界')
# 朗讀一次
engine.runAndWait()

engine.say('語音合成開始')
engine.say('我會說中文了,開森,開森')
engine.runAndWait()

engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

gTTS

Python library and CLI tool to interface with Google Translate's text-to-speech API

pip3 install gTTS
Collecting gTTS
  Downloading https://files.pythonhosted.org/packages/a1/0c/4ca77eca3b739a4a08360930643f58d714e302fee0d2f8c654e67d9af8e7/gTTS-2.1.1-py3-none-any.whl
Requirement already satisfied: six in c:\python37\lib\site-packages (from gTTS) (1.13.0)
Requirement already satisfied: click in c:\python37\lib\site-packages (from gTTS) (7.1.2)
Requirement already satisfied: beautifulsoup4 in c:\python37\lib\site-packages (from gTTS) (4.9.3)
Collecting gtts-token>=1.1.3
  Downloading https://files.pythonhosted.org/packages/e7/25/ca6e9cd3275bfc3097fe6b06cc31db6d3dfaf32e032e0f73fead9c9a03ce/gTTS-token-1.1.3.tar.gz
Requirement already satisfied: requests in c:\python37\lib\site-packages (from gTTS) (2.22.0)
Requirement already satisfied: soupsieve>1.2; python_version >= "3.0" in c:\python37\lib\site-packages (from beautifulsoup4->gTTS) (2.0.1)
Requirement already satisfied: certifi>=2017.4.17 in c:\python37\lib\site-packages (from requests->gTTS) (2019.11.28)
Requirement already satisfied: idna<2.9,>=2.5 in c:\python37\lib\site-packages (from requests->gTTS) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\python37\lib\site-packages (from requests->gTTS) (1.25.7)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\python37\lib\site-packages (from requests->gTTS) (3.0.4)
Building wheels for collected packages: gtts-token
  Building wheel for gtts-token (setup.py) ... done
  Created wheel for gtts-token: filename=gTTS_token-1.1.3-cp37-none-any.whl size=4100 sha256=4c2e316723973ed296abe0cc169d2a212d32a011e336ed4b9289a8c37b07a72d
  Stored in directory: C:\Users\USER\AppData\Local\pip\Cache\wheels\dd\11\61\33f7e51bf545e910552b2255eead2a7cd8ef54064b46dceb34
Successfully built gtts-token
Installing collected packages: gtts-token, gTTS
Successfully installed gTTS-2.1.1 gtts-token-1.1.3
from gtts import gTTS
import os
tts = gTTS(text='Good morning', lang='en')
tts.save("good.mp3")

https://ithelp.ithome.com.tw/upload/images/20201011/20119608g8bLVpkh7D.jpg

報時系統

pip3 install pygame
Collecting pygame
  Downloading https://files.pythonhosted.org/packages/ed/56/b63ab3724acff69f4080e54c4bc5f55d1fbdeeb19b92b70acf45e88a5908/pygame-1.9.6-cp37-cp37m-win_amd64.whl (4.3MB)
     |████████████████████████████████| 4.3MB 2.2MB/s
Installing collected packages: pygame
Successfully installed pygame-1.9.6
import time
from gtts import gTTS
import tempfile
from pygame import mixer
import datetime
from datetime import datetime
from time import strftime
 
def say(text, filename=None):
    with tempfile.NamedTemporaryFile(delete=True) as temp:
        tts = gTTS(text, lang='en',slow=False)
        if filename is None:
            filename = "{}.mp3".format(temp.name)
        tts.save(filename)
        mixer.init()
        mixer.music.load(filename)
        mixer.music.play()
        while mixer.music.get_busy() == True:
            continue
        mixer.quit()

while(1):
    hour=datetime.now().strftime('%H')
    minute=datetime.now().strftime('%M')
    second=datetime.now().strftime('%S')
    print(second)
    while second == '00':
        say("It is"+hour+"hour and"+minute+"", "introduction.mp3")
        second=datetime.now().strftime('%S')

    time.sleep(1)
    

https://ithelp.ithome.com.tw/upload/images/20201011/20119608zJpdaghnby.jpg


上一篇
[Python] 把 mp4 轉換成 gif
下一篇
[Python]如何Speech to Text: SpeechRecognition
系列文
軟體開發隨筆雜記--試著解決問題33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言