-
Python: Google TTS 오디오 재생하기컴퓨터/파이썬 2021. 2. 17. 19:50728x90반응형
gTTS
gTTS (Google Text-to-Speech), a Python library and CLI tool to interface with Google Translate text-to-speech API
pypi.org
1. 설치
pip install gTTS pip install pydub pip install simpleaudio
2. TTS 예제
import os from glob import glob from io import BytesIO from gtts import gTTS from pydub import AudioSegment from pydub.playback import play def tts(word, toSlow=True): tts = gTTS(text=word, lang="ko", slow=toSlow) fp = BytesIO() tts.write_to_fp(fp) fp.seek(0) # simpleaudio가 있어야 작동한다. song = AudioSegment.from_file(fp, format="mp3") play(song) # ffcache 파일이 생성돼서 glob wild card로 전부 삭제 fileList = glob("./ffcache*") for filePath in fileList: os.remove(filePath) if __name__ == "__main__": tts("안녕", toSlow=False) # 안녕 빠르게 발음 tts("안녕", toSlow=True) # 안녕 느리게 발음
728x90'컴퓨터 > 파이썬' 카테고리의 다른 글
Python: Generic 함수 만들기 (functools.singledispatch) (0) 2021.04.07 Python: 자크 비네의 피보나치 수열 방정식 (0) 2021.02.05 Python: Apache Spark 공부 예제 (pyspark) (0) 2021.02.02