본문 바로가기
python package

gTTs 사용법

by fiasco 2022. 11. 2.

gTTs : https://gtts.readthedocs.io/en/latest/index.html#

 

gTTS — gTTS documentation

© Copyright 2014-2021 Pierre Nicolas Durette. Revision 3d6cfc9d.

gtts.readthedocs.io

pyglet : https://pypi.org/project/pyglet/

 

pyglet

Cross-platform windowing and multimedia library

pypi.org

 

음성파일 생성과 읽기

1. TTS 음성 파일 생성 : gTTs 모듈

2. 음성파일 재생 : pyglet 모듈  

from gtts import gTTS
from time import sleep
import os
import pyglet

'''
source_text       : 읽기 대상 text
target_file_name  : mp3로 저장 시 파일명(실행파일 directory에 저장된다)
language          : 읽기 모드 언어 선택(영어-'en', 한국어-'ko')
'''
def tts(source_text:str='hello', target_file_name:str='temp', language:str= 'en'):
    tts = gTTS(text=source_text, lang=language)           # 텍스트 -> 음성 객체 생성
    filename = target_file_name + ".mp3"
    tts.save(filename)                                    # 음성 파일 저장

    music = pyglet.media.load(filename, streaming=False)  # gtts생성파일 로딩
    music.play()                                          # 음성파일 play

    sleep(music.duration)                                 # prevent from killing
    # os.remove(filename)                                 # remove file

if __name__ == '__main__':
    string ="""
    Welcome! Are you completely new to programming? 
    """
    tts(string)

 

'python package' 카테고리의 다른 글

Poetry를 이용한 패키지 관리  (0) 2022.11.04
openpyxl 사용법  (0) 2022.11.02
google trans 사용법  (0) 2022.11.02