πŸ“/capstone1

Python/파이썬 - Google TTS(Text to Speech) μ‚¬μš©ν•˜κΈ°

sssbin 2022. 5. 2. 13:16

 

 

λͺ¨λ“ˆ μ„€μΉ˜

pip install gtts

 

 

"μ•ˆλ…•ν•˜μ„Έμš”" 라고 λ§ν•˜λŠ” μŒμ„± 파일이 tts.save('path') μ•ˆμ˜ κ²½λ‘œμ— μ €μž₯λœλ‹€.

from gtts import gTTS 

def speak(text): 
	tts = gTTS(text=text, lang='ko') 
	tts.save('./hello.mp3') 

speak("μ•ˆλ…•ν•˜μ„Έμš”.")

 

 

이미 μ €μž₯λ˜μ–΄ μžˆλŠ” ν…μŠ€νŠΈ νŒŒμΌμ„ μ½μ–΄μ„œ μŒμ„± 파일둜 μ €μž₯ν•œλ‹€.

from gtts import gTTS

# 파일 경둜
textpath = './test.txt'

# 파일 읽기
with open(textpath, mode='r', encoding='UTF-8') as text: 
    script = text.read()

# κ°œν–‰ 문자 제거
script.replace('\n', '')

# μŒμ„± 파일 μ €μž₯
speech = gTTS(text=script, lang='ko')
speech.save('test.mp3')

 

 

μŒμ„± 좜λ ₯

# λͺ¨λ“ˆ μ„€μΉ˜
pip install playsound
from gtts import gTTS
import playsound

def speak(text):
    tts = gTTS(text=text, lang='en')
    filename = 'hello.mp3'
    tts.save(filename)
    playsound.playsound(filename)

speak("hello")

-> μ•ˆλ˜λŠ”μ€‘......why???????????